view src/java/de/intevation/mxd/reader/FeatureLayerReader.java @ 130:5991c1f90f91

Added data source "SDE" to writer.
author vc11884admin@VC11884.win.bsh.de
date Mon, 20 Jun 2011 16:01:17 +0200
parents 39957898c694
children 6b80d2c7536e
line wrap: on
line source
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.geodatabase.FeatureClassName;
import com.esri.arcgis.system.IName;
import com.esri.arcgis.system.IPropertySet;
import org.w3c.dom.Element;

import de.intevation.mxd.utils.MapToXMLUtils;
import java.io.IOException;

/**
 * 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;


    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.");
            throw new IOException(
                this.getClass()+toString() +
                "Error creating dom element");
        }

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

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

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

        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." +
                " Setting shape type to \"none\".");
            type = 0;
        }
        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." +
                " Setting empty definition query.");
            layerElement.setAttribute("definition_query", "");
        }
        try {
            IName fcn = layer.getDataSourceName();
            if(fcn instanceof FeatureClassName) {
                FeatureClassName name = (FeatureClassName)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.");
            throw new IOException(
                this.getClass().toString() +
                "Error reading datasource.");
        }

        try {
            String datatype = layer.getDataSourceType();
            if(datatype.equals("Shapefile Feature Class")) {
                layerElement.setAttribute("connection_type", "local");
                layerElement.setAttribute(
                    "workspace",
                    layer.getWorkspace().getPathName());
            }
            else if(datatype.equals("File Geodatabase Feature Class")){
                layerElement.setAttribute("connection_type", "ogr");
                layerElement.setAttribute("data", layer.getFeatureClass().getFeatureDataset().getName());
                layerElement.setAttribute(
                    "workspace",
                    layer.getWorkspace().getPathName());

            }
            else if(datatype.equals("SDE Feature Class")) {
                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());
                        }
                    }
                }
                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(Exception e) {
            logger.error(
                "Could not read layer datasource." +
                " Stopped reading layer.");
            throw new IOException(
                this.getClass().toString() +
                " Error reading data source.");
        }
        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)