~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to solr/core/src/java/org/apache/solr/search/function/distance/HaversineFunction.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package org.apache.solr.search.function.distance;
2
 
/**
3
 
 * Licensed to the Apache Software Foundation (ASF) under one or more
4
 
 * contributor license agreements.  See the NOTICE file distributed with
5
 
 * this work for additional information regarding copyright ownership.
6
 
 * The ASF licenses this file to You under the Apache License, Version 2.0
7
 
 * (the "License"); you may not use this file except in compliance with
8
 
 * the License.  You may obtain a copy of the License at
9
 
 *
10
 
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 
 *
12
 
 * Unless required by applicable law or agreed to in writing, software
13
 
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 
 * See the License for the specific language governing permissions and
16
 
 * limitations under the License.
17
 
 */
18
 
 
19
 
import org.apache.lucene.index.IndexReader;
20
 
import org.apache.lucene.search.Searcher;
21
 
import org.apache.lucene.spatial.DistanceUtils;
22
 
import org.apache.solr.common.SolrException;
23
 
import org.apache.solr.search.function.MultiValueSource;
24
 
import org.apache.solr.search.function.DocValues;
25
 
import org.apache.solr.search.function.ValueSource;
26
 
 
27
 
import java.io.IOException;
28
 
import java.util.Map;
29
 
 
30
 
 
31
 
/**
32
 
 * Calculate the Haversine formula (distance) between any two points on a sphere
33
 
 * Takes in four value sources: (latA, lonA); (latB, lonB).
34
 
 * <p/>
35
 
 * Assumes the value sources are in radians unless
36
 
 * <p/>
37
 
 * See http://en.wikipedia.org/wiki/Great-circle_distance and
38
 
 * http://en.wikipedia.org/wiki/Haversine_formula for the actual formula and
39
 
 * also http://www.movable-type.co.uk/scripts/latlong.html
40
 
 */
41
 
public class HaversineFunction extends ValueSource {
42
 
 
43
 
  private MultiValueSource p1;
44
 
  private MultiValueSource p2;
45
 
  private boolean convertToRadians = false;
46
 
  private double radius;
47
 
 
48
 
  public HaversineFunction(MultiValueSource p1, MultiValueSource p2, double radius) {
49
 
    this(p1, p2, radius, false);
50
 
  }
51
 
 
52
 
  public HaversineFunction(MultiValueSource p1, MultiValueSource p2, double radius, boolean convertToRads){
53
 
    this.p1 = p1;
54
 
    this.p2 = p2;
55
 
    if (p1.dimension() != 2 || p2.dimension() != 2) {
56
 
      throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Illegal dimension for value sources");
57
 
    }
58
 
    this.radius = radius;
59
 
    this.convertToRadians = convertToRads;
60
 
  }
61
 
 
62
 
  protected String name() {
63
 
    return "hsin";
64
 
  }
65
 
 
66
 
  /**
67
 
   * @param doc  The doc to score
68
 
   * @param p1DV
69
 
   * @param p2DV
70
 
   * @return The haversine distance formula
71
 
   */
72
 
  protected double distance(int doc, DocValues p1DV, DocValues p2DV) {
73
 
 
74
 
    double[] p1D = new double[2];
75
 
    double[] p2D = new double[2];
76
 
    p1DV.doubleVal(doc, p1D);
77
 
    p2DV.doubleVal(doc, p2D);
78
 
    double x1;
79
 
    double y1;
80
 
    double x2;
81
 
    double y2;
82
 
    if (convertToRadians) {
83
 
      x1 = p1D[0] * DistanceUtils.DEGREES_TO_RADIANS;
84
 
      y1 = p1D[1] * DistanceUtils.DEGREES_TO_RADIANS;
85
 
      x2 = p2D[0] * DistanceUtils.DEGREES_TO_RADIANS;
86
 
      y2 = p2D[1] * DistanceUtils.DEGREES_TO_RADIANS;
87
 
    } else {
88
 
      x1 = p1D[0];
89
 
      y1 = p1D[1];
90
 
      x2 = p2D[0];
91
 
      y2 = p2D[1];
92
 
    }
93
 
    return DistanceUtils.haversine(x1, y1, x2, y2, radius);
94
 
  }
95
 
 
96
 
 
97
 
  @Override
98
 
  public DocValues getValues(Map context, IndexReader reader) throws IOException {
99
 
    final DocValues vals1 = p1.getValues(context, reader);
100
 
 
101
 
    final DocValues vals2 = p2.getValues(context, reader);
102
 
    return new DocValues() {
103
 
      @Override
104
 
      public float floatVal(int doc) {
105
 
        return (float) doubleVal(doc);
106
 
      }
107
 
 
108
 
      @Override
109
 
      public int intVal(int doc) {
110
 
        return (int) doubleVal(doc);
111
 
      }
112
 
 
113
 
      @Override
114
 
      public long longVal(int doc) {
115
 
        return (long) doubleVal(doc);
116
 
      }
117
 
 
118
 
      @Override
119
 
      public double doubleVal(int doc) {
120
 
        return distance(doc, vals1, vals2);
121
 
      }
122
 
 
123
 
      @Override
124
 
      public String strVal(int doc) {
125
 
        return Double.toString(doubleVal(doc));
126
 
      }
127
 
 
128
 
      @Override
129
 
      public String toString(int doc) {
130
 
        StringBuilder sb = new StringBuilder();
131
 
        sb.append(name()).append('(');
132
 
        sb.append(vals1.toString(doc)).append(',').append(vals2.toString(doc));
133
 
        sb.append(')');
134
 
        return sb.toString();
135
 
      }
136
 
    };
137
 
  }
138
 
 
139
 
  @Override
140
 
  public void createWeight(Map context, Searcher searcher) throws IOException {
141
 
    p1.createWeight(context, searcher);
142
 
    p2.createWeight(context, searcher);
143
 
 
144
 
  }
145
 
 
146
 
  @Override
147
 
  public boolean equals(Object o) {
148
 
    if (this.getClass() != o.getClass()) return false;
149
 
    HaversineFunction other = (HaversineFunction) o;
150
 
    return this.name().equals(other.name())
151
 
            && p1.equals(other.p1) &&
152
 
            p2.equals(other.p2) && radius == other.radius;
153
 
  }
154
 
 
155
 
  @Override
156
 
  public int hashCode() {
157
 
    int result;
158
 
    long temp;
159
 
    result = p1.hashCode();
160
 
    result = 31 * result + p2.hashCode();
161
 
    result = 31 * result + name().hashCode();
162
 
    temp = Double.doubleToRawLongBits(radius);
163
 
    result = 31 * result + (int) (temp ^ (temp >>> 32));
164
 
    return result;
165
 
  }
166
 
 
167
 
  @Override
168
 
  public String description() {
169
 
    StringBuilder sb = new StringBuilder();
170
 
    sb.append(name()).append('(');
171
 
    sb.append(p1).append(',').append(p2);
172
 
    sb.append(')');
173
 
    return sb.toString();
174
 
  }
175
 
}