Apache Lucene open-source search software
LUCENE-5457: Expose SloppyMath earth diameter table
Adrien Grandd03dd4e6e7c2
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 991d303f8cc..8649526aa26 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -177,6 +177,10 @@ New Featuresrepresentations of a shape into BinaryDocValues. It supports exact geometryrelationship calculations. (David Smiley)+* LUCENE-5457: Add SloppyMath.earthDiameter(double latitude) that returns an+ approximate value of the diameter of the earth at the given latitude.+ (Adrien Grand)+Build* LUCENE-5217,LUCENE-5420: Maven config: get dependencies from Ant+Ivy config;diff --git a/lucene/core/src/java/org/apache/lucene/util/SloppyMath.java b/lucene/core/src/java/org/apache/lucene/util/SloppyMath.javaindex 23967d79465..7491c672454 100644--- a/lucene/core/src/java/org/apache/lucene/util/SloppyMath.java+++ b/lucene/core/src/java/org/apache/lucene/util/SloppyMath.java@@ -48,11 +48,10 @@ public class SloppyMath {double h2 = 1 - cos((lon1 - lon2) * TO_RADIANS);double h = (h1 + cos(x1) * cos(x2) * h2) / 2;- double avgLat = Math.abs((x1 + x2) / 2d);- int index = (int)(avgLat * RADIUS_INDEXER + 0.5) % earthDiameterPerLatitude.length;- double radius = earthDiameterPerLatitude[index];+ double avgLat = (x1 + x2) / 2d;+ double diameter = earthDiameter(avgLat);- return radius * asin(Math.min(1, Math.sqrt(h)));+ return diameter * asin(Math.min(1, Math.sqrt(h)));}@@ -137,7 +136,13 @@ public class SloppyMath {}}}-++ /** Return an approximate value of the diameter of the earth at the given latitude, in kilometers. */+ public static double earthDiameter(double latitude) {+ final int index = (int)(Math.abs(latitude) * RADIUS_INDEXER + 0.5) % earthDiameterPerLatitude.length;+ return earthDiameterPerLatitude[index];+ }+// haversinprivate static final double TO_RADIANS = Math.PI / 180D;