view src/java/de/intevation/mxd/writer/FillStyleWriter.java @ 131:cd18c61cbcf6

Do not write lines or outlines if their width is smaller than 1.0.
author vc11884admin@VC11884.win.bsh.de
date Mon, 20 Jun 2011 17:41:55 +0200
parents 3c792458a716
children b2c5a66022f1
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")) {
                        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) {
        logger.debug("writeOutline()");
        //write transparent outline
        colorObj color = new colorObj(-1, -1, -1, -4);
        if(symbolElement.hasAttribute("width")) {
            double w = Double.parseDouble(symbolElement.getAttribute("width"));
            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);
        outline.setOutlinewidth(Double.parseDouble(
            symbolElement.getAttribute("width")));
        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")) {
            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(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 = 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)