changeset 1775:0156105222c9

Improved the MapfileGenerator. It offers methods to create barrier and wsplgen layer files for mapserver. flys-artifacts/trunk@3097 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Fri, 28 Oct 2011 08:36:32 +0000
parents 092e1e5020bc
children 7d1bd710a4b3
files flys-artifacts/ChangeLog flys-artifacts/doc/conf/artifacts/winfo.xml flys-artifacts/doc/conf/mapserver/shapefile_layer.vm flys-artifacts/src/main/java/de/intevation/flys/exports/MapGenerator.java flys-artifacts/src/main/java/de/intevation/flys/utils/MapfileGenerator.java flys-artifacts/src/main/java/de/intevation/flys/wsplgen/FacetCreator.java
diffstat 6 files changed, 250 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Fri Oct 28 05:54:25 2011 +0000
+++ b/flys-artifacts/ChangeLog	Fri Oct 28 08:36:32 2011 +0000
@@ -1,3 +1,22 @@
+2011-10-28  Ingo Weinzierl <ingo@intevation.de>
+
+	* doc/conf/artifacts/winfo.xml: Defined "floodmap.barriers" as valid facet
+	  for floodmaps.
+
+	* doc/conf/mapserver/shapefile_layer.vm: New. Currently a copy of
+	  layer.vm. This template will evolve to a special mapserver layer
+	  template with a shapefile data source.
+
+	* src/main/java/de/intevation/flys/wsplgen/FacetCreator.java: Fixed broken
+	  facet name of barriers.
+
+	* src/main/java/de/intevation/flys/utils/MapfileGenerator.java: Added
+	  public methods for creating wsplgen and barriers layer files for
+	  mapserver.
+
+	* src/main/java/de/intevation/flys/exports/MapGenerator.java: Use
+	  MapfileGenerator to create new layer files for wsplgen and barriers.
+
 2011-10-28  Ingo Weinzierl <ingo@intevation.de>
 
 	* src/main/java/de/intevation/flys/utils/GeometryUtils.java: Added new
--- a/flys-artifacts/doc/conf/artifacts/winfo.xml	Fri Oct 28 05:54:25 2011 +0000
+++ b/flys-artifacts/doc/conf/artifacts/winfo.xml	Fri Oct 28 08:36:32 2011 +0000
@@ -350,6 +350,7 @@
                 <outputmode name="floodmap" description="output.uesk.map.description" type="map">
                     <facets>
                         <facet name="floodmap.wsplgen"/>
+                        <facet name="floodmap.barriers"/>
                     </facets>
                 </outputmode>
                 <outputmode name="wsplgen_report" description="output.wsplgen_report" mime-type="text/xml" type="report">
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/doc/conf/mapserver/shapefile_layer.vm	Fri Oct 28 08:36:32 2011 +0000
@@ -0,0 +1,34 @@
+LAYER
+    NAME      "$LAYER.getName()"
+    TYPE      $LAYER.getType()
+    DATA      "$LAYER.getDirectory()/$LAYER.getData()"
+    STATUS    ON
+    TEMPLATE  map.html
+    TOLERANCE 10
+    DUMP      TRUE
+    #if( $LAYER.getGroup() )
+        GROUP "$LAYER.getGroup()"
+    #end
+
+    METADATA
+        "wms_title" "$LAYER.getTitle()"
+        "gml_include_items" "all"
+        #if ( $LAYER.getGroupTitle() )
+            "wms_group_title" "$LAYER.getGroupTitle()"
+        #end
+    END
+
+    #if ( !$LAYER.getStyle() )
+        #if ( $LAYER.getGroupTitle() )
+            #if ( $LAYER.getType() == "POLYGON" )
+                #include("barrier_polygons_class.vm")
+            #else
+                #include("barrier_lines_class.vm")
+            #end
+        #else
+            #include("wsplgen_class.vm")
+        #end
+    #else
+        $LAYER.getStyle()
+    #end
+END
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/MapGenerator.java	Fri Oct 28 05:54:25 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/MapGenerator.java	Fri Oct 28 08:36:32 2011 +0000
@@ -1,5 +1,6 @@
 package de.intevation.flys.exports;
 
+import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.util.ArrayList;
@@ -25,6 +26,7 @@
 import de.intevation.flys.artifacts.model.FacetTypes;
 import de.intevation.flys.artifacts.model.WMSLayerFacet;
 import de.intevation.flys.utils.GeometryUtils;
+import de.intevation.flys.utils.MapfileGenerator;
 
 
 public class MapGenerator implements OutGenerator, FacetTypes {
@@ -94,14 +96,41 @@
             setMaxExtent(extent);
             setSrid(wms.getSrid());
 
-            if (FLOODMAP_WSPLGEN.equals(name) && initialExtent == null) {
-                setInitialExtent(extent);
+            if (FLOODMAP_WSPLGEN.equals(name)) {
+                if (initialExtent == null) {
+                    setInitialExtent(extent);
+                }
+
+                createWSPLGENLayer(flys, wms);
+            }
+            else if (FLOODMAP_BARRIERS.equals(name)) {
+                createBarriersLayer(flys, wms);
             }
         }
         else {
             logger.warn("Facet not supported: " + nativeFacet.getClass());
         }
+    }
 
+
+    protected void createWSPLGENLayer(FLYSArtifact flys, WMSLayerFacet wms) {
+        MapfileGenerator mfg = MapfileGenerator.getInstance();
+        mfg.createUeskLayer(flys, wms);
+    }
+
+
+    protected void createBarriersLayer(FLYSArtifact flys, WMSLayerFacet wms) {
+        MapfileGenerator mfg = MapfileGenerator.getInstance();
+
+        try {
+            mfg.createBarriersLayer(flys, wms);
+        }
+        catch (FileNotFoundException fnfe) {
+            logger.error(fnfe, fnfe);
+        }
+        catch (IOException ioe) {
+            logger.error(ioe, ioe);
+        }
     }
 
 
--- a/flys-artifacts/src/main/java/de/intevation/flys/utils/MapfileGenerator.java	Fri Oct 28 05:54:25 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/utils/MapfileGenerator.java	Fri Oct 28 08:36:32 2011 +0000
@@ -19,7 +19,9 @@
 
 import de.intevation.artifacts.common.utils.Config;
 
+import de.intevation.flys.artifacts.FLYSArtifact;
 import de.intevation.flys.artifacts.model.LayerInfo;
+import de.intevation.flys.artifacts.model.WMSLayerFacet;
 
 /**
  * This class iterates over a bunch of directories, searches for meta
@@ -35,10 +37,13 @@
     public static final String WSPLGEN_LINES_SHAPE    = "barrier_lines.shp";
     public static final String WSPLGEN_POLYGONS_SHAPE = "barrier_polygons.shp";
 
+    public static final String SHP_LAYER_TEMPLATE = "shapefile_layer.vm";
+
     public static final String MS_WSPLGEN_POSTFIX  = "-wsplgen";
     public static final String MS_BARRIERS_POSTFIX = "-barriers";
     public static final String MS_LINE_POSTFIX     = "-lines";
     public static final String MS_POLYGONS_POSTFIX = "-polygons";
+    public static final String MS_LAYER_PREFIX     = "ms_layer-";
 
     protected static final long SLEEPTIME = 10 * 1000L; // 10 seconds
 
@@ -128,6 +133,9 @@
         catch (FileNotFoundException fnfe) {
             logger.debug("Error while mapfile creation: " + fnfe.getMessage());
         }
+        catch (IOException ioe) {
+            logger.error(ioe, ioe);
+        }
         finally {
             logger.debug("THREAD END");
         }
@@ -150,7 +158,7 @@
      * generation.
      */
     protected void generate()
