You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ct...@apache.org on 2018/10/22 23:34:51 UTC

[06/50] [abbrv] lucene-solr:jira/solr-12746: LUCENE-8522: throw InvalidShapeException when constructing a polygon and all points are coplanar.

LUCENE-8522: throw InvalidShapeException when constructing a polygon
and all points are coplanar.


Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/2ab00371
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/2ab00371
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/2ab00371

Branch: refs/heads/jira/solr-12746
Commit: 2ab003714a426813b24ecf76eb93110e75c7a1b0
Parents: ba80f0f
Author: iverase <iv...@apache.org>
Authored: Mon Oct 15 08:51:34 2018 +0200
Committer: Cassandra Targett <ct...@apache.org>
Committed: Sun Oct 21 15:46:46 2018 -0500

----------------------------------------------------------------------
 .../spatial/spatial4j/Geo3dShapeFactory.java    |  3 +
 .../lucene/spatial/spatial4j/Geo3dRptTest.java  | 41 ----------
 .../lucene/spatial/spatial4j/Geo3dTest.java     | 85 ++++++++++++++++++++
 3 files changed, 88 insertions(+), 41 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/2ab00371/lucene/spatial-extras/src/java/org/apache/lucene/spatial/spatial4j/Geo3dShapeFactory.java
----------------------------------------------------------------------
diff --git a/lucene/spatial-extras/src/java/org/apache/lucene/spatial/spatial4j/Geo3dShapeFactory.java b/lucene/spatial-extras/src/java/org/apache/lucene/spatial/spatial4j/Geo3dShapeFactory.java
index ccbd6df..071c775 100644
--- a/lucene/spatial-extras/src/java/org/apache/lucene/spatial/spatial4j/Geo3dShapeFactory.java
+++ b/lucene/spatial-extras/src/java/org/apache/lucene/spatial/spatial4j/Geo3dShapeFactory.java
@@ -331,6 +331,9 @@ public class Geo3dShapeFactory implements S2ShapeFactory {
     public Shape build() {
       GeoPolygonFactory.PolygonDescription description = new GeoPolygonFactory.PolygonDescription(points, polyHoles);
       GeoPolygon polygon = GeoPolygonFactory.makeGeoPolygon(planetModel, description);
+      if (polygon == null) {
+        throw new InvalidShapeException("Invalid polygon, all points are coplanar");
+      }
       return new Geo3dShape<>(polygon, context);
     }
 

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/2ab00371/lucene/spatial-extras/src/test/org/apache/lucene/spatial/spatial4j/Geo3dRptTest.java
----------------------------------------------------------------------
diff --git a/lucene/spatial-extras/src/test/org/apache/lucene/spatial/spatial4j/Geo3dRptTest.java b/lucene/spatial-extras/src/test/org/apache/lucene/spatial/spatial4j/Geo3dRptTest.java
index eb6ed5b..6ec773b 100644
--- a/lucene/spatial-extras/src/test/org/apache/lucene/spatial/spatial4j/Geo3dRptTest.java
+++ b/lucene/spatial-extras/src/test/org/apache/lucene/spatial/spatial4j/Geo3dRptTest.java
@@ -41,7 +41,6 @@ import org.apache.lucene.spatial3d.geom.GeoPolygonFactory;
 import org.apache.lucene.spatial3d.geom.PlanetModel;
 import org.apache.lucene.spatial3d.geom.RandomGeo3dShapeGenerator;
 import org.junit.Test;
-import org.locationtech.spatial4j.context.SpatialContext;
 import org.locationtech.spatial4j.shape.Rectangle;
 import org.locationtech.spatial4j.shape.Shape;
 
@@ -164,44 +163,4 @@ public class Geo3dRptTest extends RandomSpatialOpStrategyTestCase {
     }
     testOperation(SpatialOperation.Intersects, indexedShapes, queryShapes, random().nextBoolean());
   }
