2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package org.apache.lucene.spatial.tier;
20 import org.apache.lucene.spatial.DistanceUtils;
22 import java.util.HashMap;
26 * Provide a high level access point to distances
27 * Used by DistanceSortSource and DistanceQuery
29 * <p><font color="red"><b>NOTE:</b> This API is still in
30 * flux and might change in incompatible ways in the next
34 public class DistanceHandler {
36 public enum Precision {EXACT, TWOFEET, TWENTYFEET, TWOHUNDREDFEET}
38 private Map<Integer,Double> distances;
39 private Map<String, Double> distanceLookupCache;
40 private Precision precise;
42 public DistanceHandler (Map<Integer,Double> distances, Map<String, Double> distanceLookupCache, Precision precise){
43 this.distances = distances;
44 this.distanceLookupCache = distanceLookupCache;
45 this.precise = precise;
49 public static double getPrecision(double x, Precision thisPrecise){
51 if(thisPrecise != null){
55 case TWOFEET: dif = x % 0.0001; break;
56 case TWENTYFEET: dif = x % 0.001; break;
57 case TWOHUNDREDFEET: dif = x % 0.01; break;
64 public Precision getPrecision() {
68 public double getDistance(int docid, double centerLat, double centerLng, double lat, double lng){
70 // check to see if we have distances
71 // if not calculate the distance
72 if(distances == null){
73 return DistanceUtils.getDistanceMi(centerLat, centerLng, lat, lng);
76 // check to see if the doc id has a cached distance
77 Double docd = distances.get( docid );
79 return docd.doubleValue();
82 //check to see if we have a precision code
83 // and if another lat/long has been calculated at
84 // that rounded location
85 if (precise != null) {
86 double xLat = getPrecision(lat, precise);
87 double xLng = getPrecision(lng, precise);
89 String k = Double.valueOf(xLat).toString() +","+ Double.valueOf(xLng).toString();
91 Double d = (distanceLookupCache.get(k));
93 return d.doubleValue();
97 //all else fails calculate the distances
98 return DistanceUtils.getDistanceMi(centerLat, centerLng, lat, lng);
102 public static void main(String args[]){
103 DistanceHandler db = new DistanceHandler(new HashMap<Integer,Double>(), new HashMap<String,Double>(), Precision.TWOHUNDREDFEET);
104 System.out.println(DistanceHandler.getPrecision(-1234.123456789, db.getPrecision()));