changeset 315:1d77ea6a915d

Add basic support for Graphics Layers. For now this is restricted to simple TextElements and the base layer.
author Andre Heinecke <aheinecke@intevation.de>
date Mon, 17 Sep 2012 17:08:15 +0200
parents cd3cb1a7f35a
children 5e3a40a84539
files ChangeLog src/java/de/intevation/mxd/reader/CompositeGraphicsLayerReader.java src/java/de/intevation/mxd/reader/MXDReader.java src/java/de/intevation/mxd/utils/MapToXMLUtils.java src/java/de/intevation/mxd/writer/MapScriptWriter.java
diffstat 5 files changed, 325 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Mon Sep 17 17:02:15 2012 +0200
+++ b/ChangeLog	Mon Sep 17 17:08:15 2012 +0200
@@ -1,3 +1,20 @@
+2012-09-17  Andre Heinecke <aheinecke@intevation.de>
+
+	Add basic support for Graphics Layer, restricted to TextElements for
+	now.
+
+	* src/java/de/intevation/mxd/reader/CompositeGraphicsLayerReader.java:
+	New Class to read CompositeGraphicsLayer
+
+	* src/java/de/intevation/mxd/utils/MapToXMLUtils.java (addFeature):
+	New. Adds an inline Feature Element
+
+	* src/java/de/intevation/mxd/writer/MapScriptWriter.java: Add Write
+	Support for inline Feature Elements.
+
+	* src/java/de/intevation/mxd/reader/MXDReader.java: Read
+	CompositeGraphicsLayer
+
 2012-09-17  Andre Heinecke <aheinecke@intevation.de>
 
 	* src/java/de/intevation/mxd/writer/MapScriptWriter.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/java/de/intevation/mxd/reader/CompositeGraphicsLayerReader.java	Mon Sep 17 17:08:15 2012 +0200
