1 package tim.prune.function.srtm;
4 import java.io.FileReader;
5 import java.io.FileOutputStream;
6 import java.io.InputStream;
7 import java.io.IOException;
8 import java.io.BufferedReader;
9 import java.io.InputStreamReader;
10 import java.net.MalformedURLException;
12 import java.net.HttpURLConnection;
13 import java.util.HashMap;
15 import tim.prune.GpsPrune;
16 import tim.prune.I18nManager;
18 public class SrtmViewfinderSource extends SrtmSource {
19 /** URL prefix for all tiles */
20 private static final String URL_PREFIX = "http://viewfinderpanoramas.org/";
21 private HashMap<SrtmTile, String> _tile_lookup = null;
23 public SrtmViewfinderSource()
27 public String getNameKey()
29 return "function.downloadsrtm." + getName();
32 public String getName()
34 return "SRTM_Viewfinder";
37 protected String getSourceExtension()
43 * Read the dat file and get the contents
44 * @return byte array containing file contents
46 private void populateLookup()
47 throws SrtmSourceException
49 BufferedReader in = null;
52 // in = SrtmViewfinderSource.class.getResourceAsStream("viewfinder/tiles.db");
53 _tile_lookup = new HashMap<SrtmTile, String>();
54 in = new BufferedReader(new InputStreamReader(SrtmViewfinderSource.class.getResourceAsStream("/tim/prune/function/srtm/viewfinder/tiles.dat")));
56 while ((line = in.readLine()) != null)
58 String[] parts = line.split(" ");
59 SrtmTile tile = new SrtmTile(Integer.parseInt(parts[0]), Integer.parseInt(parts[1]));
60 _tile_lookup.put(tile, parts[2]);
64 throw new SrtmSourceException("Exception trying to read tiles.db: " + e);
71 catch (Exception e) {} // ignore
76 * Get the Url for the given tile
77 * @param inTile Tile to get
80 private URL buildUrl(SrtmTile inTile)
81 throws SrtmSourceException
83 if (_tile_lookup == null)
87 System.out.println(_tile_lookup.get(inTile));
88 String path = _tile_lookup.get(inTile);
91 int key = inTile.hashCode();
92 System.out.println("inTile hashcode " + key);
93 for (HashMap.Entry<SrtmTile, String> e : _tile_lookup.entrySet())
95 if (e.getKey().getLatitude() == 51)
97 System.out.println(e.getKey().getTileName() + " - " + e.getKey().hashCode() + " -> " + e.getValue());
100 throw new SrtmSourceException("tile not in database "+inTile.getTileName());
104 return new URL(URL_PREFIX + path + getSourceExtension());
106 catch (MalformedURLException e)
109 throw new SrtmSourceException(e.getMessage());
113 public boolean isReadyToUse()
118 public boolean downloadTile(SrtmTile inTile)
119 throws SrtmSourceException
122 URL tileUrl = buildUrl(inTile);
123 File outputFile = getCacheFileName(inTile);
124 System.out.println("Download: Need to download: " + tileUrl);
128 HttpURLConnection conn = (HttpURLConnection) tileUrl.openConnection();
131 FileOutputStream outStream = null;
132 InputStream inStream = null;
134 conn.setRequestProperty("User-Agent", "GpsPrune v" + GpsPrune.VERSION_NUMBER);
136 int status = conn.getResponseCode();
139 inStream = conn.getInputStream();
141 else if (status == 404)
143 throw new SrtmSourceException("Tile not found: "+conn.getURL());
147 throw new SrtmSourceException("Invalid response from server: " +status+conn.getContent());
150 outStream = new FileOutputStream(outputFile);
153 while ((c = inStream.read()) != -1)
157 // Make sure streams are closed
158 try {inStream.close();} catch (Exception e) {}
159 try {outStream.close();} catch (Exception e) {}
162 catch (IOException e)
164 throw new SrtmSourceException("Error while downloading tile "+inTile.getTileName()+": "+e.getMessage());
168 public int getRowSize(SrtmTile inTile)