changeset 1792:49ad801076e4

Enabled the MapGenerator to create WMS layers with database datastore - riveraxis is the first layer that is no longer fetched from static WMS service but from user specific one. flys-artifacts/trunk@3115 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Fri, 28 Oct 2011 13:48:02 +0000
parents e0e5a5b51a40
children 1636686070f7
files flys-artifacts/ChangeLog flys-artifacts/doc/conf/artifacts/winfo.xml flys-artifacts/doc/conf/mapserver/db_layer.vm flys-artifacts/doc/conf/mapserver/dbconnection.include flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/DBLayerInfo.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WMSDBLayerFacet.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/RiverAxisState.java flys-artifacts/src/main/java/de/intevation/flys/exports/MapGenerator.java flys-artifacts/src/main/java/de/intevation/flys/utils/FLYSUtils.java flys-artifacts/src/main/java/de/intevation/flys/utils/MapfileGenerator.java flys-artifacts/src/main/java/de/intevation/flys/wsplgen/FacetCreator.java
diffstat 11 files changed, 307 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Fri Oct 28 13:39:37 2011 +0000
+++ b/flys-artifacts/ChangeLog	Fri Oct 28 13:48:02 2011 +0000
@@ -1,3 +1,42 @@
+2011-10-28  Ingo Weinzierl <ingo@intevation.de>
+
+	* doc/conf/artifacts/winfo.xml: Defined "floodmap.riveraxis" and
+	  "floodmap.wmsbackground" as compatible layers for the floodmap output.
+
+	* doc/conf/mapserver/db_layer.vm: New. This layer template is used for
+	  Mapserver layers with database datastore.
+
+	* doc/conf/mapserver/dbconnection.include: New. The database configuration
+	  used in the db_layer template.
+
+	* src/main/java/de/intevation/flys/utils/FLYSUtils.java: Added a method
+	  getUserWMSUrl() that returns the URL to the user specific WMS server.
+	  This method requires a UUID of an Artifact to identify the owner of the
+	  Artifact.
+
+	* src/main/java/de/intevation/flys/wsplgen/FacetCreator.java: Use
+	  FLYSUtils.getUserWMSUrl() to create the URL to the user WMS for
+	  WMSLayerFacets creation.
+
+	* src/main/java/de/intevation/flys/artifacts/model/WMSDBLayerFacet.java:
+	  New. Subclasses WMSLayerFacet to save data and filter parameters used
+	  for database storage in Mapfiles.
+
+	* src/main/java/de/intevation/flys/artifacts/model/DBLayerInfo.java: New.
+	  Subclasses LayerInfo to save database relevant parameters.
+
+	* src/main/java/de/intevation/flys/artifacts/states/RiverAxisState.java:
+	  Creates new WMSDBLayerFacets, so that the riveraxis layer data is
+	  fetched from database.
+
+	* src/main/java/de/intevation/flys/utils/MapfileGenerator.java: Added a
+	  public method that allows creating layers (type LINE) based on
+	  WMSDBLayerFacets.
+
+	* src/main/java/de/intevation/flys/exports/MapGenerator.java: Enabled
+	  support for Facets other than "floodmap.wsplgen" and "floodmap.barriers".
+	  Those other Facets are supposed to be WMSDBLayerFacets.
+
 2011-10-28	Felix Wolfsteller	<felix.wolfsteller@intevation.de>
 
 	Refactored, added StyledSeriesBuilder to unify adding points to
--- a/flys-artifacts/doc/conf/artifacts/winfo.xml	Fri Oct 28 13:39:37 2011 +0000
+++ b/flys-artifacts/doc/conf/artifacts/winfo.xml	Fri Oct 28 13:48:02 2011 +0000
@@ -353,6 +353,8 @@
                     <facets>
                         <facet name="floodmap.wsplgen"/>
                         <facet name="floodmap.barriers"/>
+                        <facet name="floodmap.riveraxis"/>
+                        <facet name="floodmap.wmsbackground"/>
                     </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/db_layer.vm	Fri Oct 28 13:48:02 2011 +0000
