[Schmitzm-commits] r577 - in branches/1.0-gt2-2.6/src: schmitzm/geotools skrueger/geotools

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Wed Nov 25 14:22:48 CET 2009


Author: alfonx
Date: 2009-11-25 14:22:48 +0100 (Wed, 25 Nov 2009)
New Revision: 577

Modified:
   branches/1.0-gt2-2.6/src/schmitzm/geotools/JTSUtil.java
   branches/1.0-gt2-2.6/src/skrueger/geotools/GeomFilterGenerator.java
Log:
* JTSUtil has a new method transformGeometry equivalent to transformCoordinate
* Geomfilter is builing the POINT vs POLYGON INTERSECTION with a Point now, and not wrongly with a Coordinate anymore

Modified: branches/1.0-gt2-2.6/src/schmitzm/geotools/JTSUtil.java
===================================================================
--- branches/1.0-gt2-2.6/src/schmitzm/geotools/JTSUtil.java	2009-11-25 13:21:03 UTC (rev 576)
+++ branches/1.0-gt2-2.6/src/schmitzm/geotools/JTSUtil.java	2009-11-25 13:22:48 UTC (rev 577)
@@ -43,6 +43,8 @@
 
 import com.vividsolutions.jts.geom.Coordinate;
 import com.vividsolutions.jts.geom.Envelope;
