aheinecke@315: /* aheinecke@315: * Copyright (c) 2012 by Intevation GmbH, Germany aheinecke@315: * aheinecke@315: * This file is part of MXD2map. aheinecke@315: * aheinecke@315: * This program is free software under the LGPL (>=v2.1) aheinecke@315: * Read the file LICENCE.txt coming with the software for details aheinecke@315: * or visit http://www.gnu.org/licenses/ if it does not exist. aheinecke@315: * aheinecke@315: * MXD2map has been developed on behalf of the aheinecke@315: * Bundesamt fuer Seeschifffahrt und Hydrographie (BSH) in Hamburg aheinecke@315: * by Intevation GmbH. aheinecke@315: * aheinecke@315: * Authors: aheinecke@315: * Raimund Renkert aheinecke@315: * Bjoern Schilberg aheinecke@315: * Stephan Holl aheinecke@315: * Andre Heinecke aheinecke@315: */ aheinecke@315: aheinecke@315: package de.intevation.mxd.reader; aheinecke@315: aheinecke@315: import org.apache.log4j.Logger; aheinecke@315: aheinecke@315: import com.esri.arcgis.carto.IGraphicsLayer; aheinecke@321: import com.esri.arcgis.carto.ILayer; aheinecke@315: import com.esri.arcgis.carto.CompositeGraphicsLayer; aheinecke@321: import com.esri.arcgis.carto.GraphicsSubLayer; aheinecke@321: aheinecke@315: import com.esri.arcgis.carto.AnnotateLayerPropertiesCollection; aheinecke@315: import com.esri.arcgis.carto.IAnnotateLayerProperties; aheinecke@315: import com.esri.arcgis.carto.LabelEngineLayerProperties; aheinecke@315: import com.esri.arcgis.carto.IElement; aheinecke@315: import com.esri.arcgis.carto.TextElement; aheinecke@315: import com.esri.arcgis.system.IName; aheinecke@315: import com.esri.arcgis.system.IPropertySet; aheinecke@315: import com.esri.arcgis.geometry.Envelope; aheinecke@315: import com.esri.arcgis.geometry.ISpatialReference; aheinecke@315: import com.esri.arcgis.geometry.ProjectedCoordinateSystem; aheinecke@315: import com.esri.arcgis.geometry.GeographicCoordinateSystem; aheinecke@315: import com.esri.arcgis.geometry.UnknownCoordinateSystem; aheinecke@315: import com.esri.arcgis.geometry.Projection; aheinecke@315: import com.esri.arcgis.geometry.IPoint; aheinecke@315: aheinecke@315: import com.esri.arcgis.display.ITextSymbol; aheinecke@315: import com.esri.arcgis.display.TextSymbol; aheinecke@315: aheinecke@315: import org.w3c.dom.Element; aheinecke@315: aheinecke@315: import de.intevation.mxd.utils.MapToXMLUtils; aheinecke@315: import java.io.IOException; aheinecke@315: import com.esri.arcgis.interop.AutomationException; aheinecke@315: /** aheinecke@315: * Reads Layer information. aheinecke@315: * aheinecke@315: * @author Andre Heinecke aheinecke@315: */ aheinecke@315: public class CompositeGraphicsLayerReader aheinecke@315: implements ILayerReader { aheinecke@315: aheinecke@315: /** aheinecke@315: * The logger. aheinecke@315: */ aheinecke@315: private static final Logger logger = aheinecke@315: Logger.getLogger(CompositeGraphicsLayerReader.class); aheinecke@315: aheinecke@315: /** aheinecke@315: * Privte member. aheinecke@315: */ aheinecke@315: private CompositeGraphicsLayer layer; aheinecke@315: private MapToXMLUtils util; aheinecke@315: aheinecke@315: /** aheinecke@315: * Constructor with layer. aheinecke@315: * aheinecke@315: * @param layer The ArcGIS layer object. aheinecke@315: */ aheinecke@315: public CompositeGraphicsLayerReader(IGraphicsLayer layer) aheinecke@315: throws Exception { aheinecke@315: if(layer instanceof CompositeGraphicsLayer) { aheinecke@315: this.layer = (CompositeGraphicsLayer)layer; aheinecke@315: } aheinecke@315: else { aheinecke@315: throw new Exception("Not an instance of CompositeGraphicsLayer: " + aheinecke@315: layer.getClass().toString()); aheinecke@315: } aheinecke@315: } aheinecke@315: aheinecke@315: /** aheinecke@315: * Setter for XML document helper. aheinecke@315: * aheinecke@315: * @param util The helper for storing map information. aheinecke@315: */ aheinecke@315: public void setUtil(MapToXMLUtils util) { aheinecke@315: this.util = util; aheinecke@315: } aheinecke@315: aheinecke@315: /** aheinecke@315: * Reads the Layer content. aheinecke@315: * aheinecke@315: * @return The layer XML element. aheinecke@315: */ aheinecke@315: public Element read() aheinecke@315: throws IOException{ aheinecke@315: logger.debug("read()"); aheinecke@315: Element layerElement = null; aheinecke@315: try { aheinecke@315: layerElement = util.addLayer(); aheinecke@315: } aheinecke@315: catch(Exception e) { aheinecke@315: logger.error("Failed to create DOM-Element for Layer."); aheinecke@315: return null; aheinecke@315: } aheinecke@315: aheinecke@315: // Name aheinecke@315: try { aheinecke@315: layerElement.setAttribute("name", layer.getName()); aheinecke@319: aheinecke@315: } aheinecke@315: catch(Exception e) { aheinecke@315: logger.warn( aheinecke@315: "Could not read layer name." + aheinecke@315: " Stopped reading layer."); aheinecke@315: throw new IOException("Error reading layer name."); aheinecke@315: } aheinecke@315: aheinecke@315: // Scale aheinecke@315: try { aheinecke@315: layerElement.setAttribute("min_scale", aheinecke@315: String.valueOf(layer.getMinimumScale())); aheinecke@315: } aheinecke@315: catch(IOException ioe) { aheinecke@315: logger.warn("Could not read minimum scale."); aheinecke@315: } aheinecke@315: aheinecke@315: try { aheinecke@315: layerElement.setAttribute("max_scale", aheinecke@315: String.valueOf(layer.getMaximumScale())); aheinecke@315: } aheinecke@315: catch(Exception e) { aheinecke@315: logger.warn( aheinecke@315: "Could not read maximum scale."); aheinecke@315: } aheinecke@315: aheinecke@315: // Status aheinecke@315: try { aheinecke@315: if(layer.isVisible()) { aheinecke@315: layerElement.setAttribute("status", "on"); aheinecke@315: } aheinecke@315: else { aheinecke@315: layerElement.setAttribute("status", "off"); aheinecke@315: } aheinecke@315: } aheinecke@315: catch(Exception e) { aheinecke@315: logger.warn( aheinecke@315: "Could not read layer status." + aheinecke@315: " Setting layer status to \"on\"."); aheinecke@315: layerElement.setAttribute("status", "on"); aheinecke@315: } aheinecke@315: aheinecke@315: // Extent aheinecke@315: try { aheinecke@315: Envelope rect = (Envelope)layer.getExtent(); aheinecke@315: layerElement.setAttribute( aheinecke@315: "extent_min_x", aheinecke@315: String.valueOf(rect.getXMin ())); aheinecke@315: layerElement.setAttribute( aheinecke@315: "extent_max_x", aheinecke@315: String.valueOf(rect.getXMax())); aheinecke@315: layerElement.setAttribute( aheinecke@315: "extent_min_y", aheinecke@315: String.valueOf(rect.getYMin())); aheinecke@315: layerElement.setAttribute( aheinecke@315: "extent_max_y", aheinecke@315: String.valueOf(rect.getYMax())); aheinecke@315: } aheinecke@315: catch(Exception e) { aheinecke@315: logger.warn( aheinecke@315: "Could not read extent from layer " aheinecke@315: + layer.getName() + "."); aheinecke@315: } aheinecke@315: aheinecke@315: // Read the elements aheinecke@315: try { aheinecke@315: int count = 0; aheinecke@315: IElement actElement = null; aheinecke@315: IElement prevElement = null; aheinecke@315: layer.reset(); // Reset the element container aheinecke@315: actElement = layer.next(); aheinecke@315: while (actElement != prevElement) { aheinecke@315: prevElement = actElement; aheinecke@315: if (actElement instanceof TextElement) { aheinecke@315: TextElement te = (TextElement)actElement; aheinecke@315: Element xmlTextElement = util.addFeature(layerElement); aheinecke@315: xmlTextElement.setAttribute("text", te.getText()); aheinecke@315: aheinecke@315: IPoint poi = te.getGeometry().getEnvelope().getLowerLeft(); aheinecke@315: xmlTextElement.setAttribute("X", String.valueOf(poi.getX())); aheinecke@315: xmlTextElement.setAttribute("Y", String.valueOf(poi.getY())); aheinecke@321: xmlTextElement.setAttribute("classId", String.valueOf(count)); aheinecke@315: aheinecke@315: try { aheinecke@315: ITextSymbol sym = te.getSymbol(); aheinecke@315: if(sym instanceof TextSymbol) { aheinecke@315: TextSymbolReader tsr = new TextSymbolReader(sym); aheinecke@315: tsr.setParent(xmlTextElement); aheinecke@315: tsr.setUtil(util); aheinecke@315: tsr.read(); aheinecke@315: } else { aheinecke@315: logger.warn("Unknwon Symbol of class: " + aheinecke@315: sym.getClass().toString()); aheinecke@315: } aheinecke@315: } aheinecke@315: catch(Exception e) { aheinecke@315: logger.warn("Could not read element text symbol."); aheinecke@315: } aheinecke@315: } else { aheinecke@315: logger.warn("Unhandled Element of class: " + aheinecke@315: actElement.getClass().toString() + aheinecke@315: " in conversion of layer: " + aheinecke@315: layer.getName()); aheinecke@315: } aheinecke@315: count++; aheinecke@321: actElement = layer.next(); aheinecke@315: } aheinecke@315: } aheinecke@315: catch(Exception e) { aheinecke@315: logger.warn("Could not read layer elements."); aheinecke@315: logger.debug(e); aheinecke@315: } aheinecke@315: aheinecke@315: aheinecke@315: //Read the projection. aheinecke@315: try { aheinecke@315: ISpatialReference sr = layer.getSpatialReference(); aheinecke@315: int projection = 0; aheinecke@315: if(sr instanceof ProjectedCoordinateSystem) { aheinecke@315: ProjectedCoordinateSystem pcs = (ProjectedCoordinateSystem)sr; aheinecke@315: projection = pcs.getFactoryCode(); aheinecke@315: } aheinecke@315: else if(sr instanceof GeographicCoordinateSystem) { aheinecke@315: GeographicCoordinateSystem gcs = (GeographicCoordinateSystem)sr; aheinecke@315: projection = gcs.getFactoryCode(); aheinecke@315: } aheinecke@315: else if(sr instanceof UnknownCoordinateSystem) { aheinecke@315: UnknownCoordinateSystem ucs = (UnknownCoordinateSystem)sr; aheinecke@315: projection = 0; aheinecke@315: } aheinecke@315: else{ aheinecke@315: logger.debug( aheinecke@315: "Unknown SpatialReference: " + aheinecke@315: sr.getClass().toString()); aheinecke@315: } aheinecke@315: aheinecke@315: if(projection == 0) { aheinecke@315: logger.warn( aheinecke@315: "Unknown projection for Layer:" + layer.getName() + aheinecke@315: " Please edit projection in resulting mapfile."); aheinecke@315: } aheinecke@315: layerElement.setAttribute("projection", String.valueOf(projection)); aheinecke@315: } aheinecke@315: catch(Exception e) { aheinecke@315: logger.warn("Could not read layer projection."); aheinecke@315: logger.debug(e); aheinecke@315: } aheinecke@315: aheinecke@315: // Static values for this layer aheinecke@321: layerElement.setAttribute("type", "annotation"); aheinecke@315: aheinecke@321: // Read Sublayers aheinecke@321: for (int i = 0; i < layer.getCount(); i++) { aheinecke@321: ILayer pLyr = layer.getLayer(i); aheinecke@321: GraphicsSubLayer subbie = new GraphicsSubLayer((Object)pLyr); aheinecke@321: if (subbie != null) { aheinecke@321: GraphicsSubLayerReader lr = new GraphicsSubLayerReader(subbie); aheinecke@321: lr.setUtil(util); aheinecke@321: lr.read(); aheinecke@321: } else { aheinecke@321: logger.warn("Could not convert sublayer to GraphicsSubLayer"); aheinecke@321: } aheinecke@321: } aheinecke@315: return layerElement; aheinecke@315: } aheinecke@315: } aheinecke@315: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :