diff flys-artifacts/src/main/java/de/intevation/flys/utils/GeometryUtils.java @ 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 f062b5a90e26
children 96daa8a4c48f
line wrap: on
line diff
--- 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