]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/function/srtm/TileFinder.java
Version 20.4, May 2021
[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                 if (lookup == null)
34                 {
35                         System.err.println("Build error: resource srtmtiles.dat missing!");
36                         return null;
37                 }
38                 for (int t=0; t<inTiles.size(); t++)
39                 {
40                         SrtmTile tile = inTiles.get(t);
41                         // Get byte from lookup array
42                         int idx = (tile.getLatitude() + 59)*360 + (tile.getLongitude() + 180);
43                         try
44                         {
45                                 int dir = lookup[idx];
46                                 if (dir > 0) {
47                                         try {
48                                                 urls[t] = new URL(URL_PREFIX + CONTINENTS[dir] + "/" + tile.getTileName());
49                                         } catch (MalformedURLException e) {} // ignore error, url stays null
50                                 }
51                         } catch (ArrayIndexOutOfBoundsException e) {} // ignore error, url stays null
52                 }
53                 return urls;
54         }
55
56         /**
57          * Read the dat file and get the contents
58          * @return byte array containing file contents
59          */
60         private static byte[] readDatFile()
61         {
62                 InputStream in = null;
63                 try
64                 {
65                         // Need absolute path to dat file
66                         in = TileFinder.class.getResourceAsStream("/tim/prune/function/srtm/srtmtiles.dat");
67                         if (in != null)
68                         {
69                                 byte[] buffer = new byte[in.available()];
70                                 in.read(buffer);
71                                 in.close();
72                                 return buffer;
73                         }
74                 }
75                 catch (java.io.IOException e) {
76                         System.err.println("Exception trying to read srtmtiles.dat : " + e.getMessage());
77                 }
78                 finally
79                 {
80                         try {
81                                 in.close();
82                         }
83                         catch (Exception e) {} // ignore
84                 }
85                 return null;
86         }
87 }