rrenkert@95: package de.intevation.mxd.writer; rrenkert@95: rrenkert@95: import java.awt.Color; rrenkert@95: rrenkert@95: import org.apache.log4j.Logger; rrenkert@95: rrenkert@95: import org.w3c.dom.Element; rrenkert@95: rrenkert@95: import edu.umn.gis.mapscript.mapObj; rrenkert@95: import edu.umn.gis.mapscript.classObj; rrenkert@95: import edu.umn.gis.mapscript.styleObj; rrenkert@95: import edu.umn.gis.mapscript.colorObj; rrenkert@95: import edu.umn.gis.mapscript.symbolObj; rrenkert@95: import edu.umn.gis.mapscript.symbolSetObj; rrenkert@95: rrenkert@95: rrenkert@95: /** rrenkert@95: * The interface to the mapfile writer. rrenkert@95: * rrenkert@95: * @author Raimund Renkert rrenkert@95: */ rrenkert@95: public class LineStyleWriter { rrenkert@95: rrenkert@95: /** rrenkert@95: * The Logger. rrenkert@95: */ rrenkert@95: private static final Logger logger = Logger.getLogger(LineStyleWriter.class); rrenkert@95: rrenkert@181: /** rrenkert@181: * Private mamber rrenkert@181: */ rrenkert@95: private mapObj map; rrenkert@95: private classObj cl; rrenkert@95: private styleObj style; rrenkert@95: rrenkert@181: /** rrenkert@181: * Contructor with map object and class object. rrenkert@181: * rrenkert@181: * @param map The map object. rrenkert@181: * @param cl The class object containing the style. rrenkert@181: */ rrenkert@95: public LineStyleWriter (mapObj map, classObj cl) { rrenkert@95: this.map = map; rrenkert@95: this.cl = cl; rrenkert@95: this.style = new styleObj(cl); rrenkert@95: } rrenkert@95: rrenkert@95: /** rrenkert@95: * Write the content. rrenkert@181: * rrenkert@181: * @param symbolElement DOM element containing style attributes. rrenkert@95: */ rrenkert@120: public boolean write(Element symbolElement) { rrenkert@95: logger.debug("write(Element)"); rrenkert@95: symbolSetObj symbolSet = map.getSymbolset(); vc11884admin@131: if(symbolElement.hasAttribute("width")) { rrenkert@145: double w = 0; rrenkert@145: try { rrenkert@145: w = Double.parseDouble(symbolElement.getAttribute("width")); rrenkert@145: } rrenkert@145: catch(NumberFormatException nfe) { rrenkert@145: logger.warn("Error setting width. No line width set."); rrenkert@145: w = 0; rrenkert@145: } vc11884admin@131: if(w < 1.0) { raimund@177: w = 1; vc11884admin@131: } rrenkert@145: style.setWidth((int)w); vc11884admin@131: } rrenkert@95: if(symbolElement.hasAttribute("angle")) { rrenkert@145: try { rrenkert@145: style.setAngle( rrenkert@145: Double.parseDouble(symbolElement.getAttribute("angle"))); rrenkert@145: } rrenkert@145: catch(NumberFormatException nfe) { rrenkert@145: logger.info("Error setting angle. Could not get value."); rrenkert@145: } rrenkert@95: } rrenkert@95: if(symbolElement.hasAttribute("color")) { rrenkert@95: String c = symbolElement.getAttribute("color"); rrenkert@95: Color col = Color.decode(c); rrenkert@95: colorObj color = new colorObj( rrenkert@95: col.getRed(), rrenkert@95: col.getGreen(), rrenkert@95: col.getBlue(), rrenkert@95: -4); rrenkert@95: style.setColor(color); rrenkert@95: } vc11884admin@131: rrenkert@95: if(symbolElement.hasAttribute("size")) { rrenkert@145: try { rrenkert@145: style.setSize((int)Double.parseDouble( rrenkert@145: symbolElement.getAttribute("size"))); rrenkert@145: } rrenkert@145: catch(NumberFormatException nfe) { rrenkert@145: logger.warn("Error setting size. Setting size to 1."); rrenkert@145: style.setSize (1); rrenkert@145: } rrenkert@95: } raimund@177: raimund@177: if(symbolElement.hasAttribute("offset")) { raimund@177: double offset = 0; raimund@177: try { raimund@177: offset = Double.parseDouble( raimund@177: symbolElement.getAttribute("offset")); raimund@177: if(Math.abs(offset) < 1 && offset > 0) { raimund@177: offset = 1; raimund@177: } raimund@177: else if (Math.abs(offset) < 1 && offset < 0) { raimund@177: offset = -1; raimund@177: } raimund@177: style.setOffsetx(offset); raimund@177: style.setOffsety(-99); raimund@177: } raimund@177: catch(NumberFormatException nfe) { raimund@177: logger.warn("Could not set offset."); raimund@177: } raimund@177: } raimund@177: raimund@177: if(symbolElement.hasAttribute("miterlimit")) { raimund@177: double miter = 0; raimund@177: try { raimund@177: miter = Double.parseDouble( raimund@177: symbolElement.getAttribute("miterlimit")); raimund@177: style.setLinejoinmaxsize(miter); raimund@177: } raimund@177: catch(NumberFormatException nfe) { raimund@177: logger.warn("Could not set miter limit."); raimund@177: } raimund@177: } raimund@177: vc11884admin@141: String type = symbolElement.getAttribute("type"); vc11884admin@141: if(type.equals("marker")) { vc11884admin@141: SymbolWriter sw = new SymbolWriter(this.map, this.cl); vc11884admin@141: sw.write(symbolElement); vc11884admin@147: String name = symbolElement.getAttribute("name"); vc11884admin@147: style.setSymbolByName(map, name); vc11884admin@147: symbolObj sym = symbolSet.getSymbolByName(name); vc11884admin@141: } rrenkert@95: rrenkert@95: //The following lines are for dashed and/or dotted lines. rrenkert@122: if(symbolElement.hasAttribute("linestyle")) { rrenkert@95: String ls = symbolElement.getAttribute("linestyle"); rrenkert@95: double[] vals; rrenkert@95: if(ls.equals("dash")) { rrenkert@95: style.setPatternlength(2); rrenkert@122: vals = new double[] {4.0, 4.0, 0, 0, 0, 0, 0, 0, 0, 0}; rrenkert@95: style.setPattern(vals); rrenkert@95: } rrenkert@95: else if(ls.equals("dot")) { rrenkert@95: style.setPatternlength(2); rrenkert@122: vals = new double[] {1.0, 4.0, 0, 0, 0, 0, 0, 0, 0, 0}; rrenkert@95: style.setPattern(vals); rrenkert@95: } rrenkert@95: else if(ls.equals("dashdot")) { rrenkert@95: style.setPatternlength(4); rrenkert@122: vals = new double[] {5.0, 4.0, 1.0, 4.0, 0, 0, 0, 0, 0, 0}; rrenkert@95: style.setPattern(vals); rrenkert@95: } rrenkert@95: else if (ls.equals("dashdotdot")) { rrenkert@95: style.setPatternlength(6); rrenkert@122: vals = new double[] {5.0, 4.0, 1.0, 3.0, 1.0, 4.0, 0, 0, 0, 0}; rrenkert@95: style.setPattern(vals); rrenkert@95: } rrenkert@95: } rrenkert@136: else if(symbolElement.hasAttribute("pattern_count")) { rrenkert@136: int count = 0; rrenkert@136: try { rrenkert@136: count = Integer.parseInt( rrenkert@136: symbolElement.getAttribute("pattern_count")); rrenkert@136: double[] vals = new double[10]; rrenkert@136: double move = 0.0; rrenkert@136: int pos = 0; rrenkert@136: for(int i = 0; i < count; i++) { rrenkert@136: double mark = rrenkert@180: Double.parseDouble(symbolElement.getAttribute("mark_" + rrenkert@180: i)); rrenkert@136: double gap = rrenkert@180: Double.parseDouble(symbolElement.getAttribute("gap_" + rrenkert@180: i)); rrenkert@136: if(i == 0 && mark == 0.0) { rrenkert@136: move = gap; rrenkert@136: } rrenkert@136: else if(i == count - 1 && move > 0.0) { rrenkert@136: vals[pos++] = mark; rrenkert@136: vals[pos++] = gap + move; rrenkert@136: } rrenkert@136: else { rrenkert@136: vals[pos++] = mark; rrenkert@136: vals[pos++] = gap; rrenkert@136: } rrenkert@136: } rrenkert@136: if(move > 0.0) { rrenkert@136: style.setPatternlength((count*2) -2); rrenkert@136: } rrenkert@136: else { rrenkert@136: style.setPatternlength(count*2); rrenkert@136: } rrenkert@136: style.setPattern(vals); rrenkert@136: } rrenkert@136: catch(NumberFormatException nfe) { rrenkert@136: logger.warn("Could not write PATTERN."); rrenkert@136: } rrenkert@136: } rrenkert@95: return false; rrenkert@95: } rrenkert@95: } rrenkert@95: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :