changeset 3301:c8f670ae96e8

Create extent of a map layer in the correct coordinate reference system. flys-artifacts/trunk@4986 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Fri, 13 Jul 2012 10:25:24 +0000
parents e1cf76b3ecb4
children 453d2d0c4258
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/RiverAxisArtifact.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/WMSBuildingsArtifact.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/WMSCatchmentArtifact.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/WMSFixpointsArtifact.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/WMSFloodmapsArtifact.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/WMSFloodplainArtifact.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/WMSGaugeLocationArtifact.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/WMSHwsArtifact.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/WMSHydrBoundaryArtifact.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/WMSHydrBoundaryPolyArtifact.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/WMSKmArtifact.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/WMSLineArtifact.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/WMSQPSArtifact.java flys-artifacts/src/main/java/de/intevation/flys/utils/GeometryUtils.java
diffstat 15 files changed, 177 insertions(+), 44 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Fri Jul 13 10:19:23 2012 +0000
+++ b/flys-artifacts/ChangeLog	Fri Jul 13 10:25:24 2012 +0000
@@ -1,3 +1,26 @@
+2012-07-13  Ingo Weinzierl <ingo@intevation.de>
+
+	* src/main/java/de/intevation/flys/utils/GeometryUtils.java: Added new
+	  functions to reproject an Envelope from one CoordinateReferenceSystem to
+	  antoher.
+
+	* src/main/java/de/intevation/flys/artifacts/WMSQPSArtifact.java,
+	  src/main/java/de/intevation/flys/artifacts/WMSKmArtifact.java,
+	  src/main/java/de/intevation/flys/artifacts/WMSHydrBoundaryArtifact.java,
+	  src/main/java/de/intevation/flys/artifacts/WMSFixpointsArtifact.java,
+	  src/main/java/de/intevation/flys/artifacts/WMSFloodmapsArtifact.java,
+	  src/main/java/de/intevation/flys/artifacts/WMSHwsArtifact.java,
+	  src/main/java/de/intevation/flys/artifacts/WMSCatchmentArtifact.java,
+	  src/main/java/de/intevation/flys/artifacts/states/WMSBackgroundState.java,
+	  src/main/java/de/intevation/flys/artifacts/WMSHydrBoundaryPolyArtifact.java,
+	  src/main/java/de/intevation/flys/artifacts/WMSBuildingsArtifact.java,
+	  src/main/java/de/intevation/flys/artifacts/WMSGaugeLocationArtifact.java,
+	  src/main/java/de/intevation/flys/artifacts/RiverAxisArtifact.java,
+	  src/main/java/de/intevation/flys/artifacts/WMSLineArtifact.java,
+	  src/main/java/de/intevation/flys/artifacts/WMSFloodplainArtifact.java:
+	  The getExtent() method will now return the extent of the layer in the
+	  coordinate reference system that is configured for the specific river.
+
 2012-07-13  Ingo Weinzierl <ingo@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/model/WMSLayerFacet.java,
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/RiverAxisArtifact.java	Fri Jul 13 10:19:23 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/RiverAxisArtifact.java	Fri Jul 13 10:25:24 2012 +0000
@@ -121,7 +121,9 @@
         @Override
         protected Envelope getExtent() {
             River river = RiverFactory.getRiver(getRiverId());
-            return GeometryUtils.getRiverBoundary(river.getName());
+            return GeometryUtils.transform(
+                GeometryUtils.getRiverBoundary(river.getName()),
+                getSrid());
         }
 
         @Override
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WMSBuildingsArtifact.java	Fri Jul 13 10:19:23 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WMSBuildingsArtifact.java	Fri Jul 13 10:25:24 2012 +0000
@@ -21,6 +21,7 @@
 import de.intevation.flys.artifacts.model.FacetTypes;
 import de.intevation.flys.artifacts.model.RiverFactory;
 import de.intevation.flys.utils.FLYSUtils;
