diff gnv-artifacts/src/main/java/de/intevation/gnv/utils/ShapeFileWriter.java @ 540:80630520e25a

merged gnv-artifacts/0.4
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:13:49 +0200
parents 4080b57dcb52
children 4fc97074eb90
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/utils/ShapeFileWriter.java	Fri Sep 28 12:13:49 2012 +0200
@@ -0,0 +1,294 @@
+package de.intevation.gnv.utils;
+
+import com.vividsolutions.jts.geom.MultiLineString;
+import com.vividsolutions.jts.geom.MultiPolygon;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.Serializable;
+
+import java.net.MalformedURLException;
+
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.log4j.Logger;
+
+import org.geotools.data.DataStoreFactorySpi;
+import org.geotools.data.DataUtilities;
+import org.geotools.data.DefaultTransaction;
+import org.geotools.data.FeatureStore;
+import org.geotools.data.Transaction;
+
+import org.geotools.data.shapefile.ShapefileDataStore;
+import org.geotools.data.shapefile.ShapefileDataStoreFactory;
+
+import org.geotools.feature.FeatureCollection;
+import org.geotools.feature.FeatureCollections;
+import org.geotools.feature.SchemaException;
+
+import org.geotools.feature.simple.SimpleFeatureBuilder;
+
+import org.geotools.referencing.crs.DefaultGeographicCRS;
+
+import org.opengis.feature.simple.SimpleFeature;
+import org.opengis.feature.simple.SimpleFeatureType;
+
+/**
+ * @author Sascha L. Teichmann (sascha.teichmann@intevation.de)
+ */
+public final class ShapeFileWriter
+{
+    private static Logger log = Logger.getLogger(
+        ShapeFileWriter.class);
+
+    private ShapeFileWriter() {
+    }
+
+    public static boolean writeMultiLineStringsToFile(
+        File                                shapeFile,
+        Integer                             parameterId,
+        Integer                             layer,
+        Date                                date,
+        List<Pair<Object, MultiLineString>> multiLineStrings
+    ) {
+        return writeMultiLineStringsToFile(
+            shapeFile,
+            parameterId,
+            layer,
+            date,
+            multiLineStrings, 
+            "isolines");
+    }
+
+    public static boolean writeMultiLineStringsToFile(
+        File                                shapeFile,
+        Integer                             parameterId,
+        Integer                             layer,
+        Date                                date,
+        List<Pair<Object, MultiLineString>> multiLineStrings,
+        String                              name
+    ) {
+        Map<String, Serializable> params = new HashMap<String, Serializable>();
+
+        try {
+            params.put("url", shapeFile.toURI().toURL());
+        }
+        catch (MalformedURLException mue) {
+            log.error(mue.getLocalizedMessage(), mue);
+            return false;
+        }
+
+        params.put("create spatial index", Boolean.TRUE);
+
+
+        if (name == null) {
+            name = shapeFile.getName();
+        }
+
+        SimpleFeatureType TYPE;
+        
+        try { 
+            TYPE = DataUtilities.createType(
+                name,
+                "geom:MultiLineString:srid=4326," +
+                "PARAMETER:Integer," + 
+                "LAYER:Integer,"     +
+                "DATE:Date,"         +
+                "VALUE:Double");
+        }
+        catch (SchemaException se) {
+            log.error(se.getLocalizedMessage(), se);
+            return false;
+        }
+
+        SimpleFeatureBuilder featureBuilder =
+            new SimpleFeatureBuilder(TYPE);
+
+        FeatureCollection<SimpleFeatureType, SimpleFeature> collection = 
+            FeatureCollections.newCollection();
+
+        for (Pair<Object, MultiLineString> pair: multiLineStrings) {
+            featureBuilder.add(pair.getB());
+            featureBuilder.add(parameterId);
+            featureBuilder.add(layer);
+            featureBuilder.add(date);
+            featureBuilder.add(pair.getA());
+            SimpleFeature feature = featureBuilder.buildFeature(null);
+            collection.add(feature);
+        }
+
+        DataStoreFactorySpi dataStoreFactory = new ShapefileDataStoreFactory();
+
+        Transaction transaction = null;
+
+        boolean success = false;
+        try {
+            ShapefileDataStore newDataStore =
+                (ShapefileDataStore)dataStoreFactory.createNewDataStore(params);
+
+            newDataStore.createSchema(TYPE);
+            newDataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84);
+
+            transaction = new DefaultTransaction("create");
+
+            String typeName = newDataStore.getTypeNames()[0];
+
+            FeatureStore<SimpleFeatureType, SimpleFeature> featureStore =
+                (FeatureStore<SimpleFeatureType, SimpleFeature>)
+                    newDataStore.getFeatureSource(typeName);
+
+            featureStore.setTransaction(transaction);
+
+            featureStore.addFeatures(collection);
+            transaction.commit();
+            success = true;
+        } 
+        catch (IOException ioe) {
+            log.error(ioe.getLocalizedMessage(), ioe);
+        }
+        finally {
+            if (transaction != null) {
+                if (!success) {
+                    try { transaction.rollback(); }
+                    catch (IOException ioe) {}
+                }
+                try { transaction.close(); }
+                catch (IOException ioe) {}
+            }
+        }
+
+        return success;
+    }
+
+    public static boolean writeMultiPolygonsToFile(
+        File                       shapeFile,
+        Integer                    parameterId,
+        Integer                    layer,
+        Date                       date,
+        Map<Integer, MultiPolygon> multiPolygons
+    ) {
+        return writeMultiPolygonsToFile(
+            shapeFile, 
+            parameterId,
+            layer,
+            date,
+            multiPolygons, 
+            "polygons");
+    }
+
+    public static boolean writeMultiPolygonsToFile(
+        File                       shapeFile,
+        Integer                    parameterId,
+        Integer                    layer,
+        Date                       date,
+        Map<Integer, MultiPolygon> multiPolygons,
+        String                     name
+    ) {
+        Map<String, Serializable> params = new HashMap<String, Serializable>();
+
+        try {
+            params.put("url", shapeFile.toURI().toURL());
+        }
+        catch (MalformedURLException mue) {
+            log.error(mue.getLocalizedMessage(), mue);
+            return false;
+        }
+
+        params.put("create spatial index", Boolean.TRUE);
+
+
+        if (name == null) {
+            name = shapeFile.getName();
+        }
+
+        SimpleFeatureType TYPE;
+        
+        try { 
+            TYPE = DataUtilities.createType(
+                name,
+                "geom:MultiPolygon:srid=4326," +
+                "PARAMETER:Integer," + 
+                "LAYER:Integer,"     +
+                "DATE:Date,"         +
+                "CLASS:Integer");
+        }
+        catch (SchemaException se) {
+            log.error(se.getLocalizedMessage(), se);
+            return false;
+        }
+
+        SimpleFeatureBuilder featureBuilder =
+            new SimpleFeatureBuilder(TYPE);
+
+        FeatureCollection<SimpleFeatureType, SimpleFeature> collection = 
+            FeatureCollections.newCollection();
+
+        for (Map.Entry<Integer, MultiPolygon> entry:
+            multiPolygons.entrySet()
+        ) {
+            featureBuilder.add(entry.getValue());
+            featureBuilder.add(parameterId);
+            featureBuilder.add(layer);
+            featureBuilder.add(date);
+            featureBuilder.add(entry.getKey());
+            SimpleFeature feature = featureBuilder.buildFeature(null);
+            collection.add(feature);
+        }
+
+        DataStoreFactorySpi dataStoreFactory = new ShapefileDataStoreFactory();
+
+        Transaction transaction = null;
+
+        boolean success = false;
+        try {
+            ShapefileDataStore newDataStore =
+                (ShapefileDataStore)dataStoreFactory.createNewDataStore(params);
+
+            newDataStore.createSchema(TYPE);
+            newDataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84);
+
+            transaction = new DefaultTransaction("create");
+
+            String typeName = newDataStore.getTypeNames()[0];
+
+            FeatureStore<SimpleFeatureType, SimpleFeature> featureStore =
+                (FeatureStore<SimpleFeatureType, SimpleFeature>)
+                    newDataStore.getFeatureSource(typeName);
+
+            featureStore.setTransaction(transaction);
+
+            featureStore.addFeatures(collection);
+            transaction.commit();
+            success = true;
+        } 
+        catch (IOException ioe) {
+            log.error(ioe.getLocalizedMessage(), ioe);
+        }
+        finally {
+            if (transaction != null) {
+                if (!success) {
+                    try { transaction.rollback(); }
+                    catch (IOException ioe) {}
+                }
+                try { transaction.close(); }
+                catch (IOException ioe) {}
+            }
+        }
+
+        return success;
+    }
+
+    private static final Double asDouble(Object a) {
+        if (a instanceof Double) {
+            return (Double)a;
+        }
+        if (a instanceof Number) {
+            return Double.valueOf(((Number)a).doubleValue());
+        }
+        return 0d;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org