diff flys-artifacts/src/main/java/de/intevation/flys/utils/GeometryUtils.java @ 1106:e9f66d63bdd0

Write user defined barriers into a shapefile that is placed in the Artifact's directory. flys-artifacts/trunk@2609 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 30 Aug 2011 11:09:54 +0000
parents eeebf8514c7f
children 005c2964f9af
line wrap: on
line diff
--- a/flys-artifacts/src/main/java/de/intevation/flys/utils/GeometryUtils.java	Tue Aug 30 08:09:52 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/utils/GeometryUtils.java	Tue Aug 30 11:09:54 2011 +0000
@@ -1,8 +1,33 @@
 package de.intevation.flys.utils;
 
+import java.io.IOException;
+import java.io.File;
+import java.io.Serializable;
+import java.net.MalformedURLException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
 import com.vividsolutions.jts.geom.Coordinate;
 import com.vividsolutions.jts.geom.Geometry;
 
+import org.opengis.feature.simple.SimpleFeature;
+import org.opengis.feature.simple.SimpleFeatureType;
+
+import org.geotools.data.DataStoreFactorySpi;
+import org.geotools.data.DataUtilities;
+import org.geotools.data.FeatureStore;
+import org.geotools.data.DefaultTransaction;
+import org.geotools.data.Transaction;
+import org.geotools.data.shapefile.ShapefileDataStore;
+import org.geotools.data.shapefile.ShapefileDataStoreFactory;
+import org.geotools.feature.FeatureIterator;
+import org.geotools.feature.FeatureCollection;
+import org.geotools.feature.SchemaException;
+import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
+import org.geotools.geojson.feature.FeatureJSON;
+import org.geotools.referencing.crs.DefaultGeographicCRS;
 
 
 public class GeometryUtils {
@@ -28,5 +53,96 @@
 
         return "" + c[0].x + " " + c[1].y + " " + c[1].x + " " + c[0].y;
     }
+
+
+    /**
+     * Returns a SimpleFeatureType used while parsing the GeoJSON string that
+     * represents the barriers entered by the user.
+     *
+     * @param srs The SRS that is used by the GeoJSON string.
+     *
+     * @return a SimpleFeatureType.
+     */
+    public static SimpleFeatureType buildBarriersFeatureType(String srs) {
+        SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
+
+        builder.setName("flys");
+        builder.setNamespaceURI("http://www.intevation.de/");
+        builder.setSRS(srs);
+        builder.setDefaultGeometry("geometry");
+
+        builder.add("geometry", org.opengis.geometry.Geometry.class);
+
+        return builder.buildFeatureType();
+    }
+
+
+    public static SimpleFeatureType buildFeatureType(
+        String name, String srid, String geometryType)
+    throws SchemaException
+    {
+        String schema = "geom:" + geometryType + ":srid=" + srid;
+
+        return DataUtilities.createType(name, schema);
+    }
+
+
+    public static List<SimpleFeature> parseGeoJSON(
+        String geojson, SimpleFeatureType ft
+    ) {
+        List<SimpleFeature> collection = new ArrayList<SimpleFeature>();
+
+        try {
+            FeatureJSON fjson = new FeatureJSON();
+            fjson.setFeatureType(ft);
+
+            FeatureIterator<SimpleFeature> iterator =
+                fjson.streamFeatureCollection(geojson);
+
+            while (iterator.hasNext()) {
+                collection.add(iterator.next());
+            }
+        }
+        catch (IOException ioe) {
+            // TODO handle exception
+        }
+
+        return collection;
+    }
+
+
+    public static boolean writeShapefile(
+        File              shape,
+        SimpleFeatureType featureType,
+        FeatureCollection collection)
+    throws MalformedURLException, IOException
+    {
+        Map<String, Serializable> params = new HashMap<String, Serializable>();
+        params.put("url", shape.toURI().toURL());
+        params.put("create spatial index", Boolean.TRUE);
+
+        DataStoreFactorySpi dataStoreFactory = new ShapefileDataStoreFactory();
+
+        ShapefileDataStore newDataStore =
+            (ShapefileDataStore)dataStoreFactory.createNewDataStore(params);
+
+        newDataStore.createSchema(featureType);
+        newDataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84);
+
+        Transaction 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();
+
+        return true;
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org