rrenkert@243: /*
rrenkert@243:  * Copyright (c) 2011 by Intevation GmbH, Germany <info@intevation.de>
rrenkert@243:  *
rrenkert@243:  * This file is part of MXD2map.
rrenkert@243:  *
rrenkert@243:  * This program is free software under the LGPL (>=v2.1)
rrenkert@243:  * Read the file LICENCE.txt coming with the software for details
rrenkert@243:  * or visit http://www.gnu.org/licenses/ if it does not exist.
rrenkert@243:  *
rrenkert@243:  * MXD2map has been developed on behalf of the
rrenkert@243:  * Bundesamt fuer Seeschifffahrt und Hydrographie (BSH) in Hamburg
rrenkert@243:  * by Intevation GmbH.
rrenkert@243:  *
rrenkert@243:  * Authors:
rrenkert@243:  * Raimund Renkert <raimund.renkert@intevation.de>
rrenkert@243:  * Bjoern Schilberg <bjoern.schilberg@intevation.de>
rrenkert@243:  * Stephan Holl <stephan.holl@intevation.de>
rrenkert@243:  */
rrenkert@243: 
rrenkert@33: package de.intevation.mxd.reader;
rrenkert@25: 
rrenkert@25: import org.apache.log4j.Logger;
rrenkert@25: 
rrenkert@25: import com.esri.arcgis.carto.ILayer;
rrenkert@25: import com.esri.arcgis.carto.FeatureLayer;
raimund@179: import com.esri.arcgis.carto.AnnotateLayerPropertiesCollection;
raimund@179: import com.esri.arcgis.carto.IAnnotateLayerProperties;
raimund@179: import com.esri.arcgis.carto.LabelEngineLayerProperties;
rrenkert@42: import com.esri.arcgis.geodatabase.FeatureClassName;
rrenkert@42: import com.esri.arcgis.system.IName;
vc11884admin@113: import com.esri.arcgis.system.IPropertySet;
raimund@246: import com.esri.arcgis.geometry.Envelope;
raimund@246: 
rrenkert@29: import org.w3c.dom.Element;
rrenkert@29: 
rrenkert@33: import de.intevation.mxd.utils.MapToXMLUtils;
rrenkert@117: import java.io.IOException;
vc11884admin@133: import com.esri.arcgis.interop.AutomationException;
rrenkert@25: /**
rrenkert@43:  * Reads Layer information.
rrenkert@25:  *
rrenkert@25:  * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a>
rrenkert@25:  */
rrenkert@43: public class FeatureLayerReader
rrenkert@43: implements ILayerReader {
rrenkert@25: 
rrenkert@31:     /**
rrenkert@31:      * The logger.
rrenkert@31:      */
rrenkert@31:     private static final Logger logger =
rrenkert@31:         Logger.getLogger(FeatureLayerReader.class);
rrenkert@25: 
rrenkert@31:     /**
rrenkert@31:      * Privte member.
rrenkert@31:      */
rrenkert@29:     private FeatureLayer  layer;
rrenkert@31:     private MapToXMLUtils util;
rrenkert@25: 
rrenkert@181:     /**
rrenkert@181:      * Constructor with layer.
rrenkert@181:      *
rrenkert@181:      * @param layer The ArcGIS layer object.
rrenkert@181:      */
rrenkert@31:     public FeatureLayerReader(ILayer layer)
rrenkert@31:     throws Exception {
rrenkert@43:         if(layer instanceof FeatureLayer) {
rrenkert@29:             this.layer = (FeatureLayer)layer;
rrenkert@43:         }
rrenkert@43:         else {
rrenkert@29:             throw new Exception("Not an instance of FeatureLayer: " +
rrenkert@31:                 layer.getClass().toString());
rrenkert@43:         }
rrenkert@25:     }
rrenkert@25: 
rrenkert@25:     /**
rrenkert@31:      * Setter for XML document helper.
rrenkert@31:      *
rrenkert@31:      * @param util The helper for storing map information.
rrenkert@29:      */
rrenkert@43:     public void setUtil(MapToXMLUtils util) {
rrenkert@31:         this.util = util;
rrenkert@29:     }
rrenkert@29: 
rrenkert@29:     /**
rrenkert@25:      * Reads the Layer content.
rrenkert@31:      *
rrenkert@31:      * @return The layer XML element.
rrenkert@25:      */
rrenkert@118:     public Element read()
rrenkert@118:     throws IOException{
rrenkert@25:         logger.debug("read()");
rrenkert@117:         Element layerElement;
rrenkert@117:         try {
rrenkert@117:             layerElement = util.addLayer();
rrenkert@117:         }
rrenkert@117:         catch(Exception e) {
rrenkert@117:             logger.error("Failed to create DOM-Element for Layer.");
vc11884admin@133:             return null;
rrenkert@117:         }
rrenkert@25: 
rrenkert@117:         try {
rrenkert@117:             layerElement.setAttribute("name", layer.getName());
rrenkert@117:         }
rrenkert@117:         catch(IOException ioe) {
rrenkert@117:             logger.warn(
rrenkert@135:                 "Could not read layer name." +
rrenkert@135:                 " Stopped reading layer.");
rrenkert@135:             throw new IOException("Error reading layer name.");
rrenkert@117:         }
rrenkert@117: 
rrenkert@117:         try {
rrenkert@117:             layerElement.setAttribute("min_scale",
rrenkert@117:                 String.valueOf(layer.getMinimumScale()));
rrenkert@117:         }
rrenkert@117:         catch(IOException ioe) {
rrenkert@135:             logger.warn("Could not read minimum scale.");
rrenkert@117:         }
rrenkert@117: 
rrenkert@117:         try {
rrenkert@117:             layerElement.setAttribute("max_scale",
rrenkert@117:                 String.valueOf(layer.getMaximumScale()));
rrenkert@117:         }
rrenkert@117:         catch(IOException ioe) {
rrenkert@117:             logger.warn(
rrenkert@135:                 "Could not read maximum scale.");
rrenkert@117:         }
rrenkert@117: 
rrenkert@117:         try {
rrenkert@117:             if(layer.isVisible()) {
rrenkert@117:                 layerElement.setAttribute("status", "on");
rrenkert@117:             }
rrenkert@117:             else {
rrenkert@117:                 layerElement.setAttribute("status", "off");
rrenkert@117:             }
rrenkert@117:         }
rrenkert@117:         catch(IOException ioe) {
rrenkert@117:             logger.warn(
rrenkert@117:                 "Could not read layer status." +
rrenkert@117:                 " Setting layer status to \"on\".");
rrenkert@31:             layerElement.setAttribute("status", "on");
rrenkert@25:         }
rrenkert@117: 
rrenkert@117:         int type = 0;
rrenkert@117:         try {
rrenkert@117:             type = layer.getShapeType();
rrenkert@25:         }
rrenkert@117:         catch(IOException ioe) {
rrenkert@135:             logger.warn("Could not read shape type.");
rrenkert@135:             throw new IOException("Error reading shape type.");
rrenkert@117:         }
rrenkert@40:         switch (type) {
rrenkert@62:             case 0: layerElement.setAttribute("type", "none"); break;
rrenkert@62:             case 1: layerElement.setAttribute("type", "point"); break;
rrenkert@62:             case 3: layerElement.setAttribute("type", "line"); break;
rrenkert@97:             case 4: layerElement.setAttribute("type", "polygon"); break;
rrenkert@40:         }
rrenkert@40: 
rrenkert@117:         try {
rrenkert@117:             layerElement.setAttribute("definition_query",
rrenkert@117:                 layer.getDefinitionExpression());
rrenkert@42:         }
rrenkert@117:         catch(IOException ioe) {
rrenkert@117:             logger.warn(
rrenkert@135:                 "Could not read definition query.");
rrenkert@117:         }
raimund@179: 
raimund@179:         try {
rrenkert@180:             AnnotateLayerPropertiesCollection annotation =
rrenkert@180:                 (AnnotateLayerPropertiesCollection)
rrenkert@180:                   layer.getAnnotationProperties();
rrenkert@180: 
raimund@179:             if (layer.isDisplayAnnotation() && annotation.getCount() > 0){
raimund@179:                 for(int i = 0; i < annotation.getCount(); i++) {
raimund@179:                     IAnnotateLayerProperties prop = annotation.getProperties(0);
raimund@179:                     if(prop instanceof LabelEngineLayerProperties) {
raimund@179:                         try {
raimund@179:                             LabelEngineReader lr = new LabelEngineReader(prop);
raimund@179:                             lr.setParent(layerElement);
raimund@179:                             lr.setUtil(util);
raimund@179:                             lr.read();
raimund@179:                         }
raimund@179:                         catch(Exception e) {
raimund@179:                             logger.warn("Could not read label properties.");
raimund@179:                         }
raimund@179:                     }
raimund@179:                 }
raimund@179:             }
raimund@179:         }
raimund@179:         catch(IOException ioe) {
raimund@179:             logger.warn("Could not read Annotation properties.");
raimund@179:         }
raimund@179: 
rrenkert@117:         try {
rrenkert@117:             IName fcn = layer.getDataSourceName();
rrenkert@117:             if(fcn instanceof FeatureClassName) {
rrenkert@117:                 FeatureClassName name = (FeatureClassName)fcn;
rrenkert@117:                 layerElement.setAttribute("data_source", name.getName());
rrenkert@117:             }
rrenkert@117:             else {
rrenkert@117:                 logger.debug (
rrenkert@117:                     "Unknown FeatureClass name:" +
rrenkert@117:                     fcn.getClass().toString());
rrenkert@117:             }
rrenkert@117:         }
rrenkert@117:         catch(IOException ioe) {
vc11884admin@133:             logger.warn(
vc11884admin@133:                 "Could not read datasource." +
vc11884admin@133:                 " Stopped reading layer " + layer.getName() + ".");
vc11884admin@133:             util.removeLayer(layerElement);
vc11884admin@133:             return null;
rrenkert@100:         }
rrenkert@31: 
rrenkert@117:         try {
raimund@246:             Envelope rect = (Envelope)layer.getExtent();
raimund@246:             layerElement.setAttribute(
raimund@246:                 "extent_min_x",
raimund@246:                 String.valueOf(rect.getXMin ()));
raimund@246:             layerElement.setAttribute(
raimund@246:                 "extent_max_x",
raimund@246:                 String.valueOf(rect.getXMax()));
raimund@246:             layerElement.setAttribute(
raimund@248:                 "extent_min_y",
raimund@246:                 String.valueOf(rect.getYMin()));
raimund@246:             layerElement.setAttribute(
raimund@248:                 "extent_max_y",
raimund@246:                 String.valueOf(rect.getYMax()));
raimund@246:         }
raimund@246:         catch(IOException ioe) {
raimund@246:             logger.warn(
raimund@246:                 "Could not read extent from layer "
raimund@246:                 + layer.getName() + ".");
raimund@246:         }
raimund@246:         try {
rrenkert@117:             String datatype = layer.getDataSourceType();
vc11884admin@133:             if(layer.getWorkspace().getType() == 0) {
rrenkert@117:                 layerElement.setAttribute("connection_type", "local");
rrenkert@117:                 layerElement.setAttribute(
rrenkert@117:                     "workspace",
rrenkert@117:                     layer.getWorkspace().getPathName());
rrenkert@117:             }
vc11884admin@133:             else if(layer.getWorkspace().getType() == 1){
rrenkert@117:                 layerElement.setAttribute("connection_type", "ogr");
vc11884admin@133:                 layerElement.setAttribute(
vc11884admin@133:                     "data",
vc11884admin@133:                     layer.getFeatureClass().getFeatureDataset().getName());
rrenkert@117:                 layerElement.setAttribute(
rrenkert@117:                     "workspace",
rrenkert@117:                     layer.getWorkspace().getPathName());
rrenkert@100: 
rrenkert@117:             }
vc11884admin@133:             else if(layer.getWorkspace().getType() == 2) {
rrenkert@180:                 IPropertySet set =
rrenkert@180:                     layer.getWorkspace().getConnectionProperties();
rrenkert@117:                 Object names[] = new Object[set.getCount()];
rrenkert@117:                 Object prop[] = new Object[set.getCount()];
rrenkert@117:                 set.getAllProperties(names, prop);
rrenkert@117:                 layerElement.setAttribute("connection_type", "SDE");
rrenkert@117:                 for(int i = 0; i < names.length; i++) {
rrenkert@117:                     if(names[i] != null) {
rrenkert@117:                         String[] prop_names = (String[])names[i];
rrenkert@117:                         for(int j = 0; j < prop_names.length; j++) {
rrenkert@117:                             layerElement.setAttribute(
rrenkert@117:                                 prop_names[j].toLowerCase(),
rrenkert@117:                                 set.getProperty(prop_names[j]).toString());
rrenkert@117:                         }
vc11884admin@113:                     }
vc11884admin@113:                 }
vc11884admin@133:                 try {
vc11884admin@133:                     layerElement.setAttribute(
vc11884admin@133:                         "join_table",
vc11884admin@133:                         layer.getRelationshipClass()
vc11884admin@133:                              .getOriginClass().getAliasName());
vc11884admin@133:                     layerElement.setAttribute(
vc11884admin@133:                         "join_field",
vc11884admin@133:                         layer.getRelationshipClass().getOriginPrimaryKey());
vc11884admin@133:                     layerElement.setAttribute(
vc11884admin@133:                         "join_table_target",
vc11884admin@133:                         layer.getRelationshipClass()
vc11884admin@133:                              .getDestinationClass().getAliasName());
vc11884admin@133:                     layerElement.setAttribute(
vc11884admin@133:                         "join_field_target",
vc11884admin@133:                         layer.getRelationshipClass().getOriginForeignKey());
vc11884admin@133:                 }
vc11884admin@133:                 catch(AutomationException ioe) {
vc11884admin@133:                     //Do nothing, cause no jointable defined.
vc11884admin@133:                 }
vc11884admin@133:                 catch(IOException ae) {
vc11884admin@133:                     //Do nothing, cause no jointable defined.
vc11884admin@133:                 }
vc11884admin@133:                 catch(NullPointerException npe) {
vc11884admin@133:                     //Do nothing, cause no jointable defined.
vc11884admin@133:                 }
vc11884admin@113:             }
rrenkert@100:         }
rrenkert@117:         catch(Exception e) {
rrenkert@117:             logger.error(
rrenkert@117:                 "Could not read layer datasource." +
vc11884admin@133:                 " Stopped reading layer " + layer.getName() + ".");
vc11884admin@133:             util.removeLayer(layerElement);
rrenkert@135:             return null;
rrenkert@117:         }
rrenkert@31:         return layerElement;
rrenkert@25:     }
rrenkert@25: }
rrenkert@25: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :