V1_0.java [src/java/m/svap/svap02_svapnhddata] Revision: default Date:
/*
* $Id$
*
* This file is part of the Cloud Services Integration Platform (CSIP),
* a Model-as-a-Service framework, API, and application suite.
*
* 2012-2017, OMSLab, Colorado State University.
*
* OMSLab licenses this file to you under the MIT license.
* See the LICENSE file in the project root for more information.
*/
package m.svap.svap02_svapnhddata;
import csip.ModelDataService;
import csip.ServiceException;
import csip.annotations.Resource;
import gisobjects.GISObject;
import gisobjects.GISObjectException;
import gisobjects.GISObjectFactory;
import gisobjects.db.GISEngine;
import static gisobjects.db.GISEngineFactory.createGISEngine;
import gisobjects.vector.GIS_FeatureCollection;
import java.io.IOException;
import java.net.URISyntaxException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import javax.ws.rs.Path;
import csip.annotations.Description;
import csip.annotations.Name;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;
import org.codehaus.jettison.json.JSONException;
import svap.utils.DBResources;
import static svap.utils.DBResources.CRDB;
import svap.utils.SVAPUtils;
/**
* SVAP-02: Get Drainage Area and Stream Miles for Watershed Description
*
* This service retrieves the HUC 12 watershed number, name, and area for a
* stream reach location, and computes perennial, intermittent, and ephemeral
* stream miles within the watershed.
*
* @author Rumpal Sidhu
* @version 1.0
*/
@Name("SVAP-02: Get Drainage Area and Stream Miles for Watershed Description")
@Description("This service retrieves the HUC 12 watershed number, name, and "
+ "area for a stream reach location, and computes perennial, "
+ "intermittent, and ephemeral stream miles within the watershed.")
@Path("m/svap/svapnhddata/1.0")
@Resource(from = DBResources.class)
public class V1_0 extends ModelDataService {
private JSONObject assessmentLocation;
private int assessmentId;
private String huc12Number, huc12Name;
private double huc12Area, perennialStreamMiles = 0,
intermittentStreamMiles = 0, ephemeralStreamMiles = 0;
@Override
protected void preProcess() throws ServiceException {
assessmentLocation = parameter().getJSON("assessment_location");
assessmentId = parameter().getInt("assessment_id");
}
@Override
protected void doProcess() throws IOException, JSONException, URISyntaxException, ServiceException, SQLException, GISObjectException {
try (Connection connection = resources().getJDBC(CRDB); GISEngine gisEngine = createGISEngine(connection)) {
JSONObject huc12Response = SVAPUtils.getHUC12Data(assessmentLocation, "geojson");
GISObject huc12 = GISObjectFactory.createGISObject(huc12Response, gisEngine);
if (huc12.getType() == GISObject.GISObjectType.featurecollection) {
GIS_FeatureCollection huc12Boundary = (GIS_FeatureCollection) huc12;
if (huc12Boundary.getFeatureCount() > 0) {
huc12Number = huc12Boundary.getFeatureAttribute(0, "HUC12");
huc12Name = huc12Boundary.getFeatureAttribute(0, "NAME");
huc12Area = Double.parseDouble(huc12Boundary.getFeatureAttribute(0, "AREAACRES"));
GISObject hucGeom = huc12Boundary.getGeometry(0);
JSONArray propertyBoundaryCoordinates = hucGeom.toJSON().getJSONArray("coordinates");
JSONObject propertyBoundaryGeometry = new JSONObject();
propertyBoundaryGeometry.put("rings", propertyBoundaryCoordinates);
JSONObject nhdFlowlineResponse = SVAPUtils.getNHDFlowlineData(propertyBoundaryGeometry.toString(), "geojson", "FCODE = 46000 OR FCODE = 46003 OR FCODE = 46006 OR FCODE = 46007");
if (nhdFlowlineResponse.toString().contains("geometry")) {
GISObject streams = GISObjectFactory.createGISObject(nhdFlowlineResponse, gisEngine);
HashMap<String, Double> streamMiles = SVAPUtils.computePropertyStreamMiles(streams, hucGeom);
perennialStreamMiles = streamMiles.get("perennialStreamMiles");
intermittentStreamMiles = streamMiles.get("intermittentStreamMiles");
ephemeralStreamMiles = streamMiles.get("ephemeralStreamMiles");
} else {
throw new ServiceException("USGS NHD Flowline service found no information for this HUC12 region: " + huc12Name + ", " + huc12Number + ", " + huc12Name);
}
} else {
throw new ServiceException("USGS HUC12 Service returned invalid results. No features in the featurecollection found.");
}
} else {
throw new ServiceException("USGS HUC12 Service returned invalid results. No featurecollection found.");
}
}
}
@Override
protected void postProcess() throws JSONException {
results().put("assessment_id", assessmentId, "Assessment Identifier");
results().put("assessment_location", assessmentLocation);
results().put("huc12_number", huc12Number, "HUC12 Identifier");
results().put("huc12_name", huc12Name, "HUC12 Name");
results().put("huc12_area", huc12Area, "HUC12 Area", "acres");
results().put("perennial_stream_miles", perennialStreamMiles, "HUC12 Perennial Stream in Miles", "miles");
results().put("intermittent_stream_miles", intermittentStreamMiles, "HUC12 Intermittent Stream in Miles", "miles");
results().put("ephemeral_stream_miles", ephemeralStreamMiles, "HUC12 Ephemeral Stream in Miles", "miles");
}
}