Skip to content

Apache Lucene open-source search software

clean up random poly generation; simplify geo util methods for validating lat/lon

Mike McCandless6f0488f7302a
diff --git a/lucene/sandbox/src/java/org/apache/lucene/document/LatLonPoint.java b/lucene/sandbox/src/java/org/apache/lucene/document/LatLonPoint.java
index 429f9792b97..1278ea56aee 100644
--- a/lucene/sandbox/src/java/org/apache/lucene/document/LatLonPoint.java
+++ b/lucene/sandbox/src/java/org/apache/lucene/document/LatLonPoint.java
@@ -137,9 +137,7 @@ public class LatLonPoint extends Field {
* @throws IllegalArgumentException if latitude is out of bounds
*/
public static int encodeLatitude(double latitude) {
- if (GeoUtils.isValidLat(latitude) == false) {
- throw new IllegalArgumentException("invalid latitude: " + latitude + ", must be -90 to 90");
- }
+ GeoUtils.checkLatitude(latitude);
// the maximum possible value cannot be encoded without overflow
if (latitude == 90.0D) {
latitude = Math.nextDown(latitude);
@@ -154,9 +152,7 @@ public class LatLonPoint extends Field {
* @throws IllegalArgumentException if longitude is out of bounds
*/
public static int encodeLongitude(double longitude) {
- if (GeoUtils.isValidLon(longitude) == false) {
- throw new IllegalArgumentException("invalid longitude: " + longitude + ", must be -180 to 180");
- }
+ GeoUtils.checkLongitude(longitude);
// the maximum possible value cannot be encoded without overflow
if (longitude == 180.0D) {
longitude = Math.nextDown(longitude);
@@ -171,7 +167,7 @@ public class LatLonPoint extends Field {
*/
public static double decodeLatitude(int encoded) {
double result = encoded * LATITUDE_DECODE;
- assert GeoUtils.isValidLat(result);
+ assert result >= GeoUtils.MIN_LAT_INCL && result <= GeoUtils.MAX_LAT_INCL;
return result;
}
@@ -192,7 +188,7 @@ public class LatLonPoint extends Field {
*/
public static double decodeLongitude(int encoded) {
double result = encoded * LONGITUDE_DECODE;
- assert GeoUtils.isValidLon(result);
+ assert result >= GeoUtils.MIN_LON_INCL && result <= GeoUtils.MAX_LON_INCL;
return result;
}
diff --git a/lucene/sandbox/src/java/org/apache/lucene/document/LatLonPointDistanceQuery.java b/lucene/sandbox/src/java/org/apache/lucene/document/LatLonPointDistanceQuery.java
index 10d11c6f27b..c283c7e6e9f 100644
--- a/lucene/sandbox/src/java/org/apache/lucene/document/LatLonPointDistanceQuery.java
+++ b/lucene/sandbox/src/java/org/apache/lucene/document/LatLonPointDistanceQuery.java
@@ -61,12 +61,8 @@ final class LatLonPointDistanceQuery extends Query {
if (Double.isFinite(radiusMeters) == false || radiusMeters < 0) {
throw new IllegalArgumentException("radiusMeters: '" + radiusMeters + "' is invalid");
}
- if (GeoUtils.isValidLat(latitude) == false) {
- throw new IllegalArgumentException("latitude: '" + latitude + "' is invalid");
- }
- if (GeoUtils.isValidLon(longitude) == false) {
- throw new IllegalArgumentException("longitude: '" + longitude + "' is invalid");
- }
+ GeoUtils.checkLatitude(latitude);
+ GeoUtils.checkLongitude(longitude);
this.field = field;
this.latitude = latitude;
this.longitude = longitude;
diff --git a/lucene/sandbox/src/java/org/apache/lucene/document/LatLonPointInPolygonQuery.java b/lucene/sandbox/src/java/org/apache/lucene/document/LatLonPointInPolygonQuery.java
index 494f42733c9..c263ba340ae 100644
--- a/lucene/sandbox/src/java/org/apache/lucene/document/LatLonPointInPolygonQuery.java
+++ b/lucene/sandbox/src/java/org/apache/lucene/document/LatLonPointInPolygonQuery.java
@@ -94,15 +94,11 @@ final class LatLonPointInPolygonQuery extends Query {
double maxLat = Double.NEGATIVE_INFINITY;
for(int i=0;i<polyLats.length;i++) {
double lat = polyLats[i];
- if (GeoUtils.isValidLat(lat) == false) {
- throw new IllegalArgumentException("polyLats[" + i + "]=" + lat + " is not a valid latitude");
- }
+ GeoUtils.checkLatitude(lat);
minLat = Math.min(minLat, lat);
maxLat = Math.max(maxLat, lat);
double lon = polyLons[i];
- if (GeoUtils.isValidLon(lon) == false) {
- throw new IllegalArgumentException("polyLons[" + i + "]=" + lat + " is not a valid longitude");
- }
+ GeoUtils.checkLongitude(lon);
minLon = Math.min(minLon, lon);
maxLon = Math.max(maxLon, lon);
}
diff --git a/lucene/sandbox/src/java/org/apache/lucene/document/LatLonPointSortField.java b/lucene/sandbox/src/java/org/apache/lucene/document/LatLonPointSortField.java
index f88304367e5..ea1541d669d 100644
--- a/lucene/sandbox/src/java/org/apache/lucene/document/LatLonPointSortField.java
+++ b/lucene/sandbox/src/java/org/apache/lucene/document/LatLonPointSortField.java
@@ -34,12 +34,8 @@ final class LatLonPointSortField extends SortField {
if (field == null) {
throw new IllegalArgumentException("field cannot be null");
}
- if (GeoUtils.isValidLat(latitude) == false) {
- throw new IllegalArgumentException("latitude: '" + latitude + "' is invalid");
- }
- if (GeoUtils.isValidLon(longitude) == false) {
- throw new IllegalArgumentException("longitude: '" + longitude + "' is invalid");
- }
+ GeoUtils.checkLatitude(latitude);
+ GeoUtils.checkLongitude(longitude);
this.latitude = latitude;
this.longitude = longitude;
setMissingValue(Double.POSITIVE_INFINITY);
diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/geopoint/document/GeoPointField.java b/lucene/spatial/src/java/org/apache/lucene/spatial/geopoint/document/GeoPointField.java
index a477fa90ae2..378e513d8a1 100644
--- a/lucene/spatial/src/java/org/apache/lucene/spatial/geopoint/document/GeoPointField.java
+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/geopoint/document/GeoPointField.java
@@ -163,13 +163,8 @@ public final class GeoPointField extends Field {
public GeoPointField(String name, double latitude, double longitude, FieldType type) {
super(name, type);
- if (GeoUtils.isValidLat(latitude) == false) {
- throw new IllegalArgumentException("invalid latitude=" + latitude + " for field \"" + name + "\"");
- }
-
- if (GeoUtils.isValidLon(longitude) == false) {
- throw new IllegalArgumentException("invalid longitude=" + longitude + " for field \"" + name + "\"");
- }
+ GeoUtils.checkLatitude(latitude);
+ GeoUtils.checkLongitude(longitude);
// field must be indexed
// todo does it make sense here to provide the ability to store a GeoPointField but not index?
diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/geopoint/search/GeoPointDistanceQuery.java b/lucene/spatial/src/java/org/apache/lucene/spatial/geopoint/search/GeoPointDistanceQuery.java
index 98bb2cab2c9..cdd0d6d8e69 100644
--- a/lucene/spatial/src/java/org/apache/lucene/spatial/geopoint/search/GeoPointDistanceQuery.java
+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/geopoint/search/GeoPointDistanceQuery.java
@@ -57,16 +57,12 @@ public class GeoPointDistanceQuery extends GeoPointInBBoxQuery {
}
private static double checkLatitude(double centerLat) {
- if (GeoUtils.isValidLat(centerLat) == false) {
- throw new IllegalArgumentException("invalid centerLat " + centerLat);
- }
+ GeoUtils.checkLatitude(centerLat);
return centerLat;
}
private static double checkLongitude(double centerLon) {
- if (GeoUtils.isValidLon(centerLon) == false) {
- throw new IllegalArgumentException("invalid centerLon " + centerLon);
- }
+ GeoUtils.checkLongitude(centerLon);
return centerLon;
}
diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/geopoint/search/GeoPointInBBoxQuery.java b/lucene/spatial/src/java/org/apache/lucene/spatial/geopoint/search/GeoPointInBBoxQuery.java
index e9486d37c62..65a12d6a656 100644
--- a/lucene/spatial/src/java/org/apache/lucene/spatial/geopoint/search/GeoPointInBBoxQuery.java
+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/geopoint/search/GeoPointInBBoxQuery.java
@@ -77,18 +77,10 @@ public class GeoPointInBBoxQuery extends Query {
if (termEncoding == null) {
throw new IllegalArgumentException("termEncoding cannot be null");
}
- if (GeoUtils.isValidLat(minLat) == false) {
- throw new IllegalArgumentException("invalid minimum latitude: " + minLat + ", must be -90 to 90");
- }
- if (GeoUtils.isValidLat(maxLat) == false) {
- throw new IllegalArgumentException("invalid maximum latitude: " + maxLat + ", must be -90 to 90");
- }
- if (GeoUtils.isValidLon(minLon) == false) {
- throw new IllegalArgumentException("invalid minimum longitude: " + minLon + ", must be -180 to 180");
- }
- if (GeoUtils.isValidLon(maxLon) == false) {
- throw new IllegalArgumentException("invalid maximum longitude: " + maxLon + ", must be -180 to 180");
- }
+ GeoUtils.checkLatitude(minLat);
+ GeoUtils.checkLatitude(maxLat);
+ GeoUtils.checkLongitude(minLon);
+ GeoUtils.checkLongitude(maxLon);
this.field = field;
this.minLat = minLat;
this.maxLat = maxLat;
diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/geopoint/search/GeoPointMultiTermQuery.java b/lucene/spatial/src/java/org/apache/lucene/spatial/geopoint/search/GeoPointMultiTermQuery.java
index c23ceb8244b..ec08475035e 100644
--- a/lucene/spatial/src/java/org/apache/lucene/spatial/geopoint/search/GeoPointMultiTermQuery.java
+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/geopoint/search/GeoPointMultiTermQuery.java
@@ -55,18 +55,10 @@ abstract class GeoPointMultiTermQuery extends MultiTermQuery {
public GeoPointMultiTermQuery(String field, final TermEncoding termEncoding, final double minLat, final double maxLat, final double minLon, final double maxLon) {
super(field);
- if (GeoUtils.isValidLat(minLat) == false) {
- throw new IllegalArgumentException("invalid minLat " + minLat);
- }
- if (GeoUtils.isValidLat(maxLat) == false) {
- throw new IllegalArgumentException("invalid maxLat " + maxLat);
- }
- if (GeoUtils.isValidLon(minLon) == false) {
- throw new IllegalArgumentException("invalid minLon " + minLon);
- }
- if (GeoUtils.isValidLon(maxLon) == false) {
- throw new IllegalArgumentException("invalid maxLon " + maxLon);
- }
+ GeoUtils.checkLatitude(minLat);
+ GeoUtils.checkLatitude(maxLat);
+ GeoUtils.checkLongitude(minLon);
+ GeoUtils.checkLongitude(maxLon);
final long minHash = GeoEncodingUtils.mortonHash(minLat, minLon);
final long maxHash = GeoEncodingUtils.mortonHash(maxLat, maxLon);
diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/util/GeoRect.java b/lucene/spatial/src/java/org/apache/lucene/spatial/util/GeoRect.java
index 8d947b4d867..2dbba6d6011 100644
--- a/lucene/spatial/src/java/org/apache/lucene/spatial/util/GeoRect.java
+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/util/GeoRect.java
@@ -31,18 +31,10 @@ public class GeoRect {
* Constructs a bounding box by first validating the provided latitude and longitude coordinates
*/
public GeoRect(double minLat, double maxLat, double minLon, double maxLon) {
- if (GeoUtils.isValidLon(minLon) == false) {
- throw new IllegalArgumentException("invalid minLon " + minLon);
- }
- if (GeoUtils.isValidLon(maxLon) == false) {
- throw new IllegalArgumentException("invalid maxLon " + maxLon);
- }
- if (GeoUtils.isValidLat(minLat) == false) {
- throw new IllegalArgumentException("invalid minLat " + minLat);
- }
- if (GeoUtils.isValidLat(maxLat) == false) {
- throw new IllegalArgumentException("invalid maxLat " + maxLat);
- }
+ GeoUtils.checkLatitude(minLat);
+ GeoUtils.checkLatitude(maxLat);
+ GeoUtils.checkLongitude(minLon);
+ GeoUtils.checkLongitude(maxLon);
this.minLon = minLon;
this.maxLon = maxLon;
this.minLat = minLat;
diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/util/GeoUtils.java b/lucene/spatial/src/java/org/apache/lucene/spatial/util/GeoUtils.java
index 08c36155023..faa38156498 100644
--- a/lucene/spatial/src/java/org/apache/lucene/spatial/util/GeoUtils.java
+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/util/GeoUtils.java
@@ -19,7 +19,6 @@ package org.apache.lucene.spatial.util;
import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.PI;
-import static java.lang.Math.abs;
import static org.apache.lucene.util.SloppyMath.asin;
import static org.apache.lucene.util.SloppyMath.cos;
@@ -63,13 +62,17 @@ public final class GeoUtils {
}
/** validates latitude value is within standard +/-90 coordinate bounds */
- public static boolean isValidLat(double lat) {
- return Double.isNaN(lat) == false && lat >= MIN_LAT_INCL && lat <= MAX_LAT_INCL;
+ public static void checkLatitude(double latitude) {
+ if (Double.isNaN(latitude) || latitude < MIN_LAT_INCL || latitude > MAX_LAT_INCL) {
+ throw new IllegalArgumentException("invalid latitude " + latitude + "; must be between " + MIN_LAT_INCL + " and " + MAX_LAT_INCL);
+ }
}
/** validates longitude value is within standard +/-180 coordinate bounds */
- public static boolean isValidLon(double lon) {
- return Double.isNaN(lon) == false && lon >= MIN_LON_INCL && lon <= MAX_LON_INCL;
+ public static void checkLongitude(double longitude) {
+ if (Double.isNaN(longitude) || longitude < MIN_LON_INCL || longitude > MAX_LON_INCL) {
+ throw new IllegalArgumentException("invalid longitude " + longitude + "; must be between " + MIN_LON_INCL + " and " + MAX_LON_INCL);
+ }
}
/** Compute Bounding Box for a circle using WGS-84 parameters */
@@ -115,12 +118,8 @@ public final class GeoUtils {
double maxLat = Double.NEGATIVE_INFINITY;
for (int i=0;i<polyLats.length;i++) {
- if (GeoUtils.isValidLat(polyLats[i]) == false) {
- throw new IllegalArgumentException("invalid polyLats[" + i + "]=" + polyLats[i]);
- }
- if (GeoUtils.isValidLon(polyLons[i]) == false) {
- throw new IllegalArgumentException("invalid polyLons[" + i + "]=" + polyLons[i]);
- }
+ checkLatitude(polyLats[i]);
+ checkLongitude(polyLons[i]);
minLat = min(polyLats[i], minLat);
maxLat = max(polyLats[i], maxLat);
minLon = min(polyLons[i], minLon);
diff --git a/lucene/spatial/src/test/org/apache/lucene/spatial/util/BaseGeoPointTestCase.java b/lucene/spatial/src/test/org/apache/lucene/spatial/util/BaseGeoPointTestCase.java
index c68dcbcf71a..6ca3318b63c 100644
--- a/lucene/spatial/src/test/org/apache/lucene/spatial/util/BaseGeoPointTestCase.java
+++ b/lucene/spatial/src/test/org/apache/lucene/spatial/util/BaseGeoPointTestCase.java
@@ -638,37 +638,6 @@ public abstract class BaseGeoPointTestCase extends LuceneTestCase {
return quantizeLon(result);
}
- // for pole crossing: used by surpriseMePolygon()
- private static double wrapLat(double lat) {
- //System.out.println("wrapLat " + lat);
- if (lat > 90) {
- //System.out.println(" " + (180 - lat));
- return 180 - lat;
- } else if (lat < -90) {
- //System.out.println(" " + (-180 - lat));
- return -180 - lat;
- } else {
- //System.out.println(" " + lat);
- return lat;
- }
- }
-
- // for dateline crossing: used by surpriseMePolygon()
- // TODO: can we remove this? these should not cross dateline...
- private static double wrapLon(double lon) {
- //System.out.println("wrapLon " + lon);
- if (lon > 180) {
- //System.out.println(" " + (lon - 360));
- return lon - 360;
- } else if (lon < -180) {
- //System.out.println(" " + (lon + 360));
- return lon + 360;
- } else {
- //System.out.println(" " + lon);
- return lon;
- }
- }
-
/** Returns {polyLats, polyLons} double[] array */
private double[][] surpriseMePolygon() {
// repeat until we get a poly that doesn't cross dateline:
@@ -692,14 +661,27 @@ public abstract class BaseGeoPointTestCase extends LuceneTestCase {
}
double len = radius * (1.0 - radiusDelta + radiusDelta * random().nextDouble());
//System.out.println(" len=" + len);
- double lat = wrapLat(centerLat + len * Math.cos(Math.toRadians(angle)));
+ double lat = centerLat + len * Math.cos(Math.toRadians(angle));
double lon = centerLon + len * Math.sin(Math.toRadians(angle));
if (lon <= GeoUtils.MIN_LON_INCL || lon >= GeoUtils.MAX_LON_INCL) {
// cannot cross dateline: try again!
continue newPoly;
}
- lats.add(wrapLat(lat));
- lons.add(wrapLon(lon));
+ if (lat > 90) {
+ // cross the north pole
+ lat = 180 - lat;
+ lon = 180 - lon;
+ } else if (lat < -90) {
+ // cross the south pole
+ lat = -180 - lat;
+ lon = 180 - lon;
+ }
+ if (lon <= GeoUtils.MIN_LON_INCL || lon >= GeoUtils.MAX_LON_INCL) {
+ // cannot cross dateline: try again!
+ continue newPoly;
+ }
+ lats.add(lat);
+ lons.add(lon);
//System.out.println(" lat=" + lats.get(lats.size()-1) + " lon=" + lons.get(lons.size()-1));
}