1 package tim.prune.function.srtm;
4 import java.io.FileInputStream;
5 import java.io.InputStream;
6 import java.io.IOException;
7 import java.io.BufferedReader;
8 import java.io.InputStreamReader;
9 import java.net.MalformedURLException;
11 import java.util.HashMap;
12 import java.util.zip.ZipEntry;
13 import java.util.zip.ZipInputStream;
15 import tim.prune.I18nManager;
17 public class SrtmViewfinderSource extends SrtmSource {
18 /** URL prefix for all tiles */
19 private static final String URL_PREFIX = "http://viewfinderpanoramas.org/";
20 private HashMap<SrtmTile, String> _tile_lookup = null;
21 private HashMap<SrtmTile, Integer> _tile_sizes = null;
23 public SrtmViewfinderSource()
25 _tile_lookup = new HashMap<SrtmTile, String>();
26 _tile_sizes = new HashMap<SrtmTile, Integer>();
29 public String getNameKey()
31 return "function.downloadsrtm." + getName();
34 public String getName()
36 return "SRTM_Viewfinder";
39 protected String getSourceExtension()
45 * Read the dat file and get the contents
46 * @return byte array containing file contents
48 private void populateLookup()
49 throws SrtmSourceException
51 BufferedReader in = null;
54 // in = SrtmViewfinderSource.class.getResourceAsStream("viewfinder/tiles.db");
55 in = new BufferedReader(new InputStreamReader(SrtmViewfinderSource.class.getResourceAsStream("/tim/prune/function/srtm/viewfinder/tiles.dat")));
57 while ((line = in.readLine()) != null)
59 String[] parts = line.split(" ");
60 SrtmTile tile = new SrtmTile(Integer.parseInt(parts[0]), Integer.parseInt(parts[1]));
61 _tile_lookup.put(tile, parts[2]);
65 throw new SrtmSourceException("Exception trying to read tiles.db: " + e);
72 catch (Exception e) {} // ignore
77 * Get the filename for the given tile
79 private String tileFileName(SrtmTile inTile)
80 throws SrtmSourceException
82 if (_tile_lookup.size() == 0)
86 String path = _tile_lookup.get(inTile);
89 // int key = inTile.hashCode();
90 // System.out.println("inTile hashcode " + key);
91 // for (HashMap.Entry<SrtmTile, String> e : _tile_lookup.entrySet())
93 // System.out.println(e.getKey().getTileName() + " - " + e.getKey().hashCode() + " -> " + e.getValue());
95 throw new SrtmSourceException("tile not in database "+inTile.getTileName());
97 return path + getSourceExtension();
101 * Get the UTL for the given tile
102 * @param inTile Tile to get
105 private URL buildUrl(SrtmTile inTile)
106 throws SrtmSourceException
108 String filename = tileFileName(inTile);
111 return new URL(URL_PREFIX + filename);
113 catch (MalformedURLException e)
116 throw new SrtmSourceException(e.getMessage());
120 public boolean isReadyToUse()
125 public boolean downloadTile(SrtmTile inTile)
126 throws SrtmSourceException
128 URL tileUrl = buildUrl(inTile);
129 InputStream inStream = getStreamToUrl(tileUrl);
130 return readToFile(inStream, getCacheFileName(inTile));
133 private ZipEntry advanceToEntry(ZipInputStream inStream, SrtmTile inTile)
134 throws SrtmSourceException
141 entry = inStream.getNextEntry();
143 catch (IOException e)
145 throw new SrtmSourceException("Tile file " + getCacheFileName(inTile) + " found in cache, but tile " + inTile.getTileName() + "not found inside ZIP archive");
147 String entryName = entry.getName().toUpperCase();
148 if (entryName.contains(inTile.getTileName()))
150 // tile size is one of 1201; 3601 depending on
152 if (entry.getSize() == 2 * 1201 * 1201)
154 _tile_sizes.put(inTile, 1201);
156 else if (entry.getSize() == 2 * 3601 * 3601)
158 _tile_sizes.put(inTile, 3601);
162 throw new SrtmSourceException("Tile file "+getCacheFileName(inTile)+" does not have the expected size, it is: " + entry.getSize());
169 public int[] getTileHeights(SrtmTile inTile)
170 throws SrtmSourceException
172 File cacheFileName = getCacheFileName(inTile);
173 if (cacheFileName == null)
175 throw new SrtmSourceException("Tile "+inTile.getTileName()+" not in cache");
179 ZipInputStream inStream = new ZipInputStream(new FileInputStream(cacheFileName));
180 ZipEntry entry = advanceToEntry(inStream, inTile);
181 int rowSize = getRowSize(inTile);
182 return slurpTileHeigths(inStream, rowSize * rowSize);
184 catch (IOException e)
186 throw new SrtmSourceException("Failure opening "+cacheFileName+" for reading:"+e.getMessage());
191 public int getRowSize(SrtmTile inTile)
193 return _tile_sizes.get(inTile);
196 protected File getCacheFileName(SrtmTile inTile)
199 File fileName = new File(tileFileName(inTile));
200 return new File(getCacheDir(), fileName.getName());
202 catch (SrtmSourceException e)