+import de.intevation.flys.utils.GeometryUtils;
 
 
 public class WMSBuildingsArtifact extends WMSDBArtifact {
@@ -116,8 +117,10 @@
 
                 max.expandToInclude(env);
             }
-
-            return max;
+            
+            return max != null
+                ? GeometryUtils.transform(max, getSrid())
+                : max;
         }
 
         @Override
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WMSCatchmentArtifact.java	Fri Jul 13 10:19:23 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WMSCatchmentArtifact.java	Fri Jul 13 10:25:24 2012 +0000
@@ -21,6 +21,7 @@
 import de.intevation.flys.artifacts.model.FacetTypes;
 import de.intevation.flys.artifacts.model.RiverFactory;
 import de.intevation.flys.utils.FLYSUtils;
+import de.intevation.flys.utils.GeometryUtils;
 
 
 public class WMSCatchmentArtifact extends WMSDBArtifact {
@@ -132,8 +133,10 @@
 
                 max.expandToInclude(env);
             }
-
-            return max;
+            
+            return max != null
+                ? GeometryUtils.transform(max, getSrid())
+                : max;
         }
 
         @Override
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WMSFixpointsArtifact.java	Fri Jul 13 10:19:23 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WMSFixpointsArtifact.java	Fri Jul 13 10:25:24 2012 +0000
@@ -21,6 +21,7 @@
 import de.intevation.flys.artifacts.model.FacetTypes;
 import de.intevation.flys.artifacts.model.RiverFactory;
 import de.intevation.flys.utils.FLYSUtils;
+import de.intevation.flys.utils.GeometryUtils;
 
 
 public class WMSFixpointsArtifact extends WMSDBArtifact {
@@ -115,8 +116,10 @@
 
                 max.expandToInclude(env);
             }
-
-            return max;
+            
+            return max != null
+                ? GeometryUtils.transform(max, getSrid())
+                : max;
         }
 
         @Override
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WMSFloodmapsArtifact.java	Fri Jul 13 10:19:23 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WMSFloodmapsArtifact.java	Fri Jul 13 10:25:24 2012 +0000
@@ -21,6 +21,7 @@
 import de.intevation.flys.artifacts.model.FacetTypes;
 import de.intevation.flys.artifacts.model.RiverFactory;
 import de.intevation.flys.utils.FLYSUtils;
+import de.intevation.flys.utils.GeometryUtils;
 
 
 public class WMSFloodmapsArtifact extends WMSDBArtifact {
@@ -117,7 +118,9 @@
                 max.expandToInclude(env);
             }
 
-            return max;
+            return max != null
+                ? GeometryUtils.transform(max, getSrid())
+                : max;
         }
 
         @Override
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WMSFloodplainArtifact.java	Fri Jul 13 10:19:23 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WMSFloodplainArtifact.java	Fri Jul 13 10:25:24 2012 +0000
@@ -21,6 +21,7 @@
 import de.intevation.flys.artifacts.model.FacetTypes;
 import de.intevation.flys.artifacts.model.RiverFactory;
 import de.intevation.flys.utils.FLYSUtils;
+import de.intevation.flys.utils.GeometryUtils;
 
 
 public class WMSFloodplainArtifact extends WMSDBArtifact {
@@ -122,7 +123,10 @@
         protected Envelope getExtent() {
             River      river = getRiver();
             Floodplain plain = Floodplain.getFloodplain(river.getName());
-            return plain.getGeom().getEnvelopeInternal();
+            
+            return GeometryUtils.transform(
+                plain.getGeom().getEnvelopeInternal(),
+                getSrid());
         }
 
         @Override
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WMSGaugeLocationArtifact.java	Fri Jul 13 10:19:23 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WMSGaugeLocationArtifact.java	Fri Jul 13 10:25:24 2012 +0000
@@ -23,6 +23,7 @@
 import de.intevation.flys.artifacts.model.FacetTypes;
 import de.intevation.flys.artifacts.model.RiverFactory;
 import de.intevation.flys.utils.FLYSUtils;
+import de.intevation.flys.utils.GeometryUtils;
 
 
 public class WMSGaugeLocationArtifact extends WMSDBArtifact {
@@ -119,7 +120,9 @@
                 max.expandToInclude(env);
             }
 
-            return max;
+            return max != null
+                ? GeometryUtils.transform(max, getSrid())
+                : max;
         }
 
         @Override
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WMSHwsArtifact.java	Fri Jul 13 10:19:23 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WMSHwsArtifact.java	Fri Jul 13 10:25:24 2012 +0000
@@ -21,6 +21,7 @@
 import de.intevation.flys.artifacts.model.FacetTypes;
 import de.intevation.flys.artifacts.model.RiverFactory;
 import de.intevation.flys.utils.FLYSUtils;
