fix GeoRelationUtils.pointInPolygon to include points that fall on the boundary

This commit is contained in:
nknize 2016-02-04 00:43:49 -06:00
parent 3528cc32cb
commit 62dfc815b0
3 changed files with 7 additions and 3 deletions

View File

@ -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;
}
}

View File

@ -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));
}
/**

View File

@ -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 {