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

multi-geometry.mddocs/

Multi-Geometry Collections

The Elasticsearch Geo library provides collection classes for grouping multiple geometries of the same or different types. These collections enable efficient representation of complex spatial datasets and support the visitor pattern for type-safe processing.

Capabilities

GeometryCollection Base Class

Generic collection for any geometry types with common collection operations.

/**
 * Creates a geometry collection from a list of geometries
 * @param geometries list of geometries to include in collection
 */
public GeometryCollection(List<T> geometries)

/**
 * Gets the number of geometries in the collection
 * @return number of geometries
 */
public int size()

/**
 * Gets a geometry by index
 * @param i index of geometry to retrieve
 * @return geometry at specified index
 * @throws IndexOutOfBoundsException if index is invalid
 */
public T get(int i)

/**
 * Checks if collection is empty
 * @return true if collection contains no geometries
 */
public boolean isEmpty()

// Empty collection constant
public static final GeometryCollection EMPTY

MultiPoint Collections

Collection specifically for Point geometries with optimized access patterns.

/**
 * Creates a multi-point collection from a list of points
 * @param points list of Point geometries
 */
public MultiPoint(List<Point> points)

// Inherits all methods from GeometryCollection<Point>
// Additional type safety for Point-specific operations

// Empty multi-point constant  
public static final MultiPoint EMPTY

// Example usage:
List<Point> points = Arrays.asList(
    new Point(-73.935242, 40.730610),
    new Point(-73.945242, 40.740610), 
    new Point(-73.925242, 40.720610)
);
MultiPoint multiPoint = new MultiPoint(points);

// Access individual points
Point firstPoint = multiPoint.get(0);
int pointCount = multiPoint.size();

MultiLine Collections

Collection for Line geometries representing multiple disconnected line strings.

/**
 * Creates a multi-line collection from a list of lines
 * @param lines list of Line geometries
 */
public MultiLine(List<Line> lines)

// Inherits all methods from GeometryCollection<Line>
// Type-safe access to Line-specific functionality

// Empty multi-line constant
public static final MultiLine EMPTY

// Example usage:
double[] lons1 = {-74.0, -73.9, -73.8};
double[] lats1 = {40.7, 40.75, 40.8};
Line line1 = new Line(lons1, lats1);

double[] lons2 = {-73.7, -73.6};  
double[] lats2 = {40.6, 40.65};
Line line2 = new Line(lons2, lats2);

List<Line> lines = Arrays.asList(line1, line2);
MultiLine multiLine = new MultiLine(lines);

// Access individual lines
Line firstLine = multiLine.get(0);
int lineCount = multiLine.size();

MultiPolygon Collections

Collection for Polygon geometries representing multiple disconnected polygonal areas.

/**
 * Creates a multi-polygon collection from a list of polygons
 * @param polygons list of Polygon geometries  
 */
public MultiPolygon(List<Polygon> polygons)

// Inherits all methods from GeometryCollection<Polygon>
// Type-safe access to Polygon-specific functionality

// Empty multi-polygon constant
public static final MultiPolygon EMPTY

// Example usage:
// Create first polygon
double[] lons1 = {-74.0, -73.9, -73.9, -74.0, -74.0};
double[] lats1 = {40.7, 40.7, 40.8, 40.8, 40.7};
LinearRing ring1 = new LinearRing(lons1, lats1);
Polygon polygon1 = new Polygon(ring1);

// Create second polygon
double[] lons2 = {-73.8, -73.7, -73.7, -73.8, -73.8};
double[] lats2 = {40.6, 40.6, 40.65, 40.65, 40.6};
LinearRing ring2 = new LinearRing(lons2, lats2);
Polygon polygon2 = new Polygon(ring2);

List<Polygon> polygons = Arrays.asList(polygon1, polygon2);
MultiPolygon multiPolygon = new MultiPolygon(polygons);

// Access individual polygons
Polygon firstPolygon = multiPolygon.get(0);
int polygonCount = multiPolygon.size();

Generic Geometry Collections

Collections that can hold mixed geometry types for complex spatial datasets.

