Mercurial > dive4elements > river
changeset 1107:005c2964f9af
Use the correct SRID while reading GeoJSON barriers and writing line/polygon shapefiles.
flys-artifacts/trunk@2610 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Tue, 30 Aug 2011 14:13:06 +0000 |
parents | e9f66d63bdd0 |
children | 5b1198c27d43 |
files | flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/FloodMapState.java flys-artifacts/src/main/java/de/intevation/flys/utils/GeometryUtils.java |
diffstat | 3 files changed, 69 insertions(+), 30 deletions(-) [+] |
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog Tue Aug 30 11:09:54 2011 +0000 +++ b/flys-artifacts/ChangeLog Tue Aug 30 14:13:06 2011 +0000 @@ -1,3 +1,12 @@ +2011-08-30 Ingo Weinzierl <ingo@intevation.de> + + * src/main/java/de/intevation/flys/artifacts/states/FloodMapState.java: + Use the coorect SRID for reading GeoJSON and writing line/polygon + shapefiles. + + * src/main/java/de/intevation/flys/utils/GeometryUtils.java: Use a + concrete coordinate system while feature type creation. + 2011-08-30 Ingo Weinzierl <ingo@intevation.de> * pom.xml: Added GeoTools 2.7.2 dependencies for Shapefile, GeoJSON and
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/FloodMapState.java Tue Aug 30 11:09:54 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/FloodMapState.java Tue Aug 30 14:13:06 2011 +0000 @@ -7,6 +7,7 @@ import javax.xml.xpath.XPathConstants; +import com.vividsolutions.jts.geom.Geometry; import com.vividsolutions.jts.geom.LineString; import com.vividsolutions.jts.geom.Polygon; @@ -14,10 +15,13 @@ import org.opengis.feature.simple.SimpleFeature; import org.opengis.feature.simple.SimpleFeatureType; +import org.opengis.referencing.FactoryException; +import org.opengis.referencing.NoSuchAuthorityCodeException; import org.geotools.feature.FeatureCollection; import org.geotools.feature.FeatureCollections; import org.geotools.feature.SchemaException; +import org.geotools.referencing.CRS; import de.intevation.artifacts.Artifact; import de.intevation.artifacts.CallContext; @@ -43,6 +47,10 @@ private static Logger logger = Logger.getLogger(FloodMapState.class); + public static final String KEEP_ARTIFACT_DIR = + System.getProperty("flys.uesk.keep.artifactsdir", "false"); + + public static final String XPATH_SHAPEFILE_DIR = "/artifact-database/floodmap/shapefile-path/@value"; @@ -74,7 +82,9 @@ WSPLGENJob job = prepareWSPLGENJob(artifact, artifactDir); if (job == null) { - removeDirectory(artifact); + if (KEEP_ARTIFACT_DIR.equals("false")) { + removeDirectory(artifact); + } logger.error("No WSPLGEN processing has been started!"); @@ -260,8 +270,11 @@ String srid = FLYSUtils.getRiverSrid(artifact); String srs = "EPSG:" + srid; - SimpleFeatureType ft = GeometryUtils.buildBarriersFeatureType(srs); - List<SimpleFeature> features = GeometryUtils.parseGeoJSON(geoJSON, ft); + List<SimpleFeature> features = getBarriersFeatures(geoJSON, srs); + if (features == null || features.size() == 0) { + logger.debug("No barrier features extracted."); + return; + } FeatureCollection[] fcs = splitLinesAndPolygons(features); @@ -271,13 +284,15 @@ try { GeometryUtils.writeShapefile( shapeLines, - GeometryUtils.buildFeatureType("lines", srid, "LineString"), + GeometryUtils.buildFeatureType( + "lines", srs, LineString.class, CRS.decode(srs)), fcs[0]); job.addLin(shapeLines.getAbsolutePath()); GeometryUtils.writeShapefile( shapePolys, - GeometryUtils.buildFeatureType("polygons", srid, "Polygon"), + GeometryUtils.buildFeatureType( + "polygons", srs, Polygon.class, CRS.decode(srs)), fcs[1]); job.addLin(shapePolys.getAbsolutePath()); } @@ -290,6 +305,36 @@ catch (SchemaException se) { logger.error("Error while writing shapefile: " + se.getMessage()); } + catch (NoSuchAuthorityCodeException nsae) { + logger.error("Error while writing shapefile: " + nsae.getMessage()); + } + catch (FactoryException fe) { + logger.error("Error while writing shapefile: " + fe.getMessage()); + } + } + + + protected List<SimpleFeature> getBarriersFeatures(String json, String srs) { + SimpleFeatureType ft = null; + List<SimpleFeature> features = null; + + try { + ft = GeometryUtils.buildFeatureType( + "barriers", srs, Geometry.class, CRS.decode(srs)); + + features = GeometryUtils.parseGeoJSON(json, ft); + } + catch (SchemaException se) { + logger.warn("Error while reading GeoJSON: " + se.getMessage()); + } + catch (NoSuchAuthorityCodeException nsae) { + logger.warn("Error while reading GeoJSON: " + nsae.getMessage()); + } + catch (FactoryException fe) { + logger.warn("Error while reading GeoJSON: " + fe.getMessage()); + } + + return features; }
--- a/flys-artifacts/src/main/java/de/intevation/flys/utils/GeometryUtils.java Tue Aug 30 11:09:54 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/utils/GeometryUtils.java Tue Aug 30 14:13:06 2011 +0000 @@ -14,9 +14,9 @@ import org.opengis.feature.simple.SimpleFeature; import org.opengis.feature.simple.SimpleFeatureType; +import org.opengis.referencing.crs.CoordinateReferenceSystem; import org.geotools.data.DataStoreFactorySpi; -import org.geotools.data.DataUtilities; import org.geotools.data.FeatureStore; import org.geotools.data.DefaultTransaction; import org.geotools.data.Transaction; @@ -27,7 +27,6 @@ import org.geotools.feature.SchemaException; import org.geotools.feature.simple.SimpleFeatureTypeBuilder; import org.geotools.geojson.feature.FeatureJSON; -import org.geotools.referencing.crs.DefaultGeographicCRS; public class GeometryUtils { @@ -55,38 +54,26 @@ } - /** - * Returns a SimpleFeatureType used while parsing the GeoJSON string that - * represents the barriers entered by the user. - * - * @param srs The SRS that is used by the GeoJSON string. - * - * @return a SimpleFeatureType. - */ - public static SimpleFeatureType buildBarriersFeatureType(String srs) { + public static SimpleFeatureType buildFeatureType( + String name, + String srs, + Class geometryType, + CoordinateReferenceSystem crs) + throws SchemaException + { SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder(); builder.setName("flys"); builder.setNamespaceURI("http://www.intevation.de/"); + builder.setCRS(crs); builder.setSRS(srs); - builder.setDefaultGeometry("geometry"); - builder.add("geometry", org.opengis.geometry.Geometry.class); + builder.add("geometry", geometryType, crs); return builder.buildFeatureType(); } - public static SimpleFeatureType buildFeatureType( - String name, String srid, String geometryType) - throws SchemaException - { - String schema = "geom:" + geometryType + ":srid=" + srid; - - return DataUtilities.createType(name, schema); - } - - public static List<SimpleFeature> parseGeoJSON( String geojson, SimpleFeatureType ft ) { @@ -125,9 +112,7 @@ ShapefileDataStore newDataStore = (ShapefileDataStore)dataStoreFactory.createNewDataStore(params); - newDataStore.createSchema(featureType); - newDataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84); Transaction transaction = new DefaultTransaction("create");