changeset 87:7d4cf2db43f1

Added new writer classes to be flexible in creating mapfiles.
author Raimund Renkert <rrenkert@intevation.de>
date Tue, 31 May 2011 17:54:43 +0200
parents e19c5eb43099
children df2eb0e7d229
files ChangeLog src/java/de/intevation/mxd/writer/IWriter.java src/java/de/intevation/mxd/writer/MapScriptWriter.java src/java/de/intevation/mxd/writer/MarkerStyleWriter.java src/java/de/intevation/mxd/writer/SymbolWriter.java
diffstat 5 files changed, 244 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Tue May 31 15:37:19 2011 +0200
+++ b/ChangeLog	Tue May 31 17:54:43 2011 +0200
@@ -1,3 +1,16 @@
+2011-05-31  Raimund Renkert  <raimund.renkert@intevation.de>
+
+	* src/java/de/intevation/mxd/writer/IWriter.java,
+	  src/java/de/intevation/mxd/writer/MapScriptWriter.java:
+	  Use the new classes to write the mapfile. Changed exception
+	  handling(TODO: catch all exceptions in the writer).
+
+	* src/java/de/intevation/mxd/writer/MarkerStyleWriter.java:
+	  New. Write the styles for marker symbols.
+
+	* src/java/de/intevation/mxd/writer/SymbolWriter.java:
+	  New. Write symbols to the mapfile.
+
 2011-05-31  Raimund Renkert  <raimund.renkert@intevation.de>
 
 	* src/java/de/intevation/mxd/Converter.java:
--- a/src/java/de/intevation/mxd/writer/IWriter.java	Tue May 31 15:37:19 2011 +0200
+++ b/src/java/de/intevation/mxd/writer/IWriter.java	Tue May 31 17:54:43 2011 +0200
@@ -13,6 +13,6 @@
     /**
      * Write the content.
      */
-    boolean write(Document doc) throws IOException;
+    boolean write(Document doc) throws Exception;
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/src/java/de/intevation/mxd/writer/MapScriptWriter.java	Tue May 31 15:37:19 2011 +0200
+++ b/src/java/de/intevation/mxd/writer/MapScriptWriter.java	Tue May 31 17:54:43 2011 +0200
@@ -62,7 +62,8 @@
      *
      * @return Currently always true.
      */
