01: /* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
02: * This code is licensed under the GPL 2.0 license, availible at the root
03: * application directory.
04: */
05: package org.geoserver.feature;
06:
07: import org.geotools.feature.FeatureType;
08: import org.geotools.filter.visitor.DuplicatingFilterVisitor;
09: import org.geotools.gml2.bindings.GML2EncodingUtils;
10: import org.opengis.filter.FilterFactory2;
11: import org.opengis.filter.expression.Literal;
12: import org.opengis.filter.spatial.BBOX;
13: import org.opengis.referencing.crs.CoordinateReferenceSystem;
14:
15: import com.vividsolutions.jts.geom.Geometry;
16:
17: /**
18: * Returns a clone of the provided filter where all geometries and bboxes that
19: * do not have a CRS gets the specified default one.
20: *
21: * @author Andrea Aime - The Open Planning Project
22: *
23: */
24: public class DefaultCRSFilterVisitor extends DuplicatingFilterVisitor {
25: private CoordinateReferenceSystem defaultCrs;
26:
27: public DefaultCRSFilterVisitor(FilterFactory2 factory,
28: CoordinateReferenceSystem defaultCrs) {
29: super (factory);
30: this .defaultCrs = defaultCrs;
31: }
32:
33: public Object visit(BBOX filter, Object extraData) {
34: // if no srs is specified we can't transform anyways
35: String srs = filter.getSRS();
36: if (srs != null && !"".equals(srs.trim()))
37: return super .visit(filter, extraData);
38:
39: try {
40: // grab the original envelope data
41: double minx = filter.getMinX();
42: double miny = filter.getMinY();
43: double maxx = filter.getMaxX();
44: double maxy = filter.getMaxY();
45: String propertyName = filter.getPropertyName();
46: String defaultSrs = GML2EncodingUtils.crs(defaultCrs);
47:
48: return getFactory(extraData).bbox(propertyName, minx, miny,
49: maxx, maxy, defaultSrs);
50: } catch (Exception e) {
51: throw new RuntimeException("Could not decode srs '" + srs
52: + "'", e);
53: }
54:
55: }
56:
57: public Object visit(Literal expression, Object extraData) {
58: if (!(expression.getValue() instanceof Geometry))
59: return super .visit(expression, extraData);
60:
61: // check if reprojection is needed
62: Geometry geom = (Geometry) expression.getValue();
63: if (geom.getUserData() != null
64: && geom.getUserData() instanceof CoordinateReferenceSystem)
65: return super .visit(expression, extraData);
66:
67: // clone the geometry and assign the new crs
68: CoordinateReferenceSystem geomCRS = (CoordinateReferenceSystem) geom
69: .getUserData();
70: Geometry clone = geom.getFactory().createGeometry(geom);
71: clone.setUserData(defaultCrs);
72:
73: // clone
74: return factory.literal(clone);
75: }
76: }
|