mirror of https://github.com/apache/lucene.git
SOLR-1302: slight refactoring for common code.
git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@881037 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
fa5fb127ab
commit
ac3a479def
|
@ -0,0 +1,48 @@
|
||||||
|
package org.apache.solr.search.function.distance;
|
||||||
|
/**
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||||
|
* contributor license agreements. See the NOTICE file distributed with
|
||||||
|
* this work for additional information regarding copyright ownership.
|
||||||
|
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||||
|
* (the "License"); you may not use this file except in compliance with
|
||||||
|
* the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Useful distance utiltities
|
||||||
|
*
|
||||||
|
**/
|
||||||
|
public class DistanceUtils {
|
||||||
|
/**
|
||||||
|
* @see org.apache.solr.search.function.distance.HaversineFunction
|
||||||
|
*
|
||||||
|
* @param x1
|
||||||
|
* @param y1
|
||||||
|
* @param x2
|
||||||
|
* @param y2
|
||||||
|
* @param radius
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static double haversine(double x1, double y1, double x2, double y2, double radius){
|
||||||
|
double result = 0;
|
||||||
|
if ((x1 != x2) || (y1 != y2)) {
|
||||||
|
double diffX = x1 - x2;
|
||||||
|
double diffY = y1 - y2;
|
||||||
|
double hsinX = Math.sin(diffX / 2);
|
||||||
|
double hsinY = Math.sin(diffY / 2);
|
||||||
|
double h = hsinX * hsinX +
|
||||||
|
(Math.cos(x1) * Math.cos(x2) * hsinY * hsinY);
|
||||||
|
result = (radius * 2 * Math.atan2(Math.sqrt(h), Math.sqrt(1 - h)));
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
|
@ -73,15 +73,7 @@ public class HaversineFunction extends ValueSource {
|
||||||
double y2 = y2DV.doubleVal(doc);
|
double y2 = y2DV.doubleVal(doc);
|
||||||
|
|
||||||
//make sure they aren't all the same, as then we can just return 0
|
//make sure they aren't all the same, as then we can just return 0
|
||||||
if ((x1 != x2) || (y1 != y2)) {
|
result = DistanceUtils.haversine(x1, y1, x2, y2, radius);
|
||||||
double diffX = x1 - x2;
|
|
||||||
double diffY = y1 - y2;
|
|
||||||
double hsinX = Math.sin(diffX / 2);
|
|
||||||
double hsinY = Math.sin(diffY / 2);
|
|
||||||
double h = hsinX * hsinX +
|
|
||||||
(Math.cos(x1) * Math.cos(x2) * hsinY * hsinY);
|
|
||||||
result = (radius * 2 * Math.atan2(Math.sqrt(h), Math.sqrt(1 - h)));
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue