diff gnv-artifacts/src/main/java/de/intevation/gnv/utils/ShapeFileWriter.java @ 465:f7038820df2e

Added support to trace rasters to JTS multi polygons and multi line strings. Write them to shape files with GeoTools. gnv-artifacts/trunk@526 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 11 Jan 2010 00:29:45 +0000
parents
children 4080b57dcb52
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	Mon Jan 11 00:29:45 2010 +0000
@@ -0,0 +1,218 @@
+package de.intevation.gnv.utils;
+
+import java.util.Map;
+import java.util.List;
+import java.util.HashMap;
+
+import java.io.File;
+import java.io.IOException;
+
+import java.net.MalformedURLException;
+
+import com.vividsolutions.jts.geom.MultiPolygon;
+import com.vividsolutions.jts.geom.MultiLineString;
+
+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.shapefile.ShapefileDataStore;
+import org.geotools.data.shapefile.ShapefileDataStoreFactory;
+
+import org.apache.log4j.Logger;
+
+/**
+ * @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,
+        List<Pair<Object, MultiLineString>> multiLineStrings
+    ) {
+        return writeMultiLineStringsToFile(shapeFile, multiLineStrings, null);
+    }
+
+    public static boolean writeMultiLineStringsToFile(
+        File                                shapeFile,
+        List<Pair<Object, MultiLineString>> multiLineStrings,
+        String                              typeName
+    ) {
+        HashMap params = new HashMap();
+
+        try {
+            params.put(
+                ShapefileDataStoreFactory.URLP.key,
+                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");
+
+            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();
+            }
+
+            tx.commit();
+            success = true;
+        }
+        catch (IllegalAttributeException iae) {
+            log.error(iae.getLocalizedMessage(), iae);
+        }
+        catch (SchemaException se) {
+            log.error(se.getLocalizedMessage(), se);
+        }
+        catch (IOException ioe) {
+            log.error(ioe.getLocalizedMessage(), ioe);
+        }
+        finally {
+            if (!success && tx != null) {
+                try { tx.rollback(); }
+                catch (IOException ioe) {}
+            }
+        }
+
+        if (tx != null) {
+            try { tx.close(); }
+            catch (IOException ioe) {}
+        }
+
+        return success;
+    }
+
+    public static boolean writeMultiPolygonsToFile(
+        File                       shapeFile,
+        Map<Integer, MultiPolygon> multiPolygons
+    ) {
+        return writeMultiPolygonsToFile(shapeFile, multiPolygons, null);
+    }
+
+    public static boolean writeMultiPolygonsToFile(
+        File                       shapeFile,
+        Map<Integer, MultiPolygon> multiPolygons,
+        String                     typeName
+    ) {
+        HashMap params = new HashMap();
+
+        try {
+            params.put(
+                ShapefileDataStoreFactory.URLP.key,
+                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");
+
+            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();
+            }
+
+            tx.commit();
+            success = true;
+        }
+        catch (IllegalAttributeException iae) {
+            log.error(iae.getLocalizedMessage(), iae);
+        }
+        catch (SchemaException se) {
+            log.error(se.getLocalizedMessage(), se);
+        }
+        catch (IOException ioe) {
+            log.error(ioe.getLocalizedMessage(), ioe);
+        }
+        finally {
+            if (!success && tx != null) {
+                try { tx.rollback(); }
+                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)
+            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