001/** 002 * Copyright (c) 2011, The University of Southampton and the individual contributors. 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without modification, 006 * are permitted provided that the following conditions are met: 007 * 008 * * Redistributions of source code must retain the above copyright notice, 009 * this list of conditions and the following disclaimer. 010 * 011 * * Redistributions in binary form must reproduce the above copyright notice, 012 * this list of conditions and the following disclaimer in the documentation 013 * and/or other materials provided with the distribution. 014 * 015 * * Neither the name of the University of Southampton nor the names of its 016 * contributors may be used to endorse or promote products derived from this 017 * software without specific prior written permission. 018 * 019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 029 */ 030package org.openimaj.demos.mediaeval13.placing; 031 032import gnu.trove.list.array.TLongArrayList; 033 034import java.io.BufferedReader; 035import java.io.File; 036import java.io.FileNotFoundException; 037import java.io.FileReader; 038import java.io.IOException; 039import java.util.ArrayList; 040import java.util.List; 041 042import org.apache.lucene.index.DirectoryReader; 043import org.apache.lucene.index.IndexReader; 044import org.apache.lucene.search.IndexSearcher; 045import org.apache.lucene.store.Directory; 046import org.apache.lucene.store.MMapDirectory; 047import org.openimaj.image.FImage; 048 049public class Utils { 050 private Utils() { 051 } 052 053 /** 054 * Read the lat-lng file into a list of {@link GeoLocation}s 055 * 056 * @param latlngFile 057 * @param skipIds 058 * @return 059 * @throws FileNotFoundException 060 * @throws IOException 061 */ 062 public static List<GeoLocation> readLatLng(File latlngFile, TLongArrayList skipIds) throws FileNotFoundException, 063 IOException 064 { 065 final ArrayList<GeoLocation> pts = new ArrayList<GeoLocation>(); 066 BufferedReader br = null; 067 try { 068 br = new BufferedReader(new FileReader(latlngFile)); 069 070 String line; 071 while ((line = br.readLine()) != null) { 072 try { 073 final String[] parts = line.split(" "); 074 075 if (parts.length != 3) 076 continue; 077 078 final long id = Long.parseLong(parts[0]); 079 final double lat = Double.parseDouble(parts[1]); 080 final double lng = Double.parseDouble(parts[2]); 081 082 if (skipIds.binarySearch(id) < 0) 083 pts.add(new GeoLocation(lat, lng)); 084 } catch (final NumberFormatException nfe) { 085 // ignore line 086 } 087 } 088 } finally { 089 if (br != null) 090 br.close(); 091 } 092 return pts; 093 } 094 095 public static FImage createPrior(File latlngFile, TLongArrayList skipIds) throws IOException { 096 return createPrior(latlngFile, skipIds, true); 097 } 098 099 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}