view src/java/de/intevation/mxd/writer/LineStyleWriter.java @ 201:877be7484245

Merged.
author raimund renkert <raimund.renkert@intevation.de>
date Wed, 20 Jul 2011 18:30:00 +0200
parents c79c3c6fc99a
children df4e0946ef02
line wrap: on
line source
package de.intevation.mxd.writer;

import java.awt.Color;

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.colorObj;
import edu.umn.gis.mapscript.symbolObj;
import edu.umn.gis.mapscript.symbolSetObj;


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

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

    /**
     * Private mamber
     */
    private mapObj map;
    private classObj cl;
    private styleObj style;

    /**
     * Contructor with map object and class object.
     *
     * @param map The map object.
     * @param cl  The class object containing the style.
     */
    public LineStyleWriter (mapObj map, classObj cl) {
        this.map = map;
        this.cl = cl;
        this.style = new styleObj(cl);
    }

    /**
     * Write the content.
     *
     * @param symbolElement DOM element containing style attributes.
     */
    public boolean write(Element symbolElement) {
        logger.debug("write(Element)");
        symbolSetObj symbolSet = map.getSymbolset();
        if(symbolElement.hasAttribute("width")) {
            double w = 0;
            try {
                w = Double.parseDouble(symbolElement.getAttribute("width"));
            }
            catch(NumberFormatException nfe) {
                logger.warn("Error setting width. No line width set.");
                w = 0;
            }
            if(w < 1.0) {
                w = 1;
            }
            style.setWidth((int)w);
        }
        if(symbolElement.hasAttribute("angle")) {
            try {
                style.setAngle(
                    Double.parseDouble(symbolElement.getAttribute("angle")));
            }
            catch(NumberFormatException nfe) {
                logger.info("Error setting angle. Could not get value.");
            }
        }
        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")) {
            try {
                style.setSize((int)Double.parseDouble(
                    symbolElement.getAttribute("size")));
            }
            catch(NumberFormatException nfe) {
                logger.warn("Error setting size. Setting size to 1.");
                style.setSize (1);
            }
        }

        if(symbolElement.hasAttribute("offset")) {
            double offset = 0;
            try {
                offset = Double.parseDouble(
                             symbolElement.getAttribute("offset"));
                if(Math.abs(offset) < 1 && offset > 0) {
                    offset = 1;
                }
                else if (Math.abs(offset) < 1 && offset < 0) {
                    offset = -1;
                }
                style.setOffsetx(offset);
                style.setOffsety(-99); 
            }
            catch(NumberFormatException nfe) {
                logger.warn("Could not set offset.");
            }
        }

        if(symbolElement.hasAttribute("miterlimit")) {
            double miter = 0;
            try {
                miter = Double.parseDouble(
                            symbolElement.getAttribute("miterlimit"));
                style.setLinejoinmaxsize(miter);
            }
            catch(NumberFormatException nfe) {
                logger.warn("Could not set miter limit.");
            }
        }



        //The following lines are for dashed and/or dotted lines.
        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, 4.0, 0, 0, 0, 0, 0, 0, 0, 0};
                style.setPattern(vals);
            }
            else if(ls.equals("dashdot")) {
                style.setPatternlength(4);
                vals = new double[] {5.0, 4.0, 1.0, 4.0, 0, 0, 0, 0, 0, 0};
                style.setPattern(vals);
            }
            else if (ls.equals("dashdotdot")) {
                style.setPatternlength(6);
                vals = new double[] {5.0, 4.0, 1.0, 3.0, 1.0, 4.0, 0, 0, 0, 0};
                style.setPattern(vals);
            }
        }
        else if(symbolElement.hasAttribute("pattern_count")) {
            int count = 0;
            try {
                count = Integer.parseInt(
                            symbolElement.getAttribute("pattern_count"));
                double[] vals = new double[10];
                double move = 0.0;
                int pos = 0;
                for(int i = 0; i < count; i++) {
                    double mark =
                        Double.parseDouble(symbolElement.getAttribute("mark_" +
                                                                      i));
                    double gap =
                        Double.parseDouble(symbolElement.getAttribute("gap_" +
                                                                      i));
                    if(i == 0 && mark == 0.0) {
                        move = gap;
                    }
                    else if(i == count - 1 && move > 0.0) {
                       vals[pos++] = mark;
                       vals[pos++] = gap + move;
                    }
                    else {
                        vals[pos++] = mark;
                        vals[pos++] = gap;
                    }
                }
                if(move > 0.0) {
                    style.setPatternlength((count*2) -2);
                }
                else {
                    style.setPatternlength(count*2);
                }
                style.setPattern(vals);
            }
            catch(NumberFormatException nfe) {
                logger.warn("Could not write PATTERN.");
            }
        }
        String type = symbolElement.getAttribute("type");
        if(type.equals("marker")) {
            SymbolWriter sw = new SymbolWriter(this.map, this.cl);
            sw.write(symbolElement);
            String name = symbolElement.getAttribute("name");
            style.setSymbolByName(map, name);
            symbolObj sym = symbolSet.getSymbolByName(name);
        }
        else {
            try {
                SymbolWriter sw = new SymbolWriter (this.map, this.cl);
                sw.saveSymbolSet (symbolSet);
            }
            catch (Exception e) {
                logger.warn("Could not write symbol set.");
            }
        }

        return false;
    }
}
// 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)