changeset 179:f3a91cd7440b

Added a first version of feature labeling.
author raimund renkert <raimund.renkert@intevation.de>
date Fri, 08 Jul 2011 16:32:05 +0200
parents d10d5f560b1a
children f4eb506499f5
files ChangeLog src/java/de/intevation/mxd/reader/FeatureLayerReader.java src/java/de/intevation/mxd/reader/LabelEngineReader.java src/java/de/intevation/mxd/reader/TextSymbolReader.java src/java/de/intevation/mxd/utils/MapToXMLUtils.java src/java/de/intevation/mxd/writer/MapScriptWriter.java
diffstat 6 files changed, 428 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Thu Jul 07 16:33:21 2011 +0200
+++ b/ChangeLog	Fri Jul 08 16:32:05 2011 +0200
@@ -1,3 +1,23 @@
+2011-07-07  Raimund Renkert  <raimund.renkert@intevation.de>
+
+	Added a first version of feature labeling.
+
+	* src/java/de/intevation/mxd/reader/FreatureLayerReader.java:
+	  Read label attributes using the LabaleEngineReader.
+
+	* src/java/de/intevation/mxd/reader/LabelEngineReader.java:
+	  New. Reads Label informations from MXD file.
+
+	* src/java/de/intevation/mxd/reader/TextSymbolReader.java:
+	  New. Reads text symbol attributes. Used to read label text
+	  attributes.
+
+	* src/java/de/intevation/mxd/writer/MapScriptWriter.java:
+	  Add label attributes to the mapfile.
+
+	* src/java/de/intevation/mxd/utils/MapToXMLUtils.java:
+	  Added a method to create label elements.
+
 2011-07-07  Raimund Renkert  <raimund.renkert@intevation.de>
 
 	* build.xml:
--- a/src/java/de/intevation/mxd/reader/FeatureLayerReader.java	Thu Jul 07 16:33:21 2011 +0200
+++ b/src/java/de/intevation/mxd/reader/FeatureLayerReader.java	Fri Jul 08 16:32:05 2011 +0200
@@ -4,6 +4,9 @@
 
 import com.esri.arcgis.carto.ILayer;
 import com.esri.arcgis.carto.FeatureLayer;
+import com.esri.arcgis.carto.AnnotateLayerPropertiesCollection;
+import com.esri.arcgis.carto.IAnnotateLayerProperties;
+import com.esri.arcgis.carto.LabelEngineLayerProperties;
 import com.esri.arcgis.geodatabase.FeatureClassName;
 import com.esri.arcgis.system.IName;
 import com.esri.arcgis.system.IPropertySet;
@@ -135,6 +138,32 @@
             logger.warn(
                 "Could not read definition query.");
         }
+
+        try {
+            AnnotateLayerPropertiesCollection annotation= 
+                (AnnotateLayerPropertiesCollection)layer.getAnnotationProperties();
+        
+            if (layer.isDisplayAnnotation() && annotation.getCount() > 0){
+                for(int i = 0; i < annotation.getCount(); i++) {
+                    IAnnotateLayerProperties prop = annotation.getProperties(0);
+                    if(prop instanceof LabelEngineLayerProperties) {
+                        try {
+                            LabelEngineReader lr = new LabelEngineReader(prop);
+                            lr.setParent(layerElement);
+                            lr.setUtil(util);
+                            lr.read();
+                        }
+                        catch(Exception e) {
+                            logger.warn("Could not read label properties.");
+                        }
+                    }
+                }
+            }
+        }
+        catch(IOException ioe) {
+            logger.warn("Could not read Annotation properties.");
+        }
+
         try {
             IName fcn = layer.getDataSourceName();
             if(fcn instanceof FeatureClassName) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/java/de/intevation/mxd/reader/LabelEngineReader.java	Fri Jul 08 16:32:05 2011 +0200
@@ -0,0 +1,138 @@
+package de.intevation.mxd.reader;
+
+import org.apache.log4j.Logger;
+
+import com.esri.arcgis.carto.ILayer;
+import com.esri.arcgis.carto.FeatureLayer;
+import com.esri.arcgis.carto.AnnotateLayerPropertiesCollection;
+import com.esri.arcgis.carto.IAnnotateLayerProperties;
+import com.esri.arcgis.carto.LabelEngineLayerProperties;
+import com.esri.arcgis.geodatabase.FeatureClassName;
+import com.esri.arcgis.system.IName;
+import com.esri.arcgis.system.IPropertySet;
+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 Label information.
+ *
+ * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a>
+ */
+public class LabelEngineReader {
+
+    /**
+     * The logger.
+     */
+    private static final Logger logger =
+        Logger.getLogger(LabelEngineReader.class);
+
+    /**
+     * Privte member.
+     */
+    private LabelEngineLayerProperties properties;
+    private MapToXMLUtils util;
+    private Element parent;
+
+    public LabelEngineReader(IAnnotateLayerProperties prop)
+    throws Exception {
+        if(prop instanceof LabelEngineLayerProperties) {
+            this.properties = (LabelEngineLayerProperties)prop;
+        }
+        else {
+            throw new Exception("Not an instance of LaberEngineLayerProperties: " +
+                prop.getClass().toString());
+        }
+    }
+
+    /**
+     * Setter for XML document helper.
+     *
+     * @param util The helper for storing map information.
+     */
+    public void setUtil(MapToXMLUtils util) {
+        this.util = util;
+    }
+
+    public void setParent(Element parent) {
+	this.parent = parent;     
+    }
+
+
+
+    /**
+     * Reads the Label content.
+     *
+     * @return The label XML element.
+     */
+    public Element read()
+    throws IOException{
+        logger.debug("read()");
+	Element labelElement;
+        try {
+            labelElement = util.addLabel(parent);
+        }
+        catch(Exception e) {
+            logger.error("Failed to create DOM-Element for Label.");
+            return null;
+        }
+
+	try {
+	    labelElement.setAttribute("expression", properties.getExpression());
+	}
+	catch(IOException ioe) {
+	    logger.warn("Could not read label expression.");
+	    return null;
+	}
+
+        try {
+	    labelElement.setAttribute(
+                "offset",
+	       	String.valueOf(properties.getOffset()));
+	}
+	catch(IOException ioe) {
+	    logger.warn("Could not read label offset.");
+	}
+
+	try {
+	    labelElement.setAttribute(
+                "definition_query",
+	       	properties.getWhereClause());
+	}
+	catch(IOException ioe) {
+	    logger.warn("Could not read label where clause.");
+	}
+
+        try {
+	    labelElement.setAttribute(
+		"max_scale",
+		String.valueOf(properties.getAnnotationMaximumScale()));
+	    labelElement.setAttribute(
+		"min_scale",
+		String.valueOf(properties.getAnnotationMinimumScale()));
+	}
+	catch(IOException ioe) {
+	    logger.warn("Could not read label scale.");
+	}
+
+	try {
+            ITextSymbol sym = properties.getSymbol();
+	    if(sym instanceof TextSymbol) {
+	        TextSymbolReader tsr = new TextSymbolReader(sym);
+		tsr.setParent(labelElement);
+		tsr.setUtil(util);
+		tsr.read();
+	    }
+	}
+        catch(Exception e) {
+	    logger.warn("Could not read label text symbol.");
+	}
+	
+	return labelElement;
+    }
+}
+// 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/reader/TextSymbolReader.java	Fri Jul 08 16:32:05 2011 +0200
@@ -0,0 +1,141 @@
+package de.intevation.mxd.reader;
+
+import java.awt.Color;
+
+import org.apache.log4j.Logger;
+
+import com.esri.arcgis.display.ISymbol;
+import com.esri.arcgis.display.ITextSymbol;
+import com.esri.arcgis.display.TextSymbol;
+import com.esri.arcgis.display.IRgbColor;
+import com.esri.arcgis.display.RgbColor;
+import com.esri.arcgis.support.ms.stdole.Font;
+
+import org.w3c.dom.Element;
+import java.io.IOException;
+
+/**
+ * Reads text symbol information.
+ *
+ * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a>
+ */
+public class TextSymbolReader
+extends AbstractSymbolReader {
+
+    /**
+     * The logger.
+     */
+    private static final Logger logger =
+        Logger.getLogger(TextSymbolReader.class);
+
+    /**
+     * Private member.
+     */
+    private TextSymbol symbol;
+
+    public TextSymbolReader(ITextSymbol symbol)
+    throws Exception {
+        logger.debug("contructor()");
+        if(symbol instanceof TextSymbol) {
+            this.symbol = (TextSymbol)symbol;
+        }
+        else {
+            throw new Exception("Not a TextSymbol!");
+        }
+    }
+
+    /**
+     * Reads the symbol attributes.
+     *
+     * @return The XML node.
+     */
+    public Element read() {
+        logger.debug("read()");
+        Element symbolElement = util.addSymbol(parent);
+
+        try {
+            symbolElement.setAttribute("name", symbol.getNameString());
+        }
+        catch(IOException ioe) {
+            logger.warn("Could not read name. Setting name to \"default\".");
+            symbolElement.setAttribute("name", "default");
+        }
+
+        symbolElement.setAttribute("style", "text");
+
+        try {
+            if(symbol.getColor() instanceof IRgbColor) {
+                IRgbColor color = (IRgbColor)symbol.getColor();
+                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());
+                Color c = new Color (
+                    col.getRed(),
+                    col.getGreen(),
+                    col.getBlue());
+                symbolElement.setAttribute("color", String.valueOf(c.getRGB()));
+                symbolElement.setAttribute("transparency",
+                    String.valueOf(col.getTransparency()));
+            }
+        }
+        catch(IOException ioe) {
+            logger.warn("Could not read color.");
+        }
+
+        try {
+            symbolElement.setAttribute(
+                "size",
+                String.valueOf(symbol.getSize()));
+        }
+        catch(IOException ioe) {
+            logger.warn("Could not read size. Setting size to 1.");
+            symbolElement.setAttribute("size", "1");
+        }
+
+        try {
+           symbolElement.setAttribute(
+                "angle",
+                String.valueOf(symbol.getAngle()));
+        }
+        catch(IOException ioe) {
+            logger.warn("Could not read angle.");
+        }
+
+        try {
+            symbolElement.setAttribute(
+                "offset",
+                symbol.getXOffset() + "," + symbol.getYOffset());
+        }
+        catch(IOException ioe) {
+            logger.warn("Could not read offset.");
+        }
+
+	try {
+            Font f = symbol.getFont();
+            symbolElement.setAttribute("font", f.getName());
+            symbolElement.setAttribute("char_set", String.valueOf(f.getCharset()));
+            symbolElement.setAttribute("bold", String.valueOf(f.getBold()));
+            symbolElement.setAttribute("italic", String.valueOf(f.getItalic()));
+            symbolElement.setAttribute("font_size", String.valueOf(f.getSize()));
+            symbolElement.setAttribute(
+                "strike_through",
+                String.valueOf(f.getStrikethrough()));
+            symbolElement.setAttribute("weight", String.valueOf(f.getWeight()));
+        }
+        catch(IOException ioe) {
+            logger.warn("Could not read font. Setting font to \"FreeSans\".");
+            symbolElement.setAttribute("font", "FreeSans");
+        }
+        symbolElement.setAttribute("type", "text");
+        return symbolElement;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/src/java/de/intevation/mxd/utils/MapToXMLUtils.java	Thu Jul 07 16:33:21 2011 +0200
+++ b/src/java/de/intevation/mxd/utils/MapToXMLUtils.java	Fri Jul 08 16:32:05 2011 +0200
@@ -139,6 +139,17 @@
         renderer.appendChild(node);
         return node;
     }
+    
+    /**
+     * Add a label element to the map.
+     * @param layer The parent layer element.
+     * @return The label element.
+     */
+    public Element addLabel(Element layer) {
+        Element node = creator.create("label");
+        layer.appendChild(node);
+        return node;
+    }
 
     /**
      * Print out the XML document.
--- a/src/java/de/intevation/mxd/writer/MapScriptWriter.java	Thu Jul 07 16:33:21 2011 +0200
+++ b/src/java/de/intevation/mxd/writer/MapScriptWriter.java	Fri Jul 08 16:32:05 2011 +0200
@@ -2,6 +2,8 @@
 
 import org.apache.log4j.Logger;
 
+import java.awt.Color;
+
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.NodeList;
@@ -13,10 +15,16 @@
 import edu.umn.gis.mapscript.mapObj;
 import edu.umn.gis.mapscript.layerObj;
 import edu.umn.gis.mapscript.classObj;
+import edu.umn.gis.mapscript.labelObj;
+import edu.umn.gis.mapscript.colorObj;
+import edu.umn.gis.mapscript.fontSetObj;
+import edu.umn.gis.mapscript.hashTableObj;
 
 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_FONT_TYPE;
+import edu.umn.gis.mapscript.MS_POSITIONS_ENUM;
 
 import de.intevation.mxd.utils.XMLUtils;
 
@@ -276,6 +284,18 @@
 
             }
             layer.setTemplate("PleaseInsertAValidTemplateForGFI");
+
+	    NodeList labels = layerElement.getElementsByTagName("label");
+	    if(labels.getLength() > 0) {
+		Element label = (Element)labels.item(0);
+                String expr = label.getAttribute("expression");
+		expr = expr.replaceAll("\\[", "");
+		expr = expr.replaceAll("\\]", "");
+		if(!prefix.equals("")) {
+		    expr = prefix + "." + expr;
+		}
+		layer.setLabelitem(expr);
+	    }
             //Write classes.
             writeClass(layer, layerElement);
         }
@@ -306,6 +326,13 @@
                 }
             }
             co.setName (name);
+
+       	    NodeList labels = layerElement.getElementsByTagName("label");
+	    if(labels.getLength() > 0) {
+	        Element labelElement = (Element)labels.item(0);
+		String  layerType = layerElement.getAttribute("type");
+		writeLabel(co, labelElement, layerType);
+	    } 
             if(classElement.hasAttribute("field_count")) {
                 co.setExpression(createExpression(classElement, i));
             }
@@ -332,6 +359,68 @@
     }
 
 
+    private void writeLabel(
+	classObj co,
+       	Element labelElement,
+       	String layerType) {
+
+        labelObj label = co.getLabel();
+	Element symbol = (Element)labelElement.getFirstChild();
+	if(symbol != null && symbol.getTagName().equals("symbol")) {
+	    String type = symbol.getAttribute("type");
+	    if(type.equals("text")) {
+	    	label.setType(MS_FONT_TYPE.MS_TRUETYPE);
+	    }
+	    label.setSize(Double.parseDouble(symbol.getAttribute("size")));
+            
+            if(symbol.hasAttribute("color")) {
+                String c = symbol.getAttribute("color");
+                Color col = Color.decode(c);
+                colorObj color = new colorObj(
+                    col.getRed(),
+                    col.getGreen(),
+                    col.getBlue(),
+                    -4);
+                label.setColor(color);
+	    }
+	    if(symbol.hasAttribute("font")) {
+	        String font = symbol.getAttribute("font");
+		fontSetObj fso = this.map.getFontset();
+		hashTableObj fonts = fso.getFonts();
+		String mapFont = fonts.get(font,"");
+		if(mapFont != null && !mapFont.equals("")) {
+		    label.setFont(font);
+		}
+		else {
+		    logger.info(
+			"Could not find font " + font + " in font set. " +
+			"Using FreeSans for labels.");
+                    label.setFont("FreeSans");
+		}
+	    }
+
+            if(labelElement.hasAttribute("max_scale")) {
+		double min =
+		    Double.parseDouble(labelElement.getAttribute("max_scale"));
+		if(min > 0) {
+	            label.setMinscaledenom(min);
+		}
+	    }
+	    if(labelElement.hasAttribute("min_scale")) {
+	        double max =
+		    Double.parseDouble(labelElement.getAttribute("min_scale"));
+		if(max > 0) {
+		    label.setMaxscaledenom(max);
+		}
+	    }
+
+	    if(layerType.equals("point")) {
+	        label.setPosition(MS_POSITIONS_ENUM.MS_UC.swigValue());
+	    }
+	}
+    }
+
+
     private String createExpression(Element ce, int index) {
         String expression = "(";
         int count = 0;
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)