1 package tim.prune.function.srtm;
4 import java.io.FileOutputStream;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.net.MalformedURLException;
9 import java.net.HttpURLConnection;
11 import tim.prune.GpsPrune;
12 import tim.prune.I18nManager;
14 public class Srtm3Source extends SrtmSource {
15 /** URL prefix for all tiles */
16 private static final String URL_PREFIX = "https://dds.cr.usgs.gov/srtm/version2_1/SRTM3/";
17 /** Directory names for each continent */
18 private static final String[] CONTINENTS = {"", "Eurasia", "North_America", "Australia",
19 "Islands", "South_America", "Africa"};
20 private byte[] _continents_lookup;
25 _continents_lookup = populateContinents();
28 public String getNameKey()
30 return "function.downloadsrtm." + getName();
33 public String getName()
38 protected String getSourceExtension()
44 * Read the dat file and get the contents
45 * @return byte array containing file contents
47 private static byte[] populateContinents()
49 InputStream in = null;
52 // Need absolute path to dat file
53 in = Srtm3Source.class.getResourceAsStream("/tim/prune/function/srtm/srtmtiles.dat");
56 byte[] buffer = new byte[in.available()];
62 catch (java.io.IOException e) {
63 System.err.println("Exception trying to read srtmtiles.dat : " + e.getMessage());
70 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
84 // Get byte from lookup array
85 int idx = (inTile.getLatitude() + 59)*360 + (inTile.getLongitude() + 180);
89 dir = _continents_lookup[idx];
91 catch (ArrayIndexOutOfBoundsException e)
93 throw new SrtmSourceException("Could not find continent for tile "+inTile.getTileName());
97 return new URL(URL_PREFIX + CONTINENTS[dir] + "/" + inTile.getTileName() + getSourceExtension());
99 catch (MalformedURLException e)
101 throw new SrtmSourceException("Could not build URL for tile "+inTile.getTileName());
105 public boolean isReadyToUse()
110 public boolean downloadTile(SrtmTile inTile)
111 throws SrtmSourceException
114 URL tileUrl = buildUrl(inTile);
115 File outputFile = getCacheFileName(inTile);
116 System.out.println("Download: Need to download: " + tileUrl);
120 HttpURLConnection conn = (HttpURLConnection) tileUrl.openConnection();
123 FileOutputStream outStream = null;
124 InputStream inStream = null;
126 conn.setRequestProperty("User-Agent", "GpsPrune v" + GpsPrune.VERSION_NUMBER);
128 int status = conn.getResponseCode();
131 inStream = conn.getInputStream();
133 else if (status == 404)
135 throw new SrtmSourceException("Tile not found: "+conn.getURL());
139 throw new SrtmSourceException("Invalid response from server: " +status+conn.getContent());
142 outStream = new FileOutputStream(outputFile);
145 while ((c = inStream.read()) != -1)
149 // Make sure streams are closed
150 try {inStream.close();} catch (Exception e) {}
151 try {outStream.close();} catch (Exception e) {}
154 catch (IOException e)
156 throw new SrtmSourceException("Error while downloading tile "+inTile.getTileName()+": "+e.getMessage());
160 public int getRowSize(SrtmTile inTile)