From ac3a479def1839bae485afb8da11d3ff9968b525 Mon Sep 17 00:00:00 2001 From: Grant Ingersoll Date: Mon, 16 Nov 2009 23:14:00 +0000 Subject: [PATCH] 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 --- .../function/distance/DistanceUtils.java | 48 +++++++++++++++++++ .../function/distance/HaversineFunction.java | 10 +--- 2 files changed, 49 insertions(+), 9 deletions(-) create mode 100644 src/java/org/apache/solr/search/function/distance/DistanceUtils.java diff --git a/src/java/org/apache/solr/search/function/distance/DistanceUtils.java b/src/java/org/apache/solr/search/function/distance/DistanceUtils.java new file mode 100644 index 00000000000..4f2a3f96dff --- /dev/null +++ b/src/java/org/apache/solr/search/function/distance/DistanceUtils.java @@ -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; + } +} diff --git a/src/java/org/apache/solr/search/function/distance/HaversineFunction.java b/src/java/org/apache/solr/search/function/distance/HaversineFunction.java index e72fdd36670..24886d836c2 100644 --- a/src/java/org/apache/solr/search/function/distance/HaversineFunction.java +++ b/src/java/org/apache/solr/search/function/distance/HaversineFunction.java @@ -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; }