changeset 2762:33aa37e6d98f

Reproject geometries before exporting them as shapefiles to filesystem. flys-artifacts/trunk@4498 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 24 May 2012 07:58:47 +0000
parents 28e7c1637f78
children 2f7fed1eb4bf
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/utils/GeometryUtils.java
diffstat 2 files changed, 55 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- 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 <ingo@intevation.de>
+
+	* 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	<felix.wolfsteller@intevation.de>
 
 	* doc/conf/meta-data.xml: Include other "thematic" heights for
--- 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<String, Serializable> params =
                 new HashMap<String, Serializable>();
 
@@ -231,13 +251,24 @@
 
             String typeName = newDataStore.getTypeNames()[0];
 
-            FeatureStore<SimpleFeatureType, SimpleFeature> featureStore =
-                (FeatureStore<SimpleFeatureType, SimpleFeature>)
-                    newDataStore.getFeatureSource(typeName);
+            FeatureWriter<SimpleFeatureType, SimpleFeature> 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 */ }

http://dive4elements.wald.intevation.org