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();
92 InputStream inStream = null;
94 // Documentation about HTTP interface at:
95 // https://wiki.earthdata.nasa.gov/display/EL/How+To+Access+Data+With+Java
98 while (redirects < 10) {
101 conn.setRequestProperty("User-Agent", "GpsPrune v" + GpsPrune.VERSION_NUMBER);
102 conn.setInstanceFollowRedirects(false);
103 conn.setUseCaches(false);
104 if (conn.getURL().getHost().equals(AUTH_URL))
106 conn.setRequestProperty("Authorization", auth);
109 int status = conn.getResponseCode();
112 // Found the tile, we're good
113 inStream = conn.getInputStream();
116 else if (status == 302)
118 // redirected to SSO server then back to original resource
119 String newUrl = conn.getHeaderField("Location");
120 conn = (HttpURLConnection) (new URL(newUrl)).openConnection();
122 else if (status == 404)
124 throw new SrtmSourceException("Tile " + inTile.getTileName() + " not found at " + conn.getURL());
128 throw new SrtmSourceException("Invalid response from server: " + status + conn.getResponseMessage());
132 return downloadToFile(inStream, outputFile);
134 catch (IOException e)
136 throw new SrtmSourceException("Error while downloading tile " + inTile.getTileName() + ": "+e.getMessage());
140 public boolean testAuth(String auth)
141 throws SrtmSourceException
143 // The only thing special about this tile is that it's the smallest tile
144 // It covers small islands in Malaysia
145 SrtmTile testTile = new SrtmTile(7, 117);
146 if (isCached(testTile))
148 getCacheFileName(testTile).delete();
150 return downloadTile(testTile, auth);
153 public int getRowSize(SrtmTile inTile)