+import de.intevation.flys.utils.GeometryUtils;
 
 
 public class WMSHwsArtifact extends WMSDBArtifact {
@@ -132,7 +133,9 @@
                 max.expandToInclude(env);
             }
 
-            return max;
+            return max != null
+                ? GeometryUtils.transform(max, getSrid())
+                : max;
         }
 
         @Override
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WMSHydrBoundaryArtifact.java	Fri Jul 13 10:19:23 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WMSHydrBoundaryArtifact.java	Fri Jul 13 10:25:24 2012 +0000
@@ -21,6 +21,7 @@
 import de.intevation.flys.artifacts.model.FacetTypes;
 import de.intevation.flys.artifacts.model.RiverFactory;
 import de.intevation.flys.utils.FLYSUtils;
+import de.intevation.flys.utils.GeometryUtils;
 
 
 public class WMSHydrBoundaryArtifact extends WMSDBArtifact {
@@ -117,7 +118,9 @@
                 max.expandToInclude(env);
             }
 
-            return max;
+            return max != null
+                ? GeometryUtils.transform(max, getSrid())
+                : max;
         }
 
         @Override
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WMSHydrBoundaryPolyArtifact.java	Fri Jul 13 10:19:23 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WMSHydrBoundaryPolyArtifact.java	Fri Jul 13 10:25:24 2012 +0000
@@ -21,6 +21,7 @@
 import de.intevation.flys.artifacts.model.FacetTypes;
 import de.intevation.flys.artifacts.model.RiverFactory;
 import de.intevation.flys.utils.FLYSUtils;
+import de.intevation.flys.utils.GeometryUtils;
 
 
 public class WMSHydrBoundaryPolyArtifact extends WMSDBArtifact {
@@ -117,7 +118,9 @@
                 max.expandToInclude(env);
             }
 
-            return max;
+            return max != null
+                ? GeometryUtils.transform(max, getSrid())
+                : max;
         }
 
         @Override
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WMSKmArtifact.java	Fri Jul 13 10:19:23 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WMSKmArtifact.java	Fri Jul 13 10:25:24 2012 +0000
@@ -23,6 +23,7 @@
 import de.intevation.flys.artifacts.model.FacetTypes;
 import de.intevation.flys.artifacts.model.RiverFactory;
 import de.intevation.flys.utils.FLYSUtils;
+import de.intevation.flys.utils.GeometryUtils;
 
 
 public class WMSKmArtifact extends WMSDBArtifact {
@@ -133,7 +134,9 @@
                 max.expandToInclude(env);
             }
 
-            return max;
+            return max != null
+                ? GeometryUtils.transform(max, getSrid())
+                : max;
         }
 
         @Override
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WMSLineArtifact.java	Fri Jul 13 10:19:23 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WMSLineArtifact.java	Fri Jul 13 10:25:24 2012 +0000
@@ -21,6 +21,7 @@
 import de.intevation.flys.artifacts.model.FacetTypes;
 import de.intevation.flys.artifacts.model.RiverFactory;
 import de.intevation.flys.utils.FLYSUtils;
+import de.intevation.flys.utils.GeometryUtils;
 
 
 public class WMSLineArtifact extends WMSDBArtifact {
@@ -132,7 +133,9 @@
                 max.expandToInclude(env);
             }
 
