DEMSteepness.java [src/java/crp/utils] Revision: 100716214301bbbe3b1e6bb04e186b11bfa88859  Date: Fri Dec 06 15:15:52 MST 2019
/*
 * $Id$
 *
 * This file is part of the Cloud Services Integration Platform (CSIP),
 * a Model-as-a-Service framework, API, and application suite.
 *
 * 2012-2019, 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 crp.utils;

import csip.Client;
import static csip.ModelDataService.KEY_METAINFO;
import static csip.ModelDataService.KEY_PARAMETER;
import csip.ServiceException;
import csip.utils.JSONUtils;
import data.interpretors.SlopeSteepness;
import java.net.URI;
import java.util.Map;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

/**
 *
 * @author <a href="mailto:shaun.case@colostate.edu">Shaun Case</a>
 */
public class DEMSteepness extends ServiceCall {

    private JSONObject metaInfo;
    private JSONObject[] aoa_geometry;
    private String resultFile;
    private SlopeSteepness slopes;

    public DEMSteepness(JSONObject metaInfo, JSONObject _aoa_geometry, String URI) {
        super(URI);
        this.metaInfo = metaInfo;
        aoa_geometry = new JSONObject[1];
        aoa_geometry[0] = _aoa_geometry;

        errorPrefix = "DEMSteepnessServiceCall";
    }

    public DEMSteepness(JSONObject metaInfo, JSONObject[] _aoa_geometries, String URI) {
        super(URI);
        this.metaInfo = metaInfo;
        aoa_geometry = _aoa_geometries;

        errorPrefix = "DEMSteepnessServiceCall";
    }

    public String data(){
        return resultFile;
    }
    
    public SlopeSteepness slopes(){
        return slopes;
    }
    
    @Override
    protected void parseResults() throws ServiceException {
        //   Error checking has already been done on these results.
        // If we get to this function, no further error
        // checking is necessary, except to look at the "results" array in the output, if desired.
        if (null == callResultSection) {
            throwServiceCallException("Cannot find results in the call to " + this.URI + " .");
        }

        try {
            //  Get the climate file and store it.
            Map<String, JSONObject> resultMap = JSONUtils.getResults(results);
            String fileLink = JSONUtils.getStringParam(resultMap, "avg_slope_results.csv", "");
            if (!fileLink.isEmpty()) {
                URI cliURI = new URI(fileLink);

                Client fileGet = new Client();

                resultFile = fileGet.doGET(cliURI.toString());
                if ( null != resultFile ){
                    slopes = new SlopeSteepness(resultFile);                    
                }

            } else {
                throwServiceCallException("No  avg_slope_results.csv file link was found in the results from the average slope steepness service.");
            }

        } catch (Exception ex) {
            throwServiceCallException("Could not retrieve the slope_steepness file: ", ex);
        }
    }

    @Override
    protected void createRequest() throws ServiceException {
        JSONArray dataArray;

        requestMetainfoObject = new JSONObject();
        request = new JSONObject();

        dataArray = new JSONArray();
        try {
            requestMetainfoObject.put("MultipartRequest", "Bundled Service Request From csip-crp MetaModeling CliWindSoil Service");
            requestMetainfoObject.put("CRPAssessment_Metainfo", metaInfo);
            requestMetainfoObject.put("mode", "sync");
            request.put(KEY_METAINFO, requestMetainfoObject);

            dataArray.put(createFeatureCollection("DEMSteepnessServiceCall", aoa_geometry));

            request.put(KEY_PARAMETER, dataArray);

        } catch (JSONException ex) {
            throwServiceCallException("Cannot create the JSON request.", ex);
        }
    }

    private JSONObject createFeature(int id, JSONObject geometry) throws JSONException {
        JSONObject feature = new JSONObject();
        JSONObject properties = new JSONObject();

        properties.put("ID", id);

        feature.put("type", "Feature");
        feature.put("properties", properties);
        feature.put("geometry", geometry);

        return feature;
    }

    private JSONArray createFeatureArray(JSONObject[] geometries) throws JSONException {
        JSONArray features = new JSONArray();

        for (int i = 0; i < geometries.length; i++) {
            features.put(createFeature(i + 1, geometries[i]));
        }

        return features;
    }

    private JSONObject createFeatureCollection(String name, JSONObject[] geometries) throws JSONException {
        JSONObject featureCollection = new JSONObject();
        JSONObject crs = new JSONObject();
        JSONObject crsProperties = new JSONObject();

        crsProperties.put("name", "urn:ogc:def:crs:OGC:1.3:CRS84");
        crs.put("type", "name");
        crs.put("properties", crsProperties);

        featureCollection.put("type", "FeatureCollection");
        featureCollection.put("name", name);
        featureCollection.put("crs", crs);
        featureCollection.put("features", createFeatureArray(geometries));

        return featureCollection;
    }
}