@@ -0,0 +1,36 @@
+LAYER
+    NAME "$LAYER.getName()"
+    TYPE $LAYER.getType()
+
+    INCLUDE "$CONFIGDIR/mapserver/dbconnection.include"
+    DATA    "$LAYER.getData()"
+    FILTER  '$LAYER.getFilter()'
+
+    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() )
+        $LAYER.getStyle()
+    #else
+        CLASS
+            NAME ""
+            STYLE
+                SIZE 5
+                OUTLINECOLOR "#000000"
+            END
+        END
+    #end
+END
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/doc/conf/mapserver/dbconnection.include	Fri Oct 28 13:48:02 2011 +0000
@@ -0,0 +1,2 @@
+CONNECTIONTYPE postgis
+CONNECTION "dbname='flys3' host='czech-republic.atlas.intevation.de' port=5432 user='flys' password='flys' sslmode=disable"
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/DBLayerInfo.java	Fri Oct 28 13:48:02 2011 +0000
@@ -0,0 +1,38 @@
+package de.intevation.flys.artifacts.model;
+
+
+public class DBLayerInfo extends LayerInfo {
+
+    protected String filter;
+
+
+    public DBLayerInfo(
+        String name,
+        String type,
+        String filter,
+        String data,
+        String title)
+    {
+        this(name, type, filter, data, title, null, null);
+    }
+
+
+    public DBLayerInfo(
+        String name,
+        String type,
+        String filter,
+        String data,
+        String title,
+        String group,
+        String groupTitle
+    ) {
+        super(name, type, null, data, group, groupTitle, title);
+        this.filter = filter;
+    }
+
+
+    public String getFilter() {
+        return filter;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WMSDBLayerFacet.java	Fri Oct 28 13:48:02 2011 +0000
@@ -0,0 +1,64 @@
+package de.intevation.flys.artifacts.model;
+
+import de.intevation.flys.artifacts.states.DefaultState.ComputeType;
+
+
+public class WMSDBLayerFacet extends WMSLayerFacet {
+
+    protected String data;
+    protected String filter;
+
+
+    public WMSDBLayerFacet() {
+        super();
+    }
+
+
+    public WMSDBLayerFacet(int index, String name, String description) {
+        this(index, name, description, ComputeType.FEED, null, null);
+    }
+
+
+    public WMSDBLayerFacet(
+        int         index,
+        String      name,
+        String      description,
+        ComputeType type,
+        String      stateId,
+        String      hash
+
+    ) {
+        super(index, name, description, type, stateId, hash);
+    }
+
+
+    public WMSDBLayerFacet(
+        int         index,
+        String      name,
+        String      description,
+        ComputeType type,
+        String      stateId,
+        String      hash,
+        String      url
+    ) {
+        super(index, name, description, type, stateId, hash, url);
+    }
+
+
+    public void setFilter(String filter) {
+        this.filter = filter;
+    }
+
+    public String getFilter() {
+        return filter;
+    }
+
+    public void setData(String data) {
+        this.data = data;
+    }
+
+    public String getData() {
+        return data;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/RiverAxisState.java	Fri Oct 28 13:39:37 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/RiverAxisState.java	Fri Oct 28 13:48:02 2011 +0000
@@ -1,7 +1,6 @@
 package de.intevation.flys.artifacts.states;
 
 import java.util.List;
-import java.util.Map;
 
 import org.apache.log4j.Logger;
 
@@ -12,7 +11,7 @@
 
 import de.intevation.flys.artifacts.FLYSArtifact;
 import de.intevation.flys.artifacts.context.FLYSContext;
-import de.intevation.flys.artifacts.model.WMSLayerFacet;
+import de.intevation.flys.artifacts.model.WMSDBLayerFacet;
 import de.intevation.flys.artifacts.resources.Resources;
 import de.intevation.flys.artifacts.states.DefaultState.ComputeType;
 import de.intevation.flys.utils.FLYSUtils;
@@ -23,9 +22,6 @@
 
     public static final String I18N_DESCRIPTION = "floodmap.riveraxis";
 
-    public static final String WMS_LAYER_NAME = "riveraxis";
-
-
 
     private static final Logger logger = Logger.getLogger(RiverAxisState.class);
 
@@ -49,9 +45,6 @@
             flysContext = (FLYSContext) ((CallContext) context).globalContext();
         }
 
-        Map<String, String> wms = (Map<String, String>)
-            flysContext.get(FLYSContext.RIVER_WMS);
-
         String river = artifact.getDataAsString("river");
 
         if(river == null || river.length() == 0) {
@@ -59,10 +52,9 @@
             return null;
         }
 
-        String url = wms.get(river);
+        String url = FLYSUtils.getUserWMSUrl(artifact.identifier());
 
-        // TODO Add config for background layer
-        WMSLayerFacet facet = new WMSLayerFacet(
+        WMSDBLayerFacet facet = new WMSDBLayerFacet(
             0,
             FLOODMAP_RIVERAXIS,
             Resources.getMsg(meta, I18N_DESCRIPTION, I18N_DESCRIPTION),
@@ -70,9 +62,13 @@
             getID(), hash,
             url);
 
-        facet.addLayer(WMS_LAYER_NAME);
+        String name = artifact.identifier() + "-" + FLOODMAP_RIVERAXIS;
+
+        facet.addLayer(name);
         facet.setExtent(GeometryUtils.getRiverBoundary(river));
         facet.setSrid(FLYSUtils.getRiverSrid(artifact));
+        facet.setData("geom FROM river_axes USING UNIQUE id USING SRID 31466");
+        facet.setFilter("river_id=1");
 
         facets.add(facet);
 
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/MapGenerator.java	Fri Oct 28 13:39:37 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/MapGenerator.java	Fri Oct 28 13:48:02 2011 +0000
@@ -1,5 +1,6 @@
 package de.intevation.flys.exports;
 
+import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.OutputStream;
@@ -24,6 +25,7 @@
 
 import de.intevation.flys.artifacts.FLYSArtifact;
 import de.intevation.flys.artifacts.model.FacetTypes;
+import de.intevation.flys.artifacts.model.WMSDBLayerFacet;
 import de.intevation.flys.artifacts.model.WMSLayerFacet;
 import de.intevation.flys.utils.GeometryUtils;
 import de.intevation.flys.utils.MapfileGenerator;
@@ -106,6 +108,9 @@
             else if (FLOODMAP_BARRIERS.equals(name)) {
                 createBarriersLayer(flys, wms);
             }
+            else {
+                createDatabaseLayer(flys, wms);
+            }
         }
         else {
             logger.warn("Facet not supported: " + nativeFacet.getClass());
@@ -139,6 +144,36 @@
     }
 
 
+    protected void createDatabaseLayer(FLYSArtifact flys, WMSLayerFacet wms) {
+        logger.debug("createDatabaseLayer for facet: " + wms.getName());
+
+        MapfileGenerator mfg = MapfileGenerator.getInstance();
+
+        try {
+            File baseDir = mfg.getShapefileBaseDir();
+            File artDir  = new File(baseDir, flys.identifier());
+
+            if (artDir != null && !artDir.exists()) {
+                logger.debug("Create new directory: " + artDir.getPath());
+                artDir.mkdir();
+            }
+
+            if (wms instanceof WMSDBLayerFacet) {
+                mfg.createDatabaseLayer(flys, (WMSDBLayerFacet) wms);
+            }
+            else {
+                logger.warn("Cannot create DB layer from: " + wms.getClass());
+            }
+        }
+        catch (FileNotFoundException fnfe) {
+            logger.error(fnfe, fnfe);
+        }
+        catch (IOException ioe) {
+            logger.error(ioe, ioe);
+        }
+    }
+
+
     @Override
     public void generate()
     throws IOException
--- a/flys-artifacts/src/main/java/de/intevation/flys/utils/FLYSUtils.java	Fri Oct 28 13:39:37 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/utils/FLYSUtils.java	Fri Oct 28 13:48:02 2011 +0000
@@ -343,5 +343,21 @@
         logger.debug("Did not find a named main value for: " + value);
         return null;
     }
+
+
+    /**
+     * Returns the URL of user mapfile for the owner of Artifact
+     * <i>artifactId</i>.
+     *
+     * @param artifactId The UUID of an artifact.
+     *
+     * @return the URL of the user wms.
+     */
+    public static String getUserWMSUrl(String artifactId) {
+        String url = getXPathString(XPATH_MAPSERVER_URL);
+        url = url + "user-wms";
+
+        return url;
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-artifacts/src/main/java/de/intevation/flys/utils/MapfileGenerator.java	Fri Oct 28 13:39:37 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/utils/MapfileGenerator.java	Fri Oct 28 13:48:02 2011 +0000
@@ -5,7 +5,6 @@
 import java.io.FileNotFoundException;
 import java.io.FileWriter;
 import java.io.IOException;
-import java.io.StringWriter;
 import java.io.Writer;
 
 import java.util.ArrayList;
@@ -21,8 +20,10 @@
 import de.intevation.artifacts.common.utils.Config;
 
 import de.intevation.flys.artifacts.FLYSArtifact;
+import de.intevation.flys.artifacts.model.DBLayerInfo;
 import de.intevation.flys.artifacts.model.LayerInfo;
 import de.intevation.flys.artifacts.model.WMSLayerFacet;
+import de.intevation.flys.artifacts.model.WMSDBLayerFacet;
 
 /**
  * This class iterates over a bunch of directories, searches for meta
@@ -39,6 +40,7 @@
     public static final String WSPLGEN_POLYGONS_SHAPE = "barrier_polygons.shp";
 
     public static final String SHP_LAYER_TEMPLATE = "shapefile_layer.vm";
+    public static final String DB_LAYER_TEMPLATE  = "db_layer.vm";
 
     public static final String MS_WSPLGEN_POSTFIX  = "-wsplgen";
     public static final String MS_BARRIERS_POSTFIX = "-barriers";
@@ -220,6 +222,28 @@
     }
 
 
+    protected VelocityContext getVelocityContext() {
+        VelocityContext context = new VelocityContext();
+
+        try {
+            context.put("MAPSERVERURL",
+                FLYSUtils.getXPathString(FLYSUtils.XPATH_MAPSERVER_URL));
+            context.put("SHAPEFILEPATH",
+                getShapefileBaseDir().getCanonicalPath());
+            context.put("CONFIGDIR",
+                Config.getConfigDirectory().getCanonicalPath());
+        }
+        catch (FileNotFoundException fnfe) {
+            // this is bad
+        }
+        catch (IOException ioe) {
+            // this is also bad
+        }
+
+        return context;
+    }
+
+
     /**
      * Returns a template specified by <i>model</i>.
      *
@@ -272,7 +296,7 @@
      * @throws FileNotFoundException if no shapefile path is found or
      * configured.
      */
-    protected File getShapefileBaseDir()
+    public File getShapefileBaseDir()
     throws    FileNotFoundException, IOException
     {
         if (shapefileDirectory == null) {
@@ -436,6 +460,44 @@
 
 
     /**
+     * Creates a layer file used for Mapserver's mapfile which represents
+     * geometries from database.
+     *
+     * @param flys The FLYSArtifact that owns <i>wms</i>.
+     * @param wms The WMSLayerFacet that contains information for the layer.
+     */
+    public void createDatabaseLayer(FLYSArtifact flys, WMSDBLayerFacet wms)
+    throws FileNotFoundException, IOException
+    {
+        logger.debug("createDatabaseLayer");
+
+        LayerInfo layerinfo = new DBLayerInfo(
+            flys.identifier() + "-" + wms.getName(),
+            "LINE",
+            wms.getFilter(),
+            wms.getData(),
+            wms.getDescription());
+
+        String name = MS_LAYER_PREFIX + wms.getName();
+
+        Template template = getTemplateByName(DB_LAYER_TEMPLATE);
+        if (template == null) {
+            logger.warn("Template '" + DB_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 snippet which might be included in the mapfile.
      *
      * @param layerinfo A LayerInfo object that contains all necessary
@@ -465,7 +527,7 @@
         try {
             writer = new FileWriter(layer);
 
-            VelocityContext context = new VelocityContext();
+            VelocityContext context = getVelocityContext();
             context.put("LAYER", layerinfo);
 
             tpl.merge(context, writer);
@@ -512,13 +574,7 @@
 
             writer = new FileWriter(tmp);
 
-            VelocityContext context = new VelocityContext();
-            context.put("MAPSERVERURL",
-                FLYSUtils.getXPathString(FLYSUtils.XPATH_MAPSERVER_URL));
-            context.put("SHAPEFILEPATH",
-                getShapefileBaseDir().getCanonicalPath());
-            context.put("CONFIGDIR",
-                Config.getConfigDirectory().getCanonicalPath());
+            VelocityContext context = getVelocityContext();
             context.put("LAYERS", layers);
 
             Template mapTemplate = getMapfileTemplate();
--- a/flys-artifacts/src/main/java/de/intevation/flys/wsplgen/FacetCreator.java	Fri Oct 28 13:39:37 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/wsplgen/FacetCreator.java	Fri Oct 28 13:48:02 2011 +0000
@@ -59,9 +59,7 @@
     }
 
     protected String getUrl() {
-        String url = FLYSUtils.getXPathString(FLYSUtils.XPATH_MAPSERVER_URL);
-        url = url + "user-wms";
-        return url;
+        return FLYSUtils.getUserWMSUrl(artifact.identifier());
     }
 
     protected String getSrid() {

http://dive4elements.wald.intevation.org