From 2337f4687fb29a576723c99d464da2c1a88e09f5 Mon Sep 17 00:00:00 2001 From: "Chris M. Hostetter" Date: Tue, 29 Nov 2011 22:38:08 +0000 Subject: [PATCH] LUCENE-3599: Fix DistanceUtils.haversine() javadocs -- were incorrectly stating the expected order of the arguments git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1208118 13f79535-47bb-0310-9956-ffa450edef68 --- lucene/contrib/CHANGES.txt | 5 +++++ .../apache/lucene/spatial/DistanceUtils.java | 14 ++++++------ .../function/distance/HaversineFunction.java | 22 +++++++++---------- 3 files changed, 23 insertions(+), 18 deletions(-) diff --git a/lucene/contrib/CHANGES.txt b/lucene/contrib/CHANGES.txt index 193d812fe69..5690a63c7d0 100644 --- a/lucene/contrib/CHANGES.txt +++ b/lucene/contrib/CHANGES.txt @@ -92,6 +92,11 @@ Bug Fixes assert if such a parent doc was the first doc in the segment). (Shay Banon, Mike McCandless) +Documentation + + * LUCENE-3599: Javadocs for DistanceUtils.haversine() were incorrectly + stating the expected order of the arguments (David Smiley via hossman) + ======================= Lucene 3.5.0 ================ Changes in backwards compatibility policy diff --git a/lucene/contrib/spatial/src/java/org/apache/lucene/spatial/DistanceUtils.java b/lucene/contrib/spatial/src/java/org/apache/lucene/spatial/DistanceUtils.java index e9769850aa0..9ba4ba86690 100644 --- a/lucene/contrib/spatial/src/java/org/apache/lucene/spatial/DistanceUtils.java +++ b/lucene/contrib/spatial/src/java/org/apache/lucene/spatial/DistanceUtils.java @@ -312,15 +312,15 @@ public class DistanceUtils { } /** - * @param x1 The x coordinate of the first point, in radians + * Computes the haversine distance between two points. The arguments are in radians and provided in lat,lon order. * @param y1 The y coordinate of the first point, in radians - * @param x2 The x coordinate of the second point, in radians + * @param x1 The x coordinate of the first point, in radians * @param y2 The y coordinate of the second point, in radians + * @param x2 The x coordinate of the second point, in radians * @param radius The radius of the sphere - * @return The distance between the two points, as determined by the Haversine formula. - + * @return The distance between the two points, as determined by the haversine formula. */ - public static double haversine(double x1, double y1, double x2, double y2, double radius) { + public static double haversine(double y1, double x1, double y2, double x2, double radius) { double result = 0; //make sure they aren't all the same, as then we can just return 0 if ((x1 != x2) || (y1 != y2)) { @@ -328,8 +328,8 @@ public class DistanceUtils { double diffY = y1 - y2; double hsinX = Math.sin(diffX * 0.5); double hsinY = Math.sin(diffY * 0.5); - double h = hsinX * hsinX + - (Math.cos(x1) * Math.cos(x2) * hsinY * hsinY); + double h = hsinY * hsinY + + (Math.cos(y1) * Math.cos(y2) * hsinX * hsinX); result = (radius * 2 * Math.atan2(Math.sqrt(h), Math.sqrt(1 - h))); } return result; diff --git a/solr/core/src/java/org/apache/solr/search/function/distance/HaversineFunction.java b/solr/core/src/java/org/apache/solr/search/function/distance/HaversineFunction.java index 1febdaf19f1..fa0074406c1 100644 --- a/solr/core/src/java/org/apache/solr/search/function/distance/HaversineFunction.java +++ b/solr/core/src/java/org/apache/solr/search/function/distance/HaversineFunction.java @@ -76,22 +76,22 @@ public class HaversineFunction extends ValueSource { double[] p2D = new double[2]; p1DV.doubleVal(doc, p1D); p2DV.doubleVal(doc, p2D); - double x1; double y1; - double x2; + double x1; double y2; + double x2; if (convertToRadians) { - x1 = p1D[0] * DistanceUtils.DEGREES_TO_RADIANS; - y1 = p1D[1] * DistanceUtils.DEGREES_TO_RADIANS; - x2 = p2D[0] * DistanceUtils.DEGREES_TO_RADIANS; - y2 = p2D[1] * DistanceUtils.DEGREES_TO_RADIANS; + y1 = p1D[0] * DistanceUtils.DEGREES_TO_RADIANS; + x1 = p1D[1] * DistanceUtils.DEGREES_TO_RADIANS; + y2 = p2D[0] * DistanceUtils.DEGREES_TO_RADIANS; + x2 = p2D[1] * DistanceUtils.DEGREES_TO_RADIANS; } else { - x1 = p1D[0]; - y1 = p1D[1]; - x2 = p2D[0]; - y2 = p2D[1]; + y1 = p1D[0]; + x1 = p1D[1]; + y2 = p2D[0]; + x2 = p2D[1]; } - return DistanceUtils.haversine(x1, y1, x2, y2, radius); + return DistanceUtils.haversine(y1, x1, y2, x2, radius); }