# HG changeset patch # User Ingo Weinzierl # Date 1337846327 0 # Node ID 33aa37e6d98f9d9313b3bb933569361db0bad8ff # Parent 28e7c1637f7853bd4dfda78b0b21d1b3f0d97510 Reproject geometries before exporting them as shapefiles to filesystem. flys-artifacts/trunk@4498 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r 28e7c1637f78 -r 33aa37e6d98f flys-artifacts/ChangeLog --- a/flys-artifacts/ChangeLog Thu May 24 05:59:25 2012 +0000 +++ b/flys-artifacts/ChangeLog Thu May 24 07:58:47 2012 +0000 @@ -1,3 +1,9 @@ +2012-05-24 Ingo Weinzierl + + * src/main/java/de/intevation/flys/utils/GeometryUtils.java: Reproject + geometries into the coordinate reference system defined in the + configuration. + 2012-05-24 Felix Wolfsteller * doc/conf/meta-data.xml: Include other "thematic" heights for diff -r 28e7c1637f78 -r 33aa37e6d98f flys-artifacts/src/main/java/de/intevation/flys/utils/GeometryUtils.java --- a/flys-artifacts/src/main/java/de/intevation/flys/utils/GeometryUtils.java Thu May 24 05:59:25 2012 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/utils/GeometryUtils.java Thu May 24 07:58:47 2012 +0000 @@ -20,17 +20,21 @@ import org.opengis.referencing.FactoryException; import org.opengis.referencing.NoSuchAuthorityCodeException; import org.opengis.referencing.crs.CoordinateReferenceSystem; +import org.opengis.referencing.operation.MathTransform; +import org.opengis.referencing.operation.TransformException; import org.geotools.data.DataStoreFactorySpi; -import org.geotools.data.FeatureStore; import org.geotools.data.DefaultTransaction; +import org.geotools.data.FeatureWriter; import org.geotools.data.Transaction; import org.geotools.data.shapefile.ShapefileDataStore; import org.geotools.data.shapefile.ShapefileDataStoreFactory; +import org.geotools.data.simple.SimpleFeatureIterator; import org.geotools.feature.FeatureIterator; import org.geotools.feature.FeatureCollection; import org.geotools.feature.simple.SimpleFeatureTypeBuilder; import org.geotools.geojson.feature.FeatureJSON; +import org.geotools.geometry.jts.JTS; import org.geotools.referencing.CRS; import de.intevation.flys.model.RiverAxis; @@ -40,6 +44,8 @@ private static final Logger logger = Logger.getLogger(GeometryUtils.class); + public static final String DEFAULT_EPSG = "EPSG:31467"; + private GeometryUtils() { } @@ -206,14 +212,28 @@ SimpleFeatureType featureType, FeatureCollection collection ) { + return writeShapefile( + shape, featureType, collection, featureType.getCoordinateReferenceSystem()); + } + + + public static boolean writeShapefile( + File shape, + SimpleFeatureType featureType, + FeatureCollection collection, + CoordinateReferenceSystem crs + ) { if (collection.isEmpty()) { logger.warn("Shapefile is not written - no features given!"); return false; } - Transaction transaction = null; + Transaction transaction = null; try { + MathTransform transform = CRS.findMathTransform( + CRS.decode(DEFAULT_EPSG), crs); + Map params = new HashMap(); @@ -231,13 +251,24 @@ String typeName = newDataStore.getTypeNames()[0]; - FeatureStore featureStore = - (FeatureStore) - newDataStore.getFeatureSource(typeName); + FeatureWriter writer = + newDataStore.getFeatureWriter(typeName, transaction); - featureStore.setTransaction(transaction); + SimpleFeatureIterator iterator = (SimpleFeatureIterator) collection.features(); - featureStore.addFeatures(collection); + while (iterator.hasNext()){ + SimpleFeature feature = iterator.next(); + SimpleFeature copy = writer.next(); + + copy.setAttributes(feature.getAttributes()); + + Geometry orig = (Geometry) feature.getDefaultGeometry(); + Geometry reprojected = JTS.transform(orig, transform); + + copy.setDefaultGeometry(reprojected); + writer.write(); + } + transaction.commit(); return true; @@ -248,10 +279,20 @@ catch (IOException ioe) { logger.error("Unable to write shapefile: " + ioe.getMessage()); } + catch (NoSuchAuthorityCodeException nsae) { + logger.error("Cannot get CoordinateReferenceSystem for '" + + DEFAULT_EPSG + "'"); + } + catch (FactoryException fe) { + logger.error("Cannot get CoordinateReferenceSystem for '" + + DEFAULT_EPSG + "'"); + } + catch (TransformException te) { + logger.error("Was not able to transform geometry!", te); + } finally { if (transaction != null) { try { - logger.debug("XXX CLOSE TRANSACTION!"); transaction.close(); } catch (IOException ioe) { /* do nothing */ }