view src/java/de/intevation/mxd/reader/MXDReader.java @ 348:da26f4b93c90

Link to Geospatial addresses
author Andre Heinecke <aheinecke@intevation.de>
date Fri, 16 Nov 2012 17:03:08 +0100
parents 1b8572f22ac6
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.ILayer;
import com.esri.arcgis.carto.IMap;
import com.esri.arcgis.carto.MapDocument;
import com.esri.arcgis.carto.FeatureLayer;
import com.esri.arcgis.carto.CompositeGraphicsLayer;
import com.esri.arcgis.carto.GdbRasterCatalogLayer;
import com.esri.arcgis.carto.IFeatureRenderer;
import com.esri.arcgis.carto.SimpleRenderer;
import com.esri.arcgis.carto.ClassBreaksRenderer;
import com.esri.arcgis.carto.UniqueValueRenderer;
import com.esri.arcgis.carto.GroupLayer;
import com.esri.arcgis.carto.WMSMapLayer;
import com.esri.arcgis.carto.WMSGroupLayer;
import com.esri.arcgis.carto.WMSLayer;
import com.esri.arcgis.carto.RasterLayer;
import com.esri.arcgis.carto.IGraphicsLayerProxy;
import com.esri.arcgis.carto.IGraphicsLayer;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import de.intevation.mxd.ArcGISInitializer;
import de.intevation.mxd.utils.MapToXMLUtils;

/**
 * The MXD file reader.
 *
 * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a>
 */
