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:
Grant Ingersoll 2009-11-16 23:14:00 +00:00
parent fa5fb127ab
commit ac3a479def
2 changed files with 49 additions and 9 deletions

View File

@ -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;
}
}

View File

@ -73,15 +73,7 @@ public class HaversineFunction extends ValueSource {
double y2 = y2DV.doubleVal(doc);
//make sure they aren't all the same, as then we can just return 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)));
}
result = DistanceUtils.haversine(x1, y1, x2, y2, radius);
return result;
}