@@ -5,10 +5,16 @@ |
*/ |
package m.wqm.wqsr; |
|
+import GISObjects.GISEngineFactory; |
+import GISObjects.GISObject; |
+import GISObjects.GISObjectException; |
+import GISObjects.GISObjectFactory; |
import csip.ModelDataService; |
import csip.ServiceException; |
import csip.annotations.Resource; |
+import static csip.annotations.ResourceType.JDBC; |
import csip.utils.JSONUtils; |
+import java.io.IOException; |
import java.sql.Connection; |
import java.sql.ResultSet; |
import java.sql.SQLException; |
@@ -16,6 +22,7 @@ |
import java.util.ArrayList; |
import java.util.List; |
import java.util.Map; |
+import java.util.logging.Level; |
import javax.ws.rs.Path; |
import oms3.annotations.Description; |
import oms3.annotations.Name; |
@@ -23,12 +30,12 @@ |
import org.codehaus.jettison.json.JSONException; |
import org.codehaus.jettison.json.JSONObject; |
import wqm.utils.DBResources; |
-import static wqm.utils.DBResources.SSURGO_ID; |
+import static wqm.utils.DBResources.WQM_ID; |
|
/** |
* WQM-01: Water Quality Sensitivity Rating (WQSR) |
* |
- * @author Sandeep, od |
+ * @author Sandeep, od, rumpal |
*/ |
@Name("WQM-01: Water Quality Sensitivity Rating (WQSR)") |
@Description("This service intersects area of analysis (AoA) geometry with the NRCS Water Quality " |
@@ -36,122 +43,141 @@ |
+ "level required for mitigating nutrient and pesticide loss potentials and hazards.") |
@Path("m/wqm/wqsr/1.0") |
@Resource(from = DBResources.class) |
+@Resource(type = JDBC, file = "${mssql-ssurgo.db}", id = "mssql", env = { |
+ "removeAbandoned=false", "defaultReadOnly=true", "defaultAutoCommit=false", |
+ "jdbcInterceptors=org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;" |
+ + "org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer;" |
+ + "org.apache.tomcat.jdbc.pool.interceptor.ResetAbandonedTimer" |
+}) |
public class V1_0 extends ModelDataService { |
|
- List<AoA> list = new ArrayList<>(); |
+ private List<m.wqm.wqsr.V1_0.AoA> list; |
|
@Override |
- public void preProcess() throws ServiceException, JSONException { |
- JSONArray aoaArr = getJSONArrayParam("aoas"); |
- for (int i = 0; i < aoaArr.length(); i++) { |
- //Map individual JSONObject & extract values |
- Map<String, JSONObject> thisAoA = JSONUtils.preprocess(aoaArr.getJSONArray(i)); |
- String aoaId = JSONUtils.getStringParam(thisAoA, "aoa_id", "unknown"); |
- JSONArray polygon = JSONUtils.getJSONArrayParam(thisAoA, "aoa_geometry"); |
- JSONArray coordinates = polygon.getJSONArray(0); |
- String points = coordinates.toString(); |
+ public void preProcess() throws ServiceException { |
+ list = new ArrayList<>(); |
+ try { |
+ JSONArray aoaArr = getJSONArrayParam("aoas"); |
+ for (int i = 0; i < aoaArr.length(); i++) { |
+ //Map individual JSONObject & extract values |
+ Map<String, JSONObject> thisAoA = JSONUtils.preprocess(aoaArr.getJSONArray(i)); |
|
- // Getting the array of coordinates and converting to a form that can be used in the query for intersection |
- points = points.replace("[", ""); |
- points = points.replace("]", ""); |
- points = points.replace(",", " "); |
- String temp[] = points.split(" "); |
- String finalpoints = temp[0]; |
- for (int j = 1; j < temp.length; j++) { |
- if (j % 2 == 0) { |
- finalpoints += "," + temp[j]; |
- } else { |
- finalpoints += " " + temp[j]; |
+ String aoaId = JSONUtils.getStringParam(thisAoA, "aoa_id", "unknown"); |
+ JSONObject aoaGeometry = JSONUtils.getJSONObjectParam(thisAoA, "aoa_geometry"); |
+ |
+ GISObject aoa_geometry; |
+ try (Connection connection = getResourceJDBC("mssql");) { |
+ // Sets the GIS Engine of the GISObjects to the DBMS specified in |
+ //the creation of the “connection” variable. MsSQL in this case |
+ GISObjectFactory.setGISObjectEngine(GISEngineFactory.createGISEngine(connection)); |
+ aoa_geometry = GISObjectFactory.createGISObject(aoaGeometry); |
+ System.out.println("aoa_geometry = " + aoa_geometry.getGeometry()); |
+ connection.close(); |
+ } catch (ServiceException | JSONException | SQLException | IOException | GISObjectException ex) { |
+ LOG.log(Level.SEVERE, "WQM-1: Error in processing the aoa geometry.", ex); |
+ throw new ServiceException("Error in processing the aoa geometry", ex); |
} |
+ |
+ list.add(new m.wqm.wqsr.V1_0.AoA(aoaId, aoa_geometry)); |
} |
- list.add(new V1_0.AoA(aoaId, finalpoints)); |
+ } catch (JSONException ex) { |
+ LOG.log(Level.SEVERE, "Error in processing the request JSON for WQM-1!", ex); |
+ throw new ServiceException("Error in processing the request JSON.", ex); |
} |
} |
|
@Override |
public void doProcess() throws ServiceException, SQLException { |
- try (Connection tempCon = getResourceJDBC(SSURGO_ID); |
- Statement stmt = tempCon.createStatement()) { |
- |
- for (AoA aoa : list) { |
- double aoaBaseArea = 0.0; |
- double aoaSensitiveArea = 0.0; |
- double aoaCriticalArea = 0.0; |
- //Query to select the area,rating and treatment level |
- // which given coordinates intersects wqsr_area geometry |
- try (ResultSet results = stmt.executeQuery("select shape_area,wqs_rating from wqm_huc12.wqsr_area WHERE ST_Intersects('POLYGON((" |
- + aoa.getCoordinates() + "))'::geography::geometry, geom)")) { |
- while (results.next()) { |
- String wqsRating = results.getString("wqs_rating"); |
- double shape_area = results.getDouble("shape_area"); |
- if (wqsRating.equalsIgnoreCase("base")) { |
- aoaBaseArea += shape_area; |
- } else if (wqsRating.equalsIgnoreCase("sensitive")) { |
- aoaSensitiveArea += shape_area; |
- } else if (wqsRating.equalsIgnoreCase("critical")) { |
- aoaCriticalArea += shape_area; |
- } |
+ |
+ try (Connection tempCon = getResourceJDBC(WQM_ID); |
+ Statement stmt = tempCon.createStatement()) { |
+ |
+ for (m.wqm.wqsr.V1_0.AoA aoa : list) { |
+ double aoaBaseArea = 0.0; |
+ double aoaSensitiveArea = 0.0; |
+ double aoaCriticalArea = 0.0; |
+ |
+ //Query to select the area,rating and treatment level |
+ // which given coordinates intersects wqsr_area geometry |
+ try (ResultSet results = stmt.executeQuery("SELECT shape_area, wqs_rating " |
+ + "FROM wqm_huc12.wqsr_area WHERE geom.STIntersects(geometry::STGeomFromText('" |
+ + aoa.getAoaGeometry().getGeometry() + "', 0)) = 1 AND geom.STIsValid() = 1 " |
+ + "AND geometry::STGeomFromText('" + aoa.getAoaGeometry().getGeometry() + "', 0).STIsValid() = 1;")) { |
+ while (results.next()) { |
+ String wqsRating = results.getString("wqs_rating"); |
+ double shape_area = results.getDouble("shape_area"); |
+ if (wqsRating.equalsIgnoreCase("base")) { |
+ aoaBaseArea += shape_area; |
+ } else if (wqsRating.equalsIgnoreCase("sensitive")) { |
+ aoaSensitiveArea += shape_area; |
+ } else if (wqsRating.equalsIgnoreCase("critical")) { |
+ aoaCriticalArea += shape_area; |
} |
} |
+ } |
|
- if ((aoaBaseArea >= aoaSensitiveArea) && (aoaBaseArea >= aoaCriticalArea)) { |
- aoa.setWQSRating("Base"); |
- aoa.setTreatmentLevel("I"); |
- } else if ((aoaSensitiveArea >= aoaBaseArea) && (aoaSensitiveArea >= aoaCriticalArea)) { |
- aoa.setWQSRating("Sensitive"); |
- aoa.setTreatmentLevel("II"); |
- } else { |
- aoa.setWQSRating("Critical"); |
- aoa.setTreatmentLevel("III"); |
- } |
+ if ((aoaBaseArea >= aoaSensitiveArea) && (aoaBaseArea >= aoaCriticalArea)) { |
+ aoa.setWQSRating("Base"); |
+ aoa.setTreatmentLevel("I"); |
+ } else if ((aoaSensitiveArea >= aoaBaseArea) && (aoaSensitiveArea >= aoaCriticalArea)) { |
+ aoa.setWQSRating("Sensitive"); |
+ aoa.setTreatmentLevel("II"); |
+ } else { |
+ aoa.setWQSRating("Critical"); |
+ aoa.setTreatmentLevel("III"); |
} |
} |
- catch (Exception e) { |
- LOG.info(e.toString()); |
- throw new ServiceException(e); |
+ } catch (SQLException ex) { |
+ LOG.log(Level.SEVERE, "SQLException for WQM-1!", ex); |
+ throw new ServiceException("SQL problem.", ex); |
} |
- |
+ |
} |
|
@Override |
- public void postProcess() throws JSONException { |
- JSONArray finalArr = new JSONArray(); |
- for (AoA aoa : list) { |
- JSONArray resultArr = new JSONArray(); |
- resultArr.put(JSONUtils.dataDesc("AoAId", aoa.getAoAId(), "Area of Analysis Identifier")); |
- resultArr.put(JSONUtils.dataDesc("wqs_Rating", aoa.getWQSRating(), "WQS Rating")); |
- resultArr.put(JSONUtils.dataDesc("wqs_treatment_level", aoa.getTreatmentLevel(), "WQS Treatment Level")); |
- finalArr.put(resultArr); |
+ public void postProcess() throws ServiceException { |
+ try { |
+ JSONArray finalArr = new JSONArray(); |
+ for (m.wqm.wqsr.V1_0.AoA aoa : list) { |
+ JSONArray resultArr = new JSONArray(); |
+ resultArr.put(JSONUtils.dataDesc("AoAId", aoa.getAoAId(), "Area of Analysis Identifier")); |
+ resultArr.put(JSONUtils.dataDesc("wqs_Rating", aoa.getWQSRating(), "WQS Rating")); |
+ resultArr.put(JSONUtils.dataDesc("wqs_treatment_level", aoa.getTreatmentLevel(), "WQS Treatment Level")); |
+ finalArr.put(resultArr); |
+ } |
+ putResult("", finalArr); |
+ } catch (JSONException ex) { |
+ LOG.log(Level.SEVERE, "Error in processing the response JSON for WQM-1!", ex); |
+ throw new ServiceException("Error in processing the response JSON.", ex); |
} |
- putResult("", finalArr); |
} |
|
- public class AoA { |
+ static class AoA { |
|
String aoa_id; |
- String polygonCoordinates; |
+ GISObject aoaGeometry; |
String wqsRating; |
String treatmentLevel; |
|
- public AoA(String aoa_id, String coordinates) { |
+ public AoA(String aoa_id, GISObject geometry) { |
this.aoa_id = aoa_id; |
- this.polygonCoordinates = coordinates; |
+ this.aoaGeometry = geometry; |
} |
|
public String getAoAId() { |
- return aoa_id; |
+ return this.aoa_id; |
} |
|
public String getWQSRating() { |
- return wqsRating; |
+ return this.wqsRating; |
} |
|
public String getTreatmentLevel() { |
- return treatmentLevel; |
+ return this.treatmentLevel; |
} |
|
- public String getCoordinates() { |
- return polygonCoordinates; |
+ public GISObject getAoaGeometry() { |
+ return this.aoaGeometry; |
} |
|
public void setWQSRating(String wqs) { |