V1_0.java [src/java/m/svap/svap04_svapstreammiles] 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.svap04_svapstreammiles;

import csip.ModelDataService;
import csip.ServiceException;
import csip.annotations.Description;
import csip.annotations.Name;
import csip.annotations.Resource;
import gisobjects.GISObject;
import gisobjects.GISObjectException;
import gisobjects.GISObjectFactory;
import gisobjects.db.GISEngine;
import static gisobjects.db.GISEngineFactory.createGISEngine;
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 org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import svap.utils.DBResources;
import static svap.utils.DBResources.CRDB;
import svap.utils.SVAPUtils;

/**
 * SVAP-04:Calculate Stream Miles on Property
 *
 * This service computes the miles of perennial, intermittent, and ephemeral
 * streams on the farm (or property) of the SVAP assessment.
 *
 * @author Robert Streetman
 * @author Rumpal Sidhu
 * @version 1.0
 */
@Name("SVAP-04: Calculate Stream Miles on Property")
@Description("This service computes the miles of perennial, intermittent, and "
        + "ephemeral streams on the farm (or property) of the SVAP assessment.")
@Path("m/svap/svapstreammiles/1.0")
@Resource(from = DBResources.class)

public class V1_0 extends ModelDataService {

    private JSONObject assessmentLocation, propertyBoundary;
    private int assessmentId;

    private double perennialStreamMiles = 0.0;
    private double intermittentStreamMiles = 0.0;
    private double ephemeralStreamMiles = 0.0;

    @Override
    protected void preProcess() throws ServiceException {
        assessmentId = parameter().getInt("assessment_id");
        assessmentLocation = parameter().getJSON("assessment_location");
        propertyBoundary = parameter().getJSON("property_boundary");
    }

    @Override
    protected void doProcess() throws JSONException, URISyntaxException, IOException, ServiceException, SQLException, GISObjectException {
        try (Connection connection = resources().getJDBC(CRDB);
                GISEngine gisEngine = createGISEngine(connection)) {
            GISObject property = GISObjectFactory.createGISObject(propertyBoundary, gisEngine);

            //JSONArray propertyBoundaryCoordinates = propertyBoundary.getJSONArray("coordinates");
            JSONArray propertyBoundaryCoordinates = property.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");
            
            //  Were there any results returned?
            if (nhdFlowlineResponse.toString().contains("geometry")) {
                GISObject streams = GISObjectFactory.createGISObject(nhdFlowlineResponse, gisEngine);

                HashMap<String, Double> streamMiles = SVAPUtils.computePropertyStreamMiles(streams, property);

                perennialStreamMiles = streamMiles.get("perennialStreamMiles");
                intermittentStreamMiles = streamMiles.get("intermittentStreamMiles");
                ephemeralStreamMiles = streamMiles.get("ephemeralStreamMiles");
            }
        }
    }

    @Override
    protected void postProcess() {
        results().put("assessment_id", assessmentId, "Assessment Identifier");
        results().put("assessment_location", assessmentLocation);
        results().put("property_boundary", propertyBoundary);
        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");
    }
}