X-Git-Url: https://gitweb.fperrin.net/?p=GpsPrune.git;a=blobdiff_plain;f=src%2Ftim%2Fprune%2Ffunction%2Fsrtm%2FTileFinder.java;fp=src%2Ftim%2Fprune%2Ffunction%2Fsrtm%2FTileFinder.java;h=60a9479d1289faae0523e0fd7d025b56e816d37e;hp=0000000000000000000000000000000000000000;hb=ce6f2161b8596f7018d6a76bff79bc9e571f35fd;hpb=2d8cb72e84d5cc1089ce77baf1e34ea3ea2f8465 diff --git a/src/tim/prune/function/srtm/TileFinder.java b/src/tim/prune/function/srtm/TileFinder.java new file mode 100644 index 0000000..60a9479 --- /dev/null +++ b/src/tim/prune/function/srtm/TileFinder.java @@ -0,0 +1,82 @@ +package tim.prune.function.srtm; + +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.ArrayList; + + +/** + * Class to get the URLs of the SRTM tiles + * using the srtmtiles.dat file + */ +public abstract class TileFinder +{ + /** URL prefix for all tiles */ + private static final String URL_PREFIX = "https://dds.cr.usgs.gov/srtm/version2_1/SRTM3/"; + /** Directory names for each continent */ + private static final String[] CONTINENTS = {"", "Eurasia", "North_America", "Australia", + "Islands", "South_America", "Africa"}; + + + /** + * Get the Urls for the given list of tiles + * @param inTiles list of Tiles to get + * @return array of URLs + */ + public static URL[] getUrls(ArrayList inTiles) + { + if (inTiles == null || inTiles.size() < 1) {return null;} + URL[] urls = new URL[inTiles.size()]; + // Read dat file into array + byte[] lookup = readDatFile(); + for (int t=0; t 0) { + try { + urls[t] = new URL(URL_PREFIX + CONTINENTS[dir] + "/" + tile.getTileName()); + } catch (MalformedURLException e) {} // ignore error, url stays null + } + } catch (ArrayIndexOutOfBoundsException e) {} // ignore error, url stays null + } + return urls; + } + + /** + * Read the dat file and get the contents + * @return byte array containing file contents + */ + private static byte[] readDatFile() + { + InputStream in = null; + try + { + // Need absolute path to dat file + in = TileFinder.class.getResourceAsStream("/tim/prune/function/srtm/srtmtiles.dat"); + if (in != null) + { + byte[] buffer = new byte[in.available()]; + in.read(buffer); + in.close(); + return buffer; + } + } + catch (java.io.IOException e) { + System.err.println("Exception trying to read srtmtiles.dat : " + e.getMessage()); + } + finally + { + try { + in.close(); + } + catch (Exception e) {} // ignore + } + return null; + } +}