1 package tim.prune.function.srtm;
3 import java.io.InputStream;
4 import java.net.MalformedURLException;
6 import java.util.ArrayList;
10 * Class to get the URLs of the SRTM tiles
11 * using the srtmtiles.dat file
13 public abstract class TileFinder
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"};
23 * Get the Urls for the given list of tiles
24 * @param inTiles list of Tiles to get
25 * @return array of URLs
27 public static URL[] getUrls(ArrayList<SrtmTile> inTiles)
29 if (inTiles == null || inTiles.size() < 1) {return null;}
30 URL[] urls = new URL[inTiles.size()];
31 // Read dat file into array
32 byte[] lookup = readDatFile();
33 for (int t=0; t<inTiles.size(); t++)
35 SrtmTile tile = inTiles.get(t);
36 // Get byte from lookup array
37 int idx = (tile.getLatitude() + 59)*360 + (tile.getLongitude() + 180);
40 int dir = lookup[idx];
43 urls[t] = new URL(URL_PREFIX + CONTINENTS[dir] + "/" + tile.getTileName());
44 } catch (MalformedURLException e) {} // ignore error, url stays null
46 } catch (ArrayIndexOutOfBoundsException e) {} // ignore error, url stays null
52 * Read the dat file and get the contents
53 * @return byte array containing file contents
55 private static byte[] readDatFile()
57 InputStream in = null;
60 // Need absolute path to dat file
61 in = TileFinder.class.getResourceAsStream("/tim/prune/function/srtm/srtmtiles.dat");
64 byte[] buffer = new byte[in.available()];
70 catch (java.io.IOException e) {
71 System.err.println("Exception trying to read srtmtiles.dat : " + e.getMessage());
78 catch (Exception e) {} // ignore