Displaying differences for changeset
 
display as  

src/java/crp/utils/DEMSteepness.java

@@ -11,10 +11,13 @@
  */
 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;
@@ -28,8 +31,8 @@
 
     private JSONObject metaInfo;
     private JSONObject[] aoa_geometry;
-
-    private double slopeSteepness = Double.NaN;
+    private String resultFile;
+    private SlopeSteepness slopes;
 
     public DEMSteepness(JSONObject metaInfo, JSONObject _aoa_geometry, String URI) {
         super(URI);
@@ -39,15 +42,23 @@
 
         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.
@@ -60,38 +71,41 @@
         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);
 
-            if (resultMap.containsKey("slope_steepness")) {
-                slopeSteepness = JSONUtils.getDoubleParam(resultMap, "slope_steepness", Double.NaN);
+                Client fileGet = new Client();
 
-                if (Double.isNaN(slopeSteepness)) {
-                    throwServiceCallException("No appropriate value found in the slope_steepness result object.");
+                resultFile = fileGet.doGET(cliURI.toString());
+                if ( null != resultFile ){
+                    slopes = new SlopeSteepness(resultFile);                    
                 }
 
             } else {
-                throwServiceCallException("No slope_steepness found in the results");
+                throwServiceCallException("No  avg_slope_results.csv file link was found in the results from the average slope steepness service.");
             }
 
-        } catch (JSONException ex) {
-            throwServiceCallException("Error trying to find results section", ex);
+        } 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 {            
+        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));
+            dataArray.put(createFeatureCollection("DEMSteepnessServiceCall", aoa_geometry));
 
             request.put(KEY_PARAMETER, dataArray);
 
@@ -100,10 +114,6 @@
         }
     }
 
-    public double slopeSteepness() {
-        return slopeSteepness;
-    }
-
     private JSONObject createFeature(int id, JSONObject geometry) throws JSONException {
         JSONObject feature = new JSONObject();
         JSONObject properties = new JSONObject();
@@ -121,27 +131,26 @@
         JSONArray features = new JSONArray();
 
         for (int i = 0; i < geometries.length; i++) {
-            features.put(createFeature(i+1, geometries[i]));
+            features.put(createFeature(i + 1, geometries[i]));
         }
 
         return features;
     }
-    
-    private JSONObject createFeatureCollection(String name, JSONObject[] geometries) throws JSONException{
+
+    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;        
+
+        return featureCollection;
     }
 }

src/java/m/crp/assessmenttool/V1_0.java

@@ -23,6 +23,7 @@
 import csip.utils.JSONUtils;
 import crp.utils.DBResources;
 import static crp.utils.DBResources.LOCAL_SQLSERVER;
+import crp.utils.DEMSteepness;
 import gisobjects.GISObject;
 import gisobjects.GISObjectFactory;
 import gisobjects.db.GISEngine;
@@ -40,6 +41,8 @@
 import crp.utils.WEPSFallowRotation;
 import crp.utils.WWESoilParams;
 import crp.utils.WWESoilParams.SoilResult;