+import com.vividsolutions.jts.geom.Geometry;
+import com.vividsolutions.jts.geom.Point;
 
 /**
  * Diese Klasse enthaelt allgemeine Funktionen fuer die Arbeit mit den in
@@ -122,6 +124,8 @@
 	public static Envelope transformEnvelope(Envelope sourceEnv,
 			CoordinateReferenceSystem sourceCRS,
 			CoordinateReferenceSystem destCRS) {
+		
+		// Sometimes CRS only differ in their metadata... 
 		if (CRS.equalsIgnoreMetadata(sourceCRS, destCRS))
 			return new Envelope(sourceEnv);
 
@@ -151,7 +155,7 @@
 	}
 
 	/**
-	 * Transformiert eine Koordinate von einem CRS in ein anderes.
+	 * Transformiert eine Koordinate von einem CRS in ein anderes. 
 	 * 
 	 * @param sourceCoord
 	 *            Koordinate
@@ -183,7 +187,42 @@
 		}
 		return destCoord;
 	}
+	
+	/**
+	 * Transformiert eine JTS {@link Geometry} von einem CRS in ein anderes. <br>
+	 * Wenn beide CRS semantisch gleich sind wird das selbe Geometrieobect
+	 * zurueckgeliefert.
+	 * 
+	 * @param geometry
+	 *            Koordinate
+	 * @param srcCrs
+	 *            CRS von {@code sourceCoord}
+	 * @param destCrs
+	 *            CRS in das umgerechnet werden soll
+	 * @see CRS#findMathTransform(CoordinateReferenceSystem,CoordinateReferenceSystem)
+	 * @see JTS#transform(Coordinate, Coordinate, MathTransform)
+	 */
+	public static Geometry transformGeometry(Geometry geometry,
+			CoordinateReferenceSystem srcCrs, CoordinateReferenceSystem destCrs) {
+		
+		if (CRS.equalsIgnoreMetadata(srcCrs, destCrs)) return geometry; 
 
+		Geometry destGeometry = null;
+		MathTransform transform;
+		try {
+			transform = CRS.findMathTransform(srcCrs, destCrs, getLenient());
+			destGeometry = JTS.transform(geometry, transform);
+		} catch (FactoryException e) {
+			LOGGER.warn("CRS tranformation for JTS coordinate not successful",
+					e);
+		} catch (TransformException e) {
+			LOGGER.warn("CRS tranformation for JTS coordinate not successful",
+					e);
+		}
+		return destGeometry;
+	}
+
+
 	/**
 	 * Returns an {@link Envelope} that has the same aspect ratio as the given
 	 * rectangle

Modified: branches/1.0-gt2-2.6/src/skrueger/geotools/GeomFilterGenerator.java
===================================================================
--- branches/1.0-gt2-2.6/src/skrueger/geotools/GeomFilterGenerator.java	2009-11-25 13:21:03 UTC (rev 576)
+++ branches/1.0-gt2-2.6/src/skrueger/geotools/GeomFilterGenerator.java	2009-11-25 13:22:48 UTC (rev 577)
@@ -4,9 +4,12 @@
 import java.util.HashMap;
 import java.util.Map;
 
+import org.apache.log4j.Logger;
 import org.geotools.data.FeatureSource;
+import org.geotools.factory.FactoryFinder;
 import org.geotools.filter.AbstractFilter;
 import org.geotools.filter.GeometryFilterImpl;
+import org.geotools.geometry.jts.JTS;
 import org.geotools.geometry.jts.ReferencedEnvelope;
 import org.geotools.referencing.CRS;
 import org.opengis.feature.simple.SimpleFeature;
@@ -21,7 +24,9 @@
 
 import com.vividsolutions.jts.geom.Coordinate;
 import com.vividsolutions.jts.geom.Envelope;
+import com.vividsolutions.jts.geom.GeometryFactory;
 import com.vividsolutions.jts.geom.MultiPolygon;
+import com.vividsolutions.jts.geom.Point;
 import com.vividsolutions.jts.geom.Polygon;
 
 /**
@@ -38,17 +43,23 @@
  * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a>
  */
 public abstract class GeomFilterGenerator {
+	private final static Logger LOGGER = Logger
+			.getLogger(GeomFilterGenerator.class);
+
 	/**
 	 * Holds the {@link CoordinateReferenceSystem} the filter constraint bases
 	 * on.
 	 */
 	protected CoordinateReferenceSystem baseCRS = null;
-	/**
-	 * Caches the base filter constraint for several
-	 * {@link CoordinateReferenceSystem CoordinateReferenceSystems}.
-	 */
-	protected Map<CoordinateReferenceSystem, GeometryFilterImpl> filterCache = new HashMap<CoordinateReferenceSystem, GeometryFilterImpl>();
 
+	// /**
+	// * Caches the base filter constraint for several
+	// * {@link CoordinateReferenceSystem CoordinateReferenceSystems} and the
+	// Geometry-Type of
+	// */
+	// protected Map<Integer, GeometryFilterImpl> filterCache = new
+	// HashMap<Integer, GeometryFilterImpl>();
+
 	/**
 	 * Creates a new filter generator
 	 * 
@@ -62,11 +73,17 @@
 
 	/**
 	 * Creates a filter containing the base constraint ("right" argument)
-	 * transformed to the given {@link CoordinateReferenceSystem}.
+	 * transformed to the given {@link CoordinateReferenceSystem}.<br>
+	 * Depending on the Geometry type of the targeted layer, the filter may look
+	 * different. For example checking POINT INTERSECTS POLYGON is easy, where
+	 * POINT INTERSECTS LINE will hardly ever hit, so we do
+	 * SMALLBBOXARROUND(POINT) INTERSECTS POLYGON.
 	 * 
 	 * @param crs
 	 *            the {@link CoordinateReferenceSystem} the base constraint is
 	 *            transformed to
+	 * @param Geometry
+	 *            class of the features we want to filter against.
 	 * @param Class
 	 *            the geometry class we are testing against. For point against
 	 *            polygon for example, an intersects is much faster that the
@@ -91,11 +108,14 @@
 		GeometryDescriptor geomDescr = fs.getSchema().getGeometryDescriptor();
 		CoordinateReferenceSystem fsCRS = geomDescr
 				.getCoordinateReferenceSystem();
-		GeometryFilterImpl filter = filterCache.get(fsCRS);
-		if (filter == null) {
-			filter = prepareFilter(fsCRS, geomDescr.getType().getBinding());
-			filterCache.put(fsCRS, filter);
-		}
+		Class<?> binding = geomDescr.getType().getBinding();
+
+		GeometryFilterImpl filter;
+		// = filterCache.get(fsCRS.hashCode()+binding.hashCode());
+		// if (filter == null) {
+		filter = prepareFilter(fsCRS, binding);
+		// filterCache.put(fsCRS.hashCode()+binding.hashCode(), filter);
+		// }
 		Expression geometry = FilterUtil.FILTER_FAC2.property(geomDescr
 				.getLocalName());
 		filter.setExpression1(geometry);
@@ -108,6 +128,9 @@
 	 * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a>
 	 */
 	public static class BoundingBoxFilterGenerator extends GeomFilterGenerator {
+		private final static Logger LOGGER = Logger
+				.getLogger(BoundingBoxFilterGenerator.class);
+
 		/**
 		 * Holds the base constraint (bounding box {@link Envelope}) relative to
 		 * the {@linkplain GeomFilterGenerator#baseCRS base CRS}.
@@ -157,11 +180,24 @@
 	 * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a>
 	 */
 	public static class PointFilterGenerator extends GeomFilterGenerator {
+		private final static Logger LOGGER = Logger
+				.getLogger(PointFilterGenerator.class);
+
+		private static final GeometryFactory GF = new GeometryFactory(); // TODO
+		// do
+		// we
+		// have
+		// a
+		// application
+		// wide
+		// one?
+
 		/**
 		 * Holds the base constraint (coordinate) relative to the
 		 * {@linkplain GeomFilterGenerator#baseCRS base CRS}.
 		 */
-		protected Coordinate basePoint = null;
+		protected Point basePoint = null;
+
 		private ReferencedEnvelope baseEnv;
 
 		/**
@@ -175,13 +211,13 @@
 		 * @param crs
 		 *            defines the CRS of base point
 		 */
-		public PointFilterGenerator(Coordinate basePoint, double radius,
+		public PointFilterGenerator(Point basePoint, double radius,
 				CoordinateReferenceSystem crs) {
 			super(crs);
 			this.basePoint = basePoint;
-			this.baseEnv = new ReferencedEnvelope(basePoint.x - radius,
-					basePoint.x + radius, basePoint.y - radius, basePoint.y
-							+ radius, crs);
+			this.baseEnv = new ReferencedEnvelope(basePoint.getX() - radius,
+					basePoint.getX() + radius, basePoint.getY() - radius,
+					basePoint.getY() + radius, crs);
 
 		}
 
@@ -191,13 +227,15 @@
 		 * @param basePoint
 		 *            defines the point for the "near point" constraint
 		 * @param dist
-		 *            defines the distance around the base point in base point CRS
+		 *            defines the distance around the base point in base point
+		 *            CRS
 		 * @param crs
 		 *            defines the CRS of base point
 		 */
 		public PointFilterGenerator(Point2D basePoint, double dist,
 				CoordinateReferenceSystem crs) {
-			this(new Coordinate(basePoint.getX(), basePoint.getY()), dist, crs);
+			this(GF.createPoint(new Coordinate(basePoint.getX(), basePoint
+					.getY())), dist, crs);
 		}
 
 		/**
@@ -210,44 +248,37 @@
 		 */
 		protected GeometryFilterImpl prepareFilter(
 				CoordinateReferenceSystem crs, Class<?> geomClass) {
-			Coordinate localCrsPoint = basePoint;
+			Point localCrsPoint = basePoint;
 
 			boolean isPolygonGeometry = (Polygon.class
 					.isAssignableFrom(geomClass) || MultiPolygon.class
 					.isAssignableFrom(geomClass));
 
+			LOGGER.debug("is polygon = " + isPolygonGeometry);
+
 			GeometryFilterImpl filter = (GeometryFilterImpl) FilterUtil.FILTER_FAC
-			.createGeometryFilter(AbstractFilter.GEOMETRY_INTERSECTS);
+					.createGeometryFilter(AbstractFilter.GEOMETRY_INTERSECTS);
 
 			if (isPolygonGeometry) {
-				// we chanck against a polygon with instersects
 
-				if (!CRS.equalsIgnoreMetadata(baseCRS, crs))
-					localCrsPoint = JTSUtil.transformCoordinate(basePoint,
-							baseCRS, crs);
-//
-//				GeometryFilterImpl intersectsFilter = (GeometryFilterImpl) FilterUtil.FILTER_FAC
-//						.createGeometryFilter(AbstractFilter.GEOMETRY_INTERSECTS);
+				localCrsPoint = (Point) JTSUtil.transformGeometry(
+						localCrsPoint, baseCRS, crs);
+
 				filter.setExpression2(FilterUtil.FILTER_FAC2
 						.literal(localCrsPoint));
-//
-//				return intersectsFilter;
 
 			} else {
 				// we check against lines and points with a bbox
-//				GeometryFilterImpl filter = (GeometryFilterImpl) FilterUtil.FILTER_FAC
-//				.createGeometryFilter(AbstractFilter.GEOMETRY_BBOX);
 
-				Envelope bbEnv = baseEnv;
-				if (!CRS.equalsIgnoreMetadata(baseCRS, crs))
-					bbEnv = JTSUtil.transformEnvelope(baseEnv, baseCRS, crs);
+				Envelope bbEnv = JTSUtil.transformEnvelope(baseEnv, baseCRS,
+						crs);
 
 				// Filter fuer Envelope zusammenstellen
 				Expression bbox = FilterUtil.FILTER_FAC
 						.createBBoxExpression(bbEnv);
 				filter.setExpression2(bbox);
 			}
-			
+
 			return filter;
 
 		}



More information about the Schmitzm-commits mailing list