HUC12.java [src/java/svap/utils] 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 svap.utils;

import com.vividsolutions.jts.geom.Envelope;
import csip.Config;
import csip.ServiceException;
import gisobjects.GISObject;
import gisobjects.GISObjectException;
import gisobjects.GISObjectFactory;
import gisobjects.vector.GIS_FeatureCollection;
import java.io.IOException;
import java.net.URISyntaxException;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

/**
 *
 * @author Rumpal Sidhu
 *
 * When USGS changes endpoints look for: "12-digit HU (Subwatershed)" in the new
 * services list on their website.  No need to really change the default here in the
 * code...just change the config file for the SVAP services
 *
 */
public class HUC12 extends USGS {

    GIS_FeatureCollection huc12List = null;
    Envelope envelope;
    private static final String USGS_HUC12_URL = "https://hydro.nationalmap.gov/arcgis/rest/services/wbd/MapServer/6/query";

    public HUC12() {
        outFields = "HUC12, NAME";
        format = "geojson";
        spatialRelationShip = "esriSpatialRelWithin";
    }

    public JSONObject getHUC12Data(String geometry) throws JSONException, URISyntaxException, IOException, ServiceException {
        this.geometry = geometry;
        JSONObject hucResponse = callService(Config.getString("service.usgs.wbd.url", USGS_HUC12_URL));
        if (null == hucResponse) {
            throw new ServiceException("No HUC-12 data was found for that specified location point.");
        }
        return hucResponse;
    }

    public void fillHUC12Data(String geometry) throws JSONException, URISyntaxException, IOException, ServiceException, GISObjectException {
        GISObject tGeometry = GISObjectFactory.createGISObject(getHUC12Data(geometry), null);
        if (tGeometry.getType() != GISObject.GISObjectType.featurecollection) {
            throw new ServiceException("Invalid data type returned by request for data to HUC12 service.  Expected a FeatureCollection.");
        } else {
            huc12List = (GIS_FeatureCollection) tGeometry;
            envelope = tGeometry.getEnvelope();
        }
    }

    public String getEnvelopeWKT() {
        return envelope.toString();
    }

    /**
     *
     * This function only works to format the data for the bbox input for ACIS,
     * IFF the bounding box points are contained entirely within in the same
     * hemisphere.
     *
     * @return
     */
    public String getEnvelopeList() {
        return envelope.getMinX() + ", " + envelope.getMinY() + ", " + envelope.getMaxX() + ", " + envelope.getMaxY();
    }

    public String getPropertyValue(int index, String name) throws ServiceException, GISObjectException {
        String tValue = "";
        if ((null != huc12List) && (index >= 0) && (index < huc12List.getFeatureCount())) {
            tValue = huc12List.getFeatureAttribute(index, name);
        } else {
            throw new ServiceException("Invalid property index passed to getPropertyValue of HUC12 data");
        }
        return tValue;
    }

    public int getDataLength() {
        if (null != huc12List) {
            return huc12List.getFeatureCount();
        } else {
            return 0;
        }
    }

    public JSONObject toJSON(int index) throws IOException, JSONException, ServiceException, GISObjectException {
        if (null != huc12List) {
            return huc12List.getGeometry(index).toJSON();
        } else {
            throw new ServiceException("No huc-12 data is present.  Cannot convert to JSON.");
        }
    }

    public JSONObject toJSON() throws IOException, JSONException, ServiceException {
        if (null != huc12List) {
            return huc12List.toJSON();
        } else {
            throw new ServiceException("No huc-12 data is present.  Cannot convert to JSON.");
        }
    }
}