@@ -0,0 +1,272 @@
+/*
+ * Copyright (c) 2012 by Intevation GmbH, Germany <info@intevation.de>
+ *
+ * This file is part of MXD2map.
+ *
+ * This program is free software under the LGPL (>=v2.1)
+ * Read the file LICENCE.txt coming with the software for details
+ * or visit http://www.gnu.org/licenses/ if it does not exist.
+ *
+ * MXD2map has been developed on behalf of the
+ * Bundesamt fuer Seeschifffahrt und Hydrographie (BSH) in Hamburg
+ * by Intevation GmbH.
+ *
+ * Authors:
+ * Raimund Renkert <raimund.renkert@intevation.de>
+ * Bjoern Schilberg <bjoern.schilberg@intevation.de>
+ * Stephan Holl <stephan.holl@intevation.de>
+ * Andre Heinecke <aheinecke@intevation.de>
+ */
+
+package de.intevation.mxd.reader;
+
+import org.apache.log4j.Logger;
+
+import com.esri.arcgis.carto.IGraphicsLayer;
+import com.esri.arcgis.carto.CompositeGraphicsLayer;
+import com.esri.arcgis.carto.AnnotateLayerPropertiesCollection;
+import com.esri.arcgis.carto.IAnnotateLayerProperties;
+import com.esri.arcgis.carto.LabelEngineLayerProperties;
+import com.esri.arcgis.carto.IElement;
+import com.esri.arcgis.carto.TextElement;
+import com.esri.arcgis.system.IName;
+import com.esri.arcgis.system.IPropertySet;
+import com.esri.arcgis.geometry.Envelope;
+import com.esri.arcgis.geometry.ISpatialReference;
+import com.esri.arcgis.geometry.ProjectedCoordinateSystem;
+import com.esri.arcgis.geometry.GeographicCoordinateSystem;
+import com.esri.arcgis.geometry.UnknownCoordinateSystem;
+import com.esri.arcgis.geometry.Projection;
+import com.esri.arcgis.geometry.IPoint;
+
+import com.esri.arcgis.display.ITextSymbol;
+import com.esri.arcgis.display.TextSymbol;
+
+import org.w3c.dom.Element;
+
+import de.intevation.mxd.utils.MapToXMLUtils;
+import java.io.IOException;
+import com.esri.arcgis.interop.AutomationException;
+/**
+ * Reads Layer information.
+ *
+ * @author <a href="mailto:aheinecke@intevation.de">Andre Heinecke</a>
+ */
+public class CompositeGraphicsLayerReader
+implements ILayerReader {
+
+    /**
+     * The logger.
+     */
+    private static final Logger logger =
+        Logger.getLogger(CompositeGraphicsLayerReader.class);
+
+    /**
+     * Privte member.
+     */
+    private CompositeGraphicsLayer  layer;
+    private MapToXMLUtils util;
+
+    /**
+     * Constructor with layer.
+     *
+     * @param layer The ArcGIS layer object.
+     */
+    public CompositeGraphicsLayerReader(IGraphicsLayer layer)
+    throws Exception {
+        if(layer instanceof CompositeGraphicsLayer) {
+            this.layer = (CompositeGraphicsLayer)layer;
+        }
+        else {
+            throw new Exception("Not an instance of CompositeGraphicsLayer: " +
+                layer.getClass().toString());
+        }
+    }
+
+    /**
+     * Setter for XML document helper.
+     *
+     * @param util The helper for storing map information.
+     */
+    public void setUtil(MapToXMLUtils util) {
+        this.util = util;
+    }
+
+    /**
+     * Reads the Layer content.
+     *
+     * @return The layer XML element.
+     */
+    public Element read()
+    throws IOException{
+        logger.debug("read()");
+        Element layerElement = null;
+        try {
+            layerElement = util.addLayer();
+        }
+        catch(Exception e) {
+            logger.error("Failed to create DOM-Element for Layer.");
+            return null;
+        }
+
+        // Name
+        try {
+            layerElement.setAttribute("name", layer.getName());
+            logger.debug("Adding composite graphics layer: " + layer.getName());
+            logger.debug("Sublayers: " + layer.getCount());
+        }
+        catch(Exception e) {
+            logger.warn(
+                "Could not read layer name." +
+                " Stopped reading layer.");
+            throw new IOException("Error reading layer name.");
+        }
+
+        // Scale
+        try {
+            layerElement.setAttribute("min_scale",
+                String.valueOf(layer.getMinimumScale()));
+        }
+        catch(IOException ioe) {
+            logger.warn("Could not read minimum scale.");
+        }
+
+        try {
+            layerElement.setAttribute("max_scale",
+                String.valueOf(layer.getMaximumScale()));
+        }
+        catch(Exception e) {
+            logger.warn(
+                "Could not read maximum scale.");
+        }
+
+        // Status
+        try {
+            if(layer.isVisible()) {
+                layerElement.setAttribute("status", "on");
+            }
+            else {
+                layerElement.setAttribute("status", "off");
+            }
+        }
+        catch(Exception e) {
+            logger.warn(
+                "Could not read layer status." +
+                " Setting layer status to \"on\".");
+            layerElement.setAttribute("status", "on");
+        }
+
+        // Extent
+        try {
+            Envelope rect = (Envelope)layer.getExtent();
+            layerElement.setAttribute(
+                "extent_min_x",
+                String.valueOf(rect.getXMin ()));
+            layerElement.setAttribute(
+                "extent_max_x",
+                String.valueOf(rect.getXMax()));
+            layerElement.setAttribute(
+                "extent_min_y",
+                String.valueOf(rect.getYMin()));
+            layerElement.setAttribute(
+                "extent_max_y",
+                String.valueOf(rect.getYMax()));
+        }
+        catch(Exception e) {
+            logger.warn(
+                "Could not read extent from layer "
+                + layer.getName() + ".");
+        }
+
+        // Read the elements
+        try {
+            int count = 0;
+            IElement actElement = null;
+            IElement prevElement = null;
+            layer.reset(); // Reset the element container
+            actElement = layer.next();
+            while (actElement != prevElement) {
+                prevElement = actElement;
+                if (actElement instanceof TextElement) {
+                    TextElement te = (TextElement)actElement;
+                    Element xmlTextElement = util.addFeature(layerElement);
+                    xmlTextElement.setAttribute("text", te.getText());
+
+                    IPoint poi = te.getGeometry().getEnvelope().getLowerLeft();
+                    xmlTextElement.setAttribute("X", String.valueOf(poi.getX()));
+                    xmlTextElement.setAttribute("Y", String.valueOf(poi.getY()));
+
+                    logger.debug("Text: " + te.getText());
+                    logger.debug(" X,Y: " + poi.getX() + ", " + poi.getY());
+                    try {
+                        ITextSymbol sym = te.getSymbol();
+                        if(sym instanceof TextSymbol) {
+                            TextSymbolReader tsr = new TextSymbolReader(sym);
+                            tsr.setParent(xmlTextElement);
+                            tsr.setUtil(util);
+                            tsr.read();
+                        } else {
+                            logger.warn("Unknwon Symbol of class: " +
+                                    sym.getClass().toString());
+                        }
+                    }
+                    catch(Exception e) {
+                        logger.warn("Could not read element text symbol.");
+                    }
+                } else {
+                    logger.warn("Unhandled Element of class: " +
+                            actElement.getClass().toString() + 
+                            " in conversion of layer: " +
+                            layer.getName());
+                }
+                count++;
+            }
+            logger.debug("Read " + count + " Elements");
+        }
+        catch(Exception e) {
+            logger.warn("Could not read layer elements.");
+            logger.debug(e);
+        }
+
+
+        //Read the projection.
+        try {
+            ISpatialReference sr = layer.getSpatialReference();
+            int projection = 0;
+            if(sr instanceof ProjectedCoordinateSystem) {
+                ProjectedCoordinateSystem pcs = (ProjectedCoordinateSystem)sr;
+                projection = pcs.getFactoryCode();
+            }
+            else if(sr instanceof GeographicCoordinateSystem) {
+                GeographicCoordinateSystem gcs = (GeographicCoordinateSystem)sr;
+                projection = gcs.getFactoryCode();
+            }
+            else if(sr instanceof UnknownCoordinateSystem) {
+                UnknownCoordinateSystem ucs = (UnknownCoordinateSystem)sr;
+                projection = 0;
+            }
+            else{
+                logger.debug(
+                    "Unknown SpatialReference: " +
+                    sr.getClass().toString());
+            }
+
+            if(projection == 0) {
+                logger.warn(
+                    "Unknown projection for Layer:" + layer.getName() +
+                    " Please edit projection in resulting mapfile.");
+            }
+            layerElement.setAttribute("projection", String.valueOf(projection));
+        }
+        catch(Exception e) {
+            logger.warn("Could not read layer projection.");
+            logger.debug(e);
+        }
+
+        // Static values for this layer
+        layerElement.setAttribute("type", "point");
+
+        return layerElement;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/src/java/de/intevation/mxd/reader/MXDReader.java	Mon Sep 17 17:02:15 2012 +0200
+++ b/src/java/de/intevation/mxd/reader/MXDReader.java	Mon Sep 17 17:08:15 2012 +0200
@@ -27,6 +27,7 @@
 import com.esri.arcgis.carto.IMap;
 import com.esri.arcgis.carto.MapDocument;
 import com.esri.arcgis.carto.FeatureLayer;
+import com.esri.arcgis.carto.CompositeGraphicsLayer;
 import com.esri.arcgis.carto.GdbRasterCatalogLayer;
 import com.esri.arcgis.carto.IFeatureRenderer;
 import com.esri.arcgis.carto.SimpleRenderer;
@@ -37,6 +38,8 @@
 import com.esri.arcgis.carto.WMSGroupLayer;
 import com.esri.arcgis.carto.WMSLayer;
 import com.esri.arcgis.carto.RasterLayer;
+import com.esri.arcgis.carto.IGraphicsLayerProxy;
+import com.esri.arcgis.carto.IGraphicsLayer;
 
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
@@ -151,6 +154,21 @@
         catch(Exception e) {
             throw e;
         }
+        try {
+            IGraphicsLayer grLayer = (IGraphicsLayer) map.getActiveGraphicsLayer();
+            if (map.getActiveGraphicsLayer() instanceof CompositeGraphicsLayer) {
+                CompositeGraphicsLayerReader lr = new CompositeGraphicsLayerReader(grLayer);
+                lr.setUtil(util);
+                Element graphicsLayers = lr.read();
+            } else {
+                logger.debug("Not a known graphics layer type: " +
+                             grLayer.getClass().toString());
+            }
+        }
+        catch(Exception e) {
+            logger.debug("Error reading graphics layer");
+            logger.debug(e);
+        }
 
         for(int i = 0; i < map.getLayerCount();i++) {
             ILayer layer = map.getLayer(i);
--- a/src/java/de/intevation/mxd/utils/MapToXMLUtils.java	Mon Sep 17 17:02:15 2012 +0200
+++ b/src/java/de/intevation/mxd/utils/MapToXMLUtils.java	Mon Sep 17 17:08:15 2012 +0200
@@ -164,6 +164,17 @@
     }
 
     /**
+     * Add a arbitray inline feature to the map.
+     * @param layer The parent layer element.
+     * @return The feature element.
+     */
+    public Element addFeature(Element layer) {
+        Element node = creator.create("feature");
+        layer.appendChild(node);
+        return node;
+    }
+
+    /**
      * Print out the XML document.
      */
     public void print() {
--- a/src/java/de/intevation/mxd/writer/MapScriptWriter.java	Mon Sep 17 17:02:15 2012 +0200
+++ b/src/java/de/intevation/mxd/writer/MapScriptWriter.java	Mon Sep 17 17:08:15 2012 +0200
@@ -38,10 +38,14 @@
 import edu.umn.gis.mapscript.colorObj;
 import edu.umn.gis.mapscript.fontSetObj;
 import edu.umn.gis.mapscript.hashTableObj;
+import edu.umn.gis.mapscript.shapeObj;
+import edu.umn.gis.mapscript.pointObj;
+import edu.umn.gis.mapscript.lineObj;
 
 import edu.umn.gis.mapscript.MS_UNITS;
 import edu.umn.gis.mapscript.MS_LAYER_TYPE;
 import edu.umn.gis.mapscript.MS_CONNECTION_TYPE;
+import edu.umn.gis.mapscript.MS_SHAPE_TYPE;
 import edu.umn.gis.mapscript.MS_FONT_TYPE;
 import edu.umn.gis.mapscript.MS_POSITIONS_ENUM;
 
@@ -570,6 +574,9 @@
                 layer.setLabelitem(expr.trim());
             }
 
+            //Write elements.
+            writeFeatures(layer, layerElement);
+
             //Write classes.
             writeClass(layer, layerElement);
         }
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)