LUCENE-7249: LatLonPoint polygon should use tree relate()

This commit is contained in:
Robert Muir 2016-04-22 15:10:39 -04:00
parent 666472b74f
commit 88c9da6c89
3 changed files with 8 additions and 7 deletions

View File

@ -62,12 +62,12 @@ final class LatLonGrid {
final LatLonTree[] tree; final LatLonTree[] tree;
LatLonGrid(int minLat, int maxLat, int minLon, int maxLon, Polygon... polygons) { LatLonGrid(int minLat, int maxLat, int minLon, int maxLon, LatLonTree[] tree) {
this.minLat = minLat; this.minLat = minLat;
this.maxLat = maxLat; this.maxLat = maxLat;
this.minLon = minLon; this.minLon = minLon;
this.maxLon = maxLon; this.maxLon = maxLon;
this.tree = LatLonTree.build(polygons); this.tree = tree;
if (minLon > maxLon) { if (minLon > maxLon) {
// maybe make 2 grids if you want this? // maybe make 2 grids if you want this?
throw new IllegalArgumentException("Grid cannot cross the dateline"); throw new IllegalArgumentException("Grid cannot cross the dateline");

View File

@ -92,10 +92,11 @@ final class LatLonPointInPolygonQuery extends Query {
NumericUtils.intToSortableBytes(encodeLongitude(box.minLon), minLon, 0); NumericUtils.intToSortableBytes(encodeLongitude(box.minLon), minLon, 0);
NumericUtils.intToSortableBytes(encodeLongitude(box.maxLon), maxLon, 0); NumericUtils.intToSortableBytes(encodeLongitude(box.maxLon), maxLon, 0);
final LatLonTree[] tree = LatLonTree.build(polygons);
final LatLonGrid grid = new LatLonGrid(encodeLatitude(box.minLat), final LatLonGrid grid = new LatLonGrid(encodeLatitude(box.minLat),
encodeLatitude(box.maxLat), encodeLatitude(box.maxLat),
encodeLongitude(box.minLon), encodeLongitude(box.minLon),
encodeLongitude(box.maxLon), polygons); encodeLongitude(box.maxLon), tree);
return new ConstantScoreWeight(this) { return new ConstantScoreWeight(this) {
@ -156,7 +157,7 @@ final class LatLonPointInPolygonQuery extends Query {
double cellMaxLat = decodeLatitude(maxPackedValue, 0); double cellMaxLat = decodeLatitude(maxPackedValue, 0);
double cellMaxLon = decodeLongitude(maxPackedValue, Integer.BYTES); double cellMaxLon = decodeLongitude(maxPackedValue, Integer.BYTES);
return Polygon.relate(polygons, cellMinLat, cellMaxLat, cellMinLon, cellMaxLon); return LatLonTree.relate(tree, cellMinLat, cellMaxLat, cellMinLon, cellMaxLon);
} }
}); });

View File

@ -40,7 +40,7 @@ public class TestLatLonGrid extends LuceneTestCase {
int maxLat = encodeLatitude(box.maxLat); int maxLat = encodeLatitude(box.maxLat);
int minLon = encodeLongitude(box.minLon); int minLon = encodeLongitude(box.minLon);
int maxLon = encodeLongitude(box.maxLon); int maxLon = encodeLongitude(box.maxLon);
LatLonGrid grid = new LatLonGrid(minLat, maxLat, minLon, maxLon, polygon); LatLonGrid grid = new LatLonGrid(minLat, maxLat, minLon, maxLon, LatLonTree.build(polygon));
// we are in integer space... but exhaustive testing is slow! // we are in integer space... but exhaustive testing is slow!
for (int j = 0; j < 10000; j++) { for (int j = 0; j < 10000; j++) {
int lat = TestUtil.nextInt(random(), minLat, maxLat); int lat = TestUtil.nextInt(random(), minLat, maxLat);
@ -79,7 +79,7 @@ public class TestLatLonGrid extends LuceneTestCase {
int maxLat = encodeLatitude(box.maxLat); int maxLat = encodeLatitude(box.maxLat);
int minLon = encodeLongitude(box.minLon); int minLon = encodeLongitude(box.minLon);
int maxLon = encodeLongitude(box.maxLon); int maxLon = encodeLongitude(box.maxLon);
LatLonGrid grid = new LatLonGrid(minLat, maxLat, minLon, maxLon, polygon); LatLonGrid grid = new LatLonGrid(minLat, maxLat, minLon, maxLon, LatLonTree.build(polygon));
// we are in integer space... but exhaustive testing is slow! // we are in integer space... but exhaustive testing is slow!
for (int j = 0; j < 1000; j++) { for (int j = 0; j < 1000; j++) {
int lat = TestUtil.nextInt(random(), minLat, maxLat); int lat = TestUtil.nextInt(random(), minLat, maxLat);
@ -99,7 +99,7 @@ public class TestLatLonGrid extends LuceneTestCase {
double ONE = decodeLatitude(1); double ONE = decodeLatitude(1);
Polygon tiny = new Polygon(new double[] { ZERO, ZERO, ONE, ONE, ZERO }, new double[] { ZERO, ONE, ONE, ZERO, ZERO }); Polygon tiny = new Polygon(new double[] { ZERO, ZERO, ONE, ONE, ZERO }, new double[] { ZERO, ONE, ONE, ZERO, ZERO });
for (int max = 1; max < 500000; max++) { for (int max = 1; max < 500000; max++) {
LatLonGrid grid = new LatLonGrid(0, max, 0, max, tiny); LatLonGrid grid = new LatLonGrid(0, max, 0, max, LatLonTree.build(tiny));
assertEquals(tiny.contains(decodeLatitude(max), decodeLongitude(max)), grid.contains(max, max)); assertEquals(tiny.contains(decodeLatitude(max), decodeLongitude(max)), grid.contains(max, max));
} }
} }