Search in sources :

Example 51 with Shape

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);
}
Also used : Shape(org.locationtech.spatial4j.shape.Shape) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 52 with 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);
}
Also used : Shape(org.locationtech.spatial4j.shape.Shape) LineString(com.vividsolutions.jts.geom.LineString) MultiLineString(com.vividsolutions.jts.geom.MultiLineString) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 53 with 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);
}
Also used : Shape(org.locationtech.spatial4j.shape.Shape) Coordinate(com.vividsolutions.jts.geom.Coordinate) ArrayList(java.util.ArrayList) LinearRing(com.vividsolutions.jts.geom.LinearRing) Polygon(com.vividsolutions.jts.geom.Polygon) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 54 with Shape

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);
}
Also used : Shape(org.locationtech.spatial4j.shape.Shape) LineString(com.vividsolutions.jts.geom.LineString) MultiLineString(com.vividsolutions.jts.geom.MultiLineString) Coordinate(com.vividsolutions.jts.geom.Coordinate) JtsPoint(org.locationtech.spatial4j.shape.jts.JtsPoint) Point(com.vividsolutions.jts.geom.Point) JtsPoint(org.locationtech.spatial4j.shape.jts.JtsPoint) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 55 with Shape

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);
}
Also used : CoordinatesBuilder(org.elasticsearch.common.geo.builders.CoordinatesBuilder) Shape(org.locationtech.spatial4j.shape.Shape)

Aggregations

Shape (org.locationtech.spatial4j.shape.Shape)81 Test (org.junit.Test)18 Point (org.locationtech.spatial4j.shape.Point)16 ArrayList (java.util.ArrayList)14 SpatialArgs (org.apache.lucene.spatial.query.SpatialArgs)13 CoordinatesBuilder (org.elasticsearch.common.geo.builders.CoordinatesBuilder)13 PolygonBuilder (org.elasticsearch.common.geo.builders.PolygonBuilder)11 Document (org.apache.lucene.document.Document)10 Field (org.apache.lucene.document.Field)9 Query (org.apache.lucene.search.Query)8 Rectangle (org.locationtech.spatial4j.shape.Rectangle)8 StoredField (org.apache.lucene.document.StoredField)7 SpatialOperation (org.apache.lucene.spatial.query.SpatialOperation)7 LineStringBuilder (org.elasticsearch.common.geo.builders.LineStringBuilder)7 UnitNRShape (org.apache.lucene.spatial.prefix.tree.NumberRangePrefixTree.UnitNRShape)6 StringField (org.apache.lucene.document.StringField)5 TextField (org.apache.lucene.document.TextField)4 IndexableField (org.apache.lucene.index.IndexableField)4 UnsupportedSpatialOperation (org.apache.lucene.spatial.query.UnsupportedSpatialOperation)4 SpatialContext (org.locationtech.spatial4j.context.SpatialContext)4