view src/java/de/intevation/mxd/reader/MapReader.java @ 356:1e0c7e134e7b tip

Update contact details
author Tom Gottfried <tom@intevation.de>
date Mon, 04 Jan 2021 15:52:40 +0100
parents 5a2b5bc066eb
children
line wrap: on
line source
/*
 * Copyright (c) 2011 by Intevation GmbH, Germany <info@intevation.de>
 *
 * This file is part of MXD2map.
 *
 * This program is free software under the LGPL (>=v2.1)
 * Read the file LICENCE.txt coming with the software for details
 * or visit http://www.gnu.org/licenses/ if it does not exist.
 *
 * MXD2map has been developed on behalf of the
 * Bundesamt fuer Seeschifffahrt und Hydrographie (BSH) in Hamburg
 * by Intevation GmbH.
 *
 * Authors:
 * Raimund Renkert <raimund.renkert@intevation.de>
 * Bjoern Schilberg <bjoern.schilberg@intevation.de>
 * Stephan Holl <stephan.holl@intevation.de>
 */

package de.intevation.mxd.reader;

import java.io.IOException;

import org.apache.log4j.Logger;

import com.esri.arcgis.carto.IMap;
import com.esri.arcgis.carto.Map;
import com.esri.arcgis.geometry.ISpatialReference;
import com.esri.arcgis.geometry.ProjectedCoordinateSystem;
import com.esri.arcgis.geometry.GeographicCoordinateSystem;
import com.esri.arcgis.geometry.UnknownCoordinateSystem;
import com.esri.arcgis.geometry.Projection;
import com.esri.arcgis.geometry.IEnvelope;

import org.w3c.dom.Element;

import de.intevation.mxd.utils.MapToXMLUtils;

/**
 * Reads map information.
 *
 * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a>
 */
public class MapReader {
    /**
     * The Logger.
     */
    private static final Logger logger = Logger.getLogger(MapReader.class);

    /**
     * Private member.
     */
    private Map map;
    private MapToXMLUtils util;

    /**
     * Constructor with ArcGIS map.
     *
     * @param map The ArcGIS map object.
     */
    public MapReader(IMap map)
    throws Exception {
        logger.debug("constructor()");
        if(map instanceof Map) {
            this.map = (Map)map;
        }
        else {
            throw new Exception("Not an instance of \"Map\"!");
        }
    }

    /**
     * Reads the Map attributes.
     */
    public void read()
    throws IOException {
        logger.debug("read()");
        if(util == null) {
            throw new IOException("Can not write to document.");
        }

        //Create XML Element for map.
        Element mapElement;
        try{
            mapElement = util.createMap();
        }
        catch(Exception e){
            logger.error("Could not create DOM element. Aborting.");
            throw new IOException("Error creating DOM element.");
        }

        //Read map name.
        try {
            mapElement.setAttribute("name", map.getName());
        }
        catch(IOException ioe) {
            logger.warn(
                "Could not read map name." +
                " Setting map name to \"default-map\"");
            mapElement.setAttribute("name", "default-map");
        }

        //Read map extent.
        try {
            IEnvelope ext = map.getExtent();
            mapElement.setAttribute(
                "extent_max_x",
                String.valueOf(ext.getXMax()));
            mapElement.setAttribute(
                "extent_max_y",
                String.valueOf(ext.getYMax()));
            mapElement.setAttribute(
                "extent_min_x",
                String.valueOf(ext.getXMin()));
            mapElement.setAttribute(
                "extent_min_y",
                String.valueOf(ext.getYMin()));
        }
        catch(IOException ioe) {
            logger.warn("Could not read map extend. Setting to 0, 0, 0, 0.");
            mapElement.setAttribute("extend_max_x", "0");
            mapElement.setAttribute("extend_min_x", "0");
            mapElement.setAttribute("extend_max_y", "0");
            mapElement.setAttribute("extend_min_y", "0");
        }

        //Read map units.
        int units = 0;
        try {
            units = map.getMapUnits();
        }
        catch(IOException ioe) {
            logger.warn(
                "Could not read map units." +
                " Setting map units to unknown.");
            units = 0;
        }

        String s_units;
        switch(units) {
            case   1: s_units = "inches"; break;
            case   2: s_units = "points"; break;
            case   3: s_units = "feet"; break;
            case   4: s_units = "yards"; break;
            case   5: s_units = "miles"; break;
            case   6: s_units = "nauticalmiles"; break;
            case   7: s_units = "millimeters"; break;
            case   8: s_units = "centimeters"; break;
            case   9: s_units = "meters"; break;
            case  10: s_units = "kilometers"; break;
            case  11: s_units = "degree"; break;
            case  12: s_units = "decimeters"; break;
            case  13: s_units = "units_last"; break;
            default : s_units = "unknown"; break;
        }
        mapElement.setAttribute("units", s_units);

        if(units == 0) {
            logger.warn(
                "Unknown units." +
                " Please edit units in resulting mapfile.");
        }

        try {
            mapElement.setAttribute(
                "scale",
                String.valueOf(map.getMaxScale()));
        }
        catch(IOException ioe) {
            logger.warn("Could not read map scale. Setting map scale to 1000");
            mapElement.setAttribute("scale", "1000");
        }

        //Read the projection.
        try {
            ISpatialReference sr = map.getSpatialReference();
            int projection = 0;
            if(sr instanceof ProjectedCoordinateSystem) {
                ProjectedCoordinateSystem pcs = (ProjectedCoordinateSystem)sr;
                projection = pcs.getFactoryCode();
            }
            else if(sr instanceof GeographicCoordinateSystem) {
                GeographicCoordinateSystem gcs = (GeographicCoordinateSystem)sr;
                projection = gcs.getFactoryCode();
            }
            else if(sr instanceof UnknownCoordinateSystem) {
                UnknownCoordinateSystem ucs = (UnknownCoordinateSystem)sr;
                projection = 0;
            }
            else{
                logger.debug(
                    "Unknown SpatialReference: " +
                    sr.getClass().toString());
            }

            if(projection == 0) {
                logger.warn(
                    "Unknown projection." +
                    " Please edit projection in resulting mapfile.");
            }
            mapElement.setAttribute("projection", String.valueOf(projection));
        }
        catch(IOException ioe) {
            logger.warn(
                "Could not read map projection." +
                " Setting map projection to unknown.");
            mapElement.setAttribute("projection", "Unknown");
        }

        // read the reference scale
        try {
            mapElement.setAttribute("referencescale", String.valueOf(
                        map.getReferenceScale()));
        } catch (Exception e) {
            logger.warn("Could not read referencescale of map");
        }
    }

    /**
     * Set the utilities.
     */
    public void setUtil(MapToXMLUtils util) {
        this.util = util;
    }
}
// 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)