-
-  //TODO move to a new test class?
-  @Test
-  public void testWKT() throws Exception {
-    Geo3dSpatialContextFactory factory = new Geo3dSpatialContextFactory();
-    SpatialContext ctx = factory.newSpatialContext();
-    String wkt = "POLYGON ((20.0 -60.4, 20.1 -60.4, 20.1 -60.3, 20.0  -60.3,20.0 -60.4))";
-    Shape s = ctx.getFormats().getWktReader().read(wkt);
-    assertTrue(s instanceof  Geo3dShape<?>);
-    wkt = "POINT (30 10)";
-    s = ctx.getFormats().getWktReader().read(wkt);
-    assertTrue(s instanceof  Geo3dShape<?>);
-    wkt = "LINESTRING (30 10, 10 30, 40 40)";
-    s = ctx.getFormats().getWktReader().read(wkt);
-    assertTrue(s instanceof  Geo3dShape<?>);
-    wkt = "POLYGON ((35 10, 45 45, 15 40, 10 20, 35 10), (20 30, 35 35, 30 20, 20 30))";
-    s = ctx.getFormats().getWktReader().read(wkt);
-    assertTrue(s instanceof  Geo3dShape<?>);
-    wkt = "MULTIPOINT ((10 40), (40 30), (20 20), (30 10))";
-    s = ctx.getFormats().getWktReader().read(wkt);
-    assertTrue(s instanceof  Geo3dShape<?>);
-    wkt = "MULTILINESTRING ((10 10, 20 20, 10 40),(40 40, 30 30, 40 20, 30 10))";
-    s = ctx.getFormats().getWktReader().read(wkt);
-    assertTrue(s instanceof  Geo3dShape<?>);
-    wkt = "MULTIPOLYGON (((40 40, 20 45, 45 30, 40 40)), ((20 35, 10 30, 10 10, 30 5, 45 20, 20 35),(30 20, 20 15, 20 25, 30 20)))";
-    s = ctx.getFormats().getWktReader().read(wkt);
-    assertTrue(s instanceof  Geo3dShape<?>);
-    wkt = "GEOMETRYCOLLECTION(POINT(4 6),LINESTRING(4 6,7 10))";
-    s = ctx.getFormats().getWktReader().read(wkt);
-    assertTrue(s instanceof  Geo3dShape<?>);
-    wkt = "ENVELOPE(1, 2, 4, 3)";
-    s = ctx.getFormats().getWktReader().read(wkt);
-    assertTrue(s instanceof  Geo3dShape<?>);
-    wkt = "BUFFER(POINT(-10 30), 5.2)";
-    s = ctx.getFormats().getWktReader().read(wkt);
-    assertTrue(s instanceof  Geo3dShape<?>);
-    //wkt = "BUFFER(LINESTRING(1 2, 3 4), 0.5)";
-    //s = ctx.getFormats().getWktReader().read(wkt);
-    //assertTrue(s instanceof  Geo3dShape<?>);
-  }
 }

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/2ab00371/lucene/spatial-extras/src/test/org/apache/lucene/spatial/spatial4j/Geo3dTest.java
----------------------------------------------------------------------
diff --git a/lucene/spatial-extras/src/test/org/apache/lucene/spatial/spatial4j/Geo3dTest.java b/lucene/spatial-extras/src/test/org/apache/lucene/spatial/spatial4j/Geo3dTest.java
new file mode 100644
index 0000000..89cb973
--- /dev/null
+++ b/lucene/spatial-extras/src/test/org/apache/lucene/spatial/spatial4j/Geo3dTest.java
@@ -0,0 +1,85 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.lucene.spatial.spatial4j;
+
+import org.apache.lucene.util.LuceneTestCase;
+import org.junit.Test;
+import org.locationtech.spatial4j.context.SpatialContext;
+import org.locationtech.spatial4j.exception.InvalidShapeException;
+import org.locationtech.spatial4j.shape.Shape;
+
+public class Geo3dTest extends LuceneTestCase {
+
+  @Test
+  public void testWKT() throws Exception {
+    Geo3dSpatialContextFactory factory = new Geo3dSpatialContextFactory();
+    SpatialContext ctx = factory.newSpatialContext();
+    String wkt = "POLYGON ((20.0 -60.4, 20.1 -60.4, 20.1 -60.3, 20.0  -60.3,20.0 -60.4))";
+    Shape s = ctx.getFormats().getWktReader().read(wkt);
+    assertTrue(s instanceof  Geo3dShape<?>);
+    wkt = "POINT (30 10)";
+    s = ctx.getFormats().getWktReader().read(wkt);
+    assertTrue(s instanceof  Geo3dShape<?>);
+    wkt = "LINESTRING (30 10, 10 30, 40 40)";
+    s = ctx.getFormats().getWktReader().read(wkt);
+    assertTrue(s instanceof  Geo3dShape<?>);
+    wkt = "POLYGON ((35 10, 45 45, 15 40, 10 20, 35 10), (20 30, 35 35, 30 20, 20 30))";
+    s = ctx.getFormats().getWktReader().read(wkt);
+    assertTrue(s instanceof  Geo3dShape<?>);
+    wkt = "MULTIPOINT ((10 40), (40 30), (20 20), (30 10))";
+    s = ctx.getFormats().getWktReader().read(wkt);
+    assertTrue(s instanceof  Geo3dShape<?>);
+    wkt = "MULTILINESTRING ((10 10, 20 20, 10 40),(40 40, 30 30, 40 20, 30 10))";
+    s = ctx.getFormats().getWktReader().read(wkt);
+    assertTrue(s instanceof  Geo3dShape<?>);
+    wkt = "MULTIPOLYGON (((40 40, 20 45, 45 30, 40 40)), ((20 35, 10 30, 10 10, 30 5, 45 20, 20 35),(30 20, 20 15, 20 25, 30 20)))";
+    s = ctx.getFormats().getWktReader().read(wkt);
+    assertTrue(s instanceof  Geo3dShape<?>);
+    wkt = "GEOMETRYCOLLECTION(POINT(4 6),LINESTRING(4 6,7 10))";
+    s = ctx.getFormats().getWktReader().read(wkt);
+    assertTrue(s instanceof  Geo3dShape<?>);
+    wkt = "ENVELOPE(1, 2, 4, 3)";
+    s = ctx.getFormats().getWktReader().read(wkt);
+    assertTrue(s instanceof  Geo3dShape<?>);
+    wkt = "BUFFER(POINT(-10 30), 5.2)";
+    s = ctx.getFormats().getWktReader().read(wkt);
+    assertTrue(s instanceof  Geo3dShape<?>);
+    //wkt = "BUFFER(LINESTRING(1 2, 3 4), 0.5)";
+    //s = ctx.getFormats().getWktReader().read(wkt);
+    //assertTrue(s instanceof  Geo3dShape<?>);
+  }
+
+  @Test
+  public void testPolygonWithCoplanarPoints() {
+    Geo3dSpatialContextFactory factory = new Geo3dSpatialContextFactory();
+    SpatialContext ctx = factory.newSpatialContext();
+
+    final String polygon = "POLYGON ((-180 90, -180 -90, 180 -90, 180 90,-180 -90))";
+    expectThrows(InvalidShapeException.class, () -> ctx.getFormats().getWktReader().read(polygon));
+
+    final String polygonWithHole = "POLYGON ((35 10, 45 45, 15 40, 10 20, 35 10), (20 30, 20 30, 20 30))";
+    expectThrows(InvalidShapeException.class, () -> ctx.getFormats().getWktReader().read(polygonWithHole));
+
+    final String geometryCollection = "GEOMETRYCOLLECTION(POINT(4 6), LINESTRING(4 6,7 10), POLYGON ((-180 90, -180 -90, 180 -90, 180 90,-180 -90)))";
+    expectThrows(InvalidShapeException.class, () -> ctx.getFormats().getWktReader().read(geometryCollection));
+
+    final String multiPolygon = "MULTIPOLYGON (((40 40, 20 45, 45 30, 40 40)), ((20 35, 10 30, 10 10, 30 5, 45 20, 20 35),(30 20, 20 15, 20 25, 30 20)), ((180 90, 90 90, 180 90)))";
+    expectThrows(InvalidShapeException.class, () -> ctx.getFormats().getWktReader().read(multiPolygon));
+
+  }
+}