diff flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/FloodMapState.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 9f88cc54570c
children 005c2964f9af
line wrap: on
line diff
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/FloodMapState.java	Tue Aug 30 08:09:52 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/FloodMapState.java	Tue Aug 30 11:09:54 2011 +0000
@@ -2,12 +2,23 @@
 
 import java.io.File;
 import java.io.IOException;
+import java.net.MalformedURLException;
 import java.util.List;
 
 import javax.xml.xpath.XPathConstants;
 
+import com.vividsolutions.jts.geom.LineString;
+import com.vividsolutions.jts.geom.Polygon;
+
 import org.apache.log4j.Logger;
 
+import org.opengis.feature.simple.SimpleFeature;
+import org.opengis.feature.simple.SimpleFeatureType;
+
+import org.geotools.feature.FeatureCollection;
+import org.geotools.feature.FeatureCollections;
+import org.geotools.feature.SchemaException;
+
 import de.intevation.artifacts.Artifact;
 import de.intevation.artifacts.CallContext;
 
@@ -21,6 +32,7 @@
 import de.intevation.flys.artifacts.model.WSPLGENFacet;
 import de.intevation.flys.artifacts.model.WSPLGENJob;
 import de.intevation.flys.utils.FLYSUtils;
+import de.intevation.flys.utils.GeometryUtils;
 
 
 public class FloodMapState
@@ -35,6 +47,8 @@
         "/artifact-database/floodmap/shapefile-path/@value";
 
     public static final String WSPLGEN_PARAMETER_FILE = "wsplgen.par";
+    public static final String WSPLGEN_BARRIERS_LINES = "barrier_lines.shp";
+    public static final String WSPLGEN_BARRIERS_POLY  = "barrier_polygons.shp";
 
     public static final int WSPLGEN_DEFAULT_OUTPUT = 0;
 
@@ -148,13 +162,13 @@
         setDelta(artifact, job);
         setGel(artifact, job);
         setDist(artifact, job);
+        setLine(artifact, artifactDir, job);
 
         // TODO
         // setDgm(artifact, job);    // SHP
         // setPro(artifact, job);    // SHP
         // setWsp(artifact, job);    // WSP
         // setWspTag(artifact, job);
-        // setLine(artifact, job);   // SHP
         // setAxis(artifact, job);   // SHP
         // setArea(artifact, job);   // SHP
         // setOutFile(artifact, job);
@@ -239,5 +253,68 @@
             // nothing to do here
         }
     }
+
+
+    protected void setLine(FLYSArtifact artifact, File dir, WSPLGENJob job) {
+        String geoJSON = artifact.getDataAsString("uesk.barriers");
+        String srid    = FLYSUtils.getRiverSrid(artifact);
+        String srs     = "EPSG:" + srid;
+
+        SimpleFeatureType   ft = GeometryUtils.buildBarriersFeatureType(srs);
+        List<SimpleFeature> features = GeometryUtils.parseGeoJSON(geoJSON, ft);
+
+        FeatureCollection[] fcs = splitLinesAndPolygons(features);
+
+        File shapeLines = new File(dir, WSPLGEN_BARRIERS_LINES);
+        File shapePolys = new File(dir, WSPLGEN_BARRIERS_POLY);
+
+        try {
+            GeometryUtils.writeShapefile(
+                shapeLines,
+                GeometryUtils.buildFeatureType("lines", srid, "LineString"),
+                fcs[0]);
+            job.addLin(shapeLines.getAbsolutePath());
+
+            GeometryUtils.writeShapefile(
+                shapePolys,
+                GeometryUtils.buildFeatureType("polygons", srid, "Polygon"),
+                fcs[1]);
+            job.addLin(shapePolys.getAbsolutePath());
+        }
+        catch (MalformedURLException mue) {
+            logger.error("Error while writing shapefile: " + mue.getMessage());
+        }
+        catch (IOException ioe) {
+            logger.error("Error while writing shapefile: " + ioe.getMessage());
+        }
+        catch (SchemaException se) {
+            logger.error("Error while writing shapefile: " + se.getMessage());
+        }
+    }
+
+
+    protected FeatureCollection[] splitLinesAndPolygons(List<SimpleFeature> f) {
+        FeatureCollection lines    = FeatureCollections.newCollection();
+        FeatureCollection polygons = FeatureCollections.newCollection();
+
+        for (SimpleFeature feature: f) {
+            Object geom = feature.getDefaultGeometry();
+
+            if (geom instanceof LineString) {
+                lines.add(feature);
+            }
+            else if (geom instanceof Polygon) {
+                polygons.add(feature);
+            }
+            else {
+                logger.warn("Feature not supported: " + geom.getClass());
+            }
+        }
+
+        logger.debug("Found " + lines.size() + " barrier lines.");
+        logger.debug("Found " + polygons.size() + " barrier polygons.");
+
+        return new FeatureCollection[] { lines, polygons };
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org