Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

LUCENE-6956: Fix polygon approximation for LatLonPoint Queries

Nick Knizef3ca357802be
diff --git a/lucene/sandbox/src/java/org/apache/lucene/search/GeoPointInBBoxQueryImpl.java b/lucene/sandbox/src/java/org/apache/lucene/search/GeoPointInBBoxQueryImpl.java
index c6918a24102..f192c736210 100644
--- a/lucene/sandbox/src/java/org/apache/lucene/search/GeoPointInBBoxQueryImpl.java
+++ b/lucene/sandbox/src/java/org/apache/lucene/search/GeoPointInBBoxQueryImpl.java
@@ -24,9 +24,7 @@ import org.apache.lucene.index.Terms;
import org.apache.lucene.index.TermsEnum;
import org.apache.lucene.util.AttributeSource;
import org.apache.lucene.util.GeoRelationUtils;
-import org.apache.lucene.util.GeoUtils;
import org.apache.lucene.util.SloppyMath;
-import org.apache.lucene.util.ToStringUtils;
/** Package private implementation for the public facing GeoPointInBBoxQuery delegate class.
*
@@ -102,7 +100,7 @@ class GeoPointInBBoxQueryImpl extends GeoPointTermQuery {
@Override
protected boolean postFilter(final double lon, final double lat) {
- return GeoRelationUtils.pointInRect(lon, lat, minLon, minLat, maxLon, maxLat);
+ return GeoRelationUtils.pointInRectPrecise(lon, lat, minLon, minLat, maxLon, maxLat);
}
}
diff --git a/lucene/sandbox/src/java/org/apache/lucene/search/GeoPointInPolygonQuery.java b/lucene/sandbox/src/java/org/apache/lucene/search/GeoPointInPolygonQuery.java
index 5139cb68f19..950beaeff1d 100644
--- a/lucene/sandbox/src/java/org/apache/lucene/search/GeoPointInPolygonQuery.java
+++ b/lucene/sandbox/src/java/org/apache/lucene/search/GeoPointInPolygonQuery.java
@@ -148,14 +148,14 @@ public final class GeoPointInPolygonQuery extends GeoPointInBBoxQueryImpl {
@Override
protected boolean cellCrosses(final double minLon, final double minLat, final double maxLon, final double maxLat) {
- return GeoRelationUtils.rectCrossesPoly(minLon, minLat, maxLon, maxLat, x, y, GeoPointInPolygonQuery.this.minLon,
+ return GeoRelationUtils.rectCrossesPolyApprox(minLon, minLat, maxLon, maxLat, x, y, GeoPointInPolygonQuery.this.minLon,
GeoPointInPolygonQuery.this.minLat, GeoPointInPolygonQuery.this.maxLon, GeoPointInPolygonQuery.this.maxLat);
}
@Override
protected boolean cellWithin(final double minLon, final double minLat, final double maxLon, final double maxLat) {
- return GeoRelationUtils.rectWithinPoly(minLon, minLat, maxLon, maxLat, x, y, GeoPointInPolygonQuery.this.minLon,
- GeoPointInPolygonQuery.this.minLat, GeoPointInPolygonQuery.this.maxLon, GeoPointInPolygonQuery.this.maxLat, true);
+ return GeoRelationUtils.rectWithinPolyApprox(minLon, minLat, maxLon, maxLat, x, y, GeoPointInPolygonQuery.this.minLon,
+ GeoPointInPolygonQuery.this.minLat, GeoPointInPolygonQuery.this.maxLon, GeoPointInPolygonQuery.this.maxLat);
}
@Override
diff --git a/lucene/sandbox/src/java/org/apache/lucene/search/GeoPointTermsEnum.java b/lucene/sandbox/src/java/org/apache/lucene/search/GeoPointTermsEnum.java
index e4faa232129..38f4d220a8f 100644
--- a/lucene/sandbox/src/java/org/apache/lucene/search/GeoPointTermsEnum.java
+++ b/lucene/sandbox/src/java/org/apache/lucene/search/GeoPointTermsEnum.java
@@ -62,7 +62,7 @@ abstract class GeoPointTermsEnum extends FilteredTermsEnum {
this.maxLat = GeoUtils.mortonUnhashLat(rectMaxHash);
DETAIL_LEVEL = (short)(((GeoUtils.BITS<<1)-computeMaxShift())/2);
- computeRange(0L, (short) (((GeoUtils.BITS) << 1) - 1));
+ computeRange(0L, (short) ((GeoUtils.BITS << 1) - 1));
assert rangeBounds.isEmpty() == false;
Collections.sort(rangeBounds);
}
diff --git a/lucene/sandbox/src/java/org/apache/lucene/search/PointInPolygonQuery.java b/lucene/sandbox/src/java/org/apache/lucene/search/PointInPolygonQuery.java
index abdc5b9fdcb..81e49e77cd1 100644
--- a/lucene/sandbox/src/java/org/apache/lucene/search/PointInPolygonQuery.java
+++ b/lucene/sandbox/src/java/org/apache/lucene/search/PointInPolygonQuery.java
@@ -142,11 +142,11 @@ public class PointInPolygonQuery extends Query {
if (cellMinLat <= minLat && cellMaxLat >= maxLat && cellMinLon <= minLon && cellMaxLon >= maxLon) {
// Cell fully encloses the query
return Relation.CELL_CROSSES_QUERY;
- } else if (GeoRelationUtils.rectWithinPoly(cellMinLon, cellMinLat, cellMaxLon, cellMaxLat,
+ } else if (GeoRelationUtils.rectWithinPolyPrecise(cellMinLon, cellMinLat, cellMaxLon, cellMaxLat,
polyLons, polyLats,
minLon, minLat, maxLon, maxLat)) {
return Relation.CELL_INSIDE_QUERY;
- } else if (GeoRelationUtils.rectCrossesPoly(cellMinLon, cellMinLat, cellMaxLon, cellMaxLat,
+ } else if (GeoRelationUtils.rectCrossesPolyPrecise(cellMinLon, cellMinLat, cellMaxLon, cellMaxLat,
polyLons, polyLats,
minLon, minLat, maxLon, maxLat)) {
return Relation.CELL_CROSSES_QUERY;
diff --git a/lucene/sandbox/src/java/org/apache/lucene/util/GeoDistanceUtils.java b/lucene/sandbox/src/java/org/apache/lucene/util/GeoDistanceUtils.java
index b389815c0bf..8ad09b9fbce 100644
--- a/lucene/sandbox/src/java/org/apache/lucene/util/GeoDistanceUtils.java
+++ b/lucene/sandbox/src/java/org/apache/lucene/util/GeoDistanceUtils.java
@@ -25,6 +25,8 @@ import static org.apache.lucene.util.SloppyMath.TO_RADIANS;
* @lucene.experimental
*/
public class GeoDistanceUtils {
+ /** error threshold for point-distance queries (in percent) NOTE: Guideline from USGS is 0.005 **/
+ public static final double DISTANCE_PCT_ERR = 0.005;
/**
* Compute the great-circle distance using original haversine implementation published by Sinnot in:
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 03d308e2931..829af266e65 100644
--- a/lucene/sandbox/src/java/org/apache/lucene/util/GeoRelationUtils.java
+++ b/lucene/sandbox/src/java/org/apache/lucene/util/GeoRelationUtils.java
@@ -27,10 +27,9 @@ public class GeoRelationUtils {
* NOTE: this is a basic method that does not handle dateline or pole crossing. Unwrapping must be done before
* calling this method.
*/
- public static boolean pointInRect(final double lon, final double lat, final double minLon,
- final double minLat, final double maxLon, final double maxLat) {
- return (GeoUtils.compare(lon, minLon) >= 0 && GeoUtils.compare(lon, maxLon) <= 0
- && GeoUtils.compare(lat, minLat) >= 0 && GeoUtils.compare(lat, maxLat) <= 0);
+ public static boolean pointInRectPrecise(final double lon, final double lat, final double minLon,
+ final double minLat, final double maxLon, final double maxLat) {
+ return lon >= minLon && lon <= maxLon && lat >= minLat && lat <= maxLat;
}
/**
@@ -60,6 +59,10 @@ public class GeoRelationUtils {
return inPoly;
}
+ /////////////////////////
+ // Rectangle relations
+ /////////////////////////
+
public static boolean rectDisjoint(final double aMinX, final double aMinY, final double aMaxX, final double aMaxY,
final double bMinX, final double bMinY, final double bMaxX, final double bMaxY) {
return (aMaxX < bMinX || aMinX > bMaxX || aMaxY < bMinY || aMinY > bMaxY);
@@ -95,10 +98,14 @@ public class GeoRelationUtils {
return !((aMaxX < bMinX || aMinX > bMaxX || aMaxY < bMinY || aMinY > bMaxY) );
}
+ /////////////////////////
+ // Polygon relations
+ /////////////////////////
+
/**
- * Computes whether a rectangle crosses a shape. (touching not allowed)
+ * Convenience method for accurately computing whether a rectangle crosses a poly
*/
- public static boolean rectCrossesPoly(final double rMinX, final double rMinY, final double rMaxX,
+ public static boolean rectCrossesPolyPrecise(final double rMinX, final double rMinY, final double rMaxX,
final double rMaxY, final double[] shapeX, final double[] shapeY,
final double sMinX, final double sMinY, final double sMaxX,
final double sMaxY) {
@@ -106,6 +113,21 @@ public class GeoRelationUtils {
if (rectDisjoint(rMinX, rMinY, rMaxX, rMaxY, sMinX, sMinY, sMaxX, sMaxY)) {
return false;
}
+ return rectCrossesPoly(rMinX, rMinY, rMaxX, rMaxY, shapeX, shapeY);
+ }
+
+ /**
+ * Compute whether a rectangle crosses a shape. (touching not allowed) Includes a flag for approximating the
+ * relation.
+ */
+ public static boolean rectCrossesPolyApprox(final double rMinX, final double rMinY, final double rMaxX,
+ final double rMaxY, final double[] shapeX, final double[] shapeY,
+ final double sMinX, final double sMinY, final double sMaxX,
+ final double sMaxY) {
+ // short-circuit: if the bounding boxes are disjoint then the shape does not cross
+ if (rectDisjoint(rMinX, rMinY, rMaxX, rMaxY, sMinX, sMinY, sMaxX, sMaxY)) {
+ return false;
+ }
final int polyLength = shapeX.length-1;
for (short p=0; p<polyLength; ++p) {
@@ -116,11 +138,57 @@ public class GeoRelationUtils {
return false;
}
+ /**
+ * Accurately compute (within restrictions of cartesian decimal degrees) whether a rectangle crosses a polygon
+ */
+ private static boolean rectCrossesPoly(final double rMinX, final double rMinY, final double rMaxX,
+ final double rMaxY, final double[] shapeX, final double[] shapeY) {
+ final double[][] bbox = new double[][] { {rMinX, rMinY}, {rMaxX, rMinY}, {rMaxX, rMaxY}, {rMinX, rMaxY}, {rMinX, rMinY} };
+ final int polyLength = shapeX.length-1;
+ double d, s, t, a1, b1, c1, a2, b2, c2;
+ double x00, y00, x01, y01, x10, y10, x11, y11;
+
+ // computes the intersection point between each bbox edge and the polygon edge
+ for (short b=0; b<4; ++b) {
+ a1 = bbox[b+1][1]-bbox[b][1];
+ b1 = bbox[b][0]-bbox[b+1][0];
+ c1 = a1*bbox[b+1][0] + b1*bbox[b+1][1];
+ for (int p=0; p<polyLength; ++p) {
+ a2 = shapeY[p+1]-shapeY[p];
+ b2 = shapeX[p]-shapeX[p+1];
+ // compute determinant
+ d = a1*b2 - a2*b1;
+ if (d != 0) {
+ // lines are not parallel, check intersecting points
+ c2 = a2*shapeX[p+1] + b2*shapeY[p+1];
+ s = (1/d)*(b2*c1 - b1*c2);
+ t = (1/d)*(a1*c2 - a2*c1);
+ x00 = StrictMath.min(bbox[b][0], bbox[b+1][0]) - GeoUtils.TOLERANCE;
+ x01 = StrictMath.max(bbox[b][0], bbox[b+1][0]) + GeoUtils.TOLERANCE;
+ y00 = StrictMath.min(bbox[b][1], bbox[b+1][1]) - GeoUtils.TOLERANCE;
+ y01 = StrictMath.max(bbox[b][1], bbox[b+1][1]) + GeoUtils.TOLERANCE;
+ x10 = StrictMath.min(shapeX[p], shapeX[p+1]) - GeoUtils.TOLERANCE;
+ x11 = StrictMath.max(shapeX[p], shapeX[p+1]) + GeoUtils.TOLERANCE;
+ y10 = StrictMath.min(shapeY[p], shapeY[p+1]) - GeoUtils.TOLERANCE;
+ y11 = StrictMath.max(shapeY[p], shapeY[p+1]) + GeoUtils.TOLERANCE;
+ // check whether the intersection point is touching one of the line segments
+ boolean touching = ((x00 == s && y00 == t) || (x01 == s && y01 == t))
+ || ((x10 == s && y10 == t) || (x11 == s && y11 == t));
+ // if line segments are not touching and the intersection point is within the range of either segment
+ if (!(touching || x00 > s || x01 < s || y00 > t || y01 < t || x10 > s || x11 < s || y10 > t || y11 < t)) {
+ return true;
+ }
+ }
+ } // for each poly edge
+ } // for each bbox edge
+ return false;
+ }
+
private static boolean lineCrossesRect(double aX1, double aY1, double aX2, double aY2,
final double rMinX, final double rMinY, final double rMaxX, final double rMaxY) {
// short-circuit: if one point inside rect, other outside
- if (pointInRect(aX1, aY1, rMinX, rMinY, rMaxX, rMaxY) ?
- !pointInRect(aX2, aY2, rMinX, rMinY, rMaxX, rMaxY) : pointInRect(aX2, aY2, rMinX, rMinY, rMaxX, rMaxY)) {
+ if (pointInRectPrecise(aX1, aY1, rMinX, rMinY, rMaxX, rMaxY) ?
+ !pointInRectPrecise(aX2, aY2, rMinX, rMinY, rMaxX, rMaxY) : pointInRectPrecise(aX2, aY2, rMinX, rMinY, rMaxX, rMaxY)) {
return true;
}
@@ -166,36 +234,38 @@ public class GeoRelationUtils {
return false;
}
- public static boolean rectWithinPoly(final double rMinX, final double rMinY, final double rMaxX, final double rMaxY,
+ public static boolean rectWithinPolyPrecise(final double rMinX, final double rMinY, final double rMaxX, final double rMaxY,
final double[] shapeX, final double[] shapeY, final double sMinX,
final double sMinY, final double sMaxX, final double sMaxY) {
- return rectWithinPoly(rMinX, rMinY, rMaxX, rMaxY, shapeX, shapeY, sMinX, sMinY, sMaxX, sMaxY, false);
+ // check if rectangle crosses poly (to handle concave/pacman polys), then check that all 4 corners
+ // are contained
+ return !(rectCrossesPolyPrecise(rMinX, rMinY, rMaxX, rMaxY, shapeX, shapeY, sMinX, sMinY, sMaxX, sMaxY) ||
+ !pointInPolygon(shapeX, shapeY, rMinY, rMinX) || !pointInPolygon(shapeX, shapeY, rMinY, rMaxX) ||
+ !pointInPolygon(shapeX, shapeY, rMaxY, rMaxX) || !pointInPolygon(shapeX, shapeY, rMaxY, rMinX));
}
/**
* Computes whether a rectangle is within a given polygon (shared boundaries allowed)
*/
- public static boolean rectWithinPoly(final double rMinX, final double rMinY, final double rMaxX, final double rMaxY,
+ public static boolean rectWithinPolyApprox(final double rMinX, final double rMinY, final double rMaxX, final double rMaxY,
final double[] shapeX, final double[] shapeY, final double sMinX,
- final double sMinY, final double sMaxX, final double sMaxY, boolean approx) {
+ final double sMinY, final double sMaxX, final double sMaxY) {
// approximation: check if rectangle crosses poly (to handle concave/pacman polys), then check one of the corners
// are contained
- if (approx == true) {
- // short-cut: if bounding boxes cross, rect is not within
- if (rectCrosses(rMinX, rMinY, rMaxX, rMaxY, sMinX, sMinY, sMaxX, sMaxY) == true) {
- return false;
- }
- return !(rectCrossesPoly(rMinX, rMinY, rMaxX, rMaxY, shapeX, shapeY, sMinX, sMinY, sMaxX, sMaxY) ||
- !pointInPolygon(shapeX, shapeY, rMinY, rMinX));
- }
- // check if rectangle crosses poly (to handle concave/pacman polys), then check that all 4 corners
- // are contained
- return !(rectCrossesPoly(rMinX, rMinY, rMaxX, rMaxY, shapeX, shapeY, sMinX, sMinY, sMaxX, sMaxY) ||
- !pointInPolygon(shapeX, shapeY, rMinY, rMinX) || !pointInPolygon(shapeX, shapeY, rMinY, rMaxX) ||
- !pointInPolygon(shapeX, shapeY, rMaxY, rMaxX) || !pointInPolygon(shapeX, shapeY, rMaxY, rMinX));
+ // short-cut: if bounding boxes cross, rect is not within
+ if (rectCrosses(rMinX, rMinY, rMaxX, rMaxY, sMinX, sMinY, sMaxX, sMaxY) == true) {
+ return false;
+ }
+
+ return !(rectCrossesPolyApprox(rMinX, rMinY, rMaxX, rMaxY, shapeX, shapeY, sMinX, sMinY, sMaxX, sMaxY)
+ || !pointInPolygon(shapeX, shapeY, rMinY, rMinX));
}
+ /////////////////////////
+ // Circle relations
+ /////////////////////////
+
private static boolean rectAnyCornersInCircle(final double rMinX, final double rMinY, final double rMaxX,
final double rMaxY, final double centerLon, final double centerLat,
final double radiusMeters, final boolean approx) {
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 ea329b5c375..030e1dd7126 100644
--- a/lucene/sandbox/src/java/org/apache/lucene/util/GeoUtils.java
+++ b/lucene/sandbox/src/java/org/apache/lucene/util/GeoUtils.java
@@ -217,9 +217,8 @@ public final class GeoUtils {
minLat = Math.min(polyLats[i], minLat);
maxLat = Math.max(polyLats[i], maxLat);
}
-
- return new GeoRect(GeoUtils.unscaleLon(GeoUtils.scaleLon(minLon)), GeoUtils.unscaleLon(GeoUtils.scaleLon(maxLon)),
- GeoUtils.unscaleLat(GeoUtils.scaleLat(minLat)), GeoUtils.unscaleLat(GeoUtils.scaleLat(maxLat)));
+ // expand bounding box by TOLERANCE factor to handle round-off error
+ return new GeoRect(minLon - TOLERANCE, maxLon + TOLERANCE, minLat - TOLERANCE, maxLat + TOLERANCE);
}
/**
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 e674a95483c..465f61f26d3 100644
--- a/lucene/sandbox/src/test/org/apache/lucene/search/TestGeoPointQuery.java
+++ b/lucene/sandbox/src/test/org/apache/lucene/search/TestGeoPointQuery.java
@@ -35,6 +35,8 @@ import org.apache.lucene.util.TestUtil;
import org.junit.AfterClass;
import org.junit.BeforeClass;
+import static org.apache.lucene.util.GeoDistanceUtils.DISTANCE_PCT_ERR;
+
/**
* Unit testing for basic GeoPoint query logic
*
@@ -47,13 +49,9 @@ public class TestGeoPointQuery extends BaseGeoPointTestCase {
private static IndexReader reader = null;
private static IndexSearcher searcher = null;
- // error threshold for point-distance queries (in percent) NOTE: Guideline from USGS
- private static final double DISTANCE_PCT_ERR = 0.005;
-
@Override
protected boolean forceSmall() {
- // TODO: GeoUtils are potentially slow if we use small=false with heavy testing
- return true;
+ return false;
}
@Override
@@ -172,11 +170,11 @@ public class TestGeoPointQuery extends BaseGeoPointTestCase {
}
if (rect.minLon < rect.maxLon) {
- return GeoRelationUtils.pointInRect(pointLon, pointLat, rect.minLon, rect.minLat, rect.maxLon, rect.maxLat);
+ return GeoRelationUtils.pointInRectPrecise(pointLon, pointLat, rect.minLon, rect.minLat, rect.maxLon, rect.maxLat);
} else {
// Rect crosses dateline:
- return GeoRelationUtils.pointInRect(pointLon, pointLat, -180.0, rect.minLat, rect.maxLon, rect.maxLat)
- || GeoRelationUtils.pointInRect(pointLon, pointLat, rect.minLon, rect.minLat, 180.0, rect.maxLat);
+ return GeoRelationUtils.pointInRectPrecise(pointLon, pointLat, -180.0, rect.minLat, rect.maxLon, rect.maxLat)
+ || GeoRelationUtils.pointInRectPrecise(pointLon, pointLat, rect.minLon, rect.minLat, 180.0, rect.maxLat);
}
}
@@ -262,9 +260,9 @@ public class TestGeoPointQuery extends BaseGeoPointTestCase {
double yMax = 1;//5;
// test cell crossing poly
- assertTrue(GeoRelationUtils.rectCrossesPoly(xMin, yMin, xMax, yMax, px, py, xMinA, yMinA, xMaxA, yMaxA));
- assertFalse(GeoRelationUtils.rectCrossesPoly(-5, 0, 0.000001, 5, px, py, xMin, yMin, xMax, yMax));
- assertTrue(GeoRelationUtils.rectWithinPoly(-5, 0, -2, 5, px, py, xMin, yMin, xMax, yMax));
+ assertTrue(GeoRelationUtils.rectCrossesPolyApprox(xMin, yMin, xMax, yMax, px, py, xMinA, yMinA, xMaxA, yMaxA));
+ assertFalse(GeoRelationUtils.rectCrossesPolyApprox(-5, 0, 0.000001, 5, px, py, xMin, yMin, xMax, yMax));
+ assertTrue(GeoRelationUtils.rectWithinPolyApprox(-5, 0, -2, 5, px, py, xMin, yMin, xMax, yMax));
}
public void testBBoxCrossDateline() throws Exception {
diff --git a/lucene/sandbox/src/test/org/apache/lucene/search/TestLatLonPointQueries.java b/lucene/sandbox/src/test/org/apache/lucene/search/TestLatLonPointQueries.java
index 26a52826d16..1a9e2c7ae89 100644
--- a/lucene/sandbox/src/test/org/apache/lucene/search/TestLatLonPointQueries.java
+++ b/lucene/sandbox/src/test/org/apache/lucene/search/TestLatLonPointQueries.java
@@ -24,6 +24,8 @@ import org.apache.lucene.util.GeoDistanceUtils;
import org.apache.lucene.util.GeoRect;
public class TestLatLonPointQueries extends BaseGeoPointTestCase {
+ // todo deconflict GeoPoint and BKD encoding methods and error tolerance
+ public static final double BKD_TOLERANCE = 1e-7;
@Override
protected void addPointToDoc(String field, Document doc, double lat, double lon) {
@@ -56,6 +58,14 @@ public class TestLatLonPointQueries extends BaseGeoPointTestCase {
assert Double.isNaN(pointLat) == false;
+ // false positive/negatives due to quantization error exist for both rectangles and polygons
+ if (compare(pointLat, rect.minLat) == 0
+ || compare(pointLat, rect.maxLat) == 0
+ || compare(pointLon, rect.minLon) == 0
+ || compare(pointLon, rect.maxLon) == 0) {
+ return null;
+ }
+
int rectLatMinEnc = LatLonPoint.encodeLat(rect.minLat);
int rectLatMaxEnc = LatLonPoint.encodeLat(rect.maxLat);
int rectLonMinEnc = LatLonPoint.encodeLon(rect.minLon);
@@ -78,19 +88,16 @@ public class TestLatLonPointQueries extends BaseGeoPointTestCase {
}
}
- private static final double POLY_TOLERANCE = 1e-7;
+ // todo reconcile with GeoUtils (see LUCENE-6996)
+ public static double compare(final double v1, final double v2) {
+ final double delta = v1-v2;
+ return Math.abs(delta) <= BKD_TOLERANCE ? 0 : delta;
+ }
@Override
protected Boolean polyRectContainsPoint(GeoRect rect, double pointLat, double pointLon) {
- if (Math.abs(rect.minLat-pointLat) < POLY_TOLERANCE ||
- Math.abs(rect.maxLat-pointLat) < POLY_TOLERANCE ||
- Math.abs(rect.minLon-pointLon) < POLY_TOLERANCE ||
- Math.abs(rect.maxLon-pointLon) < POLY_TOLERANCE) {
- // The poly check quantizes slightly differently, so we allow for boundary cases to disagree
- return null;
- } else {
- return rectContainsPoint(rect, pointLat, pointLon);
- }
+ // TODO write better random polygon tests
+ return rectContainsPoint(rect, pointLat, pointLon);
}
@Override
diff --git a/lucene/sandbox/src/test/org/apache/lucene/util/BaseGeoPointTestCase.java b/lucene/sandbox/src/test/org/apache/lucene/util/BaseGeoPointTestCase.java
index 91f74803909..b3d711fc980 100644
--- a/lucene/sandbox/src/test/org/apache/lucene/util/BaseGeoPointTestCase.java
+++ b/lucene/sandbox/src/test/org/apache/lucene/util/BaseGeoPointTestCase.java
@@ -436,8 +436,7 @@ public abstract class BaseGeoPointTestCase extends LuceneTestCase {
} else {
result = -90 + 180.0 * random().nextDouble();
}
- // TODO: we should not do this here! it weakens the test, and users don't pre-quantize the lat/lons they send us:
- return unscaleLat(scaleLat(result));
+ return result;
}
public double randomLon(boolean small) {
@@ -447,8 +446,7 @@ public abstract class BaseGeoPointTestCase extends LuceneTestCase {
} else {
result = -180 + 360.0 * random().nextDouble();
}
- // TODO: we should not do this here! it weakens the test, and users don't pre-quantize the lat/lons they send us:
- return unscaleLon(scaleLon(result));
+ return result;
}
protected GeoRect randomRect(boolean small, boolean canCrossDateLine) {
diff --git a/lucene/sandbox/src/test/org/apache/lucene/util/TestGeoUtils.java b/lucene/sandbox/src/test/org/apache/lucene/util/TestGeoUtils.java
index 4d29a85b8b9..cf0576d7f33 100644
--- a/lucene/sandbox/src/test/org/apache/lucene/util/TestGeoUtils.java
+++ b/lucene/sandbox/src/test/org/apache/lucene/util/TestGeoUtils.java
@@ -29,6 +29,8 @@ import org.junit.BeforeClass;
import com.carrotsearch.randomizedtesting.generators.RandomInts;
+import static org.apache.lucene.util.GeoDistanceUtils.DISTANCE_PCT_ERR;
+
/**
* Tests class for methods in GeoUtils
*
@@ -272,7 +274,7 @@ public class TestGeoUtils extends LuceneTestCase {
} else {
result = -90 + 180.0 * random().nextDouble();
}
- return unscaleLat(scaleLat(result));
+ return result;
}
public double randomLon(boolean small) {
@@ -282,7 +284,7 @@ public class TestGeoUtils extends LuceneTestCase {
} else {
result = -180 + 360.0 * random().nextDouble();
}
- return unscaleLon(scaleLon(result));
+ return result;
}
private void findMatches(Set<Integer> hits, PrintWriter log, Cell root,
@@ -450,7 +452,8 @@ public class TestGeoUtils extends LuceneTestCase {
double radiusMeters;
- // TODO: GeoUtils APIs are still buggy for large distances:
+ // TODO: large exotic rectangles created by BKD may be inaccurate up to 2 times DISTANCE_PCT_ERR.
+ // restricting size until LUCENE-6994 can be addressed
if (true || useSmallRanges) {
// Approx 3 degrees lon at the equator:
radiusMeters = random().nextDouble() * 333000;
@@ -514,14 +517,20 @@ public class TestGeoUtils extends LuceneTestCase {
// Done matching, now verify:
for(int docID=0;docID<numDocs;docID++) {
double distanceMeters = GeoDistanceUtils.haversin(centerLat, centerLon, docLats[docID], docLons[docID]);
- boolean expected = distanceMeters <= radiusMeters;
+ final Boolean expected;
+ final double percentError = Math.abs(distanceMeters - radiusMeters) / distanceMeters;
+ if (percentError <= DISTANCE_PCT_ERR) {
+ expected = null;
+ } else {
+ expected = distanceMeters <= radiusMeters;
+ }
boolean actual = hits.contains(docID);
- if (actual != expected) {
+ if (expected != null && actual != expected) {
if (actual) {
- log.println("doc=" + docID + " matched but should not on iteration " + iter);
+ log.println("doc=" + docID + " matched but should not with distance error " + percentError + " on iteration " + iter);
} else {
- log.println("doc=" + docID + " did not match but should on iteration " + iter);
+ log.println("doc=" + docID + " did not match but should with distance error " + percentError + " on iteration " + iter);
}
log.println(" lon=" + docLons[docID] + " lat=" + docLats[docID] + " distanceMeters=" + distanceMeters + " vs radiusMeters=" + radiusMeters);
failCount++;