1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
package tim.prune.function.srtm;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
/**
* High-resolution Srtm source, using 1-arcsecond data
* instead of the low-resolution 3-arcsecond data
* @author fperrin, activityworkshop
*/
public class SrtmHighResSource extends SrtmSource
{
/** Flag set to false if auth fails */
private boolean _enabled = true;
/** Auth string */
private final String _authString;
/** URL prefix for all tiles */
private static final String URL_PREFIX = "https://e4ftl01.cr.usgs.gov/MEASURES/SRTMGL1.003/2000.02.11/";
/** Auth URL */
private static final String AUTH_HOST = "urs.earthdata.nasa.gov";
private static final int HTTP_CODE_OK = 200;
private static final int HTTP_CODE_REDIRECT = 302;
private static final int HTTP_CODE_NOTFOUND = 404;
/**
* @param inDiskPath disk path, if any
* @param inAuthString authentication string
*/
SrtmHighResSource(String inDiskPath, String inAuthString)
{
super(inDiskPath);
_authString = inAuthString;
}
@Override
public int getTilePixels() {
return 3601;
}
/**
* @return the Url for the given tile
*/
@Override
public URL getUrl(SrtmTile inTile)
{
URL url = null;
try {
url = new URL(URL_PREFIX + getFilename(inTile));
} catch (MalformedURLException e) {} // ignore error, url stays null
return url;
}
@Override
public Result downloadTile(SrtmTile inTile) throws SrtmAuthException
{
final String authString = getAuthString();
if (authString == null || !_enabled) {return Result.NOT_ENABLED;}
try
{
// Retrieve a stream for the resource
URL url = getUrl(inTile);
InputStream inStream = getResource(url, authString);
if (writeFileFromStream(url, inStream)) {
return Result.DOWNLOADED;
}
}
catch (SrtmAuthException authExc)
{
_enabled = false;
throw authExc;
}
catch (Exception e) {}
return Result.DOWNLOAD_FAILED;
}
@Override
public String getFilename(SrtmTile inTile)
{
return inTile.getTileName() + ".SRTMGL1.hgt.zip";
}
/**
* @return auth string, if possible
*/
private String getAuthString()
{
if (_authString == null) {
return null;
}
return "Basic " + _authString;
}
/**
* Returns an input stream for a designated resource on a URS
* authenticated remote server.
* @param resourceUrl url to get
* @param inAuthString username/password from config
*/
private static InputStream getResource(URL resourceUrl, String inAuthString)
throws Exception
{
// Method provided by:
// https://wiki.earthdata.nasa.gov/display/EL/How+To+Access+Data+With+Java
int redirects = 0;
URL url = resourceUrl;
// Place an upper limit on the number of redirects we will follow
while( redirects < 10 )
{
++redirects;
// Configure a connection to the resource server and submit the
// request for our resource.
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setInstanceFollowRedirects(false);
connection.setUseCaches(false);
connection.setDoInput(true);
// If this is the URS server, add in the authentication header.
if (url.getHost().startsWith(AUTH_HOST))
{
connection.setDoOutput(true);
connection.setRequestProperty("Authorization", inAuthString);
}
// Execute the request and get the response status code.
int status = connection.getResponseCode();
if (status == HTTP_CODE_OK)
{
// OK means that we have got our resource
return connection.getInputStream();
}
if (status == HTTP_CODE_REDIRECT)
{
// 302 is a redirect, so we go round the loop again.
url = new URL(connection.getHeaderField("Location"));
}
else if (status == HTTP_CODE_NOTFOUND) {
throw new Exception("SRTM file not found");
}
else {
throw new SrtmAuthException(status);
}
}
// If we get here, we exceeded our redirect limit
throw new Exception("Redirection limit exceeded");
}
}
|