diff gnv-artifacts/src/main/java/de/intevation/gnv/utils/ShapeFileWriter.java @ 498:4080b57dcb52

Upgraded to Geotools 2.5.8. Write polygons and line strings to shape files. gnv-artifacts/trunk@578 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 19 Jan 2010 21:23:28 +0000
parents f7038820df2e
children 4fc97074eb90
line wrap: on
line diff
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/utils/ShapeFileWriter.java	Tue Jan 19 17:55:08 2010 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/utils/ShapeFileWriter.java	Tue Jan 19 21:23:28 2010 +0000
@@ -1,31 +1,40 @@
 package de.intevation.gnv.utils;
 
-import java.util.Map;
-import java.util.List;
-import java.util.HashMap;
+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 com.vividsolutions.jts.geom.MultiPolygon;
-import com.vividsolutions.jts.geom.MultiLineString;
+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.Transaction;
 import org.geotools.data.DefaultTransaction;
-import org.geotools.data.FeatureWriter;
-
-import org.geotools.feature.Feature;
-import org.geotools.feature.FeatureType;
-import org.geotools.feature.SchemaException;
-import org.geotools.feature.IllegalAttributeException;
+import org.geotools.data.FeatureStore;
+import org.geotools.data.Transaction;
 
 import org.geotools.data.shapefile.ShapefileDataStore;
 import org.geotools.data.shapefile.ShapefileDataStoreFactory;
 
-import org.apache.log4j.Logger;
+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)
@@ -40,178 +49,245 @@
 
     public static boolean writeMultiLineStringsToFile(
         File                                shapeFile,
+        Integer                             parameterId,
+        Integer                             layer,
+        Date                                date,
         List<Pair<Object, MultiLineString>> multiLineStrings
     ) {
-        return writeMultiLineStringsToFile(shapeFile, multiLineStrings, null);
+        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                              typeName
+        String                              name
     ) {
-        HashMap params = new HashMap();
+        Map<String, Serializable> params = new HashMap<String, Serializable>();
 
         try {
-            params.put(
-                ShapefileDataStoreFactory.URLP.key,
-                shapeFile.toURI().toURL());
+            params.put("url", shapeFile.toURI().toURL());
         }
         catch (MalformedURLException mue) {
             log.error(mue.getLocalizedMessage(), mue);
             return false;
         }
 
-        boolean     success = false;
-        Transaction tx      = null;
-
-        try {
-            ShapefileDataStoreFactory sfdsf =
-                new ShapefileDataStoreFactory();
-
-            ShapefileDataStore sfds =
-                (ShapefileDataStore)sfdsf.createNewDataStore(params);
-
-            if (typeName == null) {
-                typeName = shapeFile.getName();
-            }
-
-             FeatureType featureType = DataUtilities.createType(
-                typeName,
-                "geom:MultiLineString,VALUE:Double");
+        params.put("create spatial index", Boolean.TRUE);
 
-            sfds.createSchema(featureType);
-
-            tx = new DefaultTransaction();
-
-            FeatureWriter featureWriter = sfds.getFeatureWriter(
-                typeName, tx);
 
-            for (Pair<Object, MultiLineString> pair: multiLineStrings) {
-                log.debug("00000000000000000 -> " + pair.getA());
-                Feature feature = featureWriter.next();
-                feature.setAttribute("geom",  pair.getB());
-                feature.setAttribute("VALUE", asDouble(pair.getA()));
-                featureWriter.write();
-            }
+        if (name == null) {
+            name = shapeFile.getName();
+        }
 
-            tx.commit();
-            success = true;
-        }
-        catch (IllegalAttributeException iae) {
-            log.error(iae.getLocalizedMessage(), iae);
+        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 (!success && tx != null) {
-                try { tx.rollback(); }
+            if (transaction != null) {
+                if (!success) {
+                    try { transaction.rollback(); }
+                    catch (IOException ioe) {}
+                }
+                try { transaction.close(); }
                 catch (IOException ioe) {}
             }
         }
 
-        if (tx != null) {
-            try { tx.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, multiPolygons, null);
+        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                     typeName
+        String                     name
     ) {
-        HashMap params = new HashMap();
+        Map<String, Serializable> params = new HashMap<String, Serializable>();
 
         try {
-            params.put(
-                ShapefileDataStoreFactory.URLP.key,
-                shapeFile.toURI().toURL());
+            params.put("url", shapeFile.toURI().toURL());
         }
         catch (MalformedURLException mue) {
             log.error(mue.getLocalizedMessage(), mue);
             return false;
         }
 
-        boolean     success = false;
-        Transaction tx      = null;
-
-        try {
-            ShapefileDataStoreFactory sfdsf =
-                new ShapefileDataStoreFactory();
-
-            ShapefileDataStore sfds =
-                (ShapefileDataStore)sfdsf.createNewDataStore(params);
-
-            if (typeName == null) {
-                typeName = shapeFile.getName();
-            }
-
-             FeatureType featureType = DataUtilities.createType(
-                typeName,
-                "geom:MultiPolygon,CLASS:Integer");
+        params.put("create spatial index", Boolean.TRUE);
 
-            sfds.createSchema(featureType);
-
-            tx = new DefaultTransaction();
-
-            FeatureWriter featureWriter = sfds.getFeatureWriter(
-                typeName, tx);
 
-            for (Map.Entry<Integer, MultiPolygon> entry:
-                multiPolygons.entrySet()
-            ) {
-                Feature feature = featureWriter.next();
-                feature.setAttribute("geom",  entry.getValue());
-                feature.setAttribute("CLASS", entry.getKey());
-                featureWriter.write();
-            }
+        if (name == null) {
+            name = shapeFile.getName();
+        }
 
-            tx.commit();
-            success = true;
-        }
-        catch (IllegalAttributeException iae) {
-            log.error(iae.getLocalizedMessage(), iae);
+        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 (!success && tx != null) {
-                try { tx.rollback(); }
+            if (transaction != null) {
+                if (!success) {
+                    try { transaction.rollback(); }
+                    catch (IOException ioe) {}
+                }
+                try { transaction.close(); }
                 catch (IOException ioe) {}
             }
         }
 
-        if (tx != null) {
-            try { tx.close(); }
-            catch (IOException ioe) {}
-        }
-
         return success;
     }
 
     private static final Double asDouble(Object a) {
-        if (a instanceof Double)
+        if (a instanceof Double) {
             return (Double)a;
-        if (a instanceof Number)
+        }
+        if (a instanceof Number) {
             return Double.valueOf(((Number)a).doubleValue());
+        }
         return 0d;
     }
 }

http://dive4elements.wald.intevation.org