public class MXDReader
implements IReader {
    /**
     * The Logger.
     */
    private static final Logger logger = Logger.getLogger(MXDReader.class);

    /**
     * Private member.
     */
    private String filename = "";
    private ArcGISInitializer initializer = null;
    private MapToXMLUtils util;
    private IMap map;
    private int invalidLayerCount;

    /**
     * Default constructor.
     */
    public MXDReader()
    throws IOException {
        logger.debug("constructor()");
        initializer = new ArcGISInitializer();
        util = new MapToXMLUtils();
        invalidLayerCount = 0;
    }


    /**
     * Initialize the ArcGIS Objects.
     */
    public boolean init()
    throws IOException {
        logger.debug("init()");
        if(!initializer.initArcGIS()) {
            return false;
        }
        if(!initializer.initArcGISLicenses()) {
            return false;
        }
        return true;
    }

    /**
     * Shutdown the ArcGIS Objects.
     */
    public boolean shutdown()
    throws IOException {
        logger.debug("shutdown()");
        if(initializer == null) {
            throw new IOException("Faild to shutdown!");
        }
        else {
            initializer.shutdownArcGIS();
            return true;
        }
    }

    /**
     * Set the path and filename to the MXD-File.
     *
     * @param name The MXD filename.
     */
    public void setFilename(String name) {
        logger.debug("setFilename()");
        filename = name;
    }

    /**
     * Get the map document from MXD.
     */
    private void openMapDocument()
    throws IOException {
        MapDocument m = new MapDocument();
        if(!m.isMapDocument(filename)) {
            throw new IOException(filename + " is not a map!");
        }
        m.open(filename, null);
        map = m.getMap(0);
    }

    /**
     * Read the MXD file content.
     */
    public boolean read()
    throws Exception {
        logger.debug("read()");
        if(filename.isEmpty()) {
            throw new IOException("Please set filename!");
        }
        util.addFilename(filename);
        openMapDocument();
        MapReader mreader;
        try {
            mreader = new MapReader (map);
            mreader.setUtil(util);
            mreader.read();

        }
        catch(Exception e) {
            throw e;
        }
        try {
            IGraphicsLayer grLayer = (IGraphicsLayer) map.getActiveGraphicsLayer();
            if (map.getActiveGraphicsLayer() instanceof CompositeGraphicsLayer) {
                CompositeGraphicsLayerReader lr = new CompositeGraphicsLayerReader(grLayer);
                lr.setUtil(util);
                lr.read();
            } else {
                logger.debug("Not a known graphics layer type: " +
                             grLayer.getClass().toString());
            }
        }
        catch(Exception e) {
            logger.debug("Error reading graphics layer");
            logger.debug(e);
        }

        for(int i = 0; i < map.getLayerCount();i++) {
            ILayer layer = map.getLayer(i);
            try {
                if (layer instanceof FeatureLayer) {
                    FeatureLayerReader lr = new FeatureLayerReader(layer);
                    lr.setUtil(util);
                    Element layerElement = lr.read();
                    if (layerElement == null) {
                        continue;
                    }
                    IFeatureRenderer renderer =
                        ((FeatureLayer)layer).getRenderer();
                    IRendererReader rreader;
                    if(renderer instanceof SimpleRenderer) {
                        rreader = new SimpleRendererReader(renderer);
                        rreader.setParent(layerElement);
                        rreader.setUtil(util);
                        rreader.read();
                    }
                    else if(renderer instanceof ClassBreaksRenderer) {
                        rreader = new ClassBreakRendererReader(renderer);
                        rreader.setParent(layerElement);
                        rreader.setUtil(util);
                        rreader.read();
                    }
                    else if(renderer instanceof UniqueValueRenderer) {
                        rreader = new UniqueValueRendererReader(renderer);
                        rreader.setParent(layerElement);
                        rreader.setUtil(util);
                        rreader.read();
                    }
                    else{
                        logger.debug("Not a known renderer type " +
                                     renderer.getClass().toString());
                    }
                }
                else if(layer instanceof GdbRasterCatalogLayer) {
                    GdbRasterCatalogLayerReader gclr =
                        new GdbRasterCatalogLayerReader(layer);
                    gclr.setUtil(util);
                    Element layerElement = gclr.read();
                    IFeatureRenderer renderer =
                        ((GdbRasterCatalogLayer)layer).getRenderer();
                    IRendererReader rreader;
                    if(renderer instanceof SimpleRenderer) {
                        rreader = new SimpleRendererReader(renderer);
                        rreader.setParent(layerElement);
                        rreader.setUtil(util);
                        rreader.read();
                    }
                    else if(renderer instanceof ClassBreaksRenderer) {
                        rreader = new ClassBreakRendererReader(renderer);
                        rreader.setParent(layerElement);
                        rreader.setUtil(util);
                        rreader.read();
                    }
                    else if(renderer instanceof UniqueValueRenderer) {
                        rreader = new UniqueValueRendererReader(renderer);
                        rreader.setParent(layerElement);
                        rreader.setUtil(util);
                        rreader.read();
                    }
                    else {
                        logger.debug("Not a known renderer type " +
                                     renderer.getClass().toString());
                    }
                }
                else if(layer instanceof GroupLayer) {
                    GroupLayerReader gl = new GroupLayerReader(layer);
                    gl.setUtil(util);
                    Element layerElement = gl.read("");
                    if(layerElement == null) {
                        throw new IOException ("Error reading grouplayer.");
                    }
                }
                else if(layer instanceof WMSMapLayer) {
                    WMSMapLayerReader lr = new WMSMapLayerReader(layer);
                    lr.setUtil(util);
                    Element layerElement = lr.read("");
                    if(layerElement == null) {
                        throw new IOException ("Error reading WMSMapLayer.");
                    }
                }
                else if(layer instanceof WMSGroupLayer) {
                    WMSGroupLayerReader lr = new WMSGroupLayerReader(layer);
                    lr.setUtil(util);
                    Element layerElement = lr.read("");
                    if(layerElement == null) {
                        throw new IOException ("Error reading WMSGrouplayer.");
                    }
                }
                else if(layer instanceof WMSLayer) {
                    WMSLayerReader lr = new WMSLayerReader(layer);
                    lr.setUtil(util);
                    Element layerElement = lr.read();
                    if(layerElement == null) {
                        throw new IOException ("Error reading WMSlayer.");
                    }
                }
                else if(layer instanceof RasterLayer) {
                    RasterLayerReader lr =
                        new RasterLayerReader(layer);
                    lr.setUtil(util);
                    Element layerElement = lr.read();
                    if(layerElement == null) {
                        throw new IOException ("Error reading RasterLayer.");
                    }
/* TODO implement support for classes in Raster Layers
                    IRasterRenderer renderer =
                        ((RasterLayer)layer).getRenderer();
                    IRendererReader rreader;
                    if(renderer instanceof SimpleRenderer) {
                        rreader = new SimpleRendererReader(renderer);
                        rreader.setParent(layerElement);
                        rreader.setUtil(util);
                        rreader.read();
                    }
                    else if(renderer instanceof ClassBreaksRenderer) {
                        rreader = new ClassBreakRendererReader(renderer);
                        rreader.setParent(layerElement);
                        rreader.setUtil(util);
                        rreader.read();
                    }
                    else if(renderer instanceof UniqueValueRenderer) {
                        rreader = new UniqueValueRendererReader(renderer);
                        rreader.setParent(layerElement);
                        rreader.setUtil(util);
                        rreader.read();
                    }
                    else {
                        logger.debug("Not a known renderer type " +
                                     renderer.getClass().toString());
                    }
*/
                }
                else {
                    logger.info(
                        "Layertype not known: " +
                        layer.getClass().toString());
                }
            }
            catch(Exception e) {
                invalidLayerCount++;
                if(i == 0 && (i + 1) == map.getLayerCount() ||
                   invalidLayerCount == map.getLayerCount()) {
                    throw new IOException(
                        "No valid layer in MXD file." +
                        " Aborting.");
                }
            }
        }
        return true;
    }

    /**
     * Get the root DOM document.
     */
    public Document getMapDocument() {
        logger.debug("getMapDocument()");
        return util.getDocument();
    }
}
// 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)