view src/java/de/intevation/mxd/writer/FillStyleWriter.java @ 101:461ee9193097

Write PATTERN to the style if a hatch symbol is used to fill a polygon.
author Raimund Renkert <rrenkert@intevation.de>
date Wed, 08 Jun 2011 12:29:26 +0200
parents 18e4f143896b
children 11d63bf00326
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)
    throws Exception {
        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")) {
                        gap = Double.parseDouble(
                            symbolElement.getAttribute("xseparation"));
                    }
                    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);
                style.setOutlinewidth(Double.parseDouble(
                    symbolElement.getAttribute("outline_size")));
            }

        }

        return true;
    }

    /**
     * Write the outline for a polygon.
     */
    private void writeOutline(Element symbolElement)
    throws Exception {
        logger.debug("writeOutline()");
        //write transparent outline
        colorObj color = new colorObj(-1, -1, -1, -4);

        //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);
        outline.setOutlinewidth(Double.parseDouble(
            symbolElement.getAttribute("width")));
    }

    /**
     * Write marker attributes and a symbol for the polygon fill.
     */
    private void writeMarker(Element symbolElement, double gap)
    throws Exception {
        logger.debug("writeMarker()");
        String name = symbolElement.getAttribute("name");
        String type = symbolElement.getAttribute("type");
        if (symbolElement.hasAttribute("angle")) {
            style.setAngle(
                Double.parseDouble(symbolElement.getAttribute("angle")));
        }
        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 = Double.parseDouble(
                symbolElement.getAttribute("size"));
            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);
            style.setOutlinewidth(Double.parseDouble(
                symbolElement.getAttribute("outline_size")));
        }
        if(symbolElement.hasAttribute("linestyle")) {
            String ls = symbolElement.getAttribute("linestyle");
            double[] vals;
            if(ls.equals("dash")) {
                style.setPatternlength(2);
                vals = new double[] {2.0, 2.0};
                style.setPattern(vals);
            }
            else if(ls.equals("dot")) {
                style.setPatternlength(2);
                vals = new double[] {1.0, 2.0};
                style.setPattern(vals);
            }
            else if(ls.equals("dashdot")) {
                style.setPatternlength(4);
                vals = new double[] {2.0, 2.0, 1.0, 2.0};
                style.setPattern(vals);
            }
            else if (ls.equals("dashdotdot")) {
                style.setPatternlength(6);
                vals = new double[] {2.0, 2.0, 1.0, 2.0, 1.0, 2.0};
                style.setPattern(vals);
            }
        }

        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);
        }
    }

    /**
     * Write simple fill attributes.
     */
    private void writeSimple(Element symbolElement) {
        logger.debug("writeSimple(Element)");
        if(symbolElement.hasAttribute("transparency")) {
            double value = Double.parseDouble(
                symbolElement.getAttribute("transparency"));
            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)