--- /dev/null
+package tim.prune.function.srtm;
+
+import java.io.File;
+import java.io.FileReader;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+import java.io.IOException;
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.HttpURLConnection;
+import java.util.HashMap;
+
+import tim.prune.GpsPrune;
+import tim.prune.I18nManager;
+
+public class SrtmViewfinderSource extends SrtmSource {
+ /** URL prefix for all tiles */
+ private static final String URL_PREFIX = "http://viewfinderpanoramas.org/";
+ private HashMap<SrtmTile, String> _tile_lookup = null;
+
+ public SrtmViewfinderSource()
+ {
+ }
+
+ public String getNameKey()
+ {
+ return "function.downloadsrtm." + getName();
+ }
+
+ public String getName()
+ {
+ return "SRTM_Viewfinder";
+ }
+
+ protected String getSourceExtension()
+ {
+ return ".zip";
+ }
+
+ /**
+ * Read the dat file and get the contents
+ * @return byte array containing file contents
+ */
+ private void populateLookup()
+ throws SrtmSourceException
+ {
+ BufferedReader in = null;
+ try
+ {
+ // in = SrtmViewfinderSource.class.getResourceAsStream("viewfinder/tiles.db");
+ _tile_lookup = new HashMap<SrtmTile, String>();
+ in = new BufferedReader(new InputStreamReader(SrtmViewfinderSource.class.getResourceAsStream("/tim/prune/function/srtm/viewfinder/tiles.dat")));
+ String line;
+ while ((line = in.readLine()) != null)
+ {
+ String[] parts = line.split(" ");
+ SrtmTile tile = new SrtmTile(Integer.parseInt(parts[0]), Integer.parseInt(parts[1]));
+ _tile_lookup.put(tile, parts[2]);
+ }
+ }
+ catch (Exception e) {
+ throw new SrtmSourceException("Exception trying to read tiles.db: " + e);
+ }
+ finally
+ {
+ try {
+ in.close();
+ }
+ catch (Exception e) {} // ignore
+ }
+ }
+
+ /**
+ * Get the Url for the given tile
+ * @param inTile Tile to get
+ * @return URL
+ */
+ private URL buildUrl(SrtmTile inTile)
+ throws SrtmSourceException
+ {
+ if (_tile_lookup == null)
+ {
+ populateLookup();
+ }
+ System.out.println(_tile_lookup.get(inTile));
+ String path = _tile_lookup.get(inTile);
+ if (path == null)
+ {
+ int key = inTile.hashCode();
+ System.out.println("inTile hashcode " + key);
+ for (HashMap.Entry<SrtmTile, String> e : _tile_lookup.entrySet())
+ {
+ if (e.getKey().getLatitude() == 51)
+ {
+ System.out.println(e.getKey().getTileName() + " - " + e.getKey().hashCode() + " -> " + e.getValue());
+ }
+ }
+ throw new SrtmSourceException("tile not in database "+inTile.getTileName());
+ }
+ try
+ {
+ return new URL(URL_PREFIX + path + getSourceExtension());
+ }
+ catch (MalformedURLException e)
+ {
+ e.printStackTrace();
+ throw new SrtmSourceException(e.getMessage());
+ }
+ }
+
+ public boolean isReadyToUse()
+ {
+ return true;
+ }
+
+ public boolean downloadTile(SrtmTile inTile)
+ throws SrtmSourceException
+ {
+ int redirects = 5;
+ URL tileUrl = buildUrl(inTile);
+ File outputFile = getCacheFileName(inTile);
+ System.out.println("Download: Need to download: " + tileUrl);
+
+ try
+ {
+ HttpURLConnection conn = (HttpURLConnection) tileUrl.openConnection();
+
+ // Define streams
+ FileOutputStream outStream = null;
+ InputStream inStream = null;
+
+ conn.setRequestProperty("User-Agent", "GpsPrune v" + GpsPrune.VERSION_NUMBER);
+
+ int status = conn.getResponseCode();
+ if (status == 200)
+ {
+ inStream = conn.getInputStream();
+ }
+ else if (status == 404)
+ {
+ throw new SrtmSourceException("Tile not found: "+conn.getURL());
+ }
+ else
+ {
+ throw new SrtmSourceException("Invalid response from server: " +status+conn.getContent());
+ }
+
+ outStream = new FileOutputStream(outputFile);
+
+ int c;
+ while ((c = inStream.read()) != -1)
+ {
+ outStream.write(c);
+ }
+ // Make sure streams are closed
+ try {inStream.close();} catch (Exception e) {}
+ try {outStream.close();} catch (Exception e) {}
+ return true;
+ }
+ catch (IOException e)
+ {
+ throw new SrtmSourceException("Error while downloading tile "+inTile.getTileName()+": "+e.getMessage());
+ }
+ }
+
+ public int getRowSize(SrtmTile inTile)
+ {
+ return 1201;
+ }
+}