diff flys-artifacts/src/main/java/de/intevation/flys/utils/MapfileGenerator.java @ 2639:a78dafdd8590

Issue 657. Added file upload service to load a user defined shapefile. User the shapefile for wsplgen calculation and display a new layer. flys-artifacts/trunk@4292 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Raimund Renkert <raimund.renkert@intevation.de>
date Wed, 25 Apr 2012 07:45:00 +0000
parents ccad7a6ddaa4
children 7a124cb11d8e
line wrap: on
line diff
--- a/flys-artifacts/src/main/java/de/intevation/flys/utils/MapfileGenerator.java	Thu Apr 19 11:49:57 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/utils/MapfileGenerator.java	Wed Apr 25 07:45:00 2012 +0000
@@ -13,6 +13,10 @@
 
 import org.apache.log4j.Logger;
 
+import org.geotools.data.shapefile.ShpFiles;
+import org.geotools.data.shapefile.shp.ShapefileReader;
+import org.geotools.data.shapefile.shp.ShapefileHeader;
+
 import org.apache.velocity.Template;
 import org.apache.velocity.VelocityContext;
 import org.apache.velocity.app.VelocityEngine;
@@ -40,16 +44,18 @@
     public static final String WSPLGEN_RESULT_SHAPE   = "wsplgen.shp";
     public static final String WSPLGEN_LINES_SHAPE    = "barrier_lines.shp";
     public static final String WSPLGEN_POLYGONS_SHAPE = "barrier_polygons.shp";
+    public static final String WSPLGEN_USER_SHAPE     = "user-rgd.shp";
 
     public static final String WSPLGEN_LAYER_TEMPLATE = "wsplgen_layer.vm";
     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_PREFIX  = "wsplgen-";
-    public static final String MS_BARRIERS_PREFIX = "barriers-";
-    public static final String MS_LINE_PREFIX     = "lines-";
-    public static final String MS_POLYGONS_PREFIX = "polygons-";
-    public static final String MS_LAYER_PREFIX    = "ms_layer-";
+    public static final String MS_WSPLGEN_PREFIX   = "wsplgen-";
+    public static final String MS_BARRIERS_PREFIX  = "barriers-";
+    public static final String MS_LINE_PREFIX      = "lines-";
+    public static final String MS_POLYGONS_PREFIX  = "polygons-";
+    public static final String MS_LAYER_PREFIX     = "ms_layer-";
+    public static final String MS_USERSHAPE_PREFIX = "user-";
 
     protected static final long SLEEPTIME = 10 * 1000L; // 10 seconds
 
@@ -517,6 +523,71 @@
 
 
     /**
+     * Creates a layer file used for Mapserver's mapfile which represents the
+     * shape files uploaded by the user.
+     *
+     * @param flys The FLYSArtifact that owns <i>wms</i>.
+     * @param wms The WMSLayerFacet that contains information for the layer.
+     */
+    public void createUserShapeLayer(FLYSArtifact flys, WMSLayerFacet wms)
+    throws FileNotFoundException, IOException
+    {
+        logger.debug("createUserShapeLayer");
+
+        String uuid = flys.identifier();
+        File   dir  = new File(getShapefileBaseDir(), uuid);
+        File   test = new File(dir, WSPLGEN_USER_SHAPE);
+
+        if (!test.exists() || !test.canRead()) {
+            logger.debug("No user layer existing.");
+            return;
+        }
+
+        File userShape = new File(dir, WSPLGEN_USER_SHAPE);
+        ShpFiles sf = new ShpFiles(userShape);
+        ShapefileReader sfr = new ShapefileReader(sf, true, false, null);
+        ShapefileHeader sfh = sfr.getHeader();
+
+        String group      = uuid + MS_USERSHAPE_PREFIX;
+        String groupTitle = "I18N_USER_SHAPE_TITLE";
+
+        LayerInfo info = new LayerInfo();
+        info.setName(MS_USERSHAPE_PREFIX + uuid);
+        if (sfh.getShapeType().isLineType()) {
+            info.setType("LINE");
+        }
+        else if (sfh.getShapeType().isPolygonType()) {
+            info.setType("POLYGON");
+        }
+        else {
+            return;
+        }
+        info.setDirectory(uuid);
+        info.setData(WSPLGEN_USER_SHAPE);
+        info.setTitle("I18N_USER_SHAPE");
+        info.setGroup(group);
+        info.setGroupTitle(groupTitle);
+
+        String nameUser = MS_LAYER_PREFIX + wms.getName();
+
+        Template tpl = getTemplateByName(SHP_LAYER_TEMPLATE);
+        if (tpl == null) {
+            logger.warn("Template '" + SHP_LAYER_TEMPLATE + "' found.");
+            return;
+        }
+
+        try {
+            writeLayer(info, dir, nameUser, tpl);
+        }
+        catch (FileNotFoundException fnfe) {
+            logger.error(fnfe, fnfe);
+            logger.warn("Unable to write layer: " + nameUser);
+        }
+
+    }
+
+
+    /**
      * Creates a layer file used for Mapserver's mapfile which represents
      * geometries from database.
      *

http://dive4elements.wald.intevation.org