CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-elasticsearch--elasticsearch-geo

Elasticsearch geometry library providing core geometric shapes and spatial utility classes for geometric computations and operations.

Pending
Overview
Eval results
Files

format-conversion.mddocs/

Format Conversion

The Elasticsearch Geo library provides comprehensive support for converting geometries to and from standard geospatial formats including Well-Known Text (WKT), Well-Known Binary (WKB), and GeoHash. These utilities enable interoperability with other geospatial systems and efficient serialization.

Capabilities

Well-Known Text (WKT) Format

Convert geometries to and from the industry-standard Well-Known Text format for human-readable representation.

/**
 * Converts a geometry to WKT string representation
 * @param geometry the geometry to convert
 * @return WKT string representation
 */
public static String toWKT(Geometry geometry)

/**
 * Parses a WKT string to create a geometry object
 * @param validator geometry validator to apply
 * @param coerce whether to coerce invalid geometries (e.g., auto-close polygons)
 * @param wkt the WKT string to parse
 * @return parsed geometry object
 * @throws IOException if parsing fails
 * @throws ParseException if WKT format is invalid
 */
public static Geometry fromWKT(GeometryValidator validator, boolean coerce, String wkt) 
    throws IOException, ParseException


// WKT format constants
public static final String EMPTY = "EMPTY";
public static final String SPACE = " ";
public static final String LPAREN = "(";
public static final String RPAREN = ")";
public static final String COMMA = ",";
public static final String NAN = "NaN";
public static final String Z = "Z";
public static final String M = "M";
public static final int MAX_NESTED_DEPTH = 1000;

// Example WKT formats:
// Point: "POINT(-73.935242 40.730610)"
// Circle: "CIRCLE(-73.935242 40.730610 1000.0)"  
// Polygon: "POLYGON((-74.0 40.7,-73.9 40.7,-73.9 40.8,-74.0 40.8,-74.0 40.7))"
// MultiPoint: "MULTIPOINT(-73.935242 40.730610,-73.945242 40.740610)"

Well-Known Binary (WKB) Format

Convert geometries to binary format for efficient storage and transmission.

/**
 * Converts a geometry to Well-Known Binary format
 * @param geometry the geometry to convert
 * @param byteOrder byte order for binary encoding (BIG_ENDIAN or LITTLE_ENDIAN)
 * @return byte array containing WKB representation
 */
public static byte[] toWKB(Geometry geometry, ByteOrder byteOrder)

/**
 * Parses Well-Known Binary data to create a geometry object
 * @param validator geometry validator to apply
 * @param coerce whether to coerce invalid geometries
 * @param wkb byte array containing WKB data
 * @return parsed geometry object
 */
public static Geometry fromWKB(GeometryValidator validator, boolean coerce, byte[] wkb)

/**
 * Parses Well-Known Binary data to create a geometry object
 * @param validator geometry validator to apply
 * @param coerce whether to coerce invalid geometries  
 * @param wkb byte array containing WKB data
 * @param offset starting position in byte array
 * @param length number of bytes to read
 * @return parsed geometry object
 */
public static Geometry fromWKB(GeometryValidator validator, boolean coerce, byte[] wkb, int offset, int length)

// Usage example:
import java.nio.ByteOrder;

Point point = new Point(-73.935242, 40.730610);
byte[] wkbData = WellKnownBinary.toWKB(point, ByteOrder.BIG_ENDIAN);

// WKB format is binary and not human-readable
// Use for efficient storage and network transmission

GeoHash Encoding and Decoding

Convert between coordinates and GeoHash strings for spatial indexing and proximity searches.

/**
 * Maximum precision level for geohash strings
 */
public static final int PRECISION = 12;

/**
 * Converts geohash string to Point geometry
 * @param geohash the geohash string
 * @return Point representing geohash center
 * @throws IllegalArgumentException if geohash is invalid
 */
public static Point toPoint(String geohash)

/**
 * Computes bounding box for a geohash cell
 * @param geohash the geohash string
 * @return Rectangle representing geohash bounds
 */
