diff flys-artifacts/src/main/java/de/intevation/flys/utils/MapfileGenerator.java @ 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 055f32a5388a
children ef2300b450bf
line wrap: on
line diff
--- 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>.
      *

http://dive4elements.wald.intevation.org