lucene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mikemcc...@apache.org
Subject lucene-solr:branch_6x: clean up random poly generation; simplify geo util methods for validating lat/lon
Date Sun, 27 Mar 2016 21:46:44 GMT
Repository: lucene-solr
Updated Branches:
  refs/heads/branch_6x 0c0a25d74 -> a9b702df7


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


Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/a9b702df
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/a9b702df
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/a9b702df

Branch: refs/heads/branch_6x
Commit: a9b702df79fc2d1cd9a014ea158aad1c1aa9608f
Parents: 0c0a25d
Author: Mike McCandless <mikemccand@apache.org>
Authored: Sun Mar 27 17:47:14 2016 -0400
Committer: Mike McCandless <mikemccand@apache.org>
Committed: Sun Mar 27 17:47:50 2016 -0400

----------------------------------------------------------------------
 .../org/apache/lucene/document/LatLonPoint.java | 12 ++---
 .../document/LatLonPointDistanceQuery.java      |  8 +---
 .../document/LatLonPointInPolygonQuery.java     |  8 +---
 .../lucene/document/LatLonPointSortField.java   |  8 +---
 .../geopoint/document/GeoPointField.java        |  9 +---
 .../geopoint/search/GeoPointDistanceQuery.java  |  8 +---
 .../geopoint/search/GeoPointInBBoxQuery.java    | 16 ++-----
 .../geopoint/search/GeoPointMultiTermQuery.java | 16 ++-----
 .../org/apache/lucene/spatial/util/GeoRect.java | 16 ++-----
 .../apache/lucene/spatial/util/GeoUtils.java    | 21 ++++----
 .../spatial/util/BaseGeoPointTestCase.java      | 50 +++++++-------------
 11 files changed, 52 insertions(+), 120 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/a9b702df/lucene/sandbox/src/java/org/apache/lucene/document/LatLonPoint.java
----------------------------------------------------------------------
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 429f979..1278ea5 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;
   }
 

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/a9b702df/lucene/sandbox/src/java/org/apache/lucene/document/LatLonPointDistanceQuery.java
----------------------------------------------------------------------
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 10d11c6..c283c7e 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;

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/a9b702df/lucene/sandbox/src/java/org/apache/lucene/document/LatLonPointInPolygonQuery.java
----------------------------------------------------------------------
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 494f427..c263ba3 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);
     }

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/a9b702df/lucene/sandbox/src/java/org/apache/lucene/document/LatLonPointSortField.java
----------------------------------------------------------------------
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 f883043..ea1541d 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);

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/a9b702df/lucene/spatial/src/java/org/apache/lucene/spatial/geopoint/document/GeoPointField.java
----------------------------------------------------------------------
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 a477fa9..378e513 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?

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/a9b702df/lucene/spatial/src/java/org/apache/lucene/spatial/geopoint/search/GeoPointDistanceQuery.java
----------------------------------------------------------------------
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 98bb2ca..cdd0d6d 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;
   }
   

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/a9b702df/lucene/spatial/src/java/org/apache/lucene/spatial/geopoint/search/GeoPointInBBoxQuery.java
----------------------------------------------------------------------
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 e9486d3..65a12d6 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;

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/a9b702df/lucene/spatial/src/java/org/apache/lucene/spatial/geopoint/search/GeoPointMultiTermQuery.java
----------------------------------------------------------------------
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 c23ceb8..ec08475 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);

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/a9b702df/lucene/spatial/src/java/org/apache/lucene/spatial/util/GeoRect.java
----------------------------------------------------------------------
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 8d947b4..2dbba6d 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;

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/a9b702df/lucene/spatial/src/java/org/apache/lucene/spatial/util/GeoUtils.java
----------------------------------------------------------------------
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 08c3615..faa3815 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);

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/a9b702df/lucene/spatial/src/test/org/apache/lucene/spatial/util/BaseGeoPointTestCase.java
----------------------------------------------------------------------
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 c68dcbc..6ca3318 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));
       }


Mime
View raw message