changeset 42:395307e8b7ee

First MapScript Writer that generates valid mapfiles.
author Raimund Renkert <rrenkert@intevation.de>
date Fri, 15 Apr 2011 14:14:49 +0200
parents 60ed2164035a
children ef7ca23c4233
files ChangeLog src/java/de/intevation/mxd/reader/FeatureLayerReader.java src/java/de/intevation/mxd/reader/SimpleMarkerSymbolReader.java src/java/de/intevation/mxd/writer/MapScriptWriter.java
diffstat 4 files changed, 197 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Thu Apr 14 11:31:29 2011 +0200
+++ b/ChangeLog	Fri Apr 15 14:14:49 2011 +0200
@@ -1,3 +1,15 @@
+2011-04-15  Raimund Renkert <raimund.renkert@intevation.de>
+
+	* src/java/de/intevation/mxd/reader/FeatureLayerReader.java:
+	  Read the data source name for layer objects.
+
+	* src/java/de/intevation/mxd/reader/SimpleMarkerSymbolReader.java:
+	  Changed color processing.
+
+	* src/java/de/intevation/mxd/writer/MapScriptWriter.java:
+	  First implementation that generates valid mapfiles for simple point
+	  mxdfiles.
+
 2011-04-14  Raimund Renkert <raimund.renkert@intevation.de>
 
 	Introduced MapScript writer.
--- a/src/java/de/intevation/mxd/reader/FeatureLayerReader.java	Thu Apr 14 11:31:29 2011 +0200
+++ b/src/java/de/intevation/mxd/reader/FeatureLayerReader.java	Fri Apr 15 14:14:49 2011 +0200
@@ -6,6 +6,8 @@
 
 import com.esri.arcgis.carto.ILayer;
 import com.esri.arcgis.carto.FeatureLayer;
+import com.esri.arcgis.geodatabase.FeatureClassName;
+import com.esri.arcgis.system.IName;
 
 import org.w3c.dom.Element;
 
@@ -86,6 +88,11 @@
 
         layerElement.setAttribute("definition_query",
             layer.getDefinitionExpression());
+        IName fcn = layer.getDataSourceName();
+        if(fcn instanceof FeatureClassName) {
+            FeatureClassName name = (FeatureClassName)fcn;
+            layerElement.setAttribute("data_source", name.getName());
+        }
 
         return layerElement;
     }
--- a/src/java/de/intevation/mxd/reader/SimpleMarkerSymbolReader.java	Thu Apr 14 11:31:29 2011 +0200
+++ b/src/java/de/intevation/mxd/reader/SimpleMarkerSymbolReader.java	Fri Apr 15 14:14:49 2011 +0200
@@ -2,6 +2,8 @@
 
 import java.io.IOException;
 
+import java.awt.Color;
+
 import org.apache.log4j.Logger;
 
 import com.esri.arcgis.display.ISymbol;
@@ -11,7 +13,6 @@
 import com.esri.arcgis.display.RgbColor;
 
 import org.w3c.dom.Element;
-
 import de.intevation.mxd.utils.MapToXMLUtils;
 
 /**
@@ -84,18 +85,22 @@
 
         if(symbol.getColor() instanceof IRgbColor) {
             IRgbColor color = (IRgbColor)symbol.getColor();
-            symbolElement.setAttribute("color",  "(" + color.getRed() +
-                "," + color.getGreen() +
-                "," + color.getBlue() + ")");
+            Color c = new Color (
+                color.getRed(),
+                color.getGreen(),
+                color.getBlue());
+            symbolElement.setAttribute("color", String.valueOf(c.getRGB()));
             symbolElement.setAttribute("transparency",
                 String.valueOf(color.getTransparency()));
         }
         else {
             RgbColor col = new RgbColor();
             col.setRGB(symbol.getColor().getRGB());
-            symbolElement.setAttribute("color",  "(" + col.getRed() +
-                "," + col.getGreen() +
-                "," + col.getBlue() + ")");
+            Color c = new Color (
+                col.getRed(),
+                col.getGreen(),
+                col.getBlue());
+            symbolElement.setAttribute("color", String.valueOf(c.getRGB()));
             symbolElement.setAttribute("transparency",
                 String.valueOf(col.getTransparency()));
         }
@@ -105,18 +110,26 @@
             String.valueOf(symbol.getOutlineSize()));
         if(symbol.getOutlineColor() instanceof IRgbColor) {
             IRgbColor color = (IRgbColor)symbol.getOutlineColor();
-            symbolElement.setAttribute("outline_color",  "(" + color.getRed() +
-                "," + color.getGreen() +
-                "," + color.getBlue() + ")");
+            Color c = new Color (
+                color.getRed(),
+                color.getGreen(),
+                color.getBlue());
+            symbolElement.setAttribute(
+                    "outline_color",
+                    String.valueOf(c.getRGB()));
             symbolElement.setAttribute("outline_transparency",
                 String.valueOf(color.getTransparency()));
         }
         else {
             RgbColor col = new RgbColor();
             col.setRGB(symbol.getOutlineColor().getRGB());
-            symbolElement.setAttribute("outline_color",  "(" + col.getRed() +
-                "," + col.getGreen() +
-                "," + col.getBlue() + ")");
+            Color c = new Color (
+                col.getRed(),
+                col.getGreen(),
+                col.getBlue());
+            symbolElement.setAttribute(
+                "outline_color",
+                String.valueOf(c.getRGB()));
             symbolElement.setAttribute("outline_transparency",
                 String.valueOf(col.getTransparency()));
         }
--- a/src/java/de/intevation/mxd/writer/MapScriptWriter.java	Thu Apr 14 11:31:29 2011 +0200
+++ b/src/java/de/intevation/mxd/writer/MapScriptWriter.java	Fri Apr 15 14:14:49 2011 +0200
@@ -4,13 +4,24 @@
 
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
-import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
+
 import javax.xml.xpath.XPathConstants;
+import java.awt.Color;
 
 import edu.umn.gis.mapscript.mapObj;
 import edu.umn.gis.mapscript.layerObj;
+import edu.umn.gis.mapscript.classObj;
+import edu.umn.gis.mapscript.styleObj;
+import edu.umn.gis.mapscript.colorObj;
+import edu.umn.gis.mapscript.symbolObj;
+import edu.umn.gis.mapscript.symbolSetObj;
+import edu.umn.gis.mapscript.lineObj;
+import edu.umn.gis.mapscript.pointObj;
+
 import edu.umn.gis.mapscript.MS_UNITS;
+import edu.umn.gis.mapscript.MS_LAYER_TYPE;
+import edu.umn.gis.mapscript.MS_SYMBOL_TYPE;
 
 import de.intevation.mxd.utils.XMLUtils;
 
@@ -50,9 +61,11 @@
         }
         //Write the map attributes.
         writeMap();
-
+        //Write the layers.
+        writeLayer();
         //Save the map.
-        map.save(filename);
+        mapObj cloneMap = map.cloneMap();
+        cloneMap.save(filename);
         return true;
     }
 
@@ -64,9 +77,15 @@
             "/mxd/map",
             XPathConstants.NODE);
 
+        //TODO Get the following values from a template.
+        map.setShapepath("/home/intevation/mxd-testbed/testdata-frida");
+        map.setHeight(700);
+        map.setWidth(800);
+        map.setProjection("EPSG:4326");
+
         //Set the name.
         map.setName(mapNode.getAttribute("name"));
-
+        map.setMetaData("wms_title", mapNode.getAttribute("name"));
         //Set the extent.
         map.setExtent(
             Double.parseDouble(mapNode.getAttribute("extent_min_x")),
@@ -108,16 +127,143 @@
     }
 
     private void writeLayer() {
+        logger.debug("writeLayer()");
+        Element mapNode = (Element)XMLUtils.xpath(
+            root,
+            "/mxd/map",
+            XPathConstants.NODE);
+        NodeList list = mapNode.getElementsByTagName("layer");
+        for(int i = 0; i < list.getLength(); i++) {
+            Element layerElement = (Element)list.item(i);
+            layerObj layer = new layerObj(map);
+
+            //The layer name.
+            layer.setName(layerElement.getAttribute("name"));
+
+            //The layer status.
+            String stat = layerElement.getAttribute("status");
+            if (stat.equals("on")) {
+                layer.setStatus(1);
+            }
+            else {
+                layer.setStatus(0);
+            }
+
+            //The scale.
+            double maxScale =
+                Double.parseDouble(layerElement.getAttribute("max_scale"));
+            double minScale =
+                Double.parseDouble(layerElement.getAttribute("min_scale"));
+            layer.setMaxscaledenom(maxScale);
+            layer.setMinscaledenom(minScale);
+
+            //The layer type.
+            String type = layerElement.getAttribute("type");
+            if(type.equals("point")) {
+                layer.setType(MS_LAYER_TYPE.MS_LAYER_POINT);
+            }
+
+            //The layer datasource.
+            layer.setData(layerElement.getAttribute("data_source"));
+
+            //Write classes.
+            writeClass(layer, layerElement);
+        }
 
     }
 
-    private void writeClass() {
+    /**
+     * Adds the classes to the layer.
+     * @param layer        Mapscript layer object.
+     * @param layerElement Dom element containing the class attributes.
+     */
+    private void writeClass(layerObj layer, Element layerElement) {
+        //Get all renderer elements (renderer in arcgis equals class in the
+        //mapfile.)
+        NodeList list = layerElement.getElementsByTagName("renderer");
 
+        //Create all found class objects and write the symbols and styles for
+        //each class.
+        for(int i = 0; i < list.getLength(); i++) {
+            Element classElement = (Element)list.item(i);
+            classObj co = new classObj(layer);
+            co.setName(classElement.getAttribute("name"));
+            //Write symbols and styles.
+            writeSymbol(co, classElement);
+        }
     }
 
-    private void writeSymbol() {
+    /**
+     * Adds teh symbols and styles to the mapfile.
+     * @param co           Mapscript class object.
+     * @param classElement Dom element containing the style and symbol
+     *                     attributes.
+     */
+    private void writeSymbol(classObj co, Element classElement) {
+        //Get all symbol elements from dom.
+        NodeList list = classElement.getElementsByTagName("symbol");
 
+        //Get the symbol set from the map object.
+        symbolSetObj symbolSet = map.getSymbolset();
+
+        //
+        for(int i = 0; i < list.getLength(); i++){
+            Element symbolElement = (Element) list.item(i);
+            styleObj style = new styleObj(co);
+            style.setAngle(
+                Double.parseDouble(symbolElement.getAttribute("angle")));
+            String c = symbolElement.getAttribute("color");
+            Color col = Color.decode(c);
+            colorObj color = new colorObj(
+                col.getRed(),
+                col.getGreen(),
+                col.getBlue(),
+                -4);
+            style.setColor(color);
+            style.setSize(Double.parseDouble(symbolElement.getAttribute("size")));
+            Color oCol = Color.decode(
+                symbolElement.getAttribute("outline_color"));
+            colorObj outlineColor = new colorObj(
+                oCol.getRed(),
+                oCol.getGreen(),
+                oCol.getBlue(),
+                -4);
+            style.setOutlinecolor(outlineColor);
+            style.setOutlinewidth(
+                    Double.parseDouble(symbolElement.getAttribute("outline_size")));
+            style.setSymbolByName(map, symbolElement.getAttribute("name"));
+
+            symbolObj sym = symbolSet.getSymbolByName(symbolElement.getAttribute("name"));
+            String symType = symbolElement.getAttribute("style");
+            if(symType.equals("point")) {
+                lineObj points = new lineObj();
+                points.add(new pointObj(1,1,0));
+                sym.setType(MS_SYMBOL_TYPE.MS_SYMBOL_ELLIPSE.swigValue());
+                sym.setPoints(points);
+                sym.setFilled(1);
+            }
+        }
+        saveSymbolSet(symbolSet);
     }
 
+    /**
+     * Save the symbol set.
+     * @param 
+     */
+    private void saveSymbolSet(symbolSetObj symbols) {
+        Element fileNode = (Element)XMLUtils.xpath(
+            root,
+            "/mxd/file",
+            XPathConstants.NODE);
+        String filename = fileNode.getAttribute("name");
+        String path = "";
+        if(filename.contains("/")) {
+            path = filename.substring(0, filename.lastIndexOf("/"));
+        }
+        else if(filename.contains("\\")) {
+            path = filename.substring(0, filename.lastIndexOf("\\"));
+        }
+        symbols.save(path + "/symbols.sym");
+    }
 }
 
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)