-            return max;
+            return max != null
+                ? GeometryUtils.transform(max, getSrid())
+                : max;
         }
 
         @Override
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WMSQPSArtifact.java	Fri Jul 13 10:19:23 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WMSQPSArtifact.java	Fri Jul 13 10:25:24 2012 +0000
@@ -21,6 +21,7 @@
 import de.intevation.flys.artifacts.model.FacetTypes;
 import de.intevation.flys.artifacts.model.RiverFactory;
 import de.intevation.flys.utils.FLYSUtils;
+import de.intevation.flys.utils.GeometryUtils;
 
 
 public class WMSQPSArtifact extends WMSDBArtifact {
@@ -134,7 +135,9 @@
                 max.expandToInclude(env);
             }
 
-            return max;
+            return max != null
+                ? GeometryUtils.transform(max, getSrid())
+                : max;
         }
 
         @Override
--- a/flys-artifacts/src/main/java/de/intevation/flys/utils/GeometryUtils.java	Fri Jul 13 10:19:23 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/utils/GeometryUtils.java	Fri Jul 13 10:25:24 2012 +0000
@@ -28,6 +28,7 @@
 import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
 import org.geotools.geojson.feature.FeatureJSON;
 import org.geotools.geometry.jts.JTS;
+import org.geotools.geometry.jts.ReferencedEnvelope;
 import org.geotools.referencing.CRS;
 import org.opengis.feature.simple.SimpleFeature;
 import org.opengis.feature.simple.SimpleFeatureType;
@@ -37,18 +38,17 @@
 import org.opengis.referencing.operation.MathTransform;
 import org.opengis.referencing.operation.TransformException;
 
-
 public class GeometryUtils {
 
     private static final Logger logger = Logger.getLogger(GeometryUtils.class);
 
+    public static final String PREFIX_EPSG  = "EPSG:";
+
     public static final String DEFAULT_EPSG = "EPSG:31467";
 
-
     private GeometryUtils() {
     }
 
-
     public static Envelope getRiverBoundary(String rivername) {
         List<RiverAxis> axes = RiverAxis.getRiverAxis(rivername);
         if (axes != null && axes.size() > 0) {
@@ -73,7 +73,6 @@
         return null;
     }
 
-
     public static String getRiverBounds(String rivername) {
         Envelope env = getRiverBoundary(rivername);
 
@@ -84,10 +83,8 @@
         return null;
     }
 
-
     /**
-     * Returns the boundary of Envelope <i>env</i> in OpenLayers
-     * representation.
+     * Returns the boundary of Envelope <i>env</i> in OpenLayers representation.
      *
      * @param env The envelope of a geometry.
      *
@@ -101,7 +98,6 @@
             env.getMaxY();
     }
 
-
     public static String createOLBounds(Geometry a, Geometry b) {
         Coordinate[] ca = a.getCoordinates();
         Coordinate[] cb = b.getCoordinates();
@@ -130,29 +126,26 @@
         return "" + lowerX + " " + lowerY + " " + upperX + " " + upperY;
     }
 
-
     public static SimpleFeatureType buildFeatureType(
         String name, String srs, Class<?> geometryType)
     {
         return buildFeatureType(name, srs, geometryType, null);
     }
 
-
     /**
      * Creates a new SimpleFeatureType using a SimpleFeatureTypeBuilder.
      *
      * @param name The name of the FeatureType.
      * @param srs The SRS (e.g. "EPSG:31466").
      * @param geometryType The geometry type's class (e.g. Polygon.class).
-     * @param attrs Optional. An object with attribute-name/attribute-class
-     * pairs where index 0 specifies the name as string and index 1 the type
-     * as class.
+     * @param attrs Optional. An object with attribute-name/attribute-class pairs
+     * where index 0 specifies the name as string and index 1 the
+     * ype as class.
      *
      * @return a new SimpleFeatureType.
      */
-    public static SimpleFeatureType buildFeatureType(
-        String name, String srs, Class<?> geometryType, Object[][] attrs)
-    {
+    public static SimpleFeatureType buildFeatureType(String name, String srs,
+        Class<?> geometryType, Object[][] attrs) {
         try {
             SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
             CoordinateReferenceSystem crs    = CRS.decode(srs);
@@ -180,7 +173,6 @@
         return null;
     }
 
-
     public static List<SimpleFeature> parseGeoJSON(
         String geojson, SimpleFeatureType ft
     ) {
@@ -205,22 +197,101 @@
     }
 
 
-    public static boolean writeShapefile(
-        File              shape,
-        SimpleFeatureType featureType,
-        FeatureCollection collection
+    /**
+     * This method returns the {@link CoordinateReferenceSystem} by the
+     * {@link String} <i>epsg</i>.
+     *
+     * @param epsg An EPSG code like <b>EPSG:31466</b>
+     *
+     * @return the {@link CoordinateReferenceSystem} specified by <i>epsg</i>.
+     */
+    public static CoordinateReferenceSystem getCoordinateReferenceSystem(
+        String epsg
     ) {
-        return writeShapefile(
-            shape, featureType, collection, featureType.getCoordinateReferenceSystem());
+        if (epsg == null) {
+            logger.warn("cannot create CoordinateReferenceSystem with null");
+            return null;
+        }
+
+        if (!epsg.startsWith(PREFIX_EPSG)) {
+            epsg = PREFIX_EPSG + epsg;
+        }
+
+        try {
+            return CRS.decode(epsg);
+        }
+        catch (FactoryException fe) {
+            logger.error(
+                "unable to get CoordinateReferenceSystem for: " + epsg,
+                fe);
+        }
+
+        return null;
     }
 
 
-    public static boolean writeShapefile(
-        File                      shape,
-        SimpleFeatureType         featureType,
-        FeatureCollection         collection,
-        CoordinateReferenceSystem crs
+    public static Envelope transform(Envelope orig, String targetSrs) {
+        return transform(orig, targetSrs, DEFAULT_EPSG);
+    }
+
+
+    public static Envelope transform(
+        Envelope orig,
+        String   targetSrs,
+        String   origSrs
     ) {
+        if (targetSrs == null || orig == null || origSrs == null) {
+            logger.warn("unable to transform envelope: empty parameters");
+            return orig;
+        }
+
+        logger.debug("Transform envlope to '" + targetSrs + "'");
+        try {
+            CoordinateReferenceSystem sourceCRS =
+                getCoordinateReferenceSystem(origSrs);
+
+            CoordinateReferenceSystem targetCRS =
+                getCoordinateReferenceSystem(targetSrs);
+
+            if (sourceCRS != null && targetCRS != null) {
+                ReferencedEnvelope tmpEnv =
+                    new ReferencedEnvelope(orig, CRS.decode(origSrs));
+
+                Envelope target = tmpEnv.transform(targetCRS, false);
+
+                if (logger.isDebugEnabled()) {
+                    logger.debug("   orig envelope       : " + orig);
+                    logger.debug("   transformed envelope: " + target);
+                }
+
+                return target;
+            }
+        }
+        catch (NoSuchAuthorityCodeException nsae) {
+            logger.error("Cannot get CoordinateReferenceSystem!", nsae);
+        }
+        catch (FactoryException fe) {
+            logger.error("Cannot get CoordinateReferenceSystem!", fe);
+        }
+        catch (TransformException te) {
+            logger.error("Cannot transform envelope from source "
+                + origSrs + " to target srs " + targetSrs);
+        }
+
+        return null;
+    }
+
+
+    public static boolean writeShapefile(File shape,
+        SimpleFeatureType featureType, FeatureCollection collection) {
+        return writeShapefile(shape, featureType, collection,
+            featureType.getCoordinateReferenceSystem());
+    }
+
+
+    public static boolean writeShapefile(File shape,
+        SimpleFeatureType featureType, FeatureCollection collection,
+        CoordinateReferenceSystem crs) {
         if (collection.isEmpty()) {
             logger.warn("Shapefile is not written - no features given!");
             return false;

http://dive4elements.wald.intevation.org