PostRequest.java [src/java/svap/utils] Revision: default Date:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package svap.utils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
/**
*
* @author robert
*/
public class PostRequest {
public static final String USGS_NHD_URL = "https://services.nationalmap.gov/arcgis/rest/services/nhd/MapServer/9/query";
public static final String USGS_HUC_URL = "https://services.nationalmap.gov/arcgis/rest/services/wbd/MapServer/7/query";
public static final String USGS_ELEV = "https://nationalmap.gov/epqs/pqs.php?x=";
public static JSONArray FlowlineFrompolyPost(String coord) {
JSONArray wbdResults = null;
coord = coord.substring(1, coord.length() - 1);
StringBuilder reqBuilder = new StringBuilder();
reqBuilder.append("{\"rings\": [");
reqBuilder.append(coord);
reqBuilder.append("],\"spatialReference\":{\"wkid\":4326 }}");
System.out.format("Request Coordinates: %s\n", reqBuilder.toString());
try {
//Get NHD flowline response data from USGS endpoint
HttpUriRequest form = RequestBuilder.post().setUri(new URI(USGS_NHD_URL))
.addParameter("geometry", reqBuilder.toString())
.addParameter("geometryType", "esriGeometryPolygon")
.addParameter("inSR", "4326")
.addParameter("spatialRel", "esriSpatialRelIntersects")
.addParameter("outFields", "FTYPE,FCODE,LENGTHKM,RESOLUTION,SHAPE_Length,MAINPATH")
.addParameter("outSR", "4326")
.addParameter("f", "geojson").build();
BasicCookieStore cookieStore = new BasicCookieStore();
CloseableHttpClient client = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
//Parse NHD response
try (CloseableHttpResponse responseNlcd = client.execute(form)) {
HttpEntity nhdEntity = responseNlcd.getEntity();
BufferedReader br = new BufferedReader(new InputStreamReader(nhdEntity.getContent()));
StringBuilder build = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
build.append(line);
build.append("\n");
}
JSONObject nhdResults = new JSONObject(build.toString());
wbdResults = nhdResults.getJSONArray("features");
EntityUtils.consume(nhdEntity);
}
} catch (IOException | JSONException | URISyntaxException ex) {
}
return wbdResults;
}
/**
* Intersects a point with USGS WBD endpoint, returns the HUC-12 boundary
* @param coord
* @return
*/
public static JSONObject WBDFromPointPost(String[] coord) {
JSONObject hucPoly = null;
//Build NHD HUC-12 request using user coordinates
StringBuilder reqBuilder = new StringBuilder();
reqBuilder.append("{\"x\":");
reqBuilder.append(coord[0]);
reqBuilder.append(", \"y\":");
reqBuilder.append(coord[1]);
reqBuilder.append(",\"spatialReference\":{\"wkid\":4326 }}");
try {
//Get NHD flowline data from USGS endpoint
HttpUriRequest form = RequestBuilder.post().setUri(new URI(USGS_HUC_URL))
.addParameter("geometry", reqBuilder.toString())
.addParameter("geometryType", "esriGeometryPoint")
.addParameter("inSR", "4326")
.addParameter("spatialRel", "esriSpatialRelIntersects")
.addParameter("outFields", "HUC12,NAME")
.addParameter("outSR", "4326")
.addParameter("f", "geojson").build();
BasicCookieStore cookieStore = new BasicCookieStore();
CloseableHttpClient client = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
System.out.println(form.toString());
//Read NHD response
try (CloseableHttpResponse responseNlcd = client.execute(form)) {
HttpEntity nhdEntity = responseNlcd.getEntity();
BufferedReader br = new BufferedReader(new InputStreamReader(nhdEntity.getContent()));
StringBuilder build = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
build.append(line);
build.append("\n");
}
hucPoly = new JSONObject(build.toString());
System.out.println(build.toString());
}
} catch (IOException | JSONException | URISyntaxException ex) {
}
return hucPoly;
}
public static double ElevationFromXYPost(double lat, double lon) {
double elev = -999;
try {
//Make HTTP GET request for elevation for this x,y point
StringBuilder strBuilder = new StringBuilder(USGS_ELEV);
strBuilder.append(lon);
strBuilder.append("&y=");
strBuilder.append(lat);
strBuilder.append("&units=Meters&output=json");
URL url = new URL(strBuilder.toString());
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
//Read response
BufferedReader respReader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String respLine;
strBuilder = new StringBuilder();
while ((respLine = respReader.readLine()) != null) {
strBuilder.append(respLine);
}
JSONObject responseObj = new JSONObject(strBuilder.toString());
elev = responseObj.getJSONObject("USGS_Elevation_Point_Query_Service").getJSONObject("Elevation_Query").getDouble("Elevation");
} catch (IOException | JSONException ex) {
}
return elev;
}
}