]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/function/srtm/Srtm3Source.java
Use SRTM 1deg data from NASA servers
[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                 int redirects = 5;
114                 URL tileUrl = buildUrl(inTile);
115                 File outputFile = getCacheFileName(inTile);
116                 System.out.println("Download: Need to download: " + tileUrl);
117
118                 try
119                 {
120                         HttpURLConnection conn = (HttpURLConnection) tileUrl.openConnection();
121
122                         // Define streams
123                         FileOutputStream outStream = null;
124                         InputStream inStream = null;
125
126                         conn.setRequestProperty("User-Agent", "GpsPrune v" + GpsPrune.VERSION_NUMBER);
127
128                         int status = conn.getResponseCode();
129                         if (status == 200)
130                         {
131                                 inStream = conn.getInputStream();
132                         }
133                         else if (status == 404)
134                         {
135                                 throw new SrtmSourceException("Tile not found: "+conn.getURL());
136                         }
137                         else
138                         {
139                                 throw new SrtmSourceException("Invalid response from server: " +status+conn.getContent());
140                         }
141
142                         outStream = new FileOutputStream(outputFile);
143
144                         int c;
145                         while ((c = inStream.read()) != -1)
146                         {
147                                 outStream.write(c);
148                         }
149                         // Make sure streams are closed
150                         try {inStream.close();} catch (Exception e) {}
151                         try {outStream.close();} catch (Exception e) {}
152                         return true;
153                 }
154                 catch (IOException e)
155                 {
156                         throw new SrtmSourceException("Error while downloading tile "+inTile.getTileName()+": "+e.getMessage());
157                 }
158         }
159
160         public int getRowSize(SrtmTile inTile)
161         {
162                 return 1201;
163         }
164 }