]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/function/srtm/SrtmGl1Source.java
Use SRTM 1deg data from NASA servers
[GpsPrune.git] / src / tim / prune / function / srtm / SrtmGl1Source.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.CookieHandler;
8 import java.net.CookieManager;
9 import java.net.CookiePolicy;
10 import java.net.MalformedURLException;
11 import java.net.URL;
12 import java.net.HttpURLConnection;
13
14 import tim.prune.App;
15 import tim.prune.GpsPrune;
16 import tim.prune.config.Config;
17
18 /**
19  * Create an account at: https://urs.earthdata.nasa.gov/users/new
20  * Data policy: https://lpdaac.usgs.gov/data/data-citation-and-policies/
21  *
22  */
23
24 public class SrtmGl1Source extends SrtmSource {
25         /** URL prefix for all tiles */
26         private static final String URL_PREFIX = "https://e4ftl01.cr.usgs.gov/MEASURES/SRTMGL1.003/2000.02.11/";
27         /** Auth URL */
28         private static final String AUTH_URL = "urs.earthdata.nasa.gov";
29
30
31         public SrtmGl1Source()
32         {
33                 CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
34         }
35
36         public String getName()
37         {
38                 return "SRTMGL1_v003";
39         }
40
41         protected String getSourceExtension()
42         {
43                 return ".SRTMGL1.hgt.zip";
44         }
45
46         private URL buildUrl(SrtmTile inTile)
47                 throws SrtmSourceException
48         {
49                 try {
50                         return new URL(URL_PREFIX + inTile.getTileName() + getSourceExtension());
51                 }
52                 catch (MalformedURLException e)
53                 {
54                         throw new SrtmSourceException(e.getMessage());
55                 }
56         }
57
58         public boolean isReadyToUse()
59         {
60                 return getAuth() != null;
61         }
62
63         private String getAuth()
64         {
65                 String authString = Config.getConfigString(Config.KEY_EARTHDATA_AUTH);
66                 if (authString != null)
67                 {
68                         return "Basic " + authString; 
69                 }
70                 else
71                 {
72                         return null;
73                 }
74         }
75
76         public boolean downloadTile(SrtmTile inTile)
77                 throws SrtmSourceException
78         {
79                 return downloadTile(inTile, getAuth());
80         }
81
82         private boolean downloadTile(SrtmTile inTile, String auth)
83                 throws SrtmSourceException
84         {
85                 URL tileUrl = buildUrl(inTile);
86                 File outputFile = getCacheFileName(inTile);
87                 System.out.println("Download: Need to download: " + tileUrl);
88                 try
89                 {
90                         HttpURLConnection conn = (HttpURLConnection) tileUrl.openConnection();
91                         long fileLength = 0L;
92
93                         // Define streams
94                         FileOutputStream outStream = null;
95                         InputStream inStream = null;
96
97                         // Documentation about HTTP interface at:
98                         // https://wiki.earthdata.nasa.gov/display/EL/How+To+Access+Data+With+Java
99                         int redirects = 0;
100
101                         while (redirects < 10) {
102                                 redirects++;
103
104                                 conn.setRequestProperty("User-Agent", "GpsPrune v" + GpsPrune.VERSION_NUMBER);
105                                 conn.setInstanceFollowRedirects(false);
106                                 conn.setUseCaches(false);
107                                 if (conn.getURL().getHost().equals(AUTH_URL))
108                                 {
109                                         conn.setRequestProperty("Authorization", auth);
110                                 }
111
112                                 int status = conn.getResponseCode();
113                                 if (status == 200)
114                                 {
115                                         // Found the tile, we're good
116                                         inStream = conn.getInputStream();
117                                         fileLength = conn.getContentLengthLong();
118                                         break;
119                                 }
120                                 else if (status == 302)
121                                 {
122                                         // redirected to SSO server then back to original resource
123                                         String newUrl = conn.getHeaderField("Location");
124                                         conn = (HttpURLConnection) (new URL(newUrl)).openConnection();
125                                 }
126                                 else if (status == 404)
127                                 {
128                                         throw new SrtmSourceException("Tile " + inTile.getTileName() + " not found at " + conn.getURL());
129                                 }
130                                 else
131                                 {
132                                         throw new SrtmSourceException("Invalid response from server: " + status + conn.getResponseMessage());
133                                 }
134                         }
135
136                         // _progress.setValue(t * 10 + 1);
137                         outStream = new FileOutputStream(outputFile);
138
139                         // Copy all the bytes to the file
140                         int c;
141                         long written = 0L;
142                         while ((c = inStream.read()) != -1)
143                         {
144                                 outStream.write(c);
145                                 written++;
146                                 // _progress.setValue(t * 10 + 1 + (int) ((10 * written) / fileLength));
147                         }
148                         // Make sure streams are closed
149                         try {inStream.close();} catch (Exception e) {}
150                         try {outStream.close();} catch (Exception e) {}
151                         return true;
152                 }
153                 catch (IOException e)
154                 {
155                         throw new SrtmSourceException("Error while downloading tile " + inTile.getTileName() + ": "+e.getMessage());
156                 }
157         }
158
159         public boolean testAuth(String auth)
160                 throws SrtmSourceException
161         {
162                 // The only thing special about this tile is that it's the smallest tile
163                 // It covers small islands in Malaysia
164                 SrtmTile testTile = new SrtmTile(7, 117);
165                 if (isCached(testTile))
166                 {
167                         getCacheFileName(testTile).delete();
168                 }
169                 return downloadTile(testTile, auth);
170         }
171
172         public int getRowSize(SrtmTile inTile)
173         {
174                 return 3601;
175         }
176 }