From 62dfc815b030ca051379a12061fb0a9aa98ca09c Mon Sep 17 00:00:00 2001 From: nknize Date: Thu, 4 Feb 2016 00:43:49 -0600 Subject: [PATCH] fix GeoRelationUtils.pointInPolygon to include points that fall on the boundary --- .../src/java/org/apache/lucene/util/GeoRelationUtils.java | 4 ++-- lucene/sandbox/src/java/org/apache/lucene/util/GeoUtils.java | 3 ++- .../src/test/org/apache/lucene/search/TestGeoPointQuery.java | 3 +++ 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lucene/sandbox/src/java/org/apache/lucene/util/GeoRelationUtils.java b/lucene/sandbox/src/java/org/apache/lucene/util/GeoRelationUtils.java index 829af266e65..9d42ba18f06 100644 --- a/lucene/sandbox/src/java/org/apache/lucene/util/GeoRelationUtils.java +++ b/lucene/sandbox/src/java/org/apache/lucene/util/GeoRelationUtils.java @@ -50,8 +50,8 @@ public class GeoRelationUtils { * TODO convert coordinates to cylindrical projection (e.g. mercator) */ for (int i = 1; i < x.length; i++) { - if (x[i] < lon && x[i-1] >= lon || x[i-1] < lon && x[i] >= lon) { - if (y[i] + (lon - x[i]) / (x[i-1] - x[i]) * (y[i-1] - y[i]) < lat) { + if (x[i] <= lon && x[i-1] >= lon || x[i-1] <= lon && x[i] >= lon) { + if (y[i] + (lon - x[i]) / (x[i-1] - x[i]) * (y[i-1] - y[i]) <= lat) { inPoly = !inPoly; } } diff --git a/lucene/sandbox/src/java/org/apache/lucene/util/GeoUtils.java b/lucene/sandbox/src/java/org/apache/lucene/util/GeoUtils.java index 030e1dd7126..5239e5adaab 100644 --- a/lucene/sandbox/src/java/org/apache/lucene/util/GeoUtils.java +++ b/lucene/sandbox/src/java/org/apache/lucene/util/GeoUtils.java @@ -218,7 +218,8 @@ public final class GeoUtils { maxLat = Math.max(polyLats[i], maxLat); } // expand bounding box by TOLERANCE factor to handle round-off error - return new GeoRect(minLon - TOLERANCE, maxLon + TOLERANCE, minLat - TOLERANCE, maxLat + TOLERANCE); + return new GeoRect(Math.max(minLon - TOLERANCE, MIN_LON_INCL), Math.min(maxLon + TOLERANCE, MAX_LON_INCL), + Math.max(minLat - TOLERANCE, MIN_LAT_INCL), Math.min(maxLat + TOLERANCE, MAX_LAT_INCL)); } /** diff --git a/lucene/sandbox/src/test/org/apache/lucene/search/TestGeoPointQuery.java b/lucene/sandbox/src/test/org/apache/lucene/search/TestGeoPointQuery.java index 465f61f26d3..9763d6e0cd5 100644 --- a/lucene/sandbox/src/test/org/apache/lucene/search/TestGeoPointQuery.java +++ b/lucene/sandbox/src/test/org/apache/lucene/search/TestGeoPointQuery.java @@ -273,6 +273,9 @@ public class TestGeoPointQuery extends BaseGeoPointTestCase { public void testWholeMap() throws Exception { TopDocs td = bboxQuery(GeoUtils.MIN_LON_INCL, GeoUtils.MIN_LAT_INCL, GeoUtils.MAX_LON_INCL, GeoUtils.MAX_LAT_INCL, 20); assertEquals("testWholeMap failed", 24, td.totalHits); + td = polygonQuery(new double[] {GeoUtils.MIN_LON_INCL, GeoUtils.MIN_LON_INCL, GeoUtils.MAX_LON_INCL, GeoUtils.MAX_LON_INCL, GeoUtils.MIN_LON_INCL}, + new double[] {GeoUtils.MIN_LAT_INCL, GeoUtils.MAX_LAT_INCL, GeoUtils.MAX_LAT_INCL, GeoUtils.MIN_LAT_INCL, GeoUtils.MIN_LAT_INCL}, 20); + assertEquals("testWholeMap failed", 24, td.totalHits); } public void smallTest() throws Exception {