View Javadoc

1   /**
2    * Copyright (c) 2011, The University of Southampton and the individual contributors.
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without modification,
6    * are permitted provided that the following conditions are met:
7    *
8    *   * 	Redistributions of source code must retain the above copyright notice,
9    * 	this list of conditions and the following disclaimer.
10   *
11   *   *	Redistributions in binary form must reproduce the above copyright notice,
12   * 	this list of conditions and the following disclaimer in the documentation
13   * 	and/or other materials provided with the distribution.
14   *
15   *   *	Neither the name of the University of Southampton nor the names of its
16   * 	contributors may be used to endorse or promote products derived from this
17   * 	software without specific prior written permission.
18   *
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
23   * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26   * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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  	 * Read the lat-lng file into a list of {@link GeoLocation}s
55  	 * 
56  	 * @param latlngFile
57  	 * @param skipIds
58  	 * @return
59  	 * @throws FileNotFoundException
60  	 * @throws IOException
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  					// ignore line
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 }