DEMSteepness.java [src/java/crp/utils] Revision: fad70b35517781f034beb514ca90f123dc4edaf6  Date: Fri Dec 06 14:19:16 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 static csip.ModelDataService.KEY_METAINFO;
import static csip.ModelDataService.KEY_PARAMETER;
import csip.ServiceException;
import csip.utils.JSONUtils;
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 double slopeSteepness = Double.NaN;

    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";
    }    

    @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);

            if (resultMap.containsKey("slope_steepness")) {
                slopeSteepness = JSONUtils.getDoubleParam(resultMap, "slope_steepness", Double.NaN);

                if (Double.isNaN(slopeSteepness)) {
                    throwServiceCallException("No appropriate value found in the slope_steepness result object.");
                }

            } else {
                throwServiceCallException("No slope_steepness found in the results");
            }

        } catch (JSONException ex) {
            throwServiceCallException("Error trying to find results section", 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);
        }
    }

    public double slopeSteepness() {
        return slopeSteepness;
    }

    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;        
    }
}