]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/function/srtm/TileFinder.java
60a9479d1289faae0523e0fd7d025b56e816d37e
[GpsPrune.git] / src / tim / prune / function / srtm / TileFinder.java
1 package tim.prune.function.srtm;
2
3 import java.io.InputStream;
4 import java.net.MalformedURLException;
5 import java.net.URL;
6 import java.util.ArrayList;
7
8
9 /**
10  * Class to get the URLs of the SRTM tiles
11  * using the srtmtiles.dat file
12  */
13 public abstract class TileFinder
14 {
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
21
22         /**
23          * Get the Urls for the given list of tiles
24          * @param inTiles list of Tiles to get
25          * @return array of URLs
26          */
27         public static URL[] getUrls(ArrayList<SrtmTile> inTiles)
28         {
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++)
34                 {
35                         SrtmTile tile = inTiles.get(t);
36                         // Get byte from lookup array
37                         int idx = (tile.getLatitude() + 59)*360 + (tile.getLongitude() + 180);
38                         try
39                         {
40                                 int dir = lookup[idx];
41                                 if (dir > 0) {
42                                         try {
43                                                 urls[t] = new URL(URL_PREFIX + CONTINENTS[dir] + "/" + tile.getTileName());
44                                         } catch (MalformedURLException e) {} // ignore error, url stays null
45                                 }
46                         } catch (ArrayIndexOutOfBoundsException e) {} // ignore error, url stays null
47                 }
48                 return urls;
49         }
50
51         /**
52          * Read the dat file and get the contents
53          * @return byte array containing file contents
54          */
55         private static byte[] readDatFile()
56         {
57                 InputStream in = null;
58                 try
59                 {
60                         // Need absolute path to dat file
61                         in = TileFinder.class.getResourceAsStream("/tim/prune/function/srtm/srtmtiles.dat");
62                         if (in != null)
63                         {
64                                 byte[] buffer = new byte[in.available()];
65                                 in.read(buffer);
66                                 in.close();
67                                 return buffer;
68                         }
69                 }
70                 catch (java.io.IOException e) {
71                         System.err.println("Exception trying to read srtmtiles.dat : " + e.getMessage());
72                 }
73                 finally
74                 {
75                         try {
76                                 in.close();
77                         }
78                         catch (Exception e) {} // ignore
79                 }
80                 return null;
81         }
82 }