rrenkert@243: /* rrenkert@243: * Copyright (c) 2011 by Intevation GmbH, Germany 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 rrenkert@243: * Bjoern Schilberg rrenkert@243: * Stephan Holl rrenkert@243: */ rrenkert@243: rrenkert@33: package de.intevation.mxd.reader; rrenkert@25: rrenkert@25: import java.io.IOException; 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.IMap; rrenkert@25: import com.esri.arcgis.carto.MapDocument; rrenkert@25: import com.esri.arcgis.carto.FeatureLayer; rrenkert@36: import com.esri.arcgis.carto.GdbRasterCatalogLayer; rrenkert@25: import com.esri.arcgis.carto.IFeatureRenderer; rrenkert@25: import com.esri.arcgis.carto.SimpleRenderer; rrenkert@25: import com.esri.arcgis.carto.ClassBreaksRenderer; rrenkert@25: import com.esri.arcgis.carto.UniqueValueRenderer; vc11884admin@148: import com.esri.arcgis.carto.GroupLayer; aheinecke@301: import com.esri.arcgis.carto.WMSMapLayer; aheinecke@301: import com.esri.arcgis.carto.WMSGroupLayer; aheinecke@301: import com.esri.arcgis.carto.WMSLayer; aheinecke@303: import com.esri.arcgis.carto.RasterLayer; rrenkert@25: rrenkert@27: import org.w3c.dom.Document; rrenkert@27: import org.w3c.dom.Element; rrenkert@27: rrenkert@33: import de.intevation.mxd.ArcGISInitializer; rrenkert@33: import de.intevation.mxd.utils.MapToXMLUtils; rrenkert@33: rrenkert@25: /** rrenkert@25: * The MXD file reader. rrenkert@25: * rrenkert@25: * @author Raimund Renkert rrenkert@25: */ rrenkert@43: public class MXDReader rrenkert@43: implements IReader { rrenkert@43: /** rrenkert@43: * The Logger. rrenkert@43: */ rrenkert@43: private static final Logger logger = Logger.getLogger(MXDReader.class); rrenkert@25: rrenkert@43: /** rrenkert@43: * Private member. rrenkert@43: */ rrenkert@25: private String filename = ""; rrenkert@25: private ArcGISInitializer initializer = null; rrenkert@40: private MapToXMLUtils util; rrenkert@25: private IMap map; rrenkert@118: private int invalidLayerCount; rrenkert@25: rrenkert@181: /** rrenkert@181: * Default constructor. rrenkert@181: */ rrenkert@43: public MXDReader() rrenkert@43: throws IOException { rrenkert@25: logger.debug("constructor()"); rrenkert@25: initializer = new ArcGISInitializer(); rrenkert@40: util = new MapToXMLUtils(); rrenkert@118: invalidLayerCount = 0; rrenkert@25: } rrenkert@25: rrenkert@25: rrenkert@25: /** rrenkert@25: * Initialize the ArcGIS Objects. rrenkert@25: */ rrenkert@43: public boolean init() rrenkert@43: throws IOException { rrenkert@25: logger.debug("init()"); rrenkert@103: if(!initializer.initArcGIS()) { rrenkert@103: return false; rrenkert@103: } rrenkert@103: if(!initializer.initArcGISLicenses()) { rrenkert@103: return false; rrenkert@103: } rrenkert@25: return true; rrenkert@25: } rrenkert@25: rrenkert@25: /** rrenkert@25: * Shutdown the ArcGIS Objects. rrenkert@25: */ rrenkert@43: public boolean shutdown() rrenkert@43: throws IOException { rrenkert@25: logger.debug("shutdown()"); rrenkert@43: if(initializer == null) { rrenkert@25: throw new IOException("Faild to shutdown!"); rrenkert@25: } rrenkert@43: else { rrenkert@25: initializer.shutdownArcGIS(); rrenkert@25: return true; rrenkert@25: } rrenkert@25: } rrenkert@25: rrenkert@25: /** rrenkert@25: * Set the path and filename to the MXD-File. rrenkert@181: * rrenkert@181: * @param name The MXD filename. rrenkert@25: */ rrenkert@43: public void setFilename(String name) { rrenkert@25: logger.debug("setFilename()"); rrenkert@25: filename = name; rrenkert@25: } rrenkert@25: rrenkert@181: /** rrenkert@181: * Get the map document from MXD. rrenkert@181: */ rrenkert@43: private void openMapDocument() rrenkert@43: throws IOException { rrenkert@27: MapDocument m = new MapDocument(); rrenkert@43: if(!m.isMapDocument(filename)) { rrenkert@27: throw new IOException(filename + " is not a map!"); rrenkert@27: } rrenkert@27: m.open(filename, null); rrenkert@27: map = m.getMap(0); rrenkert@27: } rrenkert@43: rrenkert@25: /** rrenkert@25: * Read the MXD file content. rrenkert@25: */ rrenkert@43: public boolean read() rrenkert@121: throws Exception { rrenkert@25: logger.debug("read()"); rrenkert@43: if(filename == "") { rrenkert@25: throw new IOException("Please set filename!"); rrenkert@25: } rrenkert@43: else { rrenkert@40: util.addFilename(filename); rrenkert@27: openMapDocument(); rrenkert@40: MapReader mreader; rrenkert@40: try { rrenkert@40: mreader = new MapReader (map); rrenkert@40: mreader.setUtil(util); rrenkert@40: mreader.read(); rrenkert@40: rrenkert@40: } rrenkert@40: catch(Exception e) { rrenkert@121: throw e; rrenkert@40: } rrenkert@27: rrenkert@43: for(int i = 0; i < map.getLayerCount();i++) { rrenkert@27: ILayer layer = map.getLayer(i); rrenkert@43: try { rrenkert@43: if (layer instanceof FeatureLayer) { rrenkert@36: FeatureLayerReader lr = new FeatureLayerReader(layer); rrenkert@36: lr.setUtil(util); rrenkert@36: Element layerElement = lr.read(); vc11884admin@133: if (layerElement == null) { vc11884admin@133: continue; vc11884admin@133: } rrenkert@43: IFeatureRenderer renderer = rrenkert@43: ((FeatureLayer)layer).getRenderer(); rrenkert@27: IRendererReader rreader; rrenkert@43: if(renderer instanceof SimpleRenderer) { rrenkert@25: rreader = new SimpleRendererReader(renderer); rrenkert@31: rreader.setParent(layerElement); rrenkert@31: rreader.setUtil(util); rrenkert@25: rreader.read(); rrenkert@25: } rrenkert@43: else if(renderer instanceof ClassBreaksRenderer) { rrenkert@25: rreader = new ClassBreakRendererReader(renderer); rrenkert@31: rreader.setParent(layerElement); rrenkert@31: rreader.setUtil(util); rrenkert@25: rreader.read(); rrenkert@25: } rrenkert@43: else if(renderer instanceof UniqueValueRenderer) { rrenkert@25: rreader = new UniqueValueRendererReader(renderer); rrenkert@31: rreader.setParent(layerElement); rrenkert@31: rreader.setUtil(util); rrenkert@25: rreader.read(); rrenkert@25: } rrenkert@25: else{ rrenkert@69: logger.debug("Not a known renderer type " + rrenkert@69: renderer.getClass().toString()); rrenkert@25: } rrenkert@25: } rrenkert@36: else if(layer instanceof GdbRasterCatalogLayer) { rrenkert@36: GdbRasterCatalogLayerReader gclr = rrenkert@36: new GdbRasterCatalogLayerReader(layer); rrenkert@36: gclr.setUtil(util); rrenkert@36: Element layerElement = gclr.read(); rrenkert@43: IFeatureRenderer renderer = rrenkert@43: ((GdbRasterCatalogLayer)layer).getRenderer(); rrenkert@36: IRendererReader rreader; rrenkert@43: if(renderer instanceof SimpleRenderer) { rrenkert@36: rreader = new SimpleRendererReader(renderer); rrenkert@36: rreader.setParent(layerElement); rrenkert@36: rreader.setUtil(util); rrenkert@36: rreader.read(); rrenkert@36: } rrenkert@43: else if(renderer instanceof ClassBreaksRenderer) { rrenkert@36: rreader = new ClassBreakRendererReader(renderer); rrenkert@36: rreader.setParent(layerElement); rrenkert@36: rreader.setUtil(util); rrenkert@36: rreader.read(); rrenkert@36: } rrenkert@43: else if(renderer instanceof UniqueValueRenderer) { rrenkert@36: rreader = new UniqueValueRendererReader(renderer); rrenkert@36: rreader.setParent(layerElement); rrenkert@36: rreader.setUtil(util); rrenkert@36: rreader.read(); rrenkert@36: } rrenkert@43: else { rrenkert@69: logger.debug("Not a known renderer type " + rrenkert@69: renderer.getClass().toString()); rrenkert@36: } rrenkert@36: } vc11884admin@148: else if(layer instanceof GroupLayer) { vc11884admin@148: GroupLayerReader gl = new GroupLayerReader(layer); vc11884admin@148: gl.setUtil(util); raimund@244: Element layerElement = gl.read(""); vc11884admin@148: if(layerElement == null) { vc11884admin@148: throw new IOException ("Error reading grouplayer."); vc11884admin@148: } vc11884admin@148: } aheinecke@301: else if(layer instanceof WMSMapLayer) { aheinecke@301: WMSMapLayerReader lr = new WMSMapLayerReader(layer); aheinecke@301: lr.setUtil(util); aheinecke@301: Element layerElement = lr.read(""); aheinecke@301: if(layerElement == null) { aheinecke@301: throw new IOException ("Error reading WMSMapLayer."); aheinecke@301: } aheinecke@301: } aheinecke@301: else if(layer instanceof WMSGroupLayer) { aheinecke@301: WMSGroupLayerReader lr = new WMSGroupLayerReader(layer); aheinecke@301: lr.setUtil(util); aheinecke@301: Element layerElement = lr.read(""); aheinecke@301: if(layerElement == null) { aheinecke@301: throw new IOException ("Error reading WMSGrouplayer."); aheinecke@301: } aheinecke@301: } aheinecke@301: else if(layer instanceof WMSLayer) { aheinecke@301: WMSLayerReader lr = new WMSLayerReader(layer); aheinecke@301: lr.setUtil(util); aheinecke@301: Element layerElement = lr.read(); aheinecke@301: if(layerElement == null) { aheinecke@301: throw new IOException ("Error reading WMSlayer."); aheinecke@301: } aheinecke@301: } aheinecke@303: else if(layer instanceof RasterLayer) { aheinecke@303: RasterLayerReader lr = aheinecke@303: new RasterLayerReader(layer); aheinecke@303: lr.setUtil(util); aheinecke@303: Element layerElement = lr.read(); aheinecke@310: if(layerElement == null) { aheinecke@310: throw new IOException ("Error reading RasterLayer."); aheinecke@310: } aheinecke@303: /* TODO implement support for classes in Raster Layers aheinecke@303: IRasterRenderer renderer = aheinecke@303: ((RasterLayer)layer).getRenderer(); aheinecke@303: IRendererReader rreader; aheinecke@303: if(renderer instanceof SimpleRenderer) { aheinecke@303: rreader = new SimpleRendererReader(renderer); aheinecke@303: rreader.setParent(layerElement); aheinecke@303: rreader.setUtil(util); aheinecke@303: rreader.read(); aheinecke@303: } aheinecke@303: else if(renderer instanceof ClassBreaksRenderer) { aheinecke@303: rreader = new ClassBreakRendererReader(renderer); aheinecke@303: rreader.setParent(layerElement); aheinecke@303: rreader.setUtil(util); aheinecke@303: rreader.read(); aheinecke@303: } aheinecke@303: else if(renderer instanceof UniqueValueRenderer) { aheinecke@303: rreader = new UniqueValueRendererReader(renderer); aheinecke@303: rreader.setParent(layerElement); aheinecke@303: rreader.setUtil(util); aheinecke@303: rreader.read(); aheinecke@303: } aheinecke@303: else { aheinecke@303: logger.debug("Not a known renderer type " + aheinecke@303: renderer.getClass().toString()); aheinecke@303: } aheinecke@303: */ aheinecke@303: } vc11884admin@148: else { vc11884admin@148: logger.info( vc11884admin@148: "Layertype not known: " + vc11884admin@148: layer.getClass().toString()); vc11884admin@148: } rrenkert@27: } rrenkert@43: catch(Exception e) { rrenkert@118: invalidLayerCount++; vc11884admin@133: if(i == 0 && (i + 1) == map.getLayerCount() || rrenkert@118: invalidLayerCount == map.getLayerCount()) { rrenkert@118: throw new IOException( rrenkert@118: "No valid layer in MXD file." + rrenkert@118: " Aborting."); rrenkert@118: } rrenkert@25: } rrenkert@25: } rrenkert@25: } rrenkert@25: return true; rrenkert@25: } rrenkert@25: rrenkert@43: /** rrenkert@43: * Get the root DOM document. rrenkert@43: */ rrenkert@43: public Document getMapDocument() { rrenkert@27: logger.debug("getMapDocument()"); rrenkert@40: return util.getDocument(); rrenkert@25: } rrenkert@25: } rrenkert@25: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :