view src/java/de/intevation/mxd/reader/FeatureLayerReader.java @ 321:b6c0fbae16dc

Expand Graphics Layer Support
author Andre Heinecke <aheinecke@intevation.de>
date Thu, 20 Sep 2012 17:58:29 +0200
parents 5e3a40a84539
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 org.apache.log4j.Logger;

import com.esri.arcgis.carto.ILayer;
import com.esri.arcgis.carto.FeatureLayer;
import com.esri.arcgis.carto.AnnotateLayerPropertiesCollection;
import com.esri.arcgis.carto.IAnnotateLayerProperties;
import com.esri.arcgis.carto.LabelEngineLayerProperties;
import com.esri.arcgis.geodatabase.FeatureClassName;
import com.esri.arcgis.datasourcesGDB.FgdbFeatureClassName;
import com.esri.arcgis.system.IName;
import com.esri.arcgis.system.IPropertySet;
import com.esri.arcgis.geometry.Envelope;
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 org.w3c.dom.Element;

import de.intevation.mxd.utils.MapToXMLUtils;
import java.io.IOException;
import com.esri.arcgis.interop.AutomationException;
/**
 * Reads Layer information.
 *
 * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a>
 */
public class FeatureLayerReader
implements ILayerReader {

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

    /**
     * Privte member.
     */
    private FeatureLayer  layer;
    private MapToXMLUtils util;

    /**
     * Constructor with layer.
     *
     * @param layer The ArcGIS layer object.
     */
    public FeatureLayerReader(ILayer layer)
    throws Exception {
        if(layer instanceof FeatureLayer) {
            this.layer = (FeatureLayer)layer;
        }
        else {
            throw new Exception("Not an instance of FeatureLayer: " +
                layer.getClass().toString());
        }
    }

    /**
     * Setter for XML document helper.
     *
     * @param util The helper for storing map information.
     */
    public void setUtil(MapToXMLUtils util) {
        this.util = util;
    }

    /**
     * Reads the Layer content.
     *
     * @return The layer XML element.
     */
    public Element read()
    throws IOException{
        logger.debug("read()");
        Element layerElement;
        try {
            layerElement = util.addLayer();
        }
        catch(Exception e) {
            logger.error("Failed to create DOM-Element for Layer.");
            return null;
        }

        try {
            layerElement.setAttribute("name", layer.getName());
        }
        catch(IOException ioe) {
            logger.warn(
                "Could not read layer name." +
                " Stopped reading layer.");
            throw new IOException("Error reading layer name.");
        }

        try {
            layerElement.setAttribute("min_scale",
                String.valueOf(layer.getMinimumScale()));
        }
        catch(IOException ioe) {
            logger.warn("Could not read minimum scale.");
        }

        try {
            layerElement.setAttribute("max_scale",
                String.valueOf(layer.getMaximumScale()));
        }
        catch(IOException ioe) {
            logger.warn(
                "Could not read maximum scale.");
        }

        try {
            if(layer.isVisible()) {
                layerElement.setAttribute("status", "on");
            }
            else {
                layerElement.setAttribute("status", "off");
            }
        }
        catch(IOException ioe) {
            logger.warn(
                "Could not read layer status." +
                " Setting layer status to \"on\".");
            layerElement.setAttribute("status", "on");
        }

        int type = 0;
        try {
            type = layer.getShapeType();
        }
        catch(IOException ioe) {
            logger.warn("Could not read shape type.");
            throw new IOException("Error reading shape type.");
        }
        switch (type) {
            case 0: layerElement.setAttribute("type", "none"); break;
            case 1: layerElement.setAttribute("type", "point"); break;
            case 3: layerElement.setAttribute("type", "line"); break;
            case 4: layerElement.setAttribute("type", "polygon"); break;
        }

        try {
            layerElement.setAttribute("definition_query",
                layer.getDefinitionExpression());
        }
        catch(IOException ioe) {
            logger.warn(
                "Could not read definition query.");
        }

        try {
            AnnotateLayerPropertiesCollection annotation =
                (AnnotateLayerPropertiesCollection)
                  layer.getAnnotationProperties();

            if (layer.isDisplayAnnotation() && annotation.getCount() > 0){
                for(int i = 0; i < annotation.getCount(); i++) {
                    IAnnotateLayerProperties prop = annotation.getProperties(0);
                    if(prop instanceof LabelEngineLayerProperties) {
                        try {
                            LabelEngineReader lr = new LabelEngineReader(prop);
                            lr.setParent(layerElement);
                            lr.setUtil(util);
                            lr.read();
                        }
                        catch(Exception e) {
                            logger.warn("Could not read label properties.");
                        }
                    } else {
                        logger.debug("Unknown Layer class: " + prop.getClass().toString());
                    }
                }
            }
        }
        catch(IOException ioe) {
            logger.warn("Could not read Annotation properties.");
        }

        try {
            IName fcn = layer.getDataSourceName();
            if(fcn instanceof FeatureClassName) {
                FeatureClassName name = (FeatureClassName)fcn;
                layerElement.setAttribute("data_source", name.getName());
            } else if (fcn instanceof FgdbFeatureClassName) {
                FgdbFeatureClassName name = (FgdbFeatureClassName)fcn;
                layerElement.setAttribute("data_source", name.getName());
            }
            else {
                logger.debug (
                    "Unknown FeatureClass name:" +
                    fcn.getClass().toString());
            }
        }
        catch(IOException ioe) {
            logger.warn(
                "Could not read datasource." +
                " Stopped reading layer " + layer.getName() + ".");
            util.removeLayer(layerElement);
            return null;
        }

        try {
            Envelope rect = (Envelope)layer.getExtent();
            layerElement.setAttribute(
                "extent_min_x",
                String.valueOf(rect.getXMin ()));
            layerElement.setAttribute(
                "extent_max_x",
                String.valueOf(rect.getXMax()));
            layerElement.setAttribute(
                "extent_min_y",
                String.valueOf(rect.getYMin()));
            layerElement.setAttribute(
                "extent_max_y",
                String.valueOf(rect.getYMax()));
        }
        catch(IOException ioe) {
            logger.warn(
                "Could not read extent from layer "
                + layer.getName() + ".");
        }
        //Read the projection.
        try {
            ISpatialReference sr = layer.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 for Layer:" + layer.getName() +
                    " Please edit projection in resulting mapfile.");
            }
            layerElement.setAttribute("projection", String.valueOf(projection));
        }
        catch(IOException ioe) {
            logger.warn("Could not read layer projection.");
        }

        try {
            if (layer.getDataSourceName() instanceof FgdbFeatureClassName) {
                FgdbFeatureClassName name = (FgdbFeatureClassName)layer.getDataSourceName();
                layerElement.setAttribute("connection_type", "ogr");
                layerElement.setAttribute("data", name.getName());
                layerElement.setAttribute("connection", name.getWorkspaceName().getPathName());
            }
            else if(layer.getWorkspace().getType() == 0) {
                layerElement.setAttribute("connection_type", "local");
                layerElement.setAttribute(
                    "workspace",
                    layer.getWorkspace().getPathName());
            }
            else if(layer.getWorkspace().getType() == 1){
                layerElement.setAttribute("connection_type", "ogr");
                layerElement.setAttribute(
                    "data",
                    layer.getFeatureClass().getFeatureDataset().getName());
                layerElement.setAttribute(
                    "workspace",
                    layer.getWorkspace().getPathName());

            }
            else if(layer.getWorkspace().getType() == 2) {
                IPropertySet set =
                    layer.getWorkspace().getConnectionProperties();
                Object names[] = new Object[set.getCount()];
                Object prop[] = new Object[set.getCount()];
                set.getAllProperties(names, prop);
                layerElement.setAttribute("connection_type", "SDE");
                for(int i = 0; i < names.length; i++) {
                    if(names[i] != null) {
                        String[] prop_names = (String[])names[i];
                        for(int j = 0; j < prop_names.length; j++) {
                            layerElement.setAttribute(
                                prop_names[j].toLowerCase(),
                                set.getProperty(prop_names[j]).toString());
                        }
                    }
                }
                try {
                    layerElement.setAttribute(
                        "join_table",
                        layer.getRelationshipClass()
                             .getOriginClass().getAliasName());
                    layerElement.setAttribute(
                        "join_field",
                        layer.getRelationshipClass().getOriginPrimaryKey());
                    layerElement.setAttribute(
                        "join_table_target",
                        layer.getRelationshipClass()
                             .getDestinationClass().getAliasName());
                    layerElement.setAttribute(
                        "join_field_target",
                        layer.getRelationshipClass().getOriginForeignKey());
                }
                catch(AutomationException ioe) {
                    //Do nothing, cause no jointable defined.
                }
                catch(IOException ae) {
                    //Do nothing, cause no jointable defined.
                }
                catch(NullPointerException npe) {
                    //Do nothing, cause no jointable defined.
                }
            }
        }
        catch(Exception e) {
            logger.debug(e);
            logger.error(
                "Could not read layer datasource." +
                " Stopped reading layer " + layer.getName() + ".");
            util.removeLayer(layerElement);
            return null;
        }
        return layerElement;
    }
}
// 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)