use of org.opensearch.common.geo.builders.PolygonBuilder in project OpenSearch by opensearch-project.
the class GeoShapeQueryTests method testRandomGeoCollectionQuery.
public void testRandomGeoCollectionQuery() throws Exception {
// Create a random geometry collection to index.
GeometryCollectionBuilder gcb = RandomShapeGenerator.createGeometryCollection(random());
org.apache.lucene.geo.Polygon randomPoly = GeoTestUtil.nextPolygon();
assumeTrue("Skipping the check for the polygon with a degenerated dimension", randomPoly.maxLat - randomPoly.minLat > 8.4e-8 && randomPoly.maxLon - randomPoly.minLon > 8.4e-8);
CoordinatesBuilder cb = new CoordinatesBuilder();
for (int i = 0; i < randomPoly.numPoints(); ++i) {
cb.coordinate(randomPoly.getPolyLon(i), randomPoly.getPolyLat(i));
}
gcb.shape(new PolygonBuilder(cb));
XContentBuilder mapping = createRandomMapping();
Settings settings = Settings.builder().put("index.number_of_shards", 1).build();
client().admin().indices().prepareCreate("test").addMapping("_doc", mapping).setSettings(settings).get();
ensureGreen();
XContentBuilder docSource = gcb.toXContent(jsonBuilder().startObject().field("geo"), null).endObject();
client().prepareIndex("test").setId("1").setSource(docSource).setRefreshPolicy(IMMEDIATE).get();
// Create a random geometry collection to query
GeometryCollectionBuilder queryCollection = RandomShapeGenerator.createGeometryCollection(random());
queryCollection.shape(new PolygonBuilder(cb));
GeoShapeQueryBuilder geoShapeQueryBuilder = QueryBuilders.geoShapeQuery("geo", queryCollection);
geoShapeQueryBuilder.relation(ShapeRelation.INTERSECTS);
SearchResponse result = client().prepareSearch("test").setQuery(geoShapeQueryBuilder).get();
assertSearchResponse(result);
assertThat(result.getHits().getHits().length, greaterThan(0));
}
use of org.opensearch.common.geo.builders.PolygonBuilder in project OpenSearch by opensearch-project.
the class GeoFilterIT method testShapeRelations.
public void testShapeRelations() throws Exception {
assertTrue("Intersect relation is not supported", intersectSupport);
assertTrue("Disjoint relation is not supported", disjointSupport);
assertTrue("within relation is not supported", withinSupport);
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("polygon").startObject("properties").startObject("area").field("type", "geo_shape").field("tree", "geohash").endObject().endObject().endObject().endObject());
CreateIndexRequestBuilder mappingRequest = client().admin().indices().prepareCreate("shapes").addMapping("polygon", mapping, XContentType.JSON);
mappingRequest.get();
client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().get();
// Create a multipolygon with two polygons. The first is an rectangle of size 10x10
// with a hole of size 5x5 equidistant from all sides. This hole in turn contains
// the second polygon of size 4x4 equidistant from all sites
MultiPolygonBuilder polygon = new MultiPolygonBuilder().polygon(new PolygonBuilder(new CoordinatesBuilder().coordinate(-10, -10).coordinate(-10, 10).coordinate(10, 10).coordinate(10, -10).close()).hole(new LineStringBuilder(new CoordinatesBuilder().coordinate(-5, -5).coordinate(-5, 5).coordinate(5, 5).coordinate(5, -5).close()))).polygon(new PolygonBuilder(new CoordinatesBuilder().coordinate(-4, -4).coordinate(-4, 4).coordinate(4, 4).coordinate(4, -4).close()));
BytesReference data = BytesReference.bytes(jsonBuilder().startObject().field("area", polygon).endObject());
client().prepareIndex("shapes").setId("1").setSource(data, XContentType.JSON).get();
client().admin().indices().prepareRefresh().get();
// Point in polygon
SearchResponse result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoIntersectionQuery("area", new PointBuilder(3, 3))).get();
assertHitCount(result, 1);
assertFirstHit(result, hasId("1"));
// Point in polygon hole
result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoIntersectionQuery("area", new PointBuilder(4.5, 4.5))).get();
assertHitCount(result, 0);
// by definition the border of a polygon belongs to the inner
// so the border of a polygons hole also belongs to the inner
// of the polygon NOT the hole
// Point on polygon border
result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoIntersectionQuery("area", new PointBuilder(10.0, 5.0))).get();
assertHitCount(result, 1);
assertFirstHit(result, hasId("1"));
// Point on hole border
result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoIntersectionQuery("area", new PointBuilder(5.0, 2.0))).get();
assertHitCount(result, 1);
assertFirstHit(result, hasId("1"));
if (disjointSupport) {
// Point not in polygon
result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoDisjointQuery("area", new PointBuilder(3, 3))).get();
assertHitCount(result, 0);
// Point in polygon hole
result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoDisjointQuery("area", new PointBuilder(4.5, 4.5))).get();
assertHitCount(result, 1);
assertFirstHit(result, hasId("1"));
}
// Create a polygon that fills the empty area of the polygon defined above
PolygonBuilder inverse = new PolygonBuilder(new CoordinatesBuilder().coordinate(-5, -5).coordinate(-5, 5).coordinate(5, 5).coordinate(5, -5).close()).hole(new LineStringBuilder(new CoordinatesBuilder().coordinate(-4, -4).coordinate(-4, 4).coordinate(4, 4).coordinate(4, -4).close()));
data = BytesReference.bytes(jsonBuilder().startObject().field("area", inverse).endObject());
client().prepareIndex("shapes").setId("2").setSource(data, XContentType.JSON).get();
client().admin().indices().prepareRefresh().get();
// re-check point on polygon hole
result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoIntersectionQuery("area", new PointBuilder(4.5, 4.5))).get();
assertHitCount(result, 1);
assertFirstHit(result, hasId("2"));
// Create Polygon with hole and common edge
PolygonBuilder builder = new PolygonBuilder(new CoordinatesBuilder().coordinate(-10, -10).coordinate(-10, 10).coordinate(10, 10).coordinate(10, -10).close()).hole(new LineStringBuilder(new CoordinatesBuilder().coordinate(-5, -5).coordinate(-5, 5).coordinate(10, 5).coordinate(10, -5).close()));
if (withinSupport) {
// Polygon WithIn Polygon
builder = new PolygonBuilder(new CoordinatesBuilder().coordinate(-30, -30).coordinate(-30, 30).coordinate(30, 30).coordinate(30, -30).close());
result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoWithinQuery("area", builder.buildGeometry())).get();
assertHitCount(result, 2);
}
// Create a polygon crossing longitude 180.
builder = new PolygonBuilder(new CoordinatesBuilder().coordinate(170, -10).coordinate(190, -10).coordinate(190, 10).coordinate(170, 10).close());
data = BytesReference.bytes(jsonBuilder().startObject().field("area", builder).endObject());
client().prepareIndex("shapes").setId("1").setSource(data, XContentType.JSON).get();
client().admin().indices().prepareRefresh().get();
// Create a polygon crossing longitude 180 with hole.
builder = new PolygonBuilder(new CoordinatesBuilder().coordinate(170, -10).coordinate(190, -10).coordinate(190, 10).coordinate(170, 10).close()).hole(new LineStringBuilder(new CoordinatesBuilder().coordinate(175, -5).coordinate(185, -5).coordinate(185, 5).coordinate(175, 5).close()));
data = BytesReference.bytes(jsonBuilder().startObject().field("area", builder).endObject());
client().prepareIndex("shapes").setId("1").setSource(data, XContentType.JSON).get();
client().admin().indices().prepareRefresh().get();
result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoIntersectionQuery("area", new PointBuilder(174, -4).buildGeometry())).get();
assertHitCount(result, 1);
result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoIntersectionQuery("area", new PointBuilder(-174, -4).buildGeometry())).get();
assertHitCount(result, 1);
result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoIntersectionQuery("area", new PointBuilder(180, -4).buildGeometry())).get();
assertHitCount(result, 0);
result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoIntersectionQuery("area", new PointBuilder(180, -6).buildGeometry())).get();
assertHitCount(result, 1);
}
use of org.opensearch.common.geo.builders.PolygonBuilder in project OpenSearch by opensearch-project.
the class GeoFilterIT method testShapeBuilders.
public void testShapeBuilders() {
try {
// self intersection polygon
new PolygonBuilder(new CoordinatesBuilder().coordinate(-10, -10).coordinate(10, 10).coordinate(-10, 10).coordinate(10, -10).close()).buildS4J();
fail("Self intersection not detected");
} catch (InvalidShapeException e) {
}
// polygon with hole
new PolygonBuilder(new CoordinatesBuilder().coordinate(-10, -10).coordinate(-10, 10).coordinate(10, 10).coordinate(10, -10).close()).hole(new LineStringBuilder(new CoordinatesBuilder().coordinate(-5, -5).coordinate(-5, 5).coordinate(5, 5).coordinate(5, -5).close())).buildS4J();
try {
// polygon with overlapping hole
new PolygonBuilder(new CoordinatesBuilder().coordinate(-10, -10).coordinate(-10, 10).coordinate(10, 10).coordinate(10, -10).close()).hole(new LineStringBuilder(new CoordinatesBuilder().coordinate(-5, -5).coordinate(-5, 11).coordinate(5, 11).coordinate(5, -5).close())).buildS4J();
fail("Self intersection not detected");
} catch (InvalidShapeException e) {
}
try {
// polygon with intersection holes
new PolygonBuilder(new CoordinatesBuilder().coordinate(-10, -10).coordinate(-10, 10).coordinate(10, 10).coordinate(10, -10).close()).hole(new LineStringBuilder(new CoordinatesBuilder().coordinate(-5, -5).coordinate(-5, 5).coordinate(5, 5).coordinate(5, -5).close())).hole(new LineStringBuilder(new CoordinatesBuilder().coordinate(-5, -6).coordinate(5, -6).coordinate(5, -4).coordinate(-5, -4).close())).buildS4J();
fail("Intersection of holes not detected");
} catch (InvalidShapeException e) {
}
try {
// Common line in polygon
new PolygonBuilder(new CoordinatesBuilder().coordinate(-10, -10).coordinate(-10, 10).coordinate(-5, 10).coordinate(-5, -5).coordinate(-5, 20).coordinate(10, 20).coordinate(10, -10).close()).buildS4J();
fail("Self intersection not detected");
} catch (InvalidShapeException e) {
}
// Multipolygon: polygon with hole and polygon within the whole
new MultiPolygonBuilder().polygon(new PolygonBuilder(new CoordinatesBuilder().coordinate(-10, -10).coordinate(-10, 10).coordinate(10, 10).coordinate(10, -10).close()).hole(new LineStringBuilder(new CoordinatesBuilder().coordinate(-5, -5).coordinate(-5, 5).coordinate(5, 5).coordinate(5, -5).close()))).polygon(new PolygonBuilder(new CoordinatesBuilder().coordinate(-4, -4).coordinate(-4, 4).coordinate(4, 4).coordinate(4, -4).close())).buildS4J();
}
use of org.opensearch.common.geo.builders.PolygonBuilder in project OpenSearch by opensearch-project.
the class GeoWKTShapeParserTests method testParseSelfCrossingPolygon.
public void testParseSelfCrossingPolygon() throws IOException {
// test self crossing ccw poly not crossing dateline
List<Coordinate> shellCoordinates = new ArrayList<>();
shellCoordinates.add(new Coordinate(176, 15));
shellCoordinates.add(new Coordinate(-177, 10));
shellCoordinates.add(new Coordinate(-177, -10));
shellCoordinates.add(new Coordinate(176, -15));
shellCoordinates.add(new Coordinate(-177, 15));
shellCoordinates.add(new Coordinate(172, 0));
shellCoordinates.add(new Coordinate(176, 15));
PolygonBuilder poly = new PolygonBuilder(new CoordinatesBuilder().coordinates(shellCoordinates));
XContentBuilder builder = XContentFactory.jsonBuilder().value(poly.toWKT());
assertValidException(builder, InvalidShapeException.class);
}
use of org.opensearch.common.geo.builders.PolygonBuilder in project OpenSearch by opensearch-project.
the class ShapeBuilderTests method testNewPolygon_coordinate.
public void testNewPolygon_coordinate() {
PolygonBuilder pb = new PolygonBuilder(new CoordinatesBuilder().coordinate(new Coordinate(-45, 30)).coordinate(new Coordinate(45, 30)).coordinate(new Coordinate(45, -30)).coordinate(new Coordinate(-45, -30)).coordinate(new Coordinate(-45, 30)));
Polygon poly = pb.toPolygonS4J();
LineString exterior = poly.getExteriorRing();
assertEquals(exterior.getCoordinateN(0), new Coordinate(-45, 30));
assertEquals(exterior.getCoordinateN(1), new Coordinate(45, 30));
assertEquals(exterior.getCoordinateN(2), new Coordinate(45, -30));
assertEquals(exterior.getCoordinateN(3), new Coordinate(-45, -30));
LinearRing polygon = pb.toPolygonGeometry().getPolygon();
assertEquals(polygon.getY(0), 30, 0d);
assertEquals(polygon.getX(0), -45, 0d);
assertEquals(polygon.getY(1), 30, 0d);
assertEquals(polygon.getX(1), 45, 0d);
assertEquals(polygon.getY(2), -30, 0d);
assertEquals(polygon.getX(2), 45, 0d);
assertEquals(polygon.getY(3), -30, 0d);
assertEquals(polygon.getX(3), -45, 0d);
}
Aggregations