// Mixed geometry collection
List<Geometry> mixedGeometries = Arrays.asList(
    new Point(-73.935242, 40.730610),
    new Circle(-73.945242, 40.740610, 500.0),
    polygon1,
    multiPoint
);
GeometryCollection<Geometry> mixed = new GeometryCollection<>(mixedGeometries);

// Type-safe access requires visitor pattern or type checking
for (int i = 0; i < mixed.size(); i++) {
    Geometry geom = mixed.get(i);
    ShapeType type = geom.type();
    
    switch (type) {
        case POINT:
            Point p = (Point) geom;
            // Handle point-specific logic
            break;
        case CIRCLE:
            Circle c = (Circle) geom;
            // Handle circle-specific logic  
            break;
        // ... handle other types
    }
}

Collection Properties and Behavior

Dimensional Consistency

Collections maintain dimensional consistency where applicable:

// All geometries in collection should have same Z dimension
Point point2d = new Point(-73.935242, 40.730610);
Point point3d = new Point(-73.945242, 40.740610, 150.0);

// Mixed dimensions - generally allowed but may cause issues in some operations
List<Point> mixedDimensionPoints = Arrays.asList(point2d, point3d);
MultiPoint mixed = new MultiPoint(mixedDimensionPoints);

// Check if collection has Z dimension (true if any geometry has Z)
boolean hasZ = mixed.hasZ();

Empty Collections

All collection types support empty states:

// Create empty collections
MultiPoint emptyPoints = MultiPoint.EMPTY;
MultiLine emptyLines = MultiLine.EMPTY;
MultiPolygon emptyPolygons = MultiPolygon.EMPTY;
GeometryCollection<Geometry> emptyCollection = GeometryCollection.EMPTY;

// Check if collection is empty
boolean isEmpty = emptyPoints.isEmpty(); // true
int size = emptyPoints.size(); // 0

// Empty collections are safe to process
ShapeType type = emptyPoints.type(); // ShapeType.MULTIPOINT

Visitor Pattern Support

Collections fully support the visitor pattern for type-safe processing:

// Example visitor for calculating total area of all geometries
public class CollectionAreaCalculator implements GeometryVisitor<Double, RuntimeException> {
    
    @Override
    public Double visit(MultiPolygon multiPolygon) {
        double totalArea = 0.0;
        for (int i = 0; i < multiPolygon.size(); i++) {
            Polygon polygon = multiPolygon.get(i);
            // Calculate polygon area (simplified)
            totalArea += calculatePolygonArea(polygon);
        }
        return totalArea;
    }
    
    @Override
    public Double visit(GeometryCollection<?> collection) {
        double totalArea = 0.0;
        for (int i = 0; i < collection.size(); i++) {
            Geometry geometry = collection.get(i);
            totalArea += geometry.visit(this);
        }
        return totalArea;
    }
    
    // ... implement other visit methods
}

// Use visitor with collections
CollectionAreaCalculator calculator = new CollectionAreaCalculator();
Double totalArea = multiPolygon.visit(calculator);

Usage Examples

import org.elasticsearch.geometry.*;
import java.util.Arrays;
import java.util.List;

// Create multiple points
List<Point> points = Arrays.asList(
    new Point(-73.935242, 40.730610),
    new Point(-73.945242, 40.740610),
    new Point(-73.925242, 40.720610)
);
MultiPoint multiPoint = new MultiPoint(points);

// Access collection properties
int pointCount = multiPoint.size(); // 3
Point firstPoint = multiPoint.get(0);
boolean empty = multiPoint.isEmpty(); // false

// Create mixed geometry collection  
List<Geometry> geometries = Arrays.asList(
    new Point(-73.935242, 40.730610),
    new Circle(-73.945242, 40.740610, 1000.0),
    multiPoint
);
GeometryCollection<Geometry> collection = new GeometryCollection<>(geometries);

// Iterate through collection
for (int i = 0; i < collection.size(); i++) {
    Geometry geom = collection.get(i);
    System.out.println("Geometry " + i + " type: " + geom.type());
}

// Use visitor pattern for processing
SomeGeometryVisitor visitor = new SomeGeometryVisitor();
Object result = collection.visit(visitor);

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