1 package tim.prune.function.srtm;
4 import java.io.FileInputStream;
5 import java.io.FileReader;
6 import java.io.FileOutputStream;
7 import java.io.InputStream;
8 import java.io.IOException;
9 import java.io.BufferedReader;
10 import java.io.InputStreamReader;
11 import java.net.MalformedURLException;
13 import java.net.HttpURLConnection;
14 import java.util.HashMap;
15 import java.util.zip.ZipEntry;
16 import java.util.zip.ZipInputStream;
18 import tim.prune.GpsPrune;
19 import tim.prune.I18nManager;
21 public class SrtmViewfinderSource extends SrtmSource {
22 /** URL prefix for all tiles */
23 private static final String URL_PREFIX = "http://viewfinderpanoramas.org/";
24 private HashMap<SrtmTile, String> _tile_lookup = null;
25 private HashMap<SrtmTile, Integer> _tile_sizes = null;
27 public SrtmViewfinderSource()
29 _tile_lookup = new HashMap<SrtmTile, String>();
30 _tile_sizes = new HashMap<SrtmTile, Integer>();
33 public String getNameKey()
35 return "function.downloadsrtm." + getName();
38 public String getName()
40 return "SRTM_Viewfinder";
43 protected String getSourceExtension()
49 * Read the dat file and get the contents
50 * @return byte array containing file contents
52 private void populateLookup()
53 throws SrtmSourceException
55 BufferedReader in = null;
58 // in = SrtmViewfinderSource.class.getResourceAsStream("viewfinder/tiles.db");
59 in = new BufferedReader(new InputStreamReader(SrtmViewfinderSource.class.getResourceAsStream("/tim/prune/function/srtm/viewfinder/tiles.dat")));
61 while ((line = in.readLine()) != null)
63 String[] parts = line.split(" ");
64 SrtmTile tile = new SrtmTile(Integer.parseInt(parts[0]), Integer.parseInt(parts[1]));
65 _tile_lookup.put(tile, parts[2]);
69 throw new SrtmSourceException("Exception trying to read tiles.db: " + e);
76 catch (Exception e) {} // ignore
81 * Get the filename for the given tile
83 private String tileFileName(SrtmTile inTile)
84 throws SrtmSourceException
86 if (_tile_lookup.size() == 0)
90 String path = _tile_lookup.get(inTile);
93 // int key = inTile.hashCode();
94 // System.out.println("inTile hashcode " + key);
95 // for (HashMap.Entry<SrtmTile, String> e : _tile_lookup.entrySet())
97 // System.out.println(e.getKey().getTileName() + " - " + e.getKey().hashCode() + " -> " + e.getValue());
99 throw new SrtmSourceException("tile not in database "+inTile.getTileName());
101 return path + getSourceExtension();
105 * Get the UTL for the given tile
106 * @param inTile Tile to get
109 private URL buildUrl(SrtmTile inTile)
110 throws SrtmSourceException
112 String filename = tileFileName(inTile);
115 return new URL(URL_PREFIX + filename);
117 catch (MalformedURLException e)
120 throw new SrtmSourceException(e.getMessage());
124 public boolean isReadyToUse()
129 public boolean downloadTile(SrtmTile inTile)
130 throws SrtmSourceException
132 URL tileUrl = buildUrl(inTile);
133 System.out.println("Download: Need to download: " + tileUrl);
136 HttpURLConnection conn = (HttpURLConnection) tileUrl.openConnection();
139 InputStream inStream = null;
141 conn.setRequestProperty("User-Agent", "GpsPrune v" + GpsPrune.VERSION_NUMBER);
143 int status = conn.getResponseCode();
146 inStream = conn.getInputStream();
148 else if (status == 404)
150 throw new SrtmSourceException("Tile not found: "+conn.getURL());
154 throw new SrtmSourceException("Invalid response from server: " +status+conn.getContent());
157 return downloadToFile(inStream, getCacheFileName(inTile));
159 catch (IOException e)
161 throw new SrtmSourceException("Error while downloading tile "+inTile.getTileName()+": "+e.getMessage());
165 private ZipEntry advanceToEntry(ZipInputStream inStream, SrtmTile inTile)
166 throws SrtmSourceException
173 entry = inStream.getNextEntry();
175 catch (IOException e)
177 throw new SrtmSourceException("Tile file " + getCacheFileName(inTile) + " found in cache, but tile " + inTile.getTileName() + "not found inside ZIP archive");
179 String entryName = entry.getName().toUpperCase();
180 if (entryName.contains(inTile.getTileName()))
182 // tile size is one of 1201; 3601 depending on
184 if (entry.getSize() == 2 * 1201 * 1201)
186 _tile_sizes.put(inTile, 1201);
188 else if (entry.getSize() == 2 * 3601 * 3601)
190 _tile_sizes.put(inTile, 3601);
194 throw new SrtmSourceException("Tile file "+getCacheFileName(inTile)+" does not have the expected size, it is: " + entry.getSize());
201 public int[] getTileHeights(SrtmTile inTile)
202 throws SrtmSourceException
204 File cacheFileName = getCacheFileName(inTile);
205 if (cacheFileName == null)
207 throw new SrtmSourceException("Tile "+inTile.getTileName()+" not in cache");
211 ZipInputStream inStream = new ZipInputStream(new FileInputStream(cacheFileName));
212 ZipEntry entry = advanceToEntry(inStream, inTile);
213 int rowSize = getRowSize(inTile);
214 return slurpTileHeigths(inStream, rowSize * rowSize);
216 catch (IOException e)
218 throw new SrtmSourceException("Failure opening "+cacheFileName+" for reading:"+e.getMessage());
223 public int getRowSize(SrtmTile inTile)
225 return _tile_sizes.get(inTile);
228 protected File getCacheFileName(SrtmTile inTile)
231 File fileName = new File(tileFileName(inTile));
232 return new File(getCacheDir(), fileName.getName());
234 catch (SrtmSourceException e)