Commit 81d7c1ec authored by Daniel Eggert's avatar Daniel Eggert
Browse files

revised to latest usgs api version 1.4.1

parent 1d1a834e
......@@ -8,17 +8,20 @@ import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Arrays;
import de.potsdam.gfz.usgsapi.json.datamodels.InventoryScene;
import de.potsdam.gfz.usgsapi.json.datamodels.SearchFilter;
import de.potsdam.gfz.usgsapi.json.datamodels.SearchFilter.Or;
import de.potsdam.gfz.usgsapi.json.datamodels.SceneDownloadOptions;
import de.potsdam.gfz.usgsapi.json.datamodels.TemporalFilter;
import de.potsdam.gfz.usgsapi.json.request.DownloadOptionsRequest;
import de.potsdam.gfz.usgsapi.json.request.DownloadRequest;
import de.potsdam.gfz.usgsapi.json.request.HitsRequest;
import de.potsdam.gfz.usgsapi.json.request.JSONRequest;
import de.potsdam.gfz.usgsapi.json.request.JSONRequest.RequestCode;
import de.potsdam.gfz.usgsapi.json.request.LoginRequest;
import de.potsdam.gfz.usgsapi.json.request.LogoutRequest;
import de.potsdam.gfz.usgsapi.json.request.SceneSearchRequest;
import de.potsdam.gfz.usgsapi.json.response.DownloadOptionsResponse;
import de.potsdam.gfz.usgsapi.json.response.DownloadResponse;
import de.potsdam.gfz.usgsapi.json.response.HitsResponse;
import de.potsdam.gfz.usgsapi.json.response.JSONResponse;
import de.potsdam.gfz.usgsapi.json.response.JSONResponseParser;
......@@ -33,12 +36,14 @@ import net.minidev.json.parser.ParseException;
*/
public class UsgsApi {
private static final String URL = "https://earthexplorer.usgs.gov/inventory/json/";
private static final String VERSION = "1.4.1";
private static final String URL = "https://earthexplorer.usgs.gov/inventory/json/v/" + VERSION + "/";
/**
* the received api key will be invalidated after one hour
*/
public static final long APIKEY_VALIDATION_TIMEOUT = 60 * 60 * 1000; // 1 hour
public static final long APIKEY_VALIDATION_TIMEOUT = 60 * 60 * 1000; // 1
// hour
/**
* Example requests
......@@ -46,11 +51,12 @@ public class UsgsApi {
* @param args
* @throws Exception
*/
@SuppressWarnings("unused")
public static void main(String[] args) throws Exception {
String apiKey = null;
System.out.println("submitting login request");
JSONResponse response = sendRequest(new LoginRequest("paste_your_username_here", "paste_your_password_here"));
JSONResponse response = sendRequest(new LoginRequest("usgs username", "usgs password"));
System.out.println(response.getClass().getSimpleName());
System.out.println("hasError: " + response.hasError());
if (response.hasError()) {
......@@ -63,107 +69,133 @@ public class UsgsApi {
}
}
final String datasetName = "LANDSAT_8";
// test dataset search
// if (apiKey != null) {
// response = sendRequest(new DatasetSearchRequest(apiKey));
// if (response instanceof DatasetSearchResponse) {
// for (Dataset ds : ((DatasetSearchResponse) response).getDatasetList()) {
// System.out.println(ds);
// }
// }
// }
// test hits request
// if (apiKey != null) {
// System.out.println("============================");
// response = sendRequest(new HitsRequest(apiKey, "LANDSAT_8", "2015-01-01T00:00:00Z", "2016-02-23T00:00:00Z"));
// if (response instanceof HitsResponse) {
// System.out.println("hits: " + ((HitsResponse) response).getHits());
// }
// }
if (apiKey != null) {
System.out.println("============================");
System.out.println("submitting hits request");
Or filter = new SearchFilter.Or();
filter.addSearchFilter(new SearchFilter.Between(10036, "30", "35"));
// filter.addSearchFilter(new SearchFilter.Value(10036, "39", Operand.equals));
response = sendRequest(new HitsRequest(apiKey, datasetName, "2015-01-01T00:00:00Z", "2016-02-23T00:00:00Z", filter));
if (response instanceof HitsResponse) {
System.out.println("hits: " + ((HitsResponse) response).getHits());
String[] entityids = new String[] { "LC81302072017128LGN00" };
String datasetName = "Landsat_8_C1";
// downloadOptionsExample(apiKey, entityids, datasetName);
// downloadExample(apiKey, entityids, datasetName);
hitsExample(apiKey, datasetName, new TemporalFilter("2015-01-01", "2015-01-01"));
// sceneSearchExample(apiKey, datasetName, new TemporalFilter("2017-07-15", "2017-07-15"));
logout(apiKey);
}
/**
* @param apiKey
* @throws InterruptedException
* @throws IOException
* @throws ParseException
*/
private static void logout(String apiKey) throws InterruptedException, IOException, ParseException {
JSONResponse response;
System.out.println("============================");
System.out.println("submitting logout request");
Thread.sleep(100l);
response = sendRequest(new LogoutRequest(apiKey));
if (response.hasError()) {
System.out.println("error code: " + response.getErrorcode() + " (" + ErrorCode.getErrorDescription(response.getErrorcode()) + ")");
System.out.println("error msg: " + response.getErrormsg());
} else {
if (response instanceof LogoutResponse) {
Boolean loggedout = ((LogoutResponse) response).isLoggedout();
System.out.println("successful: " + loggedout);
}
}
}
@SuppressWarnings("unused")
private static void sceneSearchExample(String apiKey, String datasetName, TemporalFilter temporalFilter) throws IOException, ParseException {
if (apiKey == null) {
return;
}
System.out.println("============================");
System.out.println("submitting scene search request for " + datasetName);
// test search request
String[] entityids = null;
if (apiKey != null) {
System.out.println("============================");
System.out.println("submitting search request");
response = sendRequest(new SceneSearchRequest(apiKey, "LANDSAT_8")); // , "2016-01-01T00:00:00Z", "2016-01-01T02:00:00Z"));
if (response instanceof SceneSearchResponse) {
SceneSearchResponse searchResp = (SceneSearchResponse) response;
System.out.println("search.totalhits: " + searchResp.getTotalHits());
System.out.println("search.firstrecord: " + searchResp.getFirstRecord());
List<InventoryScene> scenes = searchResp.getScenes();
// generate entityid array
entityids = new String[scenes.size()];
for (int i = 0; i < entityids.length; ++i) {
entityids[i] = scenes.get(i).entityId;
}
JSONResponse response = sendRequest(new SceneSearchRequest(apiKey, "LANDSAT_8_C1", temporalFilter, null, null, null, null, true));
if (response instanceof SceneSearchResponse) {
SceneSearchResponse searchResp = (SceneSearchResponse) response;
System.out.println("search.totalhits: " + searchResp.getTotalHits());
System.out.println("search.firstrecord: " + searchResp.getFirstRecord());
while (searchResp.hasNextPage()) {
searchResp = sendRequest(searchResp.getNextPageRequest());
if (searchResp != null && !searchResp.hasError()) {
System.out.println("search.totalhits: " + searchResp.getTotalHits());
System.out.println("search.firstrecord: " + searchResp.getFirstRecord());
scenes = searchResp.getScenes();
// generate entityid array
entityids = new String[scenes.size()];
for (int i = 0; i < entityids.length; ++i) {
entityids[i] = scenes.get(i).entityId;
}
} else {
System.err.println("Error sending next page request.");
}
System.out.println(Arrays.toString(searchResp.getScenes().toArray()));
while (searchResp.hasNextPage()) {
searchResp = sendRequest(searchResp.getNextPageRequest());
if (searchResp != null && !searchResp.hasError()) {
System.out.println("search.totalhits: " + searchResp.getTotalHits());
System.out.println("search.firstrecord: " + searchResp.getFirstRecord());
System.out.println(Arrays.toString(searchResp.getScenes().toArray()));
}
}
}
// TODO: include downloadoptions requests
// if (apiKey != null && entityids != null) {
// System.out.println("============================");
// System.out.println("submitting download request for " + Arrays.toString(entityids));
//
// response = sendRequest(new DownloadRequest(apiKey, datasetName, entityids));
// if (response.hasError()) {
// System.out.println("Error! Code: " + response.getErrorcode() + "\tMessage: " + response.getErrormsg());
// } else if (response instanceof DownloadResponse) {
// DownloadResponse downloadResp = (DownloadResponse) response;
// for (String url : downloadResp.getDownloadUrls()) {
// System.out.println(url);
// }
//
// }
// }
if (apiKey != null) {
System.out.println("============================");
System.out.println("submitting logout request");
Thread.sleep(100l);
response = sendRequest(new LogoutRequest(apiKey));
if (response.hasError()) {
System.out.println("error code: " + response.getErrorcode() + " (" + ErrorCode.getErrorDescription(response.getErrorcode()) + ")");
System.out.println("error msg: " + response.getErrormsg());
} else {
if (response instanceof LogoutResponse) {
Boolean loggedout = ((LogoutResponse) response).isLoggedout();
System.out.println("successful: " + loggedout);
}
}
/**
* @param apiKey
* @param entityids
* @param datasetName
* @throws IOException
* @throws ParseException
*/
@SuppressWarnings("unused")
private static void downloadExample(String apiKey, String[] entityids, String datasetName) throws IOException, ParseException {
if (apiKey == null) {
return;
}
System.out.println("============================");
System.out.println("submitting download request for " + Arrays.toString(entityids));
JSONResponse response = sendRequest(new DownloadRequest(apiKey, datasetName, entityids));
if (response.hasError()) {
System.out.println("Error! Code: " + response.getErrorcode() + "\tMessage: " + response.getErrormsg());
} else if (response instanceof DownloadResponse) {
DownloadResponse downloadResp = (DownloadResponse) response;
System.out.println(Arrays.toString(downloadResp.getDownloads().toArray()));
}
}
private static void hitsExample(String apiKey, String datasetName, TemporalFilter temporalFilter) throws IOException, ParseException {
if (apiKey == null) {
return;
}
System.out.println("============================");
System.out.println("submitting hits request for " + datasetName);
JSONResponse response = sendRequest(new HitsRequest(apiKey, datasetName, temporalFilter, null, null, null, null, true));
if (response instanceof HitsResponse) {
System.out.println("hits: " + ((HitsResponse) response).getHits());
}
}
/**
* @param apiKey
* @param entityids
* @param datasetName
* @throws IOException
* @throws ParseException
*/
@SuppressWarnings("unused")
private static void downloadOptionsExample(String apiKey, String[] entityids, String datasetName) throws IOException, ParseException {
if (apiKey == null) {
return;
}
System.out.println("============================");
System.out.println("submitting download options request for " + Arrays.toString(entityids));
JSONResponse response = sendRequest(new DownloadOptionsRequest(apiKey, datasetName, entityids));
if (response instanceof DownloadOptionsResponse) {
DownloadOptionsResponse dor = (DownloadOptionsResponse) response;
for (SceneDownloadOptions sdo : dor.getSceneDownloadOptions()) {
System.out.println(sdo);
}
}
}
......
/**
*
*/
package de.potsdam.gfz.usgsapi.json.datamodels;
import net.minidev.json.JSONObject;
/**
* @author Daniel Eggert (daniel.eggert@gfz-potsdam.de)
*
*/
public class Coordinate {
private static final String LAT_KEY = "latitude";
private static final String LON_KEY = "longitude";
public double lat = 0;
public double lon = 0;
/**
*
*/
public Coordinate(double lat, double lon) {
this.lat = lat;
this.lon = lon;
}
public JSONObject toJSON() {
JSONObject obj = new JSONObject();
obj.put(LAT_KEY, lat);
obj.put(LON_KEY, lon);
return obj;
}
}
/**
*
*/
package de.potsdam.gfz.usgsapi.json.datamodels;
/**
* @author Daniel Eggert (daniel.eggert@gfz-potsdam.de)
*
*/
public class Download {
public String entityId;
public String product;
public String url;
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "(entityid:" + entityId + ", product:" + product + ", url:" + url + ")";
}
}
/**
*
*/
package de.potsdam.gfz.usgsapi.json.datamodels;
/**
* @author Daniel Eggert (daniel.eggert@gfz-potsdam.de)
*
*/
public class DownloadOption {
/**
* Denotes if the download option is available
*/
public boolean available;
/**
* Internal code to represent the download option
*/
public String downloadCode;
/**
* The size of the download in bytes
*/
public long filesize;
/**
* The user friendly name for this download option
*/
public String productName;
/**
* The URL to download this file via the web application (may require login)
*/
public String url;
/**
* Determines the storage location of the data - this is useful when storage locations require separate authentication or processes
*/
public String storageLocation;
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "(available:" + available + ", downloadCode:" + downloadCode + ", filesize:" + filesize + ", productName:" + productName + ", url:" + url + ", storageLocation:"
+ storageLocation + ")";
}
}
......@@ -12,70 +12,63 @@ public class InventoryScene {
/**
* The date the scene was acquired, ISO 8601 Formatted Date
*/
public String acquisitionDate;
public String acquisitionDate;
/**
* The date the scene began acquisition, ISO 8601 Formatted Date
*/
public String startTime;
public String startTime;
/**
* The date the scene ended acquisition, ISO 8601 Formatted Date
*/
public String endTime;
public String endTime;
/**
* scene footprint coordinates<br>
* [0] low left lat<br>
* [1] low left lon<br>
* [2] low right lat<br>
* [3] low right lon<br>
* [4] up right lat<br>
* [5] up right lon<br>
* [6] up left lat<br>
* [7] up left lon
* The scenes spatial footprint
*/
public double[] bounds = new double[8];
// sceneBounds string MBR in EPSG:4326 projection
public Coordinate[] spatialFootprint;
// sceneBounds string MBR in EPSG:4326 projection
/**
* URL to browse image
*/
public String browseUrl;
public String browseUrl;
/**
* URL to data access
*/
public String dataAccessUrl;
public String dataAccessUrl;
/**
* URL to web download
*/
public String downloadUrl;
public String downloadUrl;
/**
* Scene Identifier
*/
public String entityId;
// displayId string Scene Identifier used for display
public String entityId;
// displayId string Scene Identifier used for display
/**
* URL to XML formatted scene metadata
*/
public String metadataUrl;
// fgdcMetadataUrl string URL to FGDC formatted scene metadata
public String metadataUrl;
// fgdcMetadataUrl string URL to FGDC formatted scene metadata
/**
* The date the metadata was last modified, ISO 8601 Formatted Date
*/
public String modifiedDate;
public String modifiedDate;
/**
* URL to order products
*/
public String orderUrl;
public String orderUrl;
/**
* Additional summarized metadata
*/
public String summary;
public String summary;
}
/**
*
*/
package de.potsdam.gfz.usgsapi.json.datamodels;
import java.util.Arrays;
/**
* @author Daniel Eggert (daniel.eggert@gfz-potsdam.de)
*
*/
public class SceneDownloadOptions {
/**
* Scene Identifier
*/
public String entityId;
/**
* Array of download options
*/
public DownloadOption[] downloadOptions;
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return entityId + Arrays.toString(downloadOptions);
}
}
/**
*
*/
package de.potsdam.gfz.usgsapi.json.datamodels;
import net.minidev.json.JSONObject;
/**
* @author Daniel Eggert (daniel.eggert@gfz-potsdam.de)
*
*/
public abstract class SpatialFilter {
private static final String FILTER_TYPE_KEY = "filterType";
protected final String filterType;
public SpatialFilter(String filterType) {
this.filterType = filterType;
}
public JSONObject toJSON() {
JSONObject obj = new JSONObject();
obj.put(FILTER_TYPE_KEY, filterType);
return obj;
}
}
/**
*
*/
package de.potsdam.gfz.usgsapi.json.datamodels;
import net.minidev.json.JSONObject;
/**
* @author Daniel Eggert (daniel.eggert@gfz-potsdam.de)
*
*/
public class SpatialFilterMbr extends SpatialFilter {
private static final String LOWLEFT_KEY = "lowerLeft";
private static final String UPRIGHT_KEY = "upperRight";
private static final String MBR_FILTER_TYPE = "mbr";
public Coordinate lowLeft = null;
public Coordinate upRight = null;
/**
* @param filterType