-    throws    FileNotFoundException
+    throws    FileNotFoundException, IOException
     {
         File[]        userDirs = getUserDirs();
         List<LayerInfo> layers = parseLayers(userDirs);
@@ -263,7 +271,7 @@
      * configured.
      */
     protected File getShapefileBaseDir()
-    throws    FileNotFoundException
+    throws    FileNotFoundException, IOException
     {
         if (shapefileDirectory == null) {
             String path = FLYSUtils.getXPathString(
@@ -273,9 +281,13 @@
                 shapefileDirectory = new File(path);
             }
 
-            if (shapefileDirectory == null || !shapefileDirectory.exists()) {
+            if (shapefileDirectory == null) {
                 throw new FileNotFoundException("No shapefile directory given");
             }
+
+            if (!shapefileDirectory.exists()) {
+                shapefileDirectory.createNewFile();
+            }
         }
 
         return shapefileDirectory;
@@ -283,7 +295,7 @@
 
 
     protected File[] getUserDirs()
-    throws    FileNotFoundException
+    throws    FileNotFoundException, IOException
     {
         File   baseDir      = getShapefileBaseDir();
         File[] artifactDirs = baseDir.listFiles();
@@ -335,6 +347,107 @@
     }
 
 
+    /**
+     * Creates a layer file used for Mapserver's mapfile which represents the
+     * floodmap.
+     *
+     * @param flys The FLYSArtifact that owns <i>wms</i>.
+     * @param wms The WMSLayerFacet that contains information for the layer.
+     */
+    public void createUeskLayer(FLYSArtifact flys, WMSLayerFacet wms)
+    throws FileNotFoundException, IOException
+    {
+        logger.debug("createUeskLayer");
+
+        LayerInfo layerinfo = new LayerInfo(
+            flys.identifier() + MS_WSPLGEN_POSTFIX,
+            "POLYGON",
+            flys.identifier(),
+            WSPLGEN_RESULT_SHAPE,
+            "I18N_WSPLGEN_RESULT");
+
+        String name = MS_LAYER_PREFIX + wms.getName();
+
+        Template template = getTemplateByName(SHP_LAYER_TEMPLATE);
+        if (template == null) {
+            logger.warn("Template '" + SHP_LAYER_TEMPLATE + "' found.");
+            return;
+        }
+
+        try {
+            File dir = new File(getShapefileBaseDir(), flys.identifier());
+            writeLayer(layerinfo, dir, name, template);
+        }
+        catch (FileNotFoundException fnfe) {
+            logger.error(fnfe, fnfe);
+            logger.warn("Unable to write layer: " + name);
+        }
+    }
+
+
+    /**
+     * Creates a layer file used for Mapserver's mapfile which represents the
+     * user defined barriers.
+     *
+     * @param flys The FLYSArtifact that owns <i>wms</i>.
+     * @param wms The WMSLayerFacet that contains information for the layer.
+     */
+    public void createBarriersLayer(FLYSArtifact flys, WMSLayerFacet wms)
+    throws FileNotFoundException, IOException
+    {
+        logger.debug("createBarriersLayer");
+
+        String uuid = flys.identifier();
+        File   dir  = new File(getShapefileBaseDir(), uuid);
+
+        String group      = uuid + MS_BARRIERS_POSTFIX;
+        String groupTitle = "I18N_BARRIERS_TITLE";
+
+        LayerInfo lineInfo = new LayerInfo(
+            uuid + MS_LINE_POSTFIX,
+            "LINE",
+            uuid,
+            WSPLGEN_LINES_SHAPE,
+            "I18N_LINE_SHAPE",
+            group,
+            groupTitle);
+
+        LayerInfo polygonInfo = new LayerInfo(
+            uuid + MS_POLYGONS_POSTFIX,
+            "POLYGON",
+            uuid,
+            WSPLGEN_POLYGONS_SHAPE,
+            "I18N_POLYGON_SHAPE",
+            group,
+            groupTitle);
+
+        String nameLines    = MS_LAYER_PREFIX + wms.getName() + "-lines";
+        String namePolygons = MS_LAYER_PREFIX + wms.getName() + "-polygons";
+
+        Template tpl = getTemplateByName(SHP_LAYER_TEMPLATE);
+        if (tpl == null) {
+            logger.warn("Template '" + SHP_LAYER_TEMPLATE + "' found.");
+            return;
+        }
+
+        try {
+            writeLayer(lineInfo, dir, nameLines, tpl);
+        }
+        catch (FileNotFoundException fnfe) {
+            logger.error(fnfe, fnfe);
+            logger.warn("Unable to write layer: " + nameLines);
+        }
+
+        try {
+            writeLayer(polygonInfo, dir, namePolygons, tpl);
+        }
+        catch (FileNotFoundException fnfe) {
+            logger.error(fnfe, fnfe);
+            logger.warn("Unable to write layer: " + nameLines);
+        }
+    }
+
+
     protected List<LayerInfo> parseBarriersLayers(File dir) {
         List<LayerInfo> barriers = new ArrayList<LayerInfo>(2);
 
@@ -372,6 +485,53 @@
     }
 
 
+    protected void writeLayer(
+        LayerInfo layerinfo,
+        File      dir,
+        String    name,
+        Template  tpl
+    )
+    throws    FileNotFoundException
+    {
+        if (logger.isDebugEnabled()) {
+            logger.debug("Write layer for:");
+            logger.debug("   directory: " + dir.getName());
+            logger.debug("   name:      " + name);
+        }
+
+        File   layer  = new File(dir, name);
+        Writer writer = null;
+
+        try {
+            writer = new FileWriter(layer);
+
+            VelocityContext context = new VelocityContext();
+            context.put("LAYER", layerinfo);
+
+            tpl.merge(context, writer);
+        }
+        catch (FileNotFoundException fnfe) {
+            logger.error(fnfe, fnfe);
+        }
+        catch (IOException ioe) {
+            logger.error(ioe, ioe);
+        }
+        catch (Exception e) {
+            logger.error(e, e);
+        }
+        finally {
+            try {
+                if (writer != null) {
+                    writer.close();
+                }
+            }
+            catch (IOException ioe) {
+                logger.debug(ioe, ioe);
+            }
+        }
+    }
+
+
     /**
      * Creates a mapfile with the layer information stored in <i>layers</i>.
      *
--- a/flys-artifacts/src/main/java/de/intevation/flys/wsplgen/FacetCreator.java	Fri Oct 28 05:54:25 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/wsplgen/FacetCreator.java	Fri Oct 28 08:36:32 2011 +0000
@@ -128,7 +128,7 @@
     public void createBarrierFacet() {
         WMSLayerFacet barriers = new WMSLayerFacet(
             1,
-            FLOODMAP_WSPLGEN,
+            FLOODMAP_BARRIERS,
             Resources.getMsg(
                 cc.getMeta(),
                 I18N_BARRIERS,

http://dive4elements.wald.intevation.org