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.IMap; rrenkert@39: import com.esri.arcgis.carto.Map; rrenkert@25: import com.esri.arcgis.geometry.ISpatialReference; rrenkert@25: import com.esri.arcgis.geometry.ProjectedCoordinateSystem; rrenkert@25: import com.esri.arcgis.geometry.GeographicCoordinateSystem; rrenkert@31: import com.esri.arcgis.geometry.UnknownCoordinateSystem; rrenkert@25: import com.esri.arcgis.geometry.Projection; rrenkert@39: import com.esri.arcgis.geometry.IEnvelope; rrenkert@27: rrenkert@27: import org.w3c.dom.Element; rrenkert@27: rrenkert@33: import de.intevation.mxd.utils.MapToXMLUtils; rrenkert@33: rrenkert@25: /** rrenkert@25: * Reads map information. rrenkert@25: * rrenkert@25: * @author Raimund Renkert rrenkert@25: */ rrenkert@43: public class MapReader { rrenkert@43: /** rrenkert@43: * The Logger. rrenkert@43: */ rrenkert@43: private static final Logger logger = Logger.getLogger(MapReader.class); rrenkert@25: rrenkert@43: /** rrenkert@43: * Private member. rrenkert@43: */ rrenkert@39: private Map map; rrenkert@31: private MapToXMLUtils util; rrenkert@25: rrenkert@25: rrenkert@39: public MapReader(IMap map) rrenkert@39: throws Exception { rrenkert@25: logger.debug("constructor()"); rrenkert@39: if(map instanceof Map) { rrenkert@39: this.map = (Map)map; rrenkert@39: } rrenkert@39: else { rrenkert@39: throw new Exception("Not an instance of \"Map\"!"); rrenkert@39: } rrenkert@25: } rrenkert@25: rrenkert@25: /** rrenkert@25: * Reads the Map attributes. rrenkert@25: */ rrenkert@43: public void read() rrenkert@43: throws IOException { rrenkert@25: logger.debug("read()"); rrenkert@43: if(util == null) { rrenkert@27: throw new IOException("Can not write to document."); rrenkert@43: } rrenkert@27: rrenkert@39: //Create XML Element for map. rrenkert@31: Element mapElement; rrenkert@31: try{ rrenkert@31: mapElement = util.createMap(); rrenkert@31: } rrenkert@31: catch(Exception e){ rrenkert@31: e.printStackTrace(); rrenkert@31: return; rrenkert@31: } rrenkert@39: rrenkert@39: //Read map name. rrenkert@39: mapElement.setAttribute("name", map.getName()); rrenkert@39: rrenkert@39: //Read map extent. rrenkert@39: IEnvelope ext = map.getExtent(); rrenkert@39: mapElement.setAttribute( rrenkert@39: "extent_max_x", rrenkert@39: String.valueOf(ext.getXMax())); rrenkert@39: mapElement.setAttribute( rrenkert@39: "extent_max_y", rrenkert@39: String.valueOf(ext.getYMax())); rrenkert@39: mapElement.setAttribute( rrenkert@39: "extent_min_x", rrenkert@39: String.valueOf(ext.getXMin())); rrenkert@39: mapElement.setAttribute( rrenkert@39: "extent_min_y", rrenkert@39: String.valueOf(ext.getYMin())); rrenkert@39: rrenkert@39: //Read map units. rrenkert@39: int units = map.getMapUnits(); rrenkert@39: String s_units; rrenkert@39: switch(units) { rrenkert@39: case 3: s_units = "feet"; break; rrenkert@39: case 1: s_units = "inches"; break; rrenkert@39: case 10: s_units = "kilometers"; break; rrenkert@39: case 9: s_units = "meters"; break; rrenkert@39: case 5: s_units = "miles"; break; rrenkert@39: case 6: s_units = "nauticalmiles"; break; rrenkert@39: case 2: s_units = "points"; break; rrenkert@39: default : s_units = "not supported"; break; rrenkert@39: } rrenkert@39: mapElement.setAttribute("units", s_units); rrenkert@39: rrenkert@39: //TODO: Find out whats the correct scale value. rrenkert@39: mapElement.setAttribute( rrenkert@39: "scale", rrenkert@39: String.valueOf(map.getMaxScale())); rrenkert@39: rrenkert@39: //Read the projection. rrenkert@25: ISpatialReference sr = map.getSpatialReference(); rrenkert@43: if(sr instanceof ProjectedCoordinateSystem) { rrenkert@25: ProjectedCoordinateSystem pcs = (ProjectedCoordinateSystem)sr; rrenkert@25: Projection p = (Projection)pcs.getProjection(); rrenkert@31: mapElement.setAttribute("projection", p.getName()); rrenkert@25: } rrenkert@43: else if(sr instanceof GeographicCoordinateSystem) { rrenkert@25: GeographicCoordinateSystem gcs = (GeographicCoordinateSystem)sr; rrenkert@31: mapElement.setAttribute("projection", gcs.getName()); rrenkert@31: } rrenkert@31: else if(sr instanceof UnknownCoordinateSystem) { rrenkert@31: UnknownCoordinateSystem ucs = (UnknownCoordinateSystem)sr; rrenkert@31: mapElement.setAttribute("projection", sr.getName()); rrenkert@25: } rrenkert@27: else{ rrenkert@27: throw new IOException("Unknown SpatialReference: " + rrenkert@27: sr.getClass().toString()); rrenkert@27: } rrenkert@27: } rrenkert@27: rrenkert@43: /** rrenkert@43: * Set the utilities. rrenkert@43: */ rrenkert@43: public void setUtil(MapToXMLUtils util) { rrenkert@31: this.util = util; rrenkert@25: } rrenkert@25: } rrenkert@25: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :