Mercurial > mxd2map
view src/java/de/intevation/mxd/writer/SymbolWriter.java @ 146:e4a8c39985d7
* mxd/Styles/label/*: Added two examples to show basic labeling
within an MXD. More will not be supported though.
author | Stephan Holl <stephan.holl@intevation.de> |
---|---|
date | Wed, 29 Jun 2011 17:36:44 +0200 |
parents | b2c5a66022f1 |
children | 707f13cfba74 |
line wrap: on
line source
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) { logger.debug("write(Element)"); symbolSetObj symbolSet = map.getSymbolset(); String name = symbolElement.getAttribute("name"); symbolObj sym = symbolSet.getSymbolByName(name); String symType = symbolElement.getAttribute("style"); String type = symbolElement.getAttribute("type"); if(symType.equals("point")) { writeSimple(sym); } else if (symType.equals("arrow")) { writeArrow(sym, symbolElement); } else if (symType.equals("char")) { int exists = symbolExists(symbolElement); if(exists == -1) { String old = symbolElement.getAttribute("name"); symbolElement.setAttribute( "name", old + symbolSet.getNumsymbols() + 1); writeCharacter(sym, symbolElement); } else { symbolElement.setAttribute( "name", symbolSet.getSymbol(exists).getName()); } } else if (type.equals("line")) { writeHatch(sym); } else { return false; } try { saveSymbolSet(symbolSet); } catch(Exception e) { logger.error("Error saving symbol set."); 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 = 0; double width = 1; try { len = Double.parseDouble(symbolElement.getAttribute("length")); width = Double.parseDouble(symbolElement.getAttribute("width")); } catch(NumberFormatException nfe) { logger.warn ("Error setting arrow symbol."); return; } 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 String font = symbolElement.getAttribute("font"); //Remove all blank character to match the mapserver fonts.txt. font = font.replaceAll(" ", ""); symbol.setName(symbolElement.getAttribute("name")); symbol.setFont(font); symbol.setType(MS_SYMBOL_TYPE.MS_SYMBOL_TRUETYPE.swigValue()); symbol.setAntialias(1); symbol.setCharacter("&#" + symbolElement.getAttribute("char") + ";"); } private void writeHatch(symbolObj symbol) { logger.debug("writeHatch(symbolObj)"); symbol.setType(MS_SYMBOL_TYPE.MS_SYMBOL_HATCH.swigValue()); } public boolean canWrite(String type) { if (type.equals("point") || type.equals("arrow") || type.equals("char") || type.equals("line")) { return true; } else { return false; } } private void saveSymbolSet(symbolSetObj symbols) throws Exception { String path = this.map.getMappath(); if(path.equals("")) { return; } else { path += "symbols.sym"; } symbols.save(path); } private int symbolExists (Element elem) { symbolSetObj symbolSet = map.getSymbolset(); for (int i = 0; i < symbolSet.getNumsymbols(); i++) { symbolObj sym = symbolSet.getSymbol(i); int stype = sym.getType(); String etype = elem.getAttribute("style"); if(stype == MS_SYMBOL_TYPE.MS_SYMBOL_ELLIPSE.swigValue () && etype.equals("point")) { return i; } else if(stype == MS_SYMBOL_TYPE.MS_SYMBOL_VECTOR.swigValue () && etype.equals("arrow")) { return i; } else if(stype == MS_SYMBOL_TYPE.MS_SYMBOL_TRUETYPE.swigValue () && etype.equals("char")) { String font = elem.getAttribute("font"); font = font.replaceAll(" ", ""); String c = elem.getAttribute("char"); c = "&#" + c + ";"; if (font.equals(sym.getFont()) && c.equals(sym.getCharacter())) { return i; } } else if(stype == MS_SYMBOL_TYPE.MS_SYMBOL_HATCH.swigValue () && etype.equals("hatch")) { return i; } } return -1; } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :