# HG changeset patch # User Andre Heinecke # Date 1346950721 -7200 # Node ID f9e53dcc7424916b27e38b49b1a7b073ac34f4f2 # Parent ea3fde77ea482bfe361b8d215b34a8442207ed84 Add Read/Write Support for Projections on a per Layer basis diff -r ea3fde77ea48 -r f9e53dcc7424 ChangeLog --- a/ChangeLog Thu Sep 06 18:56:20 2012 +0200 +++ b/ChangeLog Thu Sep 06 18:58:41 2012 +0200 @@ -1,4 +1,11 @@ -2012-09-05 Andre Heinecke +2012-09-06 Andre Heinecke + + * src/java/de/intevation/mxd/reader/FeatureLayerReader.java, + src/java/de/intevation/mxd/reader/RasterLayerReader.java, + src/java/de/intevation/mxd/writer/MapScriptWriter.java: + Add Read/Write Support for Projections on a per Layer basis + +2012-09-06 Andre Heinecke * src/java/de/intevation/mxd/reader/RasterLayerReader.java: Catch generic Exceptions to avoid leaking exceptions and diff -r ea3fde77ea48 -r f9e53dcc7424 src/java/de/intevation/mxd/reader/FeatureLayerReader.java --- a/src/java/de/intevation/mxd/reader/FeatureLayerReader.java Thu Sep 06 18:56:20 2012 +0200 +++ b/src/java/de/intevation/mxd/reader/FeatureLayerReader.java Thu Sep 06 18:58:41 2012 +0200 @@ -30,6 +30,11 @@ import com.esri.arcgis.system.IName; import com.esri.arcgis.system.IPropertySet; import com.esri.arcgis.geometry.Envelope; +import com.esri.arcgis.geometry.ISpatialReference; +import com.esri.arcgis.geometry.ProjectedCoordinateSystem; +import com.esri.arcgis.geometry.GeographicCoordinateSystem; +import com.esri.arcgis.geometry.UnknownCoordinateSystem; +import com.esri.arcgis.geometry.Projection; import org.w3c.dom.Element; @@ -230,6 +235,39 @@ "Could not read extent from layer " + layer.getName() + "."); } + //Read the projection. + try { + ISpatialReference sr = layer.getSpatialReference(); + int projection = 0; + if(sr instanceof ProjectedCoordinateSystem) { + ProjectedCoordinateSystem pcs = (ProjectedCoordinateSystem)sr; + projection = pcs.getFactoryCode(); + } + else if(sr instanceof GeographicCoordinateSystem) { + GeographicCoordinateSystem gcs = (GeographicCoordinateSystem)sr; + projection = gcs.getFactoryCode(); + } + else if(sr instanceof UnknownCoordinateSystem) { + UnknownCoordinateSystem ucs = (UnknownCoordinateSystem)sr; + projection = 0; + } + else{ + logger.debug( + "Unknown SpatialReference: " + + sr.getClass().toString()); + } + + if(projection == 0) { + logger.warn( + "Unknown projection for Layer:" + layer.getName() + + " Please edit projection in resulting mapfile."); + } + layerElement.setAttribute("projection", String.valueOf(projection)); + } + catch(IOException ioe) { + logger.warn("Could not read layer projection."); + } + try { String datatype = layer.getDataSourceType(); if(layer.getWorkspace().getType() == 0) { diff -r ea3fde77ea48 -r f9e53dcc7424 src/java/de/intevation/mxd/reader/RasterLayerReader.java --- a/src/java/de/intevation/mxd/reader/RasterLayerReader.java Thu Sep 06 18:56:20 2012 +0200 +++ b/src/java/de/intevation/mxd/reader/RasterLayerReader.java Thu Sep 06 18:58:41 2012 +0200 @@ -30,6 +30,11 @@ import com.esri.arcgis.system.IName; import com.esri.arcgis.system.IPropertySet; import com.esri.arcgis.geometry.Envelope; +import com.esri.arcgis.geometry.ISpatialReference; +import com.esri.arcgis.geometry.ProjectedCoordinateSystem; +import com.esri.arcgis.geometry.GeographicCoordinateSystem; +import com.esri.arcgis.geometry.UnknownCoordinateSystem; +import com.esri.arcgis.geometry.Projection; import org.w3c.dom.Element; @@ -149,6 +154,58 @@ util.removeLayer(layerElement); return null; } + try { + Envelope rect = (Envelope)layer.getExtent(); + layerElement.setAttribute( + "extent_min_x", + String.valueOf(rect.getXMin ())); + layerElement.setAttribute( + "extent_max_x", + String.valueOf(rect.getXMax())); + layerElement.setAttribute( + "extent_min_y", + String.valueOf(rect.getYMin())); + layerElement.setAttribute( + "extent_max_y", + String.valueOf(rect.getYMax())); + } + catch(Exception e) { + logger.warn( + "Could not read extent from layer " + + layer.getName() + "."); + } + //Read the projection. + try { + ISpatialReference sr = layer.getSpatialReference(); + int projection = 0; + if(sr instanceof ProjectedCoordinateSystem) { + ProjectedCoordinateSystem pcs = (ProjectedCoordinateSystem)sr; + projection = pcs.getFactoryCode(); + } + else if(sr instanceof GeographicCoordinateSystem) { + GeographicCoordinateSystem gcs = (GeographicCoordinateSystem)sr; + projection = gcs.getFactoryCode(); + } + else if(sr instanceof UnknownCoordinateSystem) { + UnknownCoordinateSystem ucs = (UnknownCoordinateSystem)sr; + projection = 0; + } + else{ + logger.debug( + "Unknown SpatialReference: " + + sr.getClass().toString()); + } + + if(projection == 0) { + logger.warn( + "Unknown projection for Layer:" + layer.getName() + + " Please edit projection in resulting mapfile."); + } + layerElement.setAttribute("projection", String.valueOf(projection)); + } + catch(Exception e) { + logger.warn("Could not read layer projection."); + } // Static Attributes for Raster: layerElement.setAttribute("type", "raster"); diff -r ea3fde77ea48 -r f9e53dcc7424 src/java/de/intevation/mxd/writer/MapScriptWriter.java --- a/src/java/de/intevation/mxd/writer/MapScriptWriter.java Thu Sep 06 18:56:20 2012 +0200 +++ b/src/java/de/intevation/mxd/writer/MapScriptWriter.java Thu Sep 06 18:58:41 2012 +0200 @@ -296,8 +296,26 @@ } layer.setMetaData("wms_title", ulname); + //Set the projection for the Layers + if (layerElement.hasAttribute("projection")) { + int proj = Integer.parseInt(layerElement.getAttribute("projection")); + if(proj != 0) { + try { + layer.setProjection("epsg:" + layerElement.getAttribute("projection")); + } + catch(UnknownError e) { + logger.error( "Could not set projection in layer: " + layerElement.getAttribute("projection") + + ". Please ensure that it is described in your espg file."); + throw e; + } + } + } // Projection metadata. String mproj = mapNode.getAttribute("projection"); + if (layerElement.hasAttribute("projection")) { + // Overwrite the Map Projection in the layer + mproj = layerElement.getAttribute("projection"); + } if(mproj != null && !mproj.equals("") && !mproj.equals("0")) { String wmssrs = layer.getMetaData("wms_srs"); String owssrs = layer.getMetaData("ows_srs");