1 package tim.prune.function.srtm;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.net.MalformedURLException;
9 import tim.prune.I18nManager;
11 public class Srtm3Source extends SrtmSource {
12 /** URL prefix for all tiles */
13 private static final String URL_PREFIX = "https://dds.cr.usgs.gov/srtm/version2_1/SRTM3/";
14 /** Directory names for each continent */
15 private static final String[] CONTINENTS = {"", "Eurasia", "North_America", "Australia",
16 "Islands", "South_America", "Africa"};
17 private byte[] _continents_lookup;
22 _continents_lookup = populateContinents();
25 public String getNameKey()
27 return "function.downloadsrtm." + getName();
30 public String getName()
35 protected String getSourceExtension()
41 * Read the dat file and get the contents
42 * @return byte array containing file contents
44 private static byte[] populateContinents()
46 InputStream in = null;
49 // Need absolute path to dat file
50 in = Srtm3Source.class.getResourceAsStream("/tim/prune/function/srtm/srtmtiles.dat");
53 byte[] buffer = new byte[in.available()];
59 catch (IOException e) {
60 System.err.println("Exception trying to read srtmtiles.dat : " + e.getMessage());
67 catch (Exception e) {} // ignore
73 * Get the Url for the given tile
74 * @param inTile Tile to get
77 private URL buildUrl(SrtmTile inTile)
78 throws SrtmSourceException
81 // Get byte from lookup array
82 int idx = (inTile.getLatitude() + 59)*360 + (inTile.getLongitude() + 180);
86 dir = _continents_lookup[idx];
88 catch (ArrayIndexOutOfBoundsException e)
90 throw new SrtmSourceException("Could not find continent for tile "+inTile.getTileName());
94 return new URL(URL_PREFIX + CONTINENTS[dir] + "/" + inTile.getTileName() + getSourceExtension());
96 catch (MalformedURLException e)
98 throw new SrtmSourceException("Could not build URL for tile "+inTile.getTileName());
102 public boolean isReadyToUse()
107 public boolean downloadTile(SrtmTile inTile)
108 throws SrtmSourceException
110 URL tileUrl = buildUrl(inTile);
111 InputStream inStream = getStreamToUrl(tileUrl);
112 return readToFile(inStream, getCacheFileName(inTile));
115 public int getRowSize(SrtmTile inTile)