diff src/java/de/intevation/mxd/writer/MapScriptWriter.java @ 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 f11d13940626
children f4eb506499f5
line wrap: on
line diff
--- 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)