|
1 |
| package photospace.search; |
|
2 |
| |
|
3 |
| import java.io.*; |
|
4 |
| import java.util.*; |
|
5 |
| import org.apache.commons.logging.*; |
|
6 |
| import org.apache.lucene.document.*; |
|
7 |
| import org.apache.lucene.index.*; |
|
8 |
| import org.apache.lucene.search.*; |
|
9 |
| import com.carbonfive.gis.*; |
|
10 |
| |
|
11 |
| public class LocationFilter |
|
12 |
| extends Filter |
|
13 |
| { |
|
14 |
| private static final Log log = LogFactory.getLog(LocationFilter.class); |
|
15 |
| |
|
16 |
| private SpatialLocation location; |
|
17 |
| private double radius; |
|
18 |
| |
|
19 |
1
| public LocationFilter(double latitude, double longitude, double radius)
|
|
20 |
| { |
|
21 |
1
| this.location = new SpatialLocation(StrictMath.toRadians(longitude), StrictMath.toRadians(latitude));
|
|
22 |
1
| this.radius = radius;
|
|
23 |
| } |
|
24 |
| |
|
25 |
1
| public BitSet bits(IndexReader reader) throws IOException {
|
|
26 |
1
| BitSet bits = new BitSet(reader.maxDoc());
|
|
27 |
1
| for (int i = 0; i < reader.maxDoc(); i++)
|
|
28 |
| { |
|
29 |
2
| Document doc = reader.document(i);
|
|
30 |
0
| if (doc.getValues(DocumentFactory.LATITUDE_FIELD) == null) continue;
|
|
31 |
0
| if (doc.getValues(DocumentFactory.LONGITUDE_FIELD) == null) continue;
|
|
32 |
2
| SpatialLocation docLocation = new SpatialLocation(toRadians(doc.getValues(DocumentFactory.LONGITUDE_FIELD)[0]),
|
|
33 |
| toRadians(doc.getValues(DocumentFactory.LATITUDE_FIELD)[0])); |
|
34 |
| |
|
35 |
1
| if (location.getDistance(docLocation) <= radius) bits.set(i);
|
|
36 |
| } |
|
37 |
1
| return bits;
|
|
38 |
| } |
|
39 |
| |
|
40 |
4
| private double toRadians(String degrees)
|
|
41 |
| { |
|
42 |
4
| return StrictMath.toRadians(Double.parseDouble(degrees));
|
|
43 |
| } |
|
44 |
| |
|
45 |
1
| public String toString() {
|
|
46 |
1
| StringBuffer buffer = new StringBuffer();
|
|
47 |
1
| buffer.append(location);
|
|
48 |
1
| buffer.append(":");
|
|
49 |
1
| buffer.append(radius + "km radius");
|
|
50 |
1
| return buffer.toString();
|
|
51 |
| } |
|
52 |
| } |