1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 package org.openimaj.demos.mediaeval13.placing;
31
32 import gnu.trove.list.array.TLongArrayList;
33
34 import java.io.BufferedReader;
35 import java.io.File;
36 import java.io.FileNotFoundException;
37 import java.io.FileReader;
38 import java.io.IOException;
39 import java.util.ArrayList;
40 import java.util.List;
41
42 import org.apache.lucene.index.DirectoryReader;
43 import org.apache.lucene.index.IndexReader;
44 import org.apache.lucene.search.IndexSearcher;
45 import org.apache.lucene.store.Directory;
46 import org.apache.lucene.store.MMapDirectory;
47 import org.openimaj.image.FImage;
48
49 public class Utils {
50 private Utils() {
51 }
52
53
54
55
56
57
58
59
60
61
62 public static List<GeoLocation> readLatLng(File latlngFile, TLongArrayList skipIds) throws FileNotFoundException,
63 IOException
64 {
65 final ArrayList<GeoLocation> pts = new ArrayList<GeoLocation>();
66 BufferedReader br = null;
67 try {
68 br = new BufferedReader(new FileReader(latlngFile));
69
70 String line;
71 while ((line = br.readLine()) != null) {
72 try {
73 final String[] parts = line.split(" ");
74
75 if (parts.length != 3)
76 continue;
77
78 final long id = Long.parseLong(parts[0]);
79 final double lat = Double.parseDouble(parts[1]);
80 final double lng = Double.parseDouble(parts[2]);
81
82 if (skipIds.binarySearch(id) < 0)
83 pts.add(new GeoLocation(lat, lng));
84 } catch (final NumberFormatException nfe) {
85
86 }
87 }
88 } finally {
89 if (br != null)
90 br.close();
91 }
92 return pts;
93 }
94
95 public static FImage createPrior(File latlngFile, TLongArrayList skipIds) throws IOException {
96 return createPrior(latlngFile, skipIds, true);
97 }
98
99 public static FImage createPrior(File latlngFile, TLongArrayList skipIds, boolean norm) throws IOException {
100 final FImage img = new FImage(360, 180);
101 img.fill(1f / (img.height * img.width));
102
103 if (latlngFile == null)
104 return img;
105
106 final BufferedReader br = new BufferedReader(new FileReader(latlngFile));
107
108 String line;
109 br.readLine();
110 while ((line = br.readLine()) != null) {
111 final String[] parts = line.split(" ");
112
113 if (skipIds.contains(Long.parseLong(parts[0])))
114 continue;
115
116 final float x = Float.parseFloat(parts[2]) + 180;
117 final float y = 90 - Float.parseFloat(parts[1]);
118
119 img.pixels[(int) (y * img.height / 180.001)][(int) (x * img.width / 360.001)]++;
120 }
121 br.close();
122
123 if (norm)
124 logNorm(img);
125
126 return img;
127 }
128
129 public static void logNorm(final FImage img) {
130 final double norm = img.sum();
131 for (int y = 0; y < img.height; y++)
132 for (int x = 0; x < img.width; x++)
133 img.pixels[y][x] = (float) Math.log(img.pixels[y][x] / norm);
134 }
135
136 public static IndexSearcher loadLuceneIndex(File file) throws IOException {
137 final Directory directory = new MMapDirectory(file);
138 final IndexReader reader = DirectoryReader.open(directory);
139 final IndexSearcher luceneIndex = new IndexSearcher(reader);
140 return luceneIndex;
141 }
142 }