public static Rectangle toBoundingBox(String geohash)

/**
 * Encodes coordinates to geohash string with default precision
 * @param lon longitude in decimal degrees
 * @param lat latitude in decimal degrees
 * @return geohash string at precision 12
 */
public static String stringEncode(double lon, double lat)

/**
 * Encodes coordinates to geohash string with specified precision
 * @param lon longitude in decimal degrees
 * @param lat latitude in decimal degrees
 * @param level precision level (1-12)
 * @return geohash string at specified precision
 */
public static String stringEncode(double lon, double lat, int level)

/**
 * Encodes coordinates to geohash long format for efficient storage
 * @param lon longitude in decimal degrees
 * @param lat latitude in decimal degrees  
 * @param level precision level (1-12)
 * @return geohash as long value with embedded level
 */
public static long longEncode(double lon, double lat, int level)

/**
 * Converts geohash string to long format
 * @param hash geohash string
 * @return geohash as long value
 */
public static long longEncode(String hash)

/**
 * Converts long format geohash to string
 * @param geoHashLong geohash in long format
 * @return geohash string
 */
public static String stringEncode(long geoHashLong)

/**
 * Encodes geohash to morton long value for spatial operations
 * @param hash geohash string
 * @return morton encoded value
 * @throws IllegalArgumentException if geohash is empty or contains invalid characters
 */
public static long mortonEncode(String hash)

GeoHash Spatial Operations

Perform spatial queries and neighbor calculations using geohash encoding.

/**
 * Gets all child geohashes one level below the base geohash
 * @param baseGeohash parent geohash
 * @return array of child geohashes (sorted)
 */
public static String[] getSubGeohashes(String baseGeohash)

/**
 * Gets all neighbor geohashes surrounding the given geohash
 * @param geohash center geohash
 * @return collection of neighboring geohashes (up to 8 neighbors)
 */
public static Collection<? extends CharSequence> getNeighbors(String geohash)

/**
 * Adds neighbor geohashes to an existing collection
 * @param geohash center geohash
 * @param neighbors collection to add neighbors to
 * @return the provided collection with neighbors added
 */
public static <E extends Collection<? super String>> E addNeighbors(String geohash, E neighbors)

/**
 * Gets a specific neighbor in a given direction
 * @param geohash center geohash
 * @param level precision level
 * @param dx horizontal offset (-1, 0, or +1)
 * @param dy vertical offset (-1, 0, or +1)
 * @return neighbor geohash or null if at boundary
 */
public static String getNeighbor(String geohash, int level, int dx, int dy)

/**
 * Gets approximate longitude width for geohash precision
 * @param precision geohash precision level (1-12)
 * @return width in degrees
 */
public static double lonWidthInDegrees(int precision)

/**
 * Gets approximate latitude height for geohash precision  
 * @param precision geohash precision level (1-12)
 * @return height in degrees
 */
public static double latHeightInDegrees(int precision)

Coordinate Encoding and Decoding

Low-level utilities for encoding coordinates to integer representations.

/**
 * Encodes latitude to integer representation
 * @param latitude latitude in decimal degrees
 * @return encoded integer value
 */
public static int encodeLatitude(double latitude)

/**
 * Encodes longitude to integer representation
 * @param longitude longitude in decimal degrees
 * @return encoded integer value
 */
public static int encodeLongitude(double longitude)

/**
 * Decodes latitude from geohash string
 * @param geohash geohash string
 * @return latitude in decimal degrees
 */
public static double decodeLatitude(String geohash)

/**
 * Decodes longitude from geohash string
 * @param geohash geohash string
 * @return longitude in decimal degrees
 */
public static double decodeLongitude(String geohash)

/**
 * Decodes latitude from morton encoded value
 * @param hash morton encoded hash
 * @return latitude in decimal degrees
 */
public static double decodeLatitude(long hash)

/**
 * Decodes longitude from morton encoded value
 * @param hash morton encoded hash
 * @return longitude in decimal degrees
 */
public static double decodeLongitude(long hash)

Usage Examples

WKT Conversion Examples

