@@ -14,11 +14,13 @@ |
import csip.Config; |
import csip.ModelDataService; |
import csip.PayloadParameter; |
+import csip.PayloadResults; |
import csip.ServiceException; |
import csip.annotations.Description; |
import csip.annotations.Name; |
import csip.annotations.Polling; |
import csip.annotations.Resource; |
+import csip.utils.JSONUtils; |
import database.DBResources; |
import static database.DBResources.LOCAL_SQLSERVER; |
import gisobjects.GISObject; |
@@ -29,10 +31,13 @@ |
import java.util.ArrayList; |
import java.util.Map; |
import javax.ws.rs.Path; |
+import org.codehaus.jettison.json.JSONArray; |
+import org.codehaus.jettison.json.JSONException; |
import org.codehaus.jettison.json.JSONObject; |
import services.Region; |
import services.WEPP; |
import services.WEPS; |
+import services.WEPSFallowRotation; |
import services.WWESoilParams; |
import services.WWESoilParams.SoilResult; |
|
@@ -44,13 +49,12 @@ |
@Description("This service consumes a JSON request containing a request identifier and CRP Offer geometry and returns sheet/rill water and wind erosion rates for a fallow management for up to the three most dominant soil components in a CRP Offer area, plus a weighted average for each erosion rate.") |
@Path("d/crpassessment/1.0") |
@Polling(first = 2000, next = 1000) |
- |
@Resource(from = DBResources.class) |
|
public class V1_0 extends ModelDataService { |
|
public static final String SCENARIO_ID = "scenario_id"; |
- public static final String SCENARIO_GEOMETRY = "scenario_gemoetry"; |
+ public static final String SCENARIO_GEOMETRY = "scenario_geometry"; |
|
protected JSONObject scenario_geometry; |
protected String scenario_id; |
@@ -61,7 +65,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:8083/???"; |
+ protected String demSteepnessURI = "http://csip.engr.colostate.edu:8087/csip-watershed/m/average_slope/2.0"; |
|
protected String outputFile; |
protected boolean streamOutputFile = false; |
@@ -77,7 +81,7 @@ |
@Override |
protected void preProcess() throws Exception { |
PayloadParameter params = parameter(); |
- |
+ |
soilsURI = Config.getString("crp.soils", soilsURI); |
regionURI = Config.getString("crp.region", regionURI); |
weppURI = Config.getString("crp.wepp", weppURI); |
@@ -85,36 +89,33 @@ |
demSteepnessURI = Config.getString("crp.dem.steepness", demSteepnessURI); |
|
if (params.has(SCENARIO_ID)) { |
- |
+ scenario_id = params.getString(SCENARIO_ID); |
} else { |
throw new ServiceException("A required input 'scenario_id' was missing. Please specify a scenario_id value."); |
} |
|
- if (params.has(SCENARIO_GEOMETRY)) { |
- Map<String, JSONObject> paramMap; |
- paramMap = getParamMap(); |
- |
+ Map<String, JSONObject> paramMap; |
+ paramMap = getParamMap(); |
+ if (paramMap.containsKey(SCENARIO_GEOMETRY)) { |
scenario_geometry = paramMap.get(SCENARIO_GEOMETRY); |
|
try (Connection connection = resources().getJDBC(LOCAL_SQLSERVER);) { |
gEngine = GISEngineFactory.createGISEngine(connection); |
aoa_geometry = GISObjectFactory.createGISObject(scenario_geometry, gEngine); |
|
+ // Shortcut to getting the centroid of a shape...Built in to the GISObject source for getLat/Lon if shape is not a point. |
latitude = aoa_geometry.getLatitude(); |
longitude = aoa_geometry.getLongitude(); |
} |
|
} else { |
- throw new ServiceException("A required input 'scenario_id' was missing. Please specify a scenario_id value."); |
+ throw new ServiceException("A required input " + SCENARIO_GEOMETRY + " was missing. Please specify a scenario_id value."); |
} |
|
} |
|
@Override |
protected void doProcess() throws Exception { |
- // Get rotation object |
- rotation = getRotation(); |
- |
// Get Slope Steepness from DEM Service for this AoA. |
steepness = getDEMSteepness(); |
|
@@ -129,37 +130,61 @@ |
soilsCall.call(); |
topThreeComponents = soilsCall.getTopThree(); |
|
- int count = 0; |
+ //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, steepness, rotation, weppURI); |
+ WEPP weppCall = new WEPP(getMetainfo(), latitude, longitude, soilResult.cokey, soilResult.length, ((Double.isNaN(steepness)) ? soilResult.slope_r : steepness), getRotation((int) soilResult.length), weppURI); |
weppCall.call(); |
- waterErosionRate += weppCall.waterErosion(); |
+ soilResult.waterErosion = weppCall.waterErosion(); |
|
- WEPS wepsCall = new WEPS(getMetainfo(), latitude, longitude, regionLength, regionWidth, regionOrientation, soilResult.cokey, rotation, wepsURI); |
+ WEPS wepsCall = new WEPS(getMetainfo(), latitude, longitude, regionLength, regionWidth, regionOrientation, soilResult.cokey, getRotation(0), wepsURI); |
wepsCall.call(); |
- windErosionRate += wepsCall.windErosion(); |
+ soilResult.windErosion = wepsCall.windErosion(); |
|
- count++; |
+ totalArea += soilResult.area; |
} |
|
- waterErosionRate /= count; |
- windErosionRate /= count; |
- |
+ for (SoilResult soilResult : topThreeComponents) { |
+ waterErosionRate += soilResult.waterErosion * (soilResult.area / totalArea); |
+ windErosionRate += soilResult.windErosion * (soilResult.area / totalArea); |
+ } |
} |
|
@Override |
protected void postProcess() throws Exception { |
+ PayloadResults results = results(); |
+ JSONArray soilsResult = new JSONArray(); |
|
+ for (SoilResult soilResult : topThreeComponents) { |
+ JSONArray soilData = new JSONArray(); |
+ soilData.put(JSONUtils.data("soilPtr", soilResult.cokey)); |
+ soilData.put(JSONUtils.data("soilName", soilResult.soilName)); |
+ soilData.put(JSONUtils.data("area_pct", soilResult.area_pct, "The percentage of the component of the mapunit intersection with the CRP Offer Area", "Percent")); |
+ soilData.put(JSONUtils.data("area", soilResult.area, "Soil Component Area (Acres) in the CRP Offer area", "Acres")); |
+ soilData.put(JSONUtils.data("WaterSoilLoss", soilResult.waterErosion, "Average Annual Soil Loss by Water", "ton/ac/yr")); |
+ soilData.put(JSONUtils.data("WindSoilLoss", soilResult.windErosion, "Average Annual Soil Loss by Wind", "ton/ac/yr")); |
+ soilsResult.put(soilData); |
+ } |
+ results.put("top_3_soils", soilsResult); |
+ results.put("WtAvgWaterSoilLoss", waterErosionRate, "Weighted Average Annual Soil Loss by Water", "ton/ac/yr"); |
+ results.put("WtAvgWindSoilLoss", windErosionRate, "Weighted Average Annual Soil Loss by Wind", "ton/ac/yr"); |
+ results.put("WtAvgTotalSoilLoss", waterErosionRate + windErosionRate, "Weighted Average Annual Soil Loss by Water and Wind", "ton/ac/yr"); |
} |
|
- //TODO finish |
- protected JSONObject getRotation() { |
- return new JSONObject(); |
+ //TODO finish when Jack is ready. |
+ protected JSONObject getRotation(int length) throws JSONException { |
+ return new WEPSFallowRotation(length).rotation(); |
} |
|
//TODO finish when DEMSteepness service has been written |
protected double getDEMSteepness() { |
- return Double.NaN; |
+ double ret_val = Double.NaN; |
+ |
+ if (!demSteepnessURI.equals("NONE")) { |
+ // Use DEM Service |
+ } |
+ |
+ return ret_val; |
} |
} |