001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-2006, GeoTools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.caching;
017:
018: import com.vividsolutions.jts.geom.Coordinate;
019: import com.vividsolutions.jts.geom.Envelope;
020: import com.vividsolutions.jts.geom.GeometryFactory;
021: import com.vividsolutions.jts.geom.LinearRing;
022: import com.vividsolutions.jts.geom.Polygon;
023: import com.vividsolutions.jts.geom.impl.CoordinateArraySequence;
024:
025: import junit.framework.Test;
026: import junit.framework.TestCase;
027: import junit.framework.TestSuite;
028:
029: import org.geotools.caching.util.FilterSplitter;
030:
031: import org.geotools.filter.FilterFactoryImpl;
032:
033: import org.opengis.filter.Filter;
034: import org.opengis.filter.FilterFactory;
035:
036: import java.util.ArrayList;
037: import java.util.List;
038:
039: public class FilterSplitterTest extends TestCase {
040: protected FilterFactory ff;
041: protected Filter bb;
042: protected Filter bb2;
043: protected Filter att;
044: protected Filter bso;
045: protected Envelope bbenv;
046: protected Envelope bb2env;
047: protected Envelope bsoenv;
048:
049: protected void setUp() {
050: ff = new FilterFactoryImpl();
051: bb = ff.bbox("geom", 0, 10, 1000, 1100, "srs");
052: bbenv = new Envelope(0, 1000, 10, 1100);
053: bb2 = ff.bbox("geom", 500, 510, 1500, 1600, "srs");
054: bb2env = new Envelope(500, 1500, 510, 1600);
055: att = ff.like(ff.property("dummydata"), "Id: 1*");
056:
057: Polygon p = createPolygon();
058: bso = ((FilterFactoryImpl) ff).intersects(ff.property("geom"),
059: ff.literal(p));
060: bsoenv = p.getEnvelopeInternal();
061: }
062:
063: protected static Polygon createPolygon() {
064: Coordinate[] coords = new Coordinate[] {
065: new Coordinate(200, 210), new Coordinate(200, 500),
066: new Coordinate(2000, 2100), new Coordinate(500, 510),
067: new Coordinate(200, 210) };
068: CoordinateArraySequence seq = new CoordinateArraySequence(
069: coords);
070: LinearRing ls = new LinearRing(seq, new GeometryFactory());
071: Polygon ret = new Polygon(ls, null, new GeometryFactory());
072:
073: return ret;
074: }
075:
076: public static Test suite() {
077: return new TestSuite(FilterSplitterTest.class);
078: }
079:
080: public void testBBoxFilter() {
081: FilterSplitter splitter = new FilterSplitter();
082: bb.accept(splitter, null);
083: assertEquals(bbenv, splitter.getEnvelope());
084:
085: Filter result = splitter.getSpatialRestriction();
086: assertEquals(bb, result);
087: }
088:
089: public void testAndFilter() {
090: FilterSplitter splitter = new FilterSplitter();
091: Filter and = ff.and(bb, att);
092: and.accept(splitter, null);
093: assertEquals(bbenv, splitter.getEnvelope());
094: assertEquals(bb, splitter.getSpatialRestriction());
095: assertEquals(att, splitter.getOtherRestriction());
096: and = ff.and(bb, bb2);
097: splitter = new FilterSplitter();
098: and.accept(splitter, null);
099:
100: Envelope i = bbenv.intersection(bb2env);
101: Envelope s = splitter.getEnvelope();
102: assertEquals(i, s);
103: assertEquals(Filter.INCLUDE, splitter.getOtherRestriction());
104: }
105:
106: public void testOrFilter() {
107: FilterSplitter splitter = new FilterSplitter();
108: Filter or = ff.or(bb, att);
109: or.accept(splitter, null);
110: assertNull(splitter.getEnvelope());
111: assertEquals(Filter.INCLUDE, splitter.getSpatialRestriction());
112: assertEquals(or, splitter.getOtherRestriction());
113: or = ff.or(bb, bb2);
114: splitter = new FilterSplitter();
115: or.accept(splitter, null);
116: bbenv.expandToInclude(bb2env);
117: assertEquals(bbenv, splitter.getEnvelope());
118: assertEquals(or, splitter.getOtherRestriction());
119: }
120:
121: public void testNotFilter() {
122: FilterSplitter splitter = new FilterSplitter();
123: Filter not = ff.not(bb);
124: not.accept(splitter, null);
125: assertNull(splitter.getEnvelope());
126: }
127:
128: public void testBinarySpatialOperator() {
129: FilterSplitter splitter = new FilterSplitter();
130: Filter and = ff.and(bb, bso);
131: and.accept(splitter, null);
132: assertEquals(bso, splitter.getOtherRestriction());
133: assertEquals(bbenv.intersection(bsoenv), splitter.getEnvelope());
134: }
135:
136: public void testAndManyFilters() {
137: FilterSplitter splitter = new FilterSplitter();
138: List filters = new ArrayList();
139: filters.add(bb);
140: filters.add(bb2);
141: filters.add(att);
142: filters.add(bso);
143:
144: Filter and = ff.and(filters);
145: and.accept(splitter, null);
146: assertEquals(bbenv.intersection(bb2env), splitter.getEnvelope());
147: assertEquals(ff.and(att, bso), splitter.getOtherRestriction());
148: }
149:
150: public void testOrManyFilters() {
151: FilterSplitter splitter = new FilterSplitter();
152: List filters = new ArrayList();
153: filters.add(bb);
154: filters.add(bb2);
155: filters.add(att);
156: filters.add(bso);
157:
158: Filter or = ff.or(filters);
159: or.accept(splitter, null);
160:
161: Envelope e = new Envelope(bbenv);
162: e.expandToInclude(bb2env);
163: e.expandToInclude(bsoenv);
164: assertEquals(e, splitter.getEnvelope());
165: assertEquals(or, splitter.getOtherRestriction());
166: }
167:
168: public void testOrAndAndManyFilters() {
169: // TODO write test
170: }
171: }
|