import org.elasticsearch.geometry.*;
import org.elasticsearch.geometry.utils.WellKnownText;
import org.elasticsearch.geometry.utils.StandardValidator;

// Convert geometry to WKT
Point point = new Point(-73.935242, 40.730610);
String wkt = WellKnownText.toWKT(point);
// Result: "POINT(-73.935242 40.730610)"

Circle circle = new Circle(-73.935242, 40.730610, 1000.0);
String circleWkt = WellKnownText.toWKT(circle);
// Result: "CIRCLE(-73.935242 40.730610 1000.0)"

// Parse WKT to geometry
GeometryValidator validator = StandardValidator.instance(false);
String wktString = "POINT(-73.935242 40.730610)";
Geometry parsed = WellKnownText.fromWKT(validator, false, wktString);

// Parse with coercion (auto-close polygons)
String polygonWkt = "POLYGON((-74.0 40.7,-73.9 40.7,-73.9 40.8,-74.0 40.8))"; // missing closing point
Geometry coerced = WellKnownText.fromWKT(validator, true, polygonWkt);

WKB Conversion Examples

import org.elasticsearch.geometry.utils.WellKnownBinary;
import java.nio.ByteOrder;

// Convert to binary format
Point point = new Point(-73.935242, 40.730610);
byte[] wkbData = WellKnownBinary.toWKB(point, ByteOrder.BIG_ENDIAN);

// Parse binary data back to geometry
GeometryValidator validator = StandardValidator.instance(false);
Geometry parsedFromWkb = WellKnownBinary.fromWKB(validator, false, wkbData);

// Convert back to WKT if needed
String wktFromWkb = WellKnownText.toWKT(parsedFromWkb);

GeoHash Examples

import org.elasticsearch.geometry.utils.Geohash;
import java.util.Collection;

// Encode coordinates to geohash
double lon = -73.935242;
double lat = 40.730610; 
String geohash = Geohash.stringEncode(lon, lat, 12); // precision 12
// Result: "dr5regw3r5r3"

// Decode geohash to point
Point center = Geohash.toPoint(geohash);
Rectangle bounds = Geohash.toBoundingBox(geohash);

// Get geohash neighbors
Collection<? extends CharSequence> neighbors = Geohash.getNeighbors(geohash);
// Returns up to 8 surrounding geohashes

// Get specific neighbor (east)
String eastNeighbor = Geohash.getNeighbor(geohash, geohash.length(), 1, 0);

// Get child geohashes
String[] children = Geohash.getSubGeohashes(geohash);
// Returns 32 child geohashes one level more precise

// Check precision properties
double lonWidth = Geohash.lonWidthInDegrees(12); // longitude width at precision 12
double latHeight = Geohash.latHeightInDegrees(12); // latitude height at precision 12

// Long format encoding for efficient storage
long longGeohash = Geohash.longEncode(lon, lat, 12);
String backToString = Geohash.stringEncode(longGeohash);

Format Integration Example

// Complete workflow: Geometry -> WKT -> Parse -> GeoHash -> Bounds
Point originalPoint = new Point(-73.935242, 40.730610);

// Convert to WKT
String wkt = WellKnownText.toWKT(originalPoint);

// Parse back from WKT
GeometryValidator validator = StandardValidator.instance(false);
Point parsedPoint = (Point) WellKnownText.fromWKT(validator, false, wkt);

// Convert to geohash
String geohash = Geohash.stringEncode(parsedPoint.getX(), parsedPoint.getY(), 10);

// Get bounding box for geohash cell
Rectangle geohashBounds = Geohash.toBoundingBox(geohash);

// Convert bounds to WKT
String boundsWkt = WellKnownText.toWKT(geohashBounds);
// Result: "BBOX(-73.9404296875 -73.9349365234375 40.7312011719 40.728759765625)"

Install with Tessl CLI

npx tessl i tessl/maven-org-elasticsearch--elasticsearch-geo

docs

core-geometry.md

format-conversion.md

index.md

multi-geometry.md

simplification.md

spatial-utilities.md

validation.md

visitor-pattern.md

tile.json