aheinecke@317: /* aheinecke@317: * Copyright (c) 2012 by Intevation GmbH, Germany aheinecke@317: * aheinecke@317: * This file is part of MXD2map. aheinecke@317: * aheinecke@317: * This program is free software under the LGPL (>=v2.1) aheinecke@317: * Read the file LICENCE.txt coming with the software for details aheinecke@317: * or visit http://www.gnu.org/licenses/ if it does not exist. aheinecke@317: * aheinecke@317: * MXD2map has been developed on behalf of the aheinecke@317: * Bundesamt fuer Seeschifffahrt und Hydrographie (BSH) in Hamburg aheinecke@317: * by Intevation GmbH. aheinecke@317: * aheinecke@317: * Authors: aheinecke@317: * Raimund Renkert aheinecke@317: * Bjoern Schilberg aheinecke@317: * Stephan Holl aheinecke@317: */ aheinecke@317: aheinecke@317: package de.intevation.mxd.writer; aheinecke@317: aheinecke@317: import java.awt.Color; aheinecke@317: aheinecke@317: import org.apache.log4j.Logger; aheinecke@317: aheinecke@317: import org.w3c.dom.Element; aheinecke@317: aheinecke@317: import edu.umn.gis.mapscript.mapObj; aheinecke@317: import edu.umn.gis.mapscript.classObj; aheinecke@317: import edu.umn.gis.mapscript.styleObj; aheinecke@317: import edu.umn.gis.mapscript.colorObj; aheinecke@317: import edu.umn.gis.mapscript.labelObj; aheinecke@317: import edu.umn.gis.mapscript.symbolSetObj; aheinecke@317: aheinecke@317: aheinecke@317: /** aheinecke@317: * The interface to the mapfile writer. aheinecke@317: * aheinecke@317: * @author Andre Heinecke aheinecke@317: */ aheinecke@317: public class LabelStyleWriter { aheinecke@317: aheinecke@317: /** aheinecke@317: * The Logger. aheinecke@317: */ aheinecke@317: private static final Logger logger = Logger.getLogger(LabelStyleWriter.class); aheinecke@317: aheinecke@317: /** aheinecke@317: * Private member. aheinecke@317: */ aheinecke@317: private mapObj map; aheinecke@317: private classObj cl; aheinecke@317: private styleObj style; aheinecke@317: aheinecke@317: /** aheinecke@317: * Contructor with map object and class object. aheinecke@317: * aheinecke@317: * @param map The map object. aheinecke@317: * @param cl The class object containing the style. aheinecke@317: */ aheinecke@317: public LabelStyleWriter(mapObj map, classObj cl) { aheinecke@317: this.map = map; aheinecke@317: this.cl = cl; aheinecke@317: this.style = new styleObj(cl); aheinecke@317: } aheinecke@317: aheinecke@317: /** aheinecke@317: * Write the content. aheinecke@317: * aheinecke@317: * @param symbolElement DOM element containing style attributes. aheinecke@317: */ aheinecke@317: public boolean write(Element symbolElement) { aheinecke@317: logger.debug("write(Element)"); aheinecke@317: aheinecke@317: cl.getLabel().insertStyle(style, -1); aheinecke@317: aheinecke@317: symbolSetObj symbolSet = map.getSymbolset(); aheinecke@317: aheinecke@317: if (symbolElement.hasAttribute("angle")) { aheinecke@317: try { aheinecke@317: style.setAngle( aheinecke@317: Double.parseDouble(symbolElement.getAttribute("angle"))); aheinecke@317: } aheinecke@317: catch(NumberFormatException nfe) { aheinecke@317: logger.warn("Error setting angle."); aheinecke@317: style.setAngle(0.0); aheinecke@317: } aheinecke@317: } aheinecke@317: if(symbolElement.hasAttribute("angleBinding")) { aheinecke@317: style.setBinding(2, aheinecke@317: symbolElement.getAttribute("angleBinding")); aheinecke@317: } aheinecke@317: if (symbolElement.hasAttribute("bg_color")) { aheinecke@317: // Background color takes precedence over the color as aheinecke@317: // background color can be archived by geomtransform aheinecke@317: String c = symbolElement.getAttribute("bg_color"); aheinecke@317: Color col = Color.decode(c); aheinecke@317: colorObj color = new colorObj( aheinecke@317: col.getRed(), aheinecke@317: col.getGreen(), aheinecke@317: col.getBlue(), aheinecke@317: -4); aheinecke@317: style.setColor(color); aheinecke@321: style.setGeomTransform("labelpoly"); aheinecke@317: } aheinecke@317: if (symbolElement.hasAttribute ("size")) { aheinecke@317: try { aheinecke@317: style.setSize(Double.parseDouble( aheinecke@317: symbolElement.getAttribute("size"))); aheinecke@317: } aheinecke@317: catch(NumberFormatException nfe) { aheinecke@317: logger.warn("Error setting size. Setting to deafult: 1."); aheinecke@317: style.setSize(1); aheinecke@317: } aheinecke@317: } aheinecke@317: if(symbolElement.hasAttribute("outline_color")) { aheinecke@317: Color oCol = Color.decode( aheinecke@317: symbolElement.getAttribute("outline_color")); aheinecke@317: colorObj outlineColor = new colorObj( aheinecke@317: oCol.getRed(), aheinecke@317: oCol.getGreen(), aheinecke@317: oCol.getBlue(), aheinecke@317: -4); aheinecke@317: style.setOutlinecolor(outlineColor); aheinecke@317: try { aheinecke@317: style.setOutlinewidth(Double.parseDouble( aheinecke@317: symbolElement.getAttribute("outline_size"))); aheinecke@317: } aheinecke@317: catch(NumberFormatException nfe) { aheinecke@317: logger.warn("Error setting outline width."); aheinecke@317: } aheinecke@317: } aheinecke@317: aheinecke@317: if (symbolElement.hasAttribute("x_offset")) { aheinecke@317: try { aheinecke@317: double val = aheinecke@317: Double.parseDouble(symbolElement.getAttribute("x_offset")); aheinecke@317: double r = Math.round(val); aheinecke@317: style.setOffsetx(r); aheinecke@317: } aheinecke@317: catch (NumberFormatException nfe) { aheinecke@317: logger.warn("Error setting the symbol x-offset"); aheinecke@317: } aheinecke@317: } aheinecke@317: if (symbolElement.hasAttribute("y_offset")) { aheinecke@317: try { aheinecke@317: double val = aheinecke@317: Double.parseDouble(symbolElement.getAttribute("y_offset")); aheinecke@317: double r = Math.round(val); aheinecke@317: //In ArcGIS positive y offset values move the symbol upwards, aheinecke@317: //in Mapserver positive values move downwards. aheinecke@317: r = -r; aheinecke@317: style.setOffsety(r); aheinecke@317: } aheinecke@317: catch (NumberFormatException nfe) { aheinecke@317: logger.warn("Error setting the symbol y-offset."); aheinecke@317: } aheinecke@317: } aheinecke@317: String symType = symbolElement.getAttribute("style"); aheinecke@317: if(symType.equals("point") || aheinecke@317: symType.equals("arrow") || aheinecke@317: symType.equals("char") || aheinecke@317: symType.equals("picture")) { aheinecke@317: SymbolWriter sw = new SymbolWriter(this.map, this.cl); aheinecke@317: sw.write(symbolElement); aheinecke@317: } aheinecke@317: else { aheinecke@317: return false; aheinecke@317: } aheinecke@317: aheinecke@317: String name = symbolElement.getAttribute("name"); aheinecke@317: style.setSymbolByName(map, name); aheinecke@317: aheinecke@317: return true; aheinecke@317: } aheinecke@317: } aheinecke@317: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :