]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/function/srtm/TileFinder.java
Version 20.1, December 2020
[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
7
8 /**
9  * Class to get the URLs of the SRTM tiles
10  * using the srtmtiles.dat file
11  */
12 public class TileFinder
13 {
14         /** tile data loaded from file */
15         private byte[] _tileData = null;
16
17         /** URL prefix for all tiles */
18         private static final String URL_PREFIX = "https://dds.cr.usgs.gov/srtm/version2_1/SRTM3/";
19         /** Directory names for each continent */
20         private static final String[] CONTINENTS = {"", "Eurasia", "North_America", "Australia",
21                 "Islands", "South_America", "Africa"};
22
23
24         /**
25          * Get the Urls for the given list of tiles
26          * @param inTiles list of Tiles to get
27          * @return array of URLs
28          */
29         public URL getUrl(SrtmTile inTile)
30         {
31                 if (inTile == null) {return null;}
32                 if (_tileData == null)
33                 {
34                         _tileData = readDatFile();
35                         if (_tileData == null)
36                         {
37                                 System.err.println("Build error: resource srtmtiles.dat missing!");
38                                 return null;
39                         }
40                 }
41
42                 URL url = null;
43                 // Get byte from lookup array
44                 int idx = (inTile.getLatitude() + 59)*360 + (inTile.getLongitude() + 180);
45                 try
46                 {
47                         int dir = _tileData[idx];
48                         if (dir > 0) {
49                                 try {
50                                         url = new URL(URL_PREFIX + CONTINENTS[dir] + "/" + inTile.getTileName());
51                                 } catch (MalformedURLException e) {} // ignore error, url stays null
52                         }
53                 } catch (ArrayIndexOutOfBoundsException e) {} // ignore error, url stays null
54
55                 return url;
56         }
57
58         /**
59          * Read the dat file and get the contents
60          * @return byte array containing file contents
61          */
62         private static byte[] readDatFile()
63         {
64                 InputStream in = null;
65                 try
66                 {
67                         // Need absolute path to dat file
68                         in = TileFinder.class.getResourceAsStream("/tim/prune/function/srtm/srtmtiles.dat");
69                         if (in != null)
70                         {
71                                 byte[] buffer = new byte[in.available()];
72                                 in.read(buffer);
73                                 in.close();
74                                 return buffer;
75                         }
76                 }
77                 catch (java.io.IOException e) {
78                         System.err.println("Exception trying to read srtmtiles.dat : " + e.getMessage());
79                 }
80                 finally
81                 {
82                         try {
83                                 in.close();
84                         }
85                         catch (Exception e) {} // ignore
86                 }
87                 return null;
88         }
89 }