view src/java/de/intevation/mxd/writer/FillStyleWriter.java @ 147:03daf386f19a

Manage symbol name for line symbols.
author vc11884admin@VC11884.win.bsh.de
date Wed, 29 Jun 2011 17:36:07 +0200
parents b2c5a66022f1
children 7b1433d3b574
line wrap: on
line source
package de.intevation.mxd.writer;

import java.io.IOException;
import java.awt.Color;
import org.apache.log4j.Logger;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import edu.umn.gis.mapscript.mapObj;
import edu.umn.gis.mapscript.layerObj;
import edu.umn.gis.mapscript.classObj;
import edu.umn.gis.mapscript.styleObj;
import edu.umn.gis.mapscript.colorObj;
import edu.umn.gis.mapscript.symbolObj;
import edu.umn.gis.mapscript.symbolSetObj;
import edu.umn.gis.mapscript.lineObj;
import edu.umn.gis.mapscript.pointObj;


/**
 * The interface to the mapfile writer.
 *
 * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a>
 */
public class FillStyleWriter {

    /**
     * The Logger.
     */
    private static final Logger logger =
        Logger.getLogger(FillStyleWriter.class);

    private mapObj map;
    private classObj cl;
    private styleObj style;

    public FillStyleWriter (mapObj map, classObj cl) {
        logger.debug("contructor(mapObj, classObj)");
        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();

        if(symbolElement.hasChildNodes()) {
            NodeList symbols = symbolElement.getChildNodes();
            for (int i = 0; i < symbols.getLength(); i++) {
                Element nextSym = (Element)symbols.item(i);
                String type = nextSym.getAttribute("type");

                if(((symbols.getLength() > 1 && i == 0) ||
                    (symbols.getLength() == 1 &&
                     !symbolElement.hasAttribute("hatch"))) &&
                   type.equals("line")) {
                    writeOutline(nextSym);
                    if (symbols.getLength() == 1) {
                        writeSimple(symbolElement);
                    }
                }
                else if(nextSym.getTagName().equals("symbol") &&
                   !symbolElement.hasAttribute("hatch") ||
                   (i == 1 && type.equals("marker"))) {
                    double gap = 0;
                    if(symbolElement.hasAttribute("xseparation")) {
                        try {
                            gap = Double.parseDouble(
                                symbolElement.getAttribute("xseparation"));
                        }
                        catch(NumberFormatException nfe) {
                            gap = 0;
                        }
                    }
                    writeMarker(nextSym, gap);
                }
                else if (nextSym.getTagName().equals("symbol") &&
                         symbolElement.hasAttribute("hatch")) {
                    if(symbolElement.hasAttribute("angle")) {
                        nextSym.setAttribute(
                            "angle",
                            symbolElement.getAttribute("angle"));
                    }
                    if(symbolElement.hasAttribute("separation")) {
                        nextSym.setAttribute(
                            "size",
                            symbolElement.getAttribute("separation"));
                    }
                    writeMarker(nextSym, -1);
                }
                else {
                    writeSimple(symbolElement);
                }
            }
        }
        else {
            writeSimple(symbolElement);
            if(symbolElement.hasAttribute("outline_color")) {
                Color oCol = Color.decode(
                    symbolElement.getAttribute("outline_color"));
                colorObj outlineColor = new colorObj(
                    oCol.getRed(),
                    oCol.getGreen(),
                    oCol.getBlue(),
                    -4);
                style.setOutlinecolor(outlineColor);
                try {
                    style.setOutlinewidth(Double.parseDouble(
                        symbolElement.getAttribute("outline_size")));
                }
                catch(NumberFormatException nfe) {
                    logger.warn("Error setting outline width.");
                    style.setOutlinewidth(0.0);
                }
            }

        }

        return true;
    }

    /**
     * Write the outline for a polygon.
     */
    private void writeOutline(Element symbolElement) {
        logger.debug("writeOutline()");
        //write transparent outline
        colorObj color = new colorObj(-1, -1, -1, -4);
        if(symbolElement.hasAttribute("width")) {
            double w = 0;
            try {
                w = Double.parseDouble(symbolElement.getAttribute("width"));
            }
            catch(NumberFormatException nfe) {
                logger.warn("Error setting outline width.");
                w = 0;
            }
            if (w < 1.0) {
                return;
            }
        }
        //write new style for the outline
        //TODO write further attribute like pattern etc.
        Color oCol = Color.decode(
            symbolElement.getAttribute("color"));

        styleObj outline = new styleObj (cl);

        colorObj outlinecolor = new colorObj(
            oCol.getRed(),
            oCol.getGreen(),
            oCol.getBlue(),
            -4);
        outline.setOutlinecolor(outlinecolor);
        try {
            outline.setOutlinewidth(Double.parseDouble(
                symbolElement.getAttribute("width")));
        }
        catch(NumberFormatException nfe) {
            logger.warn("Error setting outline width.");
            outline.setOutlinewidth(0.0);
        }
        if(symbolElement.hasAttribute("linestyle")) {
            String ls = symbolElement.getAttribute("linestyle");
            double[] vals;
            if(ls.equals("dash")) {
                outline.setPatternlength(2);
                vals = new double[] {4.0, 4.0, 0, 0, 0, 0, 0, 0, 0, 0};
                outline.setPattern(vals);
            }
            else if(ls.equals("dot")) {
                outline.setPatternlength(2);
                vals = new double[] {1.0, 3.0, 0, 0, 0, 0, 0, 0, 0, 0};
                outline.setPattern(vals);
            }
            else if(ls.equals("dashdot")) {
                outline.setPatternlength(4);
                vals = new double[] {4.0, 3.0, 1.0, 3.0, 0, 0, 0, 0, 0, 0, 0};
                outline.setPattern(vals);
            }
            else if (ls.equals("dashdotdot")) {
                outline.setPatternlength(6);
                vals = new double[] {5.0, 3.0, 1.0, 3.0, 1.0, 4.0, 0, 0, 0, 0};
                outline.setPattern(vals);
            }
        }


    }

    /**
     * Write marker attributes and a symbol for the polygon fill.
     */
    private void writeMarker(Element symbolElement, double gap) {
        logger.debug("writeMarker()");
        String name = symbolElement.getAttribute("name");
        String type = symbolElement.getAttribute("type");
        if (symbolElement.hasAttribute("angle")) {
            try {
               style.setAngle(
                   Double.parseDouble(symbolElement.getAttribute("angle")));
            }
            catch(NumberFormatException nfe) {
                logger.warn("Error setting angle.");
                style.setAngle(0.0);
            }
        }
        if(symbolElement.hasAttribute("color")) {
            String c = symbolElement.getAttribute("color");
            Color col = Color.decode(c);
            colorObj color = new colorObj(
                col.getRed(),
                col.getGreen(),
                col.getBlue(),
                -4);
            style.setColor(color);
        }
        if (symbolElement.hasAttribute ("size")) {
            double size = 1;
            try {
                size = Double.parseDouble(symbolElement.getAttribute("size"));
            }
            catch(NumberFormatException nfe) {
                size = 1.0;
            }
            style.setSize(size);
            //In arcgis the separation goes from center to center, so the gap is
            //the separation - size
            if (gap > 0) {
                style.setGap(gap - size);
            }
        }
        if(symbolElement.hasAttribute("outline_color")) {
            Color oCol = Color.decode(
                symbolElement.getAttribute("outline_color"));
            colorObj outlineColor = new colorObj(
                oCol.getRed(),
                oCol.getGreen(),
                oCol.getBlue(),
                -4);
            style.setOutlinecolor(outlineColor);
            try {
                style.setOutlinewidth(Double.parseDouble(
                    symbolElement.getAttribute("outline_size")));
            }
            catch(NumberFormatException nfe) {
                logger.warn("Error setting angle.");
                style.setOutlinewidth(0.0);
            }
        }
        if(type.equals("marker")) {
            style.setSymbolByName(map, name);
            SymbolWriter sw = new SymbolWriter(this.map, this.cl);
            sw.write(symbolElement);
        }
        else if(type.equals("line")) {
            style.setSymbolByName(map, "hatch");
            SymbolWriter sw = new SymbolWriter(this.map, this.cl);
            symbolElement.setAttribute("name", "hatch");
            sw.write(symbolElement);
            if(symbolElement.hasAttribute("linestyle")) {
                String ls = symbolElement.getAttribute("linestyle");
                double[] vals;
                if(ls.equals("dash")) {
                    style.setPatternlength(2);
                    vals = new double[] {4.0, 4.0, 0, 0, 0, 0, 0, 0, 0, 0};
                    style.setPattern(vals);
                }
                else if(ls.equals("dot")) {
                    style.setPatternlength(2);
                    vals = new double[] {1.0, 3.0, 0, 0, 0, 0, 0, 0, 0, 0};
                    style.setPattern(vals);
                }
                else if(ls.equals("dashdot")) {
                    style.setPatternlength(4);
                    vals = new double[] {4.0, 3.0, 1.0, 3.0, 0, 0, 0, 0, 0, 0, 0};
                    style.setPattern(vals);
                }
                else if (ls.equals("dashdotdot")) {
                    style.setPatternlength(6);
                    vals = new double[] {5.0, 3.0, 1.0, 3.0, 1.0, 4.0, 0, 0, 0, 0};
                    style.setPattern(vals);
                }
            }
        }
    }

    /**
     * Write simple fill attributes.
     */
    private void writeSimple(Element symbolElement) {
        logger.debug("writeSimple(Element)");
        if(symbolElement.hasAttribute("transparency")) {
            double value = 0;
            try {
                value = Double.parseDouble(
                    symbolElement.getAttribute("transparency"));
            }
            catch(NumberFormatException nfe) {
                value = 0;
            }
            int opacity = (int)(value/255) * 100;
            if(value >= 0) {
                style.setOpacity(opacity);
            }
        }
        if(symbolElement.hasAttribute("color")) {
            String c = symbolElement.getAttribute("color");
            Color col = Color.decode(c);
            colorObj color = new colorObj(
                col.getRed(),
                col.getGreen(),
                col.getBlue(),
                -4);
            style.setColor(color);
        }
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)