-    public boolean write(Document doc) {
+    public boolean write(Document doc)
+    throws Exception {
         logger.debug("write()");
         this.root = doc;
 
@@ -138,7 +139,8 @@
     /**
      * Create layer objects and set the attributes.
      */
-    private void writeLayer() {
+    private void writeLayer()
+    throws Exception {
         logger.debug("writeLayer()");
         Element mapNode = (Element)XMLUtils.xpath(
             root,
@@ -192,7 +194,9 @@
      * @param layer        Mapscript layer object.
      * @param layerElement Dom element containing the class attributes.
      */
-    private void writeClass(layerObj layer, Element layerElement) {
+    private void writeClass(layerObj layer, Element layerElement)
+    throws Exception {
+        logger.debug("writeClass(layerObj, Element)");
         //Get all renderer elements (renderer in arcgis equals class in the
         //mapfile.)
         NodeList list = layerElement.getElementsByTagName("renderer");
@@ -216,7 +220,24 @@
                 co.setExpression(exp);
             }
             //Write symbols and styles.
-            writeSymbol(co, classElement);
+            NodeList slist = classElement.getElementsByTagName("symbol");
+            String type = layerElement.getAttribute("type");
+            if(type.equals("point")) {
+                for(int j = 0; j < slist.getLength(); j++){
+                    Element symbolElement = (Element) slist.item(j);
+                    MarkerStyleWriter swriter = new MarkerStyleWriter (this.map, co);
+                    swriter.write (symbolElement);
+                }
+            }
+            else if(type.equals("line")) {
+                for(int j = 0; j < slist.getLength(); j++){
+                    Element symbolElement = (Element) slist.item(j);
+                    LineStyleWriter swriter = new LineStyleWriter (this.map, co);
+                    swriter.write (symbolElement);
+                }
+            }
+            symbolSetObj symbolSet = map.getSymbolset();
+            saveSymbolSet(symbolSet);
         }
     }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/java/de/intevation/mxd/writer/MarkerStyleWriter.java	Tue May 31 17:54:43 2011 +0200
@@ -0,0 +1,93 @@
+package de.intevation.mxd.writer;
+
+import java.awt.Color;
+
+import org.apache.log4j.Logger;
+
+import org.w3c.dom.Element;
+
+import edu.umn.gis.mapscript.mapObj;
+import edu.umn.gis.mapscript.classObj;
+import edu.umn.gis.mapscript.styleObj;
+import edu.umn.gis.mapscript.colorObj;
+import edu.umn.gis.mapscript.symbolSetObj;
+
+
+/**
+ * The interface to the mapfile writer.
+ *
+ * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a>
+ */
+public class MarkerStyleWriter {
+
+    /**
+     * The Logger.
+     */
+    private static final Logger logger = Logger.getLogger(MarkerStyleWriter.class);
+
+    private mapObj map;
+    private classObj cl;
+    private styleObj style;
+
+    public MarkerStyleWriter (mapObj map, classObj cl) {
+        this.map = map;
+        this.cl = cl;
+        this.style = new styleObj(cl);
+    }
+
+    /**
+     * Write the content.
+     */
+    public boolean write(Element symbolElement)
+    throws Exception {
+        logger.debug("write(Element)");
+        symbolSetObj symbolSet = map.getSymbolset();
+
+        if (symbolElement.hasAttribute("angle")) {
+            style.setAngle(
+                Double.parseDouble(symbolElement.getAttribute("angle")));
+        }
+        if(symbolElement.hasAttribute("color")) {
+            String c = symbolElement.getAttribute("color");
+            Color col = Color.decode(c);
+            colorObj color = new colorObj(
+                col.getRed(),
+                col.getGreen(),
+                col.getBlue(),
+                -4);
+            style.setColor(color);
+        }
+        if (symbolElement.hasAttribute ("size")) {
+            style.setSize(Double.parseDouble(
+                symbolElement.getAttribute("size")));
+        }
+        if(symbolElement.hasAttribute("outline_color")) {
+            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")));
+        }
+
+        String name = symbolElement.getAttribute("name");
+        style.setSymbolByName(map, name);
+
+        String symType = symbolElement.getAttribute("style");
+        if(symType.equals("point") ||
+           symType.equals("arrow") ||
+           symType.equals("char")) {
+           SymbolWriter sw = new SymbolWriter(this.map, this.cl);
+           sw.write(symbolElement);
+        }
+        else {
+            return false;
+        }
+        return true;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/java/de/intevation/mxd/writer/SymbolWriter.java	Tue May 31 17:54:43 2011 +0200
@@ -0,0 +1,112 @@
+package de.intevation.mxd.writer;
+
+import org.apache.log4j.Logger;
+
+import org.w3c.dom.Element;
+
+import edu.umn.gis.mapscript.mapObj;
+import edu.umn.gis.mapscript.classObj;
+import edu.umn.gis.mapscript.styleObj;
+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_SYMBOL_TYPE;
+
+
+/**
+ * The interface to the mapfile writer.
+ *
+ * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a>
+ */
+public class SymbolWriter {
+
+    /**
+     * The Logger.
+     */
+    private static final Logger logger = Logger.getLogger(SymbolWriter.class);
+
+    private mapObj map;
+    private classObj cl;
+    private styleObj style;
+
+    public SymbolWriter (mapObj map, classObj cl) {
+        this.map = map;
+        this.cl = cl;
+        this.style = new styleObj(cl);
+    }
+
+    /**
+     * Write the content.
+     */
+    public boolean write(Element symbolElement)
+    throws Exception {
+        logger.debug("write(Element)");
+        symbolSetObj symbolSet = map.getSymbolset();
+
+        String name = symbolElement.getAttribute("name");
+        symbolObj sym = symbolSet.getSymbolByName(name);
+
+        String symType = symbolElement.getAttribute("style");
+        if(symType.equals("point")) {
+            writeSimple(sym);
+        }
+        else if (symType.equals("arrow")) {
+            writeArrow(sym, symbolElement);
+        }
+        else if (symType.equals("char")) {
+            //TODO Does not work at all...  waiting for fix.
+            writeCharacter(sym, symbolElement);
+        }
+        else {
+            return false;
+        }
+        return true;
+    }
+
+    private void writeSimple(symbolObj symbol) {
+        logger.debug("writeSimple(symbolObj)");
+        lineObj points = new lineObj();
+        points.add(new pointObj(1,1,0));
+        symbol.setType(MS_SYMBOL_TYPE.MS_SYMBOL_ELLIPSE.swigValue());
+        symbol.setPoints(points);
+        symbol.setFilled(1);
+    }
+
+    private void writeArrow(symbolObj symbol, Element symbolElement) {
+        logger.debug("writeArrow(symbolObj, Element)");
+        double len = Double.parseDouble(symbolElement.getAttribute("length"));
+        double width = Double.parseDouble(symbolElement.getAttribute("width"));
+        double ratio = len/width;
+        lineObj points = new lineObj();
+
+        points.add(new pointObj(0, 0, 0));
+        points.add(new pointObj((1*ratio), 0.5, 0));
+        points.add(new pointObj(0, 1, 0));
+        points.add(new pointObj(0, 0, 0));
+        symbol.setType(MS_SYMBOL_TYPE.MS_SYMBOL_VECTOR.swigValue());
+        symbol.setPoints(points);
+        symbol.setFilled(1);
+    }
+
+    private void writeCharacter(symbolObj symbol, Element symbolElement) {
+        logger.debug("writeCharacter(symbolObj, Element)");
+        //TODO Write the symbol correctly. See Issue 3885 on trac.osgeo.org
+        symbol.setFont(symbolElement.getAttribute("font"));
+        symbol.setType(MS_SYMBOL_TYPE.MS_SYMBOL_TRUETYPE.swigValue());
+        symbol.setAntialias(1);
+        symbol.setCharacter("#&" + symbolElement.getAttribute("char") + ";");
+    }
+
+    public boolean canWrite(String type) {
+        if (type.equals("point") ||
+            type.equals("arrow") ||
+            type.equals("char")) {
+            return true;
+        }
+        else {
+            return false;
+        }
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)