001: package org.geoserver.feature;
002:
003: import java.util.Collections;
004:
005: import junit.framework.TestCase;
006:
007: import org.geoserver.feature.ReprojectingFilterVisitor;
008: import org.geotools.data.DataUtilities;
009: import org.geotools.factory.CommonFactoryFinder;
010: import org.geotools.factory.GeoTools;
011: import org.geotools.feature.FeatureType;
012: import org.geotools.referencing.CRS;
013: import org.opengis.filter.Filter;
014: import org.opengis.filter.FilterFactory2;
015: import org.opengis.filter.expression.Literal;
016: import org.opengis.filter.spatial.BBOX;
017: import org.opengis.filter.spatial.Intersects;
018:
019: import com.vividsolutions.jts.geom.Coordinate;
020: import com.vividsolutions.jts.geom.GeometryFactory;
021: import com.vividsolutions.jts.geom.LineString;
022:
023: public class ReprojectingFilterVisitorTest extends TestCase {
024:
025: FeatureType ft;
026: FilterFactory2 ff;
027: ReprojectingFilterVisitor reprojector;
028:
029: protected void setUp() throws Exception {
030: System.setProperty("org.geotools.referencing.forceXY", "true");
031: ft = DataUtilities
032: .createType("testType",
033: "geom:Point:srid=4326,line:LineString,name:String,id:int");
034: ff = CommonFactoryFinder.getFilterFactory2(GeoTools
035: .getDefaultHints());
036: reprojector = new ReprojectingFilterVisitor(ff, ft);
037: }
038:
039: /**
040: * Make sure it does not break with non spatial filters
041: */
042: public void testNoProjection() {
043: Filter idFilter = ff.id(Collections.singleton(ff
044: .featureId("testType:1")));
045: Filter clone = (Filter) idFilter.accept(reprojector, null);
046: assertNotSame(idFilter, clone);
047: assertEquals(idFilter, clone);
048: }
049:
050: public void testBboxNoReprojection() {
051: // no reprojection needed in fact
052: Filter bbox = ff.bbox(ff.property("geom"), 10, 10, 20, 20,
053: "EPSG:4326");
054: Filter clone = (Filter) bbox.accept(reprojector, null);
055: assertNotSame(bbox, clone);
056: assertEquals(bbox, clone);
057: }
058:
059: public void testBboxReproject() {
060: // see if coordinates gets flipped, urn forces lat/lon interpretation
061: BBOX bbox = ff.bbox(ff.property("geom"), 10, 15, 20, 25,
062: "urn:x-ogc:def:crs:EPSG:6.11.2:4326");
063: Filter clone = (Filter) bbox.accept(reprojector, null);
064: assertNotSame(bbox, clone);
065: BBOX clonedBbox = (BBOX) clone;
066: assertEquals(bbox.getPropertyName(), clonedBbox
067: .getPropertyName());
068: assertTrue(15 == clonedBbox.getMinX());
069: assertTrue(10 == clonedBbox.getMinY());
070: assertTrue(25 == clonedBbox.getMaxX());
071: assertTrue(20 == clonedBbox.getMaxY());
072: assertEquals("EPSG:4326", clonedBbox.getSRS());
073: }
074:
075: public void testBboxReprojectUnreferencedProperty() {
076: // see if coordinates gets flipped, urn forces lat/lon interpretation
077: BBOX bbox = ff.bbox(ff.property("line"), 10, 15, 20, 25,
078: "urn:x-ogc:def:crs:EPSG:6.11.2:4326");
079: Filter clone = (Filter) bbox.accept(reprojector, null);
080: assertNotSame(bbox, clone);
081: assertEquals(bbox, clone);
082: }
083:
084: public void testBboxReprojectUnreferencedBBox() {
085: // see if coordinates gets flipped, urn forces lat/lon interpretation
086: BBOX bbox = ff.bbox(ff.property("geom"), 10, 15, 20, 25, null);
087: Filter clone = (Filter) bbox.accept(reprojector, null);
088: assertNotSame(bbox, clone);
089: assertEquals(bbox, clone);
090: }
091:
092: public void testIntersectsReproject() throws Exception {
093: GeometryFactory gf = new GeometryFactory();
094: LineString ls = gf.createLineString(new Coordinate[] {
095: new Coordinate(10, 15), new Coordinate(20, 25) });
096: ls
097: .setUserData(CRS
098: .decode("urn:x-ogc:def:crs:EPSG:6.11.2:4326"));
099:
100: // see if coordinates gets flipped, urn forces lat/lon interpretation
101: Intersects original = ff.intersects(ff.property("geom"), ff
102: .literal(ls));
103: Filter clone = (Filter) original.accept(reprojector, null);
104: assertNotSame(original, clone);
105: Intersects isClone = (Intersects) clone;
106: assertEquals(isClone.getExpression1(), original
107: .getExpression1());
108: LineString clonedLs = (LineString) ((Literal) isClone
109: .getExpression2()).getValue();
110: assertTrue(15 == clonedLs.getCoordinateN(0).x);
111: assertTrue(10 == clonedLs.getCoordinateN(0).y);
112: assertTrue(25 == clonedLs.getCoordinateN(1).x);
113: assertTrue(20 == clonedLs.getCoordinateN(1).y);
114: assertEquals(CRS.decode("EPSG:4326"), clonedLs.getUserData());
115: }
116:
117: public void testIntersectsUnreferencedGeometry() throws Exception {
118: GeometryFactory gf = new GeometryFactory();
119: LineString ls = gf.createLineString(new Coordinate[] {
120: new Coordinate(10, 15), new Coordinate(20, 25) });
121:
122: // see if coordinates gets flipped, urn forces lat/lon interpretation
123: Intersects original = ff.intersects(ff.property("geom"), ff
124: .literal(ls));
125: Filter clone = (Filter) original.accept(reprojector, null);
126: assertNotSame(original, clone);
127: assertEquals(original, clone);
128: }
129:
130: public void testIntersectsUnreferencedProperty() throws Exception {
131: GeometryFactory gf = new GeometryFactory();
132: LineString ls = gf.createLineString(new Coordinate[] {
133: new Coordinate(10, 15), new Coordinate(20, 25) });
134: ls
135: .setUserData(CRS
136: .decode("urn:x-ogc:def:crs:EPSG:6.11.2:4326"));
137:
138: // see if coordinates gets flipped, urn forces lat/lon interpretation
139: Intersects original = ff.intersects(ff.property("line"), ff
140: .literal(ls));
141: Filter clone = (Filter) original.accept(reprojector, null);
142: assertNotSame(original, clone);
143: assertEquals(original, clone);
144: }
145:
146: }
|