From faa78ad72cffaccbb49315c8aabe10cd27379f58 Mon Sep 17 00:00:00 2001 From: Nicholas Knize Date: Thu, 18 Apr 2019 13:14:27 -0500 Subject: [PATCH] LUCENE-8736: Fix Polygon.contains to approriately check longitude range, and pass correct line segment vertices in EdgeTree --- lucene/core/src/java/org/apache/lucene/geo/EdgeTree.java | 2 ++ lucene/core/src/java/org/apache/lucene/geo/Polygon2D.java | 4 +++- .../src/java/org/apache/lucene/geo/GeoTestUtil.java | 4 +++- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lucene/core/src/java/org/apache/lucene/geo/EdgeTree.java b/lucene/core/src/java/org/apache/lucene/geo/EdgeTree.java index cbecd25ddda..bc81dcd1f00 100644 --- a/lucene/core/src/java/org/apache/lucene/geo/EdgeTree.java +++ b/lucene/core/src/java/org/apache/lucene/geo/EdgeTree.java @@ -140,6 +140,8 @@ public abstract class EdgeTree { double maxLon = StrictMath.max(StrictMath.max(ax, bx), cx); if (maxLon < this.minLon || minLon > this.maxLon || maxLat < this.minLat || minLat > this.maxLat) { return Relation.CELL_OUTSIDE_QUERY; + } else if (bx == cx && by == cy) { + return componentRelateTriangle(bx, by, ax, ay, cx, cy); } return componentRelateTriangle(ax, ay, bx, by, cx, cy); } diff --git a/lucene/core/src/java/org/apache/lucene/geo/Polygon2D.java b/lucene/core/src/java/org/apache/lucene/geo/Polygon2D.java index 7db8287f61b..277d02b7954 100644 --- a/lucene/core/src/java/org/apache/lucene/geo/Polygon2D.java +++ b/lucene/core/src/java/org/apache/lucene/geo/Polygon2D.java @@ -271,7 +271,9 @@ public final class Polygon2D extends EdgeTree { if (isOnEdge.get() == false && lat <= edge.max) { if (lat == edge.lat1 && lat == edge.lat2 || (lat <= edge.lat1 && lat >= edge.lat2) != (lat >= edge.lat1 && lat <= edge.lat2)) { - if (GeoUtils.orient(edge.lon1, edge.lat1, edge.lon2, edge.lat2, lon, lat) == 0) { + if ((lon == edge.lon1 && lon == edge.lon2) || + ((lon <= edge.lon1 && lon >= edge.lon2) != (lon >= edge.lon1 && lon <= edge.lon2) && + GeoUtils.orient(edge.lon1, edge.lat1, edge.lon2, edge.lat2, lon, lat) == 0)) { // if its on the boundary return true isOnEdge.set(true); return true; diff --git a/lucene/test-framework/src/java/org/apache/lucene/geo/GeoTestUtil.java b/lucene/test-framework/src/java/org/apache/lucene/geo/GeoTestUtil.java index de21ee24024..c5a4afe56b4 100644 --- a/lucene/test-framework/src/java/org/apache/lucene/geo/GeoTestUtil.java +++ b/lucene/test-framework/src/java/org/apache/lucene/geo/GeoTestUtil.java @@ -698,7 +698,9 @@ public class GeoTestUtil { for (i = 0, j = 1; j < nvert; ++i, ++j) { if (testy == verty[j] && testy == verty[i] || ((testy <= verty[j] && testy >= verty[i]) != (testy >= verty[j] && testy <= verty[i]))) { - if (GeoUtils.orient(vertx[i], verty[i], vertx[j], verty[j], testx, testy) == 0) { + if ((testx == vertx[j] && testx == vertx[i]) || + ((testx <= vertx[j] && testx >= vertx[i]) != (testx >= vertx[j] && testx <= vertx[i]) && + GeoUtils.orient(vertx[i], verty[i], vertx[j], verty[j], testx, testy) == 0)) { // return true if point is on boundary return true; } else if ( ((verty[i] > testy) != (verty[j] > testy)) &&