use of org.opensearch.geometry.GeometryCollection in project OpenSearch by opensearch-project.
the class GeoJson method createGeometry.
private static Geometry createGeometry(String type, List<Geometry> geometries, CoordinateNode coordinates, Boolean orientation, boolean defaultOrientation, boolean coerce, DistanceUnit.Distance radius) {
ShapeType shapeType;
if ("bbox".equalsIgnoreCase(type)) {
shapeType = ShapeType.ENVELOPE;
} else {
shapeType = ShapeType.forName(type);
}
if (shapeType == ShapeType.GEOMETRYCOLLECTION) {
if (geometries == null) {
throw new OpenSearchParseException("geometries not included");
}
if (coordinates != null) {
throw new OpenSearchParseException("parameter coordinates is not supported for type " + type);
}
verifyNulls(type, null, orientation, radius);
return new GeometryCollection<>(geometries);
}
// We expect to have coordinates for all the rest
if (coordinates == null) {
throw new OpenSearchParseException("coordinates not included");
}
switch(shapeType) {
case CIRCLE:
if (radius == null) {
throw new OpenSearchParseException("radius is not specified");
}
verifyNulls(type, geometries, orientation, null);
Point point = coordinates.asPoint();
return new Circle(point.getX(), point.getY(), point.getZ(), radius.convert(DistanceUnit.METERS).value);
case POINT:
verifyNulls(type, geometries, orientation, radius);
return coordinates.asPoint();
case MULTIPOINT:
verifyNulls(type, geometries, orientation, radius);
return coordinates.asMultiPoint();
case LINESTRING:
verifyNulls(type, geometries, orientation, radius);
return coordinates.asLineString(coerce);
case MULTILINESTRING:
verifyNulls(type, geometries, orientation, radius);
return coordinates.asMultiLineString(coerce);
case POLYGON:
verifyNulls(type, geometries, null, radius);
// handle possible null in orientation
return coordinates.asPolygon(orientation != null ? orientation : defaultOrientation, coerce);
case MULTIPOLYGON:
verifyNulls(type, geometries, null, radius);
// handle possible null in orientation
return coordinates.asMultiPolygon(orientation != null ? orientation : defaultOrientation, coerce);
case ENVELOPE:
verifyNulls(type, geometries, orientation, radius);
return coordinates.asRectangle();
default:
throw new OpenSearchParseException("unsupported shape type " + type);
}
}
use of org.opensearch.geometry.GeometryCollection in project OpenSearch by opensearch-project.
the class GeometryIndexerTests method testCollection.
public void testCollection() {
assertEquals(GeometryCollection.EMPTY, indexer.prepareForIndexing(GeometryCollection.EMPTY));
GeometryCollection<Geometry> collection = new GeometryCollection<>(Collections.singletonList(new Point(2, 1)));
Geometry indexed = new Point(2, 1);
assertEquals(indexed, indexer.prepareForIndexing(collection));
collection = new GeometryCollection<>(Arrays.asList(new Point(2, 1), new Point(4, 3), new Line(new double[] { 160, 200 }, new double[] { 10, 20 })));
indexed = new GeometryCollection<>(Arrays.asList(new Point(2, 1), new Point(4, 3), new MultiLine(Arrays.asList(new Line(new double[] { 160, 180 }, new double[] { 10, 15 }), new Line(new double[] { -180, -160 }, new double[] { 15, 20 })))));
assertEquals(indexed, indexer.prepareForIndexing(collection));
}
use of org.opensearch.geometry.GeometryCollection in project OpenSearch by opensearch-project.
the class GeoJsonShapeParserTests method testParseGeometryCollection.
@Override
public void testParseGeometryCollection() throws IOException, ParseException {
XContentBuilder geometryCollectionGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "GeometryCollection").startArray("geometries").startObject().field("type", "LineString").startArray("coordinates").startArray().value(100.0).value(0.0).endArray().startArray().value(101.0).value(1.0).endArray().endArray().endObject().startObject().field("type", "Point").startArray("coordinates").value(102.0).value(2.0).endArray().endObject().startObject().field("type", "Polygon").startArray("coordinates").startArray().startArray().value(-177.0).value(10.0).endArray().startArray().value(176.0).value(15.0).endArray().startArray().value(172.0).value(0.0).endArray().startArray().value(176.0).value(-15.0).endArray().startArray().value(-177.0).value(-10.0).endArray().startArray().value(-177.0).value(10.0).endArray().endArray().endArray().endObject().endArray().endObject();
ArrayList<Coordinate> shellCoordinates1 = new ArrayList<>();
shellCoordinates1.add(new Coordinate(180.0, -12.142857142857142));
shellCoordinates1.add(new Coordinate(180.0, 12.142857142857142));
shellCoordinates1.add(new Coordinate(176.0, 15.0));
shellCoordinates1.add(new Coordinate(172.0, 0.0));
shellCoordinates1.add(new Coordinate(176.0, -15));
shellCoordinates1.add(new Coordinate(180.0, -12.142857142857142));
ArrayList<Coordinate> shellCoordinates2 = new ArrayList<>();
shellCoordinates2.add(new Coordinate(-180.0, 12.142857142857142));
shellCoordinates2.add(new Coordinate(-180.0, -12.142857142857142));
shellCoordinates2.add(new Coordinate(-177.0, -10.0));
shellCoordinates2.add(new Coordinate(-177.0, 10.0));
shellCoordinates2.add(new Coordinate(-180.0, 12.142857142857142));
Shape[] expected = new Shape[3];
LineString expectedLineString = GEOMETRY_FACTORY.createLineString(new Coordinate[] { new Coordinate(100, 0), new Coordinate(101, 1) });
expected[0] = jtsGeom(expectedLineString);
Point expectedPoint = GEOMETRY_FACTORY.createPoint(new Coordinate(102.0, 2.0));
expected[1] = new JtsPoint(expectedPoint, SPATIAL_CONTEXT);
LinearRing shell1 = GEOMETRY_FACTORY.createLinearRing(shellCoordinates1.toArray(new Coordinate[shellCoordinates1.size()]));
LinearRing shell2 = GEOMETRY_FACTORY.createLinearRing(shellCoordinates2.toArray(new Coordinate[shellCoordinates2.size()]));
MultiPolygon expectedMultiPoly = GEOMETRY_FACTORY.createMultiPolygon(new Polygon[] { GEOMETRY_FACTORY.createPolygon(shell1), GEOMETRY_FACTORY.createPolygon(shell2) });
expected[2] = jtsGeom(expectedMultiPoly);
// equals returns true only if geometries are in the same order
assertGeometryEquals(shapeCollection(expected), geometryCollectionGeoJson, true);
GeometryCollection<Geometry> geometryExpected = new GeometryCollection<>(Arrays.asList(new Line(new double[] { 100d, 101d }, new double[] { 0d, 1d }), new org.opensearch.geometry.Point(102d, 2d), new org.opensearch.geometry.MultiPolygon(Arrays.asList(new org.opensearch.geometry.Polygon(new org.opensearch.geometry.LinearRing(new double[] { 180d, 180d, 176d, 172d, 176d, 180d }, new double[] { -12.142857142857142d, 12.142857142857142d, 15d, 0d, -15d, -12.142857142857142d })), new org.opensearch.geometry.Polygon(new org.opensearch.geometry.LinearRing(new double[] { -180d, -180d, -177d, -177d, -180d }, new double[] { 12.142857142857142d, -12.142857142857142d, -10d, 10d, 12.142857142857142d }))))));
assertGeometryEquals(geometryExpected, geometryCollectionGeoJson, false);
}
use of org.opensearch.geometry.GeometryCollection in project OpenSearch by opensearch-project.
the class GeoJsonParserTests method testParseGeometryCollection.
@Override
public void testParseGeometryCollection() throws IOException {
XContentBuilder geometryCollectionGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "GeometryCollection").startArray("geometries").startObject().field("type", "LineString").startArray("coordinates").startArray().value(100.0).value(0.0).endArray().startArray().value(101.0).value(1.0).endArray().endArray().endObject().startObject().field("type", "Point").startArray("coordinates").value(102.0).value(2.0).endArray().endObject().startObject().field("type", "Polygon").startArray("coordinates").startArray().startArray().value(-177.0).value(10.0).endArray().startArray().value(176.0).value(15.0).endArray().startArray().value(172.0).value(0.0).endArray().startArray().value(176.0).value(-15.0).endArray().startArray().value(-177.0).value(-10.0).endArray().startArray().value(-177.0).value(10.0).endArray().endArray().endArray().endObject().endArray().endObject();
GeometryCollection<Geometry> geometryExpected = new GeometryCollection<>(Arrays.asList(new Line(new double[] { 100d, 101d }, new double[] { 0d, 1d }), new Point(102d, 2d), new Polygon(new LinearRing(new double[] { -177, 176, 172, 176, -177, -177 }, new double[] { 10, 15, 0, -15, -10, 10 }))));
assertGeometryEquals(geometryExpected, geometryCollectionGeoJson);
}
use of org.opensearch.geometry.GeometryCollection in project OpenSearch by opensearch-project.
the class GeometryParser method parseGeometry.
/**
* Parses the value as a {@link Geometry}. The following types of values are supported:
* <p>
* Object: has to contain either lat and lon or geohash fields
* <p>
* String: expected to be in "latitude, longitude" format, a geohash or WKT
* <p>
* Array: two or more elements, the first element is longitude, the second is latitude, the rest is ignored if ignoreZValue is true
* <p>
* Json structure: valid geojson definition
*/
public Geometry parseGeometry(Object value) throws OpenSearchParseException {
if (value instanceof List) {
List<?> values = (List<?>) value;
if (values.size() == 2 && values.get(0) instanceof Number) {
GeoPoint point = GeoUtils.parseGeoPoint(values, ignoreZValue);
return new Point(point.lon(), point.lat());
} else {
List<Geometry> geometries = new ArrayList<>(values.size());
for (Object object : values) {
geometries.add(parseGeometry(object));
}
return new GeometryCollection<>(geometries);
}
}
try (XContentParser parser = new MapXContentParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, Collections.singletonMap("null_value", value), null)) {
// start object
parser.nextToken();
// field name
parser.nextToken();
// field value
parser.nextToken();
if (isPoint(value)) {
GeoPoint point = GeoUtils.parseGeoPoint(parser, new GeoPoint(), ignoreZValue);
return new Point(point.lon(), point.lat());
} else {
return parse(parser);
}
} catch (IOException | ParseException ex) {
throw new OpenSearchParseException("error parsing geometry ", ex);
}
}
Aggregations