@@ -5,7 +5,7 @@ |
*/ |
package m.example.gis_geotools; |
|
-import com.vividsolutions.jts.geom.Geometry; |
+//import com.vividsolutions.jts.geom.Geometry; |
import csip.ModelDataService; |
import static csip.ModelDataService.EXEC_OK; |
import csip.ServiceException; |
@@ -19,12 +19,12 @@ |
import org.apache.commons.io.IOUtils; |
import org.codehaus.jettison.json.JSONException; |
import org.codehaus.jettison.json.JSONObject; |
-import org.geotools.feature.FeatureCollection; |
-import org.geotools.feature.FeatureIterator; |
-import org.geotools.geojson.feature.FeatureJSON; |
-import org.geotools.geojson.geom.GeometryJSON; |
-import org.opengis.feature.Feature; |
-import org.opengis.feature.simple.SimpleFeature; |
+//import org.geotools.feature.FeatureCollection; |
+//import org.geotools.feature.FeatureIterator; |
+//import org.geotools.geojson.feature.FeatureJSON; |
+//import org.geotools.geojson.geom.GeometryJSON; |
+//import org.opengis.feature.Feature; |
+//import org.opengis.feature.simple.SimpleFeature; |
|
/** |
* Basic usage of the GeoTools Geometry and Feature objects. |
@@ -48,111 +48,111 @@ |
*/ |
@Name("GIS_GeoTools") |
@Description("An example of how to read in geometries or featurecollections using the geotools libraries. Also demonstrates proper GeoJSON, error passing with ModelDataServices and SQL differences between PostGIS and MsSQL") |
-@Path("m/gis_geotools/1.0") |
+//@Path("m/gis_geotools/1.0") |
public class V1_0 extends ModelDataService { |
|
- Geometry aoa_geometry; |
- JSONObject aoa_input_geometry; |
- |
- @Override |
- // NOTE: Changed "Exception" to csip.ServiceException. (It's considered bad form to throw a generic exception in Java) |
- // We will check the validity of the param array, if it is present, and return a distinct message identifying what is |
- // missing, if necessary, when we throw a csip.ServiceException. This is much better than just throwing the exception |
- // with no message at all. |
- protected void preProcess() throws csip.ServiceException { |
- Map<String, JSONObject> inputParams = getParamMap(); |
- if (null != inputParams) { |
- this.aoa_input_geometry = inputParams.get("aoa_geometry"); |
- if (null == this.aoa_input_geometry) { |
- throw new csip.ServiceException("No 'aoa_geometry' item was found in the JSON input. Please specify an 'aoa_geometry' ."); |
- } |
- } else { |
- throw new csip.ServiceException(" No JSON input parameters were found. Please specify some input."); |
- } |
- } |
- |
- @Override |
- // NOTE: Notice the usage of the ability to return an error message string via the process() routine here, instead of throwing a generic exeption. |
- // This feature gives the programmer the ability to return very helpful messages to the client of this service. This makes debugging much |
- // simpler when such features are used to their full extent. |
- // |
- // Also NOTE: This feature will soon be deprecated and using the csip.ServiceException only will be the prefered pathway once the output of the |
- // stack traces associated with an exception are moved to a different section of the Metadata of the result rather than the body of the |
- // result. |
- |
- protected String process() throws ServiceException { |
- String ret_val = EXEC_OK; |
- |
- GeometryJSON geoJSON = new GeometryJSON(); |
- try { |
- if (this.aoa_input_geometry.has("features")) { |
- FeatureJSON featureJSON = new FeatureJSON(); |
- |
- FeatureCollection inputFeatures = featureJSON.readFeatureCollection(this.aoa_input_geometry.toString()); |
- FeatureIterator tFeatures = inputFeatures.features(); |
- int count = 1; |
- while (tFeatures.hasNext()) { |
- Feature tFeature = tFeatures.next(); |
- this.aoa_geometry = (Geometry) ((SimpleFeature) tFeature).getAttribute("geometry"); |
- Logger.getLogger(V1_0.class.getName()).log(Level.INFO, "Feature geometry " + count + " recieved was: {0}", this.aoa_geometry.toText()); |
- count++; |
- // Do something with this particular feature here...OR just grab the first one if only one was specified. |
- // This particular code assumes only one was specified, but can easily be altered to send more than one |
- // and do something with each.... |
- |
- } |
- } else { |
- if ( this.aoa_input_geometry.has("type")){ //Try to uncover if this is a "Feature" or a geometry object |
- if ( this.aoa_input_geometry.getString("type").equalsIgnoreCase("feature")){ |
- FeatureJSON featureJSON = new FeatureJSON(); |
- Feature tFeature = featureJSON.readFeature(this.aoa_input_geometry.toString()); |
- this.aoa_geometry = (Geometry) ((SimpleFeature) tFeature).getAttribute("geometry"); |
- // Here we could pull out the Feature's properies as well... |
- Logger.getLogger(V1_0.class.getName()).log(Level.INFO, "This Feature''s Id is: {0}", ((SimpleFeature) tFeature).getID()); |
- Logger.getLogger(V1_0.class.getName()).log(Level.INFO, "This Feature''s name is: {0}", ((SimpleFeature) tFeature).getAttribute("name")); |
- |
- //NOTE: For some reason, property values that are arrays (as in the example JSON), don't get read, so watch out for this! |
- Logger.getLogger(V1_0.class.getName()).log(Level.INFO, "This Feature''s adjustment factor is: {0}", ((SimpleFeature) tFeature).getAttribute("AdjustmentFactor")); |
- |
- } |
- else{ //Try to read the GeoJSON geometry object |
- this.aoa_geometry = geoJSON.read(IOUtils.toInputStream(this.aoa_input_geometry.toString())); |
- } |
- } |
- else{ |
- |
- } |
- } |
- |
- // TODO: Add your code here to do something with the geometry(ies) you read in. |
- // For instance: See code lines below: (NOTE: The .toText() funciton of "Geometry" returns WKT..) |
- // Log the WKT of the input. |
- Logger.getLogger(V1_0.class.getName()).log(Level.INFO, "Last aoa_geometry value recieved was: {0}", this.aoa_geometry.toText()); |
- |
- String Query; |
- // PostGIS specific SQL statement below to calculate area in acres. |
- Query = "SELECT st_area(st_transform(ST_PolygonFromText('" + this.aoa_geometry.toText() + "', 4326),3541))/43560 as areaInAcres; "; |
- |
- // MsSQL specific SQL statement below to calculate area in acres. (NOTE: Assumes point rotation is openGIS compliant, i.e. non-ESRI Shapefile) |
- Query = "SELECT geography::STGeomFromText('" + this.aoa_geometry.toText() + "', 4326).MakeValid().STArea() / 4046.86 as areaInAcres; "; |
- |
- // MsSQL specific SQL statement below to calculate area in acres. (NOTE: Assumes point rotation is non-openGIS compliant, i.e. ESRI Shapefile) |
- Query = "SELECT geography::STGeomFromText('" + this.aoa_geometry.toText() + "', 4326).ReorientObject().MakeValid().STArea() / 4046.86 as areaInAcres; "; |
- |
- } catch (IOException ex) { |
- Logger.getLogger(V1_0.class.getName()).log(Level.SEVERE, "Cannot parse the input GeoJSON into valid GIS entities: ", ex); |
- // This will return a useable error message to the user via the process() return value, rather than a exception stacktrace, with no explanations. |
- ret_val = "Cannot parse the input GeoJSON into valid GIS entities: " + ex.getMessage(); |
- // Or |
- //throw new csip.ServiceException( ex ); |
- } catch (JSONException ex) { |
- Logger.getLogger(V1_0.class.getName()).log(Level.SEVERE, "Cannot parse the aoa_geometry geometry type. Please check your syntax. ", ex); |
- ret_val = "Cannot parse the aoa_geometry geometry type. Please check your syntax. " + ex.getMessage(); |
- // Or |
- //throw new csip.ServiceException( ex ); |
- } |
- |
- return ret_val; |
- } |
+// Geometry aoa_geometry; |
+// JSONObject aoa_input_geometry; |
+// |
+// @Override |
+// // NOTE: Changed "Exception" to csip.ServiceException. (It's considered bad form to throw a generic exception in Java) |
+// // We will check the validity of the param array, if it is present, and return a distinct message identifying what is |
+// // missing, if necessary, when we throw a csip.ServiceException. This is much better than just throwing the exception |
+// // with no message at all. |
+// protected void preProcess() throws csip.ServiceException { |
+// Map<String, JSONObject> inputParams = getParamMap(); |
+// if (null != inputParams) { |
+// this.aoa_input_geometry = inputParams.get("aoa_geometry"); |
+// if (null == this.aoa_input_geometry) { |
+// throw new csip.ServiceException("No 'aoa_geometry' item was found in the JSON input. Please specify an 'aoa_geometry' ."); |
+// } |
+// } else { |
+// throw new csip.ServiceException(" No JSON input parameters were found. Please specify some input."); |
+// } |
+// } |
+// |
+// @Override |
+// // NOTE: Notice the usage of the ability to return an error message string via the process() routine here, instead of throwing a generic exeption. |
+// // This feature gives the programmer the ability to return very helpful messages to the client of this service. This makes debugging much |
+// // simpler when such features are used to their full extent. |
+// // |
+// // Also NOTE: This feature will soon be deprecated and using the csip.ServiceException only will be the prefered pathway once the output of the |
+// // stack traces associated with an exception are moved to a different section of the Metadata of the result rather than the body of the |
+// // result. |
+// |
+// protected String process() throws ServiceException { |
+// String ret_val = EXEC_OK; |
+// |
+// GeometryJSON geoJSON = new GeometryJSON(); |
+// try { |
+// if (this.aoa_input_geometry.has("features")) { |
+// FeatureJSON featureJSON = new FeatureJSON(); |
+// |
+// FeatureCollection inputFeatures = featureJSON.readFeatureCollection(this.aoa_input_geometry.toString()); |
+// FeatureIterator tFeatures = inputFeatures.features(); |
+// int count = 1; |
+// while (tFeatures.hasNext()) { |
+// Feature tFeature = tFeatures.next(); |
+// this.aoa_geometry = (Geometry) ((SimpleFeature) tFeature).getAttribute("geometry"); |
+// Logger.getLogger(V1_0.class.getName()).log(Level.INFO, "Feature geometry " + count + " recieved was: {0}", this.aoa_geometry.toText()); |
+// count++; |
+// // Do something with this particular feature here...OR just grab the first one if only one was specified. |
+// // This particular code assumes only one was specified, but can easily be altered to send more than one |
+// // and do something with each.... |
+// |
+// } |
+// } else { |
+// if ( this.aoa_input_geometry.has("type")){ //Try to uncover if this is a "Feature" or a geometry object |
+// if ( this.aoa_input_geometry.getString("type").equalsIgnoreCase("feature")){ |
+// FeatureJSON featureJSON = new FeatureJSON(); |
+// Feature tFeature = featureJSON.readFeature(this.aoa_input_geometry.toString()); |
+// this.aoa_geometry = (Geometry) ((SimpleFeature) tFeature).getAttribute("geometry"); |
+// // Here we could pull out the Feature's properies as well... |
+// Logger.getLogger(V1_0.class.getName()).log(Level.INFO, "This Feature''s Id is: {0}", ((SimpleFeature) tFeature).getID()); |
+// Logger.getLogger(V1_0.class.getName()).log(Level.INFO, "This Feature''s name is: {0}", ((SimpleFeature) tFeature).getAttribute("name")); |
+// |
+// //NOTE: For some reason, property values that are arrays (as in the example JSON), don't get read, so watch out for this! |
+// Logger.getLogger(V1_0.class.getName()).log(Level.INFO, "This Feature''s adjustment factor is: {0}", ((SimpleFeature) tFeature).getAttribute("AdjustmentFactor")); |
+// |
+// } |
+// else{ //Try to read the GeoJSON geometry object |
+// this.aoa_geometry = geoJSON.read(IOUtils.toInputStream(this.aoa_input_geometry.toString())); |
+// } |
+// } |
+// else{ |
+// |
+// } |
+// } |
+// |
+// // TODO: Add your code here to do something with the geometry(ies) you read in. |
+// // For instance: See code lines below: (NOTE: The .toText() funciton of "Geometry" returns WKT..) |
+// // Log the WKT of the input. |
+// Logger.getLogger(V1_0.class.getName()).log(Level.INFO, "Last aoa_geometry value recieved was: {0}", this.aoa_geometry.toText()); |
+// |
+// String Query; |
+// // PostGIS specific SQL statement below to calculate area in acres. |
+// Query = "SELECT st_area(st_transform(ST_PolygonFromText('" + this.aoa_geometry.toText() + "', 4326),3541))/43560 as areaInAcres; "; |
+// |
+// // MsSQL specific SQL statement below to calculate area in acres. (NOTE: Assumes point rotation is openGIS compliant, i.e. non-ESRI Shapefile) |
+// Query = "SELECT geography::STGeomFromText('" + this.aoa_geometry.toText() + "', 4326).MakeValid().STArea() / 4046.86 as areaInAcres; "; |
+// |
+// // MsSQL specific SQL statement below to calculate area in acres. (NOTE: Assumes point rotation is non-openGIS compliant, i.e. ESRI Shapefile) |
+// Query = "SELECT geography::STGeomFromText('" + this.aoa_geometry.toText() + "', 4326).ReorientObject().MakeValid().STArea() / 4046.86 as areaInAcres; "; |
+// |
+// } catch (IOException ex) { |
+// Logger.getLogger(V1_0.class.getName()).log(Level.SEVERE, "Cannot parse the input GeoJSON into valid GIS entities: ", ex); |
+// // This will return a useable error message to the user via the process() return value, rather than a exception stacktrace, with no explanations. |
+// ret_val = "Cannot parse the input GeoJSON into valid GIS entities: " + ex.getMessage(); |
+// // Or |
+// //throw new csip.ServiceException( ex ); |
+// } catch (JSONException ex) { |
+// Logger.getLogger(V1_0.class.getName()).log(Level.SEVERE, "Cannot parse the aoa_geometry geometry type. Please check your syntax. ", ex); |
+// ret_val = "Cannot parse the aoa_geometry geometry type. Please check your syntax. " + ex.getMessage(); |
+// // Or |
+// //throw new csip.ServiceException( ex ); |
+// } |
+// |
+// return ret_val; |
+// } |
|
} |