1 package tim.prune.function.srtm;
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;
12 import java.net.HttpURLConnection;
15 import tim.prune.GpsPrune;
16 import tim.prune.config.Config;
19 * Create an account at: https://urs.earthdata.nasa.gov/users/new
20 * Data policy: https://lpdaac.usgs.gov/data/data-citation-and-policies/
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/";
28 private static final String AUTH_URL = "urs.earthdata.nasa.gov";
31 public SrtmGl1Source()
33 CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
36 public String getName()
38 return "SRTMGL1_v003";
41 protected String getSourceExtension()
43 return ".SRTMGL1.hgt.zip";
46 private URL buildUrl(SrtmTile inTile)
47 throws SrtmSourceException
50 return new URL(URL_PREFIX + inTile.getTileName() + getSourceExtension());
52 catch (MalformedURLException e)
54 throw new SrtmSourceException(e.getMessage());
58 public boolean isReadyToUse()
60 return getAuth() != null;
63 private String getAuth()
65 String authString = Config.getConfigString(Config.KEY_EARTHDATA_AUTH);
66 if (authString != null)
68 return "Basic " + authString;
76 public boolean downloadTile(SrtmTile inTile)
77 throws SrtmSourceException
79 return downloadTile(inTile, getAuth());
82 private boolean downloadTile(SrtmTile inTile, String auth)
83 throws SrtmSourceException
85 URL tileUrl = buildUrl(inTile);
86 File outputFile = getCacheFileName(inTile);
87 System.out.println("Download: Need to download: " + tileUrl);
90 HttpURLConnection conn = (HttpURLConnection) tileUrl.openConnection();
94 FileOutputStream outStream = null;
95 InputStream inStream = null;
97 // Documentation about HTTP interface at:
98 // https://wiki.earthdata.nasa.gov/display/EL/How+To+Access+Data+With+Java
101 while (redirects < 10) {
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))
109 conn.setRequestProperty("Authorization", auth);
112 int status = conn.getResponseCode();
115 // Found the tile, we're good
116 inStream = conn.getInputStream();
117 fileLength = conn.getContentLengthLong();
120 else if (status == 302)
122 // redirected to SSO server then back to original resource
123 String newUrl = conn.getHeaderField("Location");
124 conn = (HttpURLConnection) (new URL(newUrl)).openConnection();
126 else if (status == 404)
128 throw new SrtmSourceException("Tile " + inTile.getTileName() + " not found at " + conn.getURL());
132 throw new SrtmSourceException("Invalid response from server: " + status + conn.getResponseMessage());
136 // _progress.setValue(t * 10 + 1);
137 outStream = new FileOutputStream(outputFile);
139 // Copy all the bytes to the file
142 while ((c = inStream.read()) != -1)
146 // _progress.setValue(t * 10 + 1 + (int) ((10 * written) / fileLength));
148 // Make sure streams are closed
149 try {inStream.close();} catch (Exception e) {}
150 try {outStream.close();} catch (Exception e) {}
153 catch (IOException e)
155 throw new SrtmSourceException("Error while downloading tile " + inTile.getTileName() + ": "+e.getMessage());
159 public boolean testAuth(String auth)
160 throws SrtmSourceException
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))
167 getCacheFileName(testTile).delete();
169 return downloadTile(testTile, auth);
172 public int getRowSize(SrtmTile inTile)