From 18d56f154c78d1bdd6a7d5d9992966926b0b3426 Mon Sep 17 00:00:00 2001 From: Nicholas Knize Date: Tue, 16 Dec 2014 10:15:22 -0600 Subject: [PATCH] Adding unit tests for clockwise non-OGC ordering Adding unit tests to validate cw defined polys not-crossing and crossing the dateline, respectively --- .../common/geo/GeoJSONShapeParserTests.java | 102 +++++++++++++++++- .../common/geo/ShapeBuilderTests.java | 10 +- 2 files changed, 103 insertions(+), 9 deletions(-) diff --git a/src/test/java/org/elasticsearch/common/geo/GeoJSONShapeParserTests.java b/src/test/java/org/elasticsearch/common/geo/GeoJSONShapeParserTests.java index e41f8db61bf..de1bf890609 100644 --- a/src/test/java/org/elasticsearch/common/geo/GeoJSONShapeParserTests.java +++ b/src/test/java/org/elasticsearch/common/geo/GeoJSONShapeParserTests.java @@ -212,7 +212,7 @@ public class GeoJSONShapeParserTests extends ElasticsearchTestCase { @Test public void testParse_OGCPolygonWithoutHoles() throws IOException { - // test ccw poly not crossing dateline + // test 1: ccw poly not crossing dateline String polygonGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "Polygon") .startArray("coordinates") .startArray() @@ -232,7 +232,7 @@ public class GeoJSONShapeParserTests extends ElasticsearchTestCase { ElasticsearchGeoAssertions.assertPolygon(shape); - // test ccw poly crossing dateline + // test 2: ccw poly crossing dateline polygonGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "Polygon") .startArray("coordinates") .startArray() @@ -251,11 +251,51 @@ public class GeoJSONShapeParserTests extends ElasticsearchTestCase { 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(180.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() + .endArray() + .endObject().string(); + + parser = JsonXContent.jsonXContent.createParser(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(176.0).value(15.0).endArray() + .startArray().value(184.0).value(15.0).endArray() + .startArray().value(184.0).value(0.0).endArray() + .startArray().value(176.0).value(-15.0).endArray() + .startArray().value(174.0).value(-10.0).endArray() + .startArray().value(176.0).value(15.0).endArray() + .endArray() + .endArray() + .endObject().string(); + + parser = JsonXContent.jsonXContent.createParser(polygonGeoJson); + parser.nextToken(); + shape = ShapeBuilder.parse(parser).build(); + + ElasticsearchGeoAssertions.assertMultiPolygon(shape); } @Test public void testParse_OGCPolygonWithHoles() throws IOException { - // test ccw poly not crossing dateline + // test 1: ccw poly not crossing dateline String polygonGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "Polygon") .startArray("coordinates") .startArray() @@ -281,7 +321,7 @@ public class GeoJSONShapeParserTests extends ElasticsearchTestCase { ElasticsearchGeoAssertions.assertPolygon(shape); - // test ccw poly crossing dateline + // test 2: ccw poly crossing dateline polygonGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "Polygon") .startArray("coordinates") .startArray() @@ -306,6 +346,58 @@ public class GeoJSONShapeParserTests extends ElasticsearchTestCase { 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 = JsonXContent.jsonXContent.createParser(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 = JsonXContent.jsonXContent.createParser(polygonGeoJson); + parser.nextToken(); + shape = ShapeBuilder.parse(parser).build(); + + ElasticsearchGeoAssertions.assertMultiPolygon(shape); } @Test @@ -538,7 +630,7 @@ public class GeoJSONShapeParserTests extends ElasticsearchTestCase { @Test public void testParse_geometryCollection() throws IOException { String geometryCollectionGeoJson = XContentFactory.jsonBuilder().startObject() - .field("type","GeometryCollection") + .field("type", "GeometryCollection") .startArray("geometries") .startObject() .field("type", "LineString") diff --git a/src/test/java/org/elasticsearch/common/geo/ShapeBuilderTests.java b/src/test/java/org/elasticsearch/common/geo/ShapeBuilderTests.java index 252b2402eec..cb9c53846f2 100644 --- a/src/test/java/org/elasticsearch/common/geo/ShapeBuilderTests.java +++ b/src/test/java/org/elasticsearch/common/geo/ShapeBuilderTests.java @@ -241,8 +241,9 @@ public class ShapeBuilderTests extends ElasticsearchTestCase { @Test public void testDatelineOGC() { - // view shape at https://gist.github.com/anonymous/7f1bb6d7e9cd72f5977c - // expect 3 polygons, 1 with a hole + // tests that the following shape (defined in counterclockwise OGC order) + // https://gist.github.com/anonymous/7f1bb6d7e9cd72f5977c crosses the dateline + // expected results: 3 polygons, 1 with a hole // a giant c shape PolygonBuilder builder = ShapeBuilder.newPolygon() @@ -283,8 +284,9 @@ public class ShapeBuilderTests extends ElasticsearchTestCase { @Test public void testDateline() { - // view shape at https://gist.github.com/anonymous/7f1bb6d7e9cd72f5977c - // expect 3 polygons, 1 with a hole + // tests that the following shape (defined in clockwise non-OGC order) + // https://gist.github.com/anonymous/7f1bb6d7e9cd72f5977c crosses the dateline + // expected results: 3 polygons, 1 with a hole // a giant c shape PolygonBuilder builder = ShapeBuilder.newPolygon()