]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/function/srtm/Srtm3Source.java
cc0da79b1c790f85e4a21ca473ea06b8bd457c9d
[GpsPrune.git] / src / tim / prune / function / srtm / Srtm3Source.java
1 package tim.prune.function.srtm;
2
3 import java.io.File;
4 import java.io.FileOutputStream;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.net.MalformedURLException;
8 import java.net.URL;
9 import java.net.HttpURLConnection;
10
11 import tim.prune.GpsPrune;
12 import tim.prune.I18nManager;
13
14 public class Srtm3Source extends SrtmSource {
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         private byte[] _continents_lookup;
21
22
23         public Srtm3Source()
24         {
25                 _continents_lookup = populateContinents();
26         }
27
28         public String getNameKey()
29         {
30                 return "function.downloadsrtm." + getName();
31         }
32
33         public String getName()
34         {
35                 return "SRTM3_v21";
36         }
37
38         protected String getSourceExtension()
39         {
40                 return ".hgt.zip";
41         }
42
43         /**
44          * Read the dat file and get the contents
45          * @return byte array containing file contents
46          */
47         private static byte[] populateContinents()
48         {
49                 InputStream in = null;
50                 try
51                 {
52                         // Need absolute path to dat file
53                         in = Srtm3Source.class.getResourceAsStream("/tim/prune/function/srtm/srtmtiles.dat");
54                         if (in != null)
55                         {
56                                 byte[] buffer = new byte[in.available()];
57                                 in.read(buffer);
58                                 in.close();
59                                 return buffer;
60                         }
61                 }
62                 catch (java.io.IOException e) {
63                         System.err.println("Exception trying to read srtmtiles.dat : " + e.getMessage());
64                 }
65                 finally
66                 {
67                         try {
68                                 in.close();
69                         }
70                         catch (Exception e) {} // ignore
71                 }
72                 return null;
73         }
74
75         /**
76          * Get the Url for the given tile
77          * @param inTile Tile to get
78          * @return URL
79          */
80         private URL buildUrl(SrtmTile inTile)
81                 throws SrtmSourceException
82         {
83                 
84                 // Get byte from lookup array
85                 int idx = (inTile.getLatitude() + 59)*360 + (inTile.getLongitude() + 180);
86                 int dir;
87                 try
88                 {
89                         dir = _continents_lookup[idx];
90                 }
91                 catch (ArrayIndexOutOfBoundsException e)
92                 {
93                         throw new SrtmSourceException("Could not find continent for tile "+inTile.getTileName());
94                 }
95                 try
96                 {
97                         return new URL(URL_PREFIX + CONTINENTS[dir] + "/" + inTile.getTileName() + getSourceExtension());
98                 }
99                 catch (MalformedURLException e)
100                 {
101                         throw new SrtmSourceException("Could not build URL for tile "+inTile.getTileName());
102                 }
103         }
104
105         public boolean isReadyToUse()
106         {
107                 return true;
108         }
109
110         public boolean downloadTile(SrtmTile inTile)
111                 throws SrtmSourceException
112         {
113                 URL tileUrl = buildUrl(inTile);
114                 File outputFile = getCacheFileName(inTile);
115                 System.out.println("Download: Need to download: " + tileUrl);
116
117                 try
118                 {
119                         HttpURLConnection conn = (HttpURLConnection) tileUrl.openConnection();
120
121                         // Define streams
122                         InputStream inStream = null;
123
124                         conn.setRequestProperty("User-Agent", "GpsPrune v" + GpsPrune.VERSION_NUMBER);
125
126                         int status = conn.getResponseCode();
127                         if (status == 200)
128                         {
129                                 inStream = conn.getInputStream();
130                         }
131                         else if (status == 404)
132                         {
133                                 throw new SrtmSourceException("Tile not found: "+conn.getURL());
134                         }
135                         else
136                         {
137                                 throw new SrtmSourceException("Invalid response from server: " +status+conn.getContent());
138                         }
139                         return downloadToFile(inStream, outputFile);
140                 }
141                 catch (IOException e)
142                 {
143                         throw new SrtmSourceException("Error while downloading tile "+inTile.getTileName()+": "+e.getMessage());
144                 }
145         }
146
147         public int getRowSize(SrtmTile inTile)
148         {
149                 return 1201;
150         }
151 }