view src/java/de/intevation/mxd/writer/MapScriptWriter.java @ 41:60ed2164035a

Introduced MapScript writer.
author Raimund Renkert <rrenkert@intevation.de>
date Thu, 14 Apr 2011 11:31:29 +0200
parents
children 395307e8b7ee
line wrap: on
line source
package de.intevation.mxd.writer;

import org.apache.log4j.Logger;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import javax.xml.xpath.XPathConstants;

import edu.umn.gis.mapscript.mapObj;
import edu.umn.gis.mapscript.layerObj;
import edu.umn.gis.mapscript.MS_UNITS;

import de.intevation.mxd.utils.XMLUtils;

/**
 * The MXD file reader.
 *
 * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a>
 */
public class MapScriptWriter
implements IWriter
{
    private Document root;
    private mapObj map;

    private static final Logger logger = Logger.getLogger(MapScriptWriter.class);

    public MapScriptWriter() {
        map = new mapObj("");
    }

    public MapScriptWriter(String path) {
        map = new mapObj(path);
    }

    public boolean write(Document doc) {
        logger.debug("write()");
        this.root = doc;

        //Get the filename.
        Element fileNode = (Element)XMLUtils.xpath(
            root,
            "/mxd/file",
            XPathConstants.NODE);
        String filename = fileNode.getAttribute("name");
        if(filename.endsWith(".mxd")) {
            filename = filename.replace(".mxd", ".map");
        }
        //Write the map attributes.
        writeMap();

        //Save the map.
        map.save(filename);
        return true;
    }

    private void writeMap() {
        logger.debug("writeMap()");
        //Get the map.
        Element mapNode = (Element)XMLUtils.xpath(
            root,
            "/mxd/map",
            XPathConstants.NODE);

        //Set the name.
        map.setName(mapNode.getAttribute("name"));

        //Set the extent.
        map.setExtent(
            Double.parseDouble(mapNode.getAttribute("extent_min_x")),
            Double.parseDouble(mapNode.getAttribute("extent_min_y")),
            Double.parseDouble(mapNode.getAttribute("extent_max_x")),
            Double.parseDouble(mapNode.getAttribute("extent_max_y")));

        //Set the units.
        String units = mapNode.getAttribute("units");
        MS_UNITS msu;
        if(units.equals("feet")) {
            msu = MS_UNITS.MS_FEET;
        }
        else if(units.equals("inches")) {
            msu = MS_UNITS.MS_INCHES;
        }
        else if(units.equals("kilometers")) {
            msu = MS_UNITS.MS_KILOMETERS;
        }
        else if(units.equals("meters")) {
            msu = MS_UNITS.MS_METERS;
        }
        else if(units.equals("miles")) {
            msu = MS_UNITS.MS_MILES;
        }
        else if(units.equals("nauticalmiles")) {
            msu = MS_UNITS.MS_NAUTICALMILES;
        }
        else if(units.equals("points")) {
            msu = MS_UNITS.MS_PIXELS;
        }
        else {
            msu = MS_UNITS.MS_DD;
        }
        map.setUnits(msu);

        //TODO: Find out whats the correct scale value.
        //map.setScaledenom(Double.parseDouble(mapNode.getAttribute("scale")));
    }

    private void writeLayer() {

    }

    private void writeClass() {

    }

    private void writeSymbol() {

    }

}
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)