use of org.locationtech.spatial4j.shape.Shape in project elasticsearch by elastic.
the class GeoJSONShapeParserTests method testParseOrientationOption.
public void testParseOrientationOption() throws IOException {
// test 1: valid ccw (right handed system) poly not crossing dateline (with 'right' field)
XContentBuilder polygonGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "Polygon").field("orientation", "right").startArray("coordinates").startArray().startArray().value(176.0).value(15.0).endArray().startArray().value(-177.0).value(10.0).endArray().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().endArray().startArray().startArray().value(-172.0).value(8.0).endArray().startArray().value(174.0).value(10.0).endArray().startArray().value(-172.0).value(-8.0).endArray().startArray().value(-172.0).value(8.0).endArray().endArray().endArray().endObject();
XContentParser parser = createParser(polygonGeoJson);
parser.nextToken();
Shape shape = ShapeBuilder.parse(parser).build();
ElasticsearchGeoAssertions.assertPolygon(shape);
// test 2: valid ccw (right handed system) poly not crossing dateline (with 'ccw' field)
polygonGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "Polygon").field("orientation", "ccw").startArray("coordinates").startArray().startArray().value(176.0).value(15.0).endArray().startArray().value(-177.0).value(10.0).endArray().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().endArray().startArray().startArray().value(-172.0).value(8.0).endArray().startArray().value(174.0).value(10.0).endArray().startArray().value(-172.0).value(-8.0).endArray().startArray().value(-172.0).value(8.0).endArray().endArray().endArray().endObject();
parser = createParser(polygonGeoJson);
parser.nextToken();
shape = ShapeBuilder.parse(parser).build();
ElasticsearchGeoAssertions.assertPolygon(shape);
// test 3: valid ccw (right handed system) poly not crossing dateline (with 'counterclockwise' field)
polygonGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "Polygon").field("orientation", "counterclockwise").startArray("coordinates").startArray().startArray().value(176.0).value(15.0).endArray().startArray().value(-177.0).value(10.0).endArray().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().endArray().startArray().startArray().value(-172.0).value(8.0).endArray().startArray().value(174.0).value(10.0).endArray().startArray().value(-172.0).value(-8.0).endArray().startArray().value(-172.0).value(8.0).endArray().endArray().endArray().endObject();
parser = createParser(polygonGeoJson);
parser.nextToken();
shape = ShapeBuilder.parse(parser).build();
ElasticsearchGeoAssertions.assertPolygon(shape);
// test 4: valid cw (left handed system) poly crossing dateline (with 'left' field)
polygonGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "Polygon").field("orientation", "left").startArray("coordinates").startArray().startArray().value(176.0).value(15.0).endArray().startArray().value(-177.0).value(10.0).endArray().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().endArray().startArray().startArray().value(-178.0).value(8.0).endArray().startArray().value(178.0).value(8.0).endArray().startArray().value(180.0).value(-8.0).endArray().startArray().value(-178.0).value(8.0).endArray().endArray().endArray().endObject();
parser = createParser(polygonGeoJson);
parser.nextToken();
shape = ShapeBuilder.parse(parser).build();
ElasticsearchGeoAssertions.assertMultiPolygon(shape);
// test 5: valid cw multipoly (left handed system) poly crossing dateline (with 'cw' field)
polygonGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "Polygon").field("orientation", "cw").startArray("coordinates").startArray().startArray().value(176.0).value(15.0).endArray().startArray().value(-177.0).value(10.0).endArray().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().endArray().startArray().startArray().value(-178.0).value(8.0).endArray().startArray().value(178.0).value(8.0).endArray().startArray().value(180.0).value(-8.0).endArray().startArray().value(-178.0).value(8.0).endArray().endArray().endArray().endObject();
parser = createParser(polygonGeoJson);
parser.nextToken();
shape = ShapeBuilder.parse(parser).build();
ElasticsearchGeoAssertions.assertMultiPolygon(shape);
// test 6: valid cw multipoly (left handed system) poly crossing dateline (with 'clockwise' field)
polygonGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "Polygon").field("orientation", "clockwise").startArray("coordinates").startArray().startArray().value(176.0).value(15.0).endArray().startArray().value(-177.0).value(10.0).endArray().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().endArray().startArray().startArray().value(-178.0).value(8.0).endArray().startArray().value(178.0).value(8.0).endArray().startArray().value(180.0).value(-8.0).endArray().startArray().value(-178.0).value(8.0).endArray().endArray().endArray().endObject();
parser = createParser(polygonGeoJson);
parser.nextToken();
shape = ShapeBuilder.parse(parser).build();
ElasticsearchGeoAssertions.assertMultiPolygon(shape);
}
use of org.locationtech.spatial4j.shape.Shape in project elasticsearch by elastic.
the class GeoJSONShapeParserTests method testParseOGCPolygonWithHoles.
public void testParseOGCPolygonWithHoles() throws IOException {
// test 1: ccw poly not crossing dateline
String polygonGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "Polygon").startArray("coordinates").startArray().startArray().value(176.0).value(15.0).endArray().startArray().value(-177.0).value(10.0).endArray().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().endArray().startArray().startArray().value(-172.0).value(8.0).endArray().startArray().value(174.0).value(10.0).endArray().startArray().value(-172.0).value(-8.0).endArray().startArray().value(-172.0).value(8.0).endArray().endArray().endArray().endObject().string();
XContentParser parser = createParser(JsonXContent.jsonXContent, polygonGeoJson);
parser.nextToken();
Shape shape = ShapeBuilder.parse(parser).build();
ElasticsearchGeoAssertions.assertPolygon(shape);
// test 2: ccw poly crossing dateline
polygonGeoJson = XContentFactory.jsonBuilder().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().startArray().startArray().value(178.0).value(8.0).endArray().startArray().value(-178.0).value(8.0).endArray().startArray().value(-180.0).value(-8.0).endArray().startArray().value(178.0).value(8.0).endArray().endArray().endArray().endObject().string();
parser = createParser(JsonXContent.jsonXContent, polygonGeoJson);
parser.nextToken();
shape = ShapeBuilder.parse(parser).build();
ElasticsearchGeoAssertions.assertMultiPolygon(shape);
// test 3: cw poly not crossing dateline
polygonGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "Polygon").startArray("coordinates").startArray().startArray().value(176.0).value(15.0).endArray().startArray().value(180.0).value(10.0).endArray().startArray().value(179.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().endArray().startArray().startArray().value(177.0).value(8.0).endArray().startArray().value(179.0).value(10.0).endArray().startArray().value(179.0).value(-8.0).endArray().startArray().value(177.0).value(8.0).endArray().endArray().endArray().endObject().string();
parser = createParser(JsonXContent.jsonXContent, polygonGeoJson);
parser.nextToken();
shape = ShapeBuilder.parse(parser).build();
ElasticsearchGeoAssertions.assertPolygon(shape);
// test 4: cw poly crossing dateline
polygonGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "Polygon").startArray("coordinates").startArray().startArray().value(183.0).value(10.0).endArray().startArray().value(183.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(183.0).value(10.0).endArray().endArray().startArray().startArray().value(178.0).value(8.0).endArray().startArray().value(182.0).value(8.0).endArray().startArray().value(180.0).value(-8.0).endArray().startArray().value(178.0).value(8.0).endArray().endArray().endArray().endObject().string();
parser = createParser(JsonXContent.jsonXContent, polygonGeoJson);
parser.nextToken();
shape = ShapeBuilder.parse(parser).build();
ElasticsearchGeoAssertions.assertMultiPolygon(shape);
}
use of org.locationtech.spatial4j.shape.Shape in project elasticsearch by elastic.
the class GeoJSONShapeParserTests method testParseMultiPolygon.
public void testParseMultiPolygon() throws IOException {
// test #1: two polygons; one without hole, one with hole
XContentBuilder multiPolygonGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "MultiPolygon").startArray("coordinates").startArray().startArray().startArray().value(102.0).value(2.0).endArray().startArray().value(103.0).value(2.0).endArray().startArray().value(103.0).value(3.0).endArray().startArray().value(102.0).value(3.0).endArray().startArray().value(102.0).value(2.0).endArray().endArray().endArray().startArray().startArray().startArray().value(100.0).value(0.0).endArray().startArray().value(101.0).value(0.0).endArray().startArray().value(101.0).value(1.0).endArray().startArray().value(100.0).value(1.0).endArray().startArray().value(100.0).value(0.0).endArray().endArray().startArray().startArray().value(100.2).value(0.8).endArray().startArray().value(100.2).value(0.2).endArray().startArray().value(100.8).value(0.2).endArray().startArray().value(100.8).value(0.8).endArray().startArray().value(100.2).value(0.8).endArray().endArray().endArray().endArray().endObject();
List<Coordinate> shellCoordinates = new ArrayList<>();
shellCoordinates.add(new Coordinate(100, 0));
shellCoordinates.add(new Coordinate(101, 0));
shellCoordinates.add(new Coordinate(101, 1));
shellCoordinates.add(new Coordinate(100, 1));
shellCoordinates.add(new Coordinate(100, 0));
List<Coordinate> holeCoordinates = new ArrayList<>();
holeCoordinates.add(new Coordinate(100.2, 0.2));
holeCoordinates.add(new Coordinate(100.8, 0.2));
holeCoordinates.add(new Coordinate(100.8, 0.8));
holeCoordinates.add(new Coordinate(100.2, 0.8));
holeCoordinates.add(new Coordinate(100.2, 0.2));
LinearRing shell = GEOMETRY_FACTORY.createLinearRing(shellCoordinates.toArray(new Coordinate[shellCoordinates.size()]));
LinearRing[] holes = new LinearRing[1];
holes[0] = GEOMETRY_FACTORY.createLinearRing(holeCoordinates.toArray(new Coordinate[holeCoordinates.size()]));
Polygon withHoles = GEOMETRY_FACTORY.createPolygon(shell, holes);
shellCoordinates = new ArrayList<>();
shellCoordinates.add(new Coordinate(102, 3));
shellCoordinates.add(new Coordinate(103, 3));
shellCoordinates.add(new Coordinate(103, 2));
shellCoordinates.add(new Coordinate(102, 2));
shellCoordinates.add(new Coordinate(102, 3));
shell = GEOMETRY_FACTORY.createLinearRing(shellCoordinates.toArray(new Coordinate[shellCoordinates.size()]));
Polygon withoutHoles = GEOMETRY_FACTORY.createPolygon(shell, null);
Shape expected = shapeCollection(withoutHoles, withHoles);
assertGeometryEquals(expected, multiPolygonGeoJson);
// test #2: multipolygon; one polygon with one hole
// this test converting the multipolygon from a ShapeCollection type
// to a simple polygon (jtsGeom)
multiPolygonGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "MultiPolygon").startArray("coordinates").startArray().startArray().startArray().value(100.0).value(1.0).endArray().startArray().value(101.0).value(1.0).endArray().startArray().value(101.0).value(0.0).endArray().startArray().value(100.0).value(0.0).endArray().startArray().value(100.0).value(1.0).endArray().endArray().startArray().startArray().value(100.2).value(0.8).endArray().startArray().value(100.2).value(0.2).endArray().startArray().value(100.8).value(0.2).endArray().startArray().value(100.8).value(0.8).endArray().startArray().value(100.2).value(0.8).endArray().endArray().endArray().endArray().endObject();
shellCoordinates = new ArrayList<>();
shellCoordinates.add(new Coordinate(100, 1));
shellCoordinates.add(new Coordinate(101, 1));
shellCoordinates.add(new Coordinate(101, 0));
shellCoordinates.add(new Coordinate(100, 0));
shellCoordinates.add(new Coordinate(100, 1));
holeCoordinates = new ArrayList<>();
holeCoordinates.add(new Coordinate(100.2, 0.8));
holeCoordinates.add(new Coordinate(100.2, 0.2));
holeCoordinates.add(new Coordinate(100.8, 0.2));
holeCoordinates.add(new Coordinate(100.8, 0.8));
holeCoordinates.add(new Coordinate(100.2, 0.8));
shell = GEOMETRY_FACTORY.createLinearRing(shellCoordinates.toArray(new Coordinate[shellCoordinates.size()]));
holes = new LinearRing[1];
holes[0] = GEOMETRY_FACTORY.createLinearRing(holeCoordinates.toArray(new Coordinate[holeCoordinates.size()]));
withHoles = GEOMETRY_FACTORY.createPolygon(shell, holes);
assertGeometryEquals(jtsGeom(withHoles), multiPolygonGeoJson);
}
use of org.locationtech.spatial4j.shape.Shape in project elasticsearch by elastic.
the class GeoJSONShapeParserTests method testParseGeometryCollection.
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().endArray().endObject();
Shape[] expected = new Shape[2];
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);
//equals returns true only if geometries are in the same order
assertGeometryEquals(shapeCollection(expected), geometryCollectionGeoJson);
}
use of org.locationtech.spatial4j.shape.Shape in project elasticsearch by elastic.
the class ShapeBuilderTests method testLineStringWrapping.
public void testLineStringWrapping() {
Shape shape = ShapeBuilders.newLineString(new CoordinatesBuilder().coordinate(-150.0, 65.0).coordinate(-250.0, 65.0).coordinate(-250.0, -65.0).coordinate(-150.0, -65.0).close()).build();
assertMultiLineString(shape);
}
Aggregations