changeset 6710:ffc9cb39cea7

Remove duplicate code for mapfile generation. ID problem still not fixed (#657).
author Christian Lins <christian.lins@intevation.de>
date Tue, 30 Jul 2013 16:09:40 +0200
parents fe496c477173
children 0f0f09badd29
files artifacts/src/main/java/org/dive4elements/river/artifacts/states/FloodMapState.java artifacts/src/main/java/org/dive4elements/river/artifacts/states/HWSBarriersState.java
diffstat 2 files changed, 39 insertions(+), 116 deletions(-) [+]
line wrap: on
line diff
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/states/FloodMapState.java	Tue Jul 30 12:33:05 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/states/FloodMapState.java	Tue Jul 30 16:09:40 2013 +0200
@@ -8,55 +8,35 @@
 
 package org.dive4elements.river.artifacts.states;
 
+import com.vividsolutions.jts.geom.Coordinate;
+import com.vividsolutions.jts.geom.Geometry;
+import com.vividsolutions.jts.geom.LineString;
+import com.vividsolutions.jts.geom.Polygon;
+
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
-
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 
 import org.apache.log4j.Logger;
-
 import org.apache.velocity.Template;
-
-import org.geotools.feature.FeatureCollection;
-import org.geotools.feature.FeatureCollections;
-
-import org.geotools.feature.simple.SimpleFeatureBuilder;
-
-import org.hibernate.HibernateException;
-
-import org.opengis.feature.simple.SimpleFeature;
-import org.opengis.feature.simple.SimpleFeatureType;
-
-import com.vividsolutions.jts.geom.Coordinate;
-import com.vividsolutions.jts.geom.Geometry;
-import com.vividsolutions.jts.geom.LineString;
-import com.vividsolutions.jts.geom.Polygon;
-
 import org.dive4elements.artifactdatabase.state.Facet;
-
 import org.dive4elements.artifacts.Artifact;
 import org.dive4elements.artifacts.CallContext;
 import org.dive4elements.artifacts.CallMeta;
 import org.dive4elements.artifacts.GlobalContext;
-
 import org.dive4elements.artifacts.common.utils.FileTools;
-
 import org.dive4elements.river.artifacts.D4EArtifact;
-
 import org.dive4elements.river.artifacts.access.RangeAccess;
-
 import org.dive4elements.river.artifacts.context.RiverContext;
-
 import org.dive4elements.river.artifacts.model.CalculationMessage;
 import org.dive4elements.river.artifacts.model.CalculationResult;
 import org.dive4elements.river.artifacts.model.FacetTypes;
 import org.dive4elements.river.artifacts.model.LayerInfo;
 import org.dive4elements.river.artifacts.model.WQKms;
-
 import org.dive4elements.river.artifacts.model.map.HWS;
 import org.dive4elements.river.artifacts.model.map.HWSContainer;
 import org.dive4elements.river.artifacts.model.map.HWSFactory;
@@ -64,24 +44,25 @@
 import org.dive4elements.river.artifacts.model.map.WSPLGENCalculation;
 import org.dive4elements.river.artifacts.model.map.WSPLGENJob;
 import org.dive4elements.river.artifacts.model.map.WSPLGENReportFacet;
-
 import org.dive4elements.river.artifacts.resources.Resources;
-
 import org.dive4elements.river.exports.WstWriter;
-
 import org.dive4elements.river.model.CrossSectionTrack;
 import org.dive4elements.river.model.DGM;
 import org.dive4elements.river.model.Floodplain;
 import org.dive4elements.river.model.RiverAxis;
-
 import org.dive4elements.river.utils.ArtifactMapfileGenerator;
-import org.dive4elements.river.utils.RiverUtils;
 import org.dive4elements.river.utils.GeometryUtils;
 import org.dive4elements.river.utils.MapfileGenerator;
-
+import org.dive4elements.river.utils.RiverUtils;
 import org.dive4elements.river.wsplgen.FacetCreator;
 import org.dive4elements.river.wsplgen.JobObserver;
 import org.dive4elements.river.wsplgen.Scheduler;
+import org.geotools.feature.FeatureCollection;
+import org.geotools.feature.FeatureCollections;
+import org.geotools.feature.simple.SimpleFeatureBuilder;
+import org.hibernate.HibernateException;
+import org.opengis.feature.simple.SimpleFeature;
+import org.opengis.feature.simple.SimpleFeatureType;
 
 public class FloodMapState
 extends      DefaultState
@@ -109,9 +90,9 @@
     public static final String WSPLGEN_FLOODPLAIN     = "talaue.shp";
     public static final String WSPLGEN_WSP_FILE       = "waterlevel.wst";
     public static final String WSPLGEN_OUTPUT_FILE    = "wsplgen.shp";
-    public static final String WSPLGEN_USER_SHAPE     = "user-rgd.shp";
-    public static final String WSPLGEN_USER_ZIP       = "user-rgd.zip";
-    public static final String WSPLGEN_USER_FILENAME  = "user-rgd";
+    public static final String WSPLGEN_USER_RGD_SHAPE = "user-rgd.shp";
+    public static final String WSPLGEN_USER_RGD_ZIP   = "user-rgd.zip";
+    public static final String WSPLGEN_USER_RGD       = "user-rgd";
 
     public static final String WSPLGEN_QPS_NAME = "qps";
 
@@ -428,7 +409,7 @@
             createMapfile(
                 artifact,
                 artifactDir,
-                MapfileGenerator.MS_LAYER_PREFIX + "hws-lines",
+                MapfileGenerator.MS_LAYER_PREFIX + HWS_LINES,
                 HWS_LINES_SHAPE,
                 "LINE",
                 "31467",
@@ -441,7 +422,7 @@
     }
 
 
-    private void createMapfile(
+    public static void createMapfile(
         D4EArtifact artifact,
         File artifactDir,
         String name,
@@ -650,13 +631,13 @@
         File         dir,
         WSPLGENJob   job
     ) {
-        File archive = new File(dir, WSPLGEN_USER_SHAPE);
+        File archive = new File(dir, WSPLGEN_USER_RGD_SHAPE);
         boolean exists = archive.exists();
         logger.debug("shp file exists: " + exists);
         if (exists) {
-            job.addLin(dir + "/" + WSPLGEN_USER_SHAPE);
+            job.addLin(dir + "/" + WSPLGEN_USER_RGD_SHAPE);
             facetCreator.createShapeFacet(FacetCreator.I18N_USERSHAPE,
-                MapfileGenerator.MS_LAYER_PREFIX + "user-rgd",
+                MapfileGenerator.MS_LAYER_PREFIX + WSPLGEN_USER_RGD,
                 FLOODMAP_USERSHAPE,
                 4);
         }
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/states/HWSBarriersState.java	Tue Jul 30 12:33:05 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/states/HWSBarriersState.java	Tue Jul 30 16:09:40 2013 +0200
@@ -9,51 +9,29 @@
 package org.dive4elements.river.artifacts.states;
 
 import java.io.File;
-import java.io.FileNotFoundException;
 import java.io.IOException;
-
-import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.List;
 
 import org.apache.log4j.Logger;
-
-import org.apache.velocity.Template;
-
-import org.geotools.data.shapefile.ShapefileDataStore;
-
-import org.geotools.feature.FeatureCollection;
-import org.geotools.feature.FeatureCollections;
-
-import org.opengis.feature.simple.SimpleFeatureType;
-
-import org.opengis.feature.type.GeometryDescriptor;
-
-import org.w3c.dom.Element;
-
 import org.dive4elements.artifactdatabase.state.Facet;
-
 import org.dive4elements.artifacts.Artifact;
 import org.dive4elements.artifacts.CallContext;
-
 import org.dive4elements.artifacts.common.utils.FileTools;
-
 import org.dive4elements.artifacts.common.utils.XMLUtils.ElementCreator;
-
 import org.dive4elements.river.artifacts.D4EArtifact;
-
 import org.dive4elements.river.artifacts.access.MapAccess;
-
-import org.dive4elements.river.artifacts.model.LayerInfo;
-
 import org.dive4elements.river.artifacts.model.map.HWS;
 import org.dive4elements.river.artifacts.model.map.HWSContainer;
 import org.dive4elements.river.artifacts.model.map.HWSFactory;
-
-import org.dive4elements.river.utils.ArtifactMapfileGenerator;
-import org.dive4elements.river.utils.RiverUtils;
 import org.dive4elements.river.utils.GeometryUtils;
 import org.dive4elements.river.utils.MapfileGenerator;
+import org.dive4elements.river.utils.RiverUtils;
+import org.geotools.data.shapefile.ShapefileDataStore;
+import org.geotools.feature.FeatureCollection;
+import org.geotools.feature.FeatureCollections;
+import org.opengis.feature.simple.SimpleFeatureType;
+import org.opengis.feature.type.GeometryDescriptor;
+import org.w3c.dom.Element;
 
 public class HWSBarriersState
 extends DefaultState
@@ -63,9 +41,8 @@
     private static Logger logger = Logger.getLogger(HWSBarriersState.class);
     private static final String HWS_SHAPEFILE_LINES = "hws-lines.shp";
     private static final String HWS_SHAPEFILE_POINTS = "hws-points.shp";
-    private static final String USER_RGD_SHAPE     = "user-rgd.shp";
-    private static final String USER_RGD_ZIP       = "user-rgd.zip";
-    private static final String USER_RGD_FILENAME  = "user-rgd";
+
+
     @Override
     protected String getUIProvider() {
         return "map_digitize";
@@ -146,7 +123,7 @@
         }
 
         if (successLines) {
-            createMapfile(
+            FloodMapState.createMapfile(
                 artifact,
                 artifactDir,
                 MapfileGenerator.MS_LAYER_PREFIX + "hws-lines",
@@ -156,7 +133,7 @@
                 "hws");
         }
         if (successPoints) {
-            createMapfile(
+            FloodMapState.createMapfile(
                 artifact,
                 artifactDir,
                 MapfileGenerator.MS_LAYER_PREFIX + "hws-points",
@@ -172,7 +149,7 @@
                 try {
                     ShapefileDataStore store = new ShapefileDataStore(
                     new File(artifactDir.getCanonicalPath() +
-                        "/" + USER_RGD_SHAPE)
+                        "/" + FloodMapState.WSPLGEN_USER_RGD_SHAPE)
                             .toURI().toURL());
                     GeometryDescriptor desc =
                         store.getSchema().getGeometryDescriptor();
@@ -201,14 +178,14 @@
                     else {
                         type = "POINT";
                     }
-                    createMapfile(
+                    FloodMapState.createMapfile(
                         artifact,
                         artifactDir,
-                        MapfileGenerator.MS_LAYER_PREFIX + USER_RGD_FILENAME,
-                        USER_RGD_SHAPE,
+                        MapfileGenerator.MS_LAYER_PREFIX + FloodMapState.WSPLGEN_USER_RGD,
+                        FloodMapState.WSPLGEN_USER_RGD_SHAPE,
                         type,
                         epsg,
-                        "user-rgd");
+                        FloodMapState.WSPLGEN_USER_RGD);
                 }
                 catch (IOException e) {
                     logger.warn("No mapfile for user-rgd created!");
@@ -219,7 +196,7 @@
     }
 
     private boolean extractUserShp(File dir) {
-        File archive = new File(dir, USER_RGD_ZIP);
+        File archive = new File(dir, FloodMapState.WSPLGEN_USER_RGD_ZIP);
         boolean exists = archive.exists();
         logger.debug("Zip file exists: " + exists);
         if (exists) {
@@ -231,7 +208,7 @@
             }
             catch (IOException ioe) {
                 logger.warn("Zip archive " + dir + "/"
-                    + USER_RGD_ZIP + " could not be extracted.");
+                    + FloodMapState.WSPLGEN_USER_RGD_ZIP + " could not be extracted.");
                 return false;
             }
         }
@@ -261,7 +238,7 @@
                         return true;
                     }
                     try {
-                        FileTools.copyFile(file, new File(target, USER_RGD_FILENAME + "." + suffix));
+                        FileTools.copyFile(file, new File(target, FloodMapState.WSPLGEN_USER_RGD + "." + suffix));
                     }
                     catch (IOException ioe) {
                         logger.warn ("Error while copying file " + file.getName());
@@ -275,41 +252,6 @@
         FileTools.deleteRecursive(source);
     }
 
-    private void createMapfile(
-        D4EArtifact artifact,
-        File artifactDir,
-        String name,
-        String hwsShapefile,
-        String type,
-        String srid,
-        String group
-    ) {
-        LayerInfo info = new LayerInfo();
-        info.setName(name + artifact.identifier());
-        info.setType(type);
-        info.setDirectory(artifact.identifier());
-        info.setTitle(name);
-        info.setData(hwsShapefile);
-        info.setSrid(srid);
-        info.setGroupTitle(group);
-        MapfileGenerator generator = new ArtifactMapfileGenerator();
-        Template tpl = generator.getTemplateByName(MapfileGenerator.SHP_LAYER_TEMPLATE);
-        try {
-            File layer = new File(artifactDir.getCanonicalPath() + "/" + name);
-            generator.writeLayer(info, layer, tpl);
-            List<String> layers = new ArrayList<String>();
-            layers.add(layer.getAbsolutePath());
-            generator.generate();
-        }
-        catch(FileNotFoundException fnfe) {
-            logger.warn("Could not find mapfile for hws layer");
-        }
-        catch (Exception ioe) {
-            logger.warn("Could not create mapfile for hws layer");
-            logger.warn(Arrays.toString(ioe.getStackTrace()));
-        }
-    }
-
 
     @Override
     public void endOfLife(Artifact artifact, Object callContext) {

http://dive4elements.wald.intevation.org