+import data.interpretors.SlopeSteepness;
+import data.interpretors.SlopeSteepness.SlopeData;
 
 /**
  *
@@ -65,7 +68,7 @@
     protected String soilsURI = "http://csip.engr.colostate.edu:8083/csip-soils/d/wwesoilparams/2.0";
     protected String weppURI = "http://csip.engr.colostate.edu:8083/csip-wepp/m/wepp/3.1";
     protected String wepsURI = "http://csip.engr.colostate.edu:8083/csip-weps/m/weps/5.2";
-    protected String demSteepnessURI = "http://csip.engr.colostate.edu:8087/csip-watershed/m/average_slope/2.0";
+    protected String demSteepnessURI = "NONE"; //"http://csip.engr.colostate.edu:8087/csip-watershed/m/average_slope/2.0";
 
     protected String outputFile;
     protected boolean streamOutputFile = false;
@@ -76,7 +79,7 @@
     protected JSONObject rotation;
     protected double waterErosionRate = 0.0;
     protected double windErosionRate = 0.0;
-    protected double steepness;
+    protected SlopeSteepness slopes;
 
     @Override
     protected void preProcess() throws Exception {
@@ -102,10 +105,9 @@
             try (Connection connection = resources().getJDBC(LOCAL_SQLSERVER);) {
                 gEngine = GISEngineFactory.createGISEngine(connection);
                 aoa_geometry = GISObjectFactory.createGISObject(scenario_geometry, gEngine);
-                
-                if ( aoa_geometry.getType() != GISObject.GISObjectType.polygon 
-                        && aoa_geometry.getType() == GISObject.GISObjectType.multipolygon
-                        ){
+
+                if (aoa_geometry.getType() != GISObject.GISObjectType.polygon
+                        && aoa_geometry.getType() == GISObject.GISObjectType.multipolygon) {
                     throw new ServiceException("The input geometry must be a Polygon or a Multipolygon.");
                 }
 
@@ -123,7 +125,17 @@
     @Override
     protected void doProcess() throws Exception {
         // Get Slope Steepness from DEM Service for this AoA.
-        steepness = getDEMSteepness();
+        slopes = getDEMSteepness();
+        double tSlope = Double.NaN;
+        if (null != slopes) {
+            if (!slopes.badSlopeData() && slopes.slopeDataMessages().isEmpty()) {
+                SlopeData slope = slopes.slope("1");
+
+                if (null != slope) {
+                    tSlope = slope.mean();
+                }
+            }
+        }
 
         //  Get region data
         Region regionCall = new Region(getMetainfo(), scenario_geometry, regionURI);
@@ -135,16 +147,18 @@
         WWESoilParams soilsCall = new WWESoilParams(getMetainfo(), scenario_geometry, soilsURI);
         soilsCall.call();
         topThreeComponents = soilsCall.getTopThree();
-                
+
         //TODO:  Currently rotations are the default fallow operation...final version of this service requires user to provide rotation.
         double totalArea = 0.0;
         for (SoilResult soilResult : topThreeComponents) {
 
-            WEPP weppCall = new WEPP(getMetainfo(), latitude, longitude, soilResult.cokey, soilResult.length, ((Double.isNaN(steepness)) ? soilResult.slope_r : steepness), getRotation((int) soilResult.length), weppURI);
+            WEPP weppCall = new WEPP(getMetainfo(), latitude, longitude, soilResult.cokey,
+                    soilResult.length, ((Double.isNaN(tSlope)) ? soilResult.slope_r : tSlope), getRotation((int) soilResult.length), weppURI);
             weppCall.call();
             soilResult.waterErosion = weppCall.waterErosion();
 
-            WEPS wepsCall = new WEPS(getMetainfo(), latitude, longitude, regionLength, regionWidth, regionOrientation, soilResult.cokey, getRotation(0), wepsURI);
+            WEPS wepsCall = new WEPS(getMetainfo(), latitude, longitude, regionLength,
+                    regionWidth, regionOrientation, soilResult.cokey, getRotation(0), wepsURI);
             wepsCall.call();
             soilResult.windErosion = wepsCall.windErosion();
 
@@ -183,14 +197,16 @@
         return new WEPSFallowRotation(length).rotation();
     }
 
-    //TODO finish when DEMSteepness service has been written
-    protected double getDEMSteepness() {
-        double ret_val = Double.NaN;
+    protected SlopeSteepness getDEMSteepness() throws ServiceException {
 
         if (!demSteepnessURI.equals("NONE")) {
-            //  Use DEM Service
+            DEMSteepness slopeCall = new DEMSteepness(getMetainfo(), scenario_geometry, demSteepnessURI);
+            slopeCall.call();
+
+            return slopeCall.slopes();
+
         }
 
-        return ret_val;
+        return null;
     }
 }