001: /*
002: * Geotools2 - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2005-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: */
017: package org.geotools.data.feature.memory;
018:
019: import java.io.IOException;
020: import java.util.Collection;
021: import java.util.Iterator;
022: import java.util.LinkedList;
023: import java.util.List;
024:
025: import junit.framework.TestCase;
026:
027: import org.geotools.data.ComplexTestData;
028: import org.geotools.data.Query;
029: import org.geotools.data.Source;
030: import org.geotools.data.feature.FeatureSource2;
031: import org.geotools.factory.CommonFactoryFinder;
032: import org.geotools.feature.iso.AttributeBuilder;
033: import org.geotools.feature.iso.AttributeFactoryImpl;
034: import org.geotools.feature.iso.TypeBuilder;
035: import org.geotools.feature.iso.Types;
036: import org.geotools.feature.iso.simple.SimpleTypeFactoryImpl;
037: import org.geotools.feature.iso.type.TypeFactoryImpl;
038: import org.geotools.filter.IllegalFilterException;
039: import org.opengis.feature.Attribute;
040: import org.opengis.feature.ComplexAttribute;
041: import org.opengis.feature.Feature;
042: import org.opengis.feature.FeatureCollection;
043: import org.opengis.feature.FeatureFactory;
044: import org.opengis.feature.simple.SimpleFeatureType;
045: import org.opengis.feature.type.AttributeDescriptor;
046: import org.opengis.feature.type.AttributeType;
047: import org.opengis.feature.type.ComplexType;
048: import org.opengis.feature.type.FeatureType;
049: import org.opengis.feature.type.Name;
050: import org.opengis.feature.type.PropertyDescriptor;
051: import org.opengis.feature.type.TypeFactory;
052: import org.opengis.filter.Filter;
053: import org.opengis.filter.FilterFactory;
054: import org.opengis.filter.PropertyIsGreaterThan;
055: import org.opengis.filter.PropertyIsNull;
056: import org.opengis.filter.expression.Expression;
057:
058: import com.vividsolutions.jts.geom.Coordinate;
059: import com.vividsolutions.jts.geom.Envelope;
060: import com.vividsolutions.jts.geom.GeometryFactory;
061:
062: /**
063: *
064: * @author Gabriel Roldan, Axios Engineering
065: * @version $Id: MemoryDataAccessTest.java 28577 2008-01-03 15:44:29Z groldan $
066: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/community-schemas/community-schema-ds/src/test/java/org/geotools/data/feature/memory/MemoryDataAccessTest.java $
067: * @since 2.4
068: */
069: public class MemoryDataAccessTest extends TestCase {
070:
071: private MemoryDataAccess dataStore;
072:
073: FeatureType wq_plusType;
074:
075: static List wq_plusFeatures;
076:
077: static final int NUM_FEATURES = 20;
078:
079: private TypeBuilder builder;
080:
081: private FilterFactory ff;
082:
083: protected void setUp() throws Exception {
084: super .setUp();
085: wq_plusType = ComplexTestData
086: .createExample01MultiValuedComplexProperty(new TypeFactoryImpl());
087: TypeFactory tf = new TypeFactoryImpl();
088: builder = new TypeBuilder(tf);
089: ff = CommonFactoryFinder.getFilterFactory(null);
090:
091: dataStore = complexFixture();
092:
093: }
094:
095: protected void tearDown() throws Exception {
096: super .tearDown();
097: wq_plusType = null;
098: wq_plusFeatures = null;
099: }
100:
101: public void testFilter() throws Exception {
102: TypeFactory tf = new SimpleTypeFactoryImpl();
103: builder = new TypeBuilder(tf);
104:
105: FeatureFactory af = new AttributeFactoryImpl();
106: AttributeBuilder attBuilder = new AttributeBuilder(af);
107:
108: builder.setName("testType");
109: builder.setBinding(Integer.class);
110: AttributeType testType = builder.attribute();
111:
112: builder.addAttribute("test", testType);
113: builder.setName("typeName");
114:
115: SimpleFeatureType t = (SimpleFeatureType) builder.feature();
116:
117: attBuilder.setType(t);
118: attBuilder.add(null, "test");
119: Attribute attribute = attBuilder.build();
120:
121: PropertyIsGreaterThan gtFilter = ff.greater(
122: ff.property("test"), ff.literal(12));
123: PropertyIsNull nullf = ff.isNull(ff.property("test"));
124:
125: Filter filter = ff.or(nullf, gtFilter);
126:
127: boolean contains = filter.evaluate(attribute);
128: assertTrue(contains);
129: }
130:
131: public void testDescribeType() throws IOException {
132: AttributeDescriptor descriptor = (AttributeDescriptor) dataStore
133: .describe(wq_plusType.getName());
134: assertNotNull(descriptor);
135: FeatureType schema = (FeatureType) descriptor.type();
136: assertNotNull(schema);
137: assertEquals(wq_plusType, schema);
138: }
139:
140: public void testFeatureReader() throws IOException {
141: Name typeName = wq_plusType.getName();
142: FeatureSource2 source = (FeatureSource2) dataStore
143: .access(typeName);
144:
145: FeatureCollection features = (FeatureCollection) source
146: .content();
147: assertNotNull(features);
148:
149: int count = 0;
150: Iterator reader = features.iterator();
151: for (; reader.hasNext();) {
152: Feature object = (Feature) reader.next();
153: assertNotNull(object);
154: count++;
155: }
156: features.close(reader);
157: assertEquals(NUM_FEATURES, count);
158: }
159:
160: public void testFeatureSource() throws IOException {
161: Name name = wq_plusType.getName();
162: FeatureSource2 fs = (FeatureSource2) dataStore.access(name);
163: assertNotNull(fs);
164: AttributeDescriptor describe = (AttributeDescriptor) fs
165: .describe();
166: assertEquals(wq_plusType, describe.type());
167:
168: assertNotNull(fs.getBounds());
169: Envelope expected = new Envelope();
170: for (Iterator it = wq_plusFeatures.iterator(); it.hasNext();) {
171: expected.expandToInclude((Envelope) ((Feature) it.next())
172: .getBounds());
173: }
174: assertTrue(expected.equals(fs.getBounds()));
175: assertEquals(NUM_FEATURES, fs.getCount(Query.ALL));
176:
177: assertSame(dataStore, fs.getDataStore());
178: }
179:
180: /**
181: * Query:
182: *
183: * <pre>
184: * (measurement/determinand_description = 'determinand_description_5_0')
185: * OR
186: * ( length(project_no) > 12) //at least ending with two digits
187: * </pre>
188: */
189: public void testComplexQuery() throws IllegalFilterException,
190: IOException {
191: FilterFactory ff = CommonFactoryFinder.getFilterFactory(null);
192:
193: Filter determinand = ff.equals(ff
194: .property("measurement/determinand_description"), ff
195: .literal("determinand_description_5_0"));
196:
197: Expression length = ff.function("LengthFunction", ff
198: .property("project_no"));
199: Filter project_no = ff.greater(length, ff.literal(13));
200:
201: Filter filter = ff.or(determinand, project_no);
202:
203: Source source = dataStore.access(wq_plusType.getName());
204: FeatureCollection result = (FeatureCollection) source
205: .content(filter);
206:
207: assertNotNull(result);
208:
209: int expected = 1 + (NUM_FEATURES - 10);
210: int actual = ((Collection) result).size();
211: assertEquals(expected, actual);
212: }
213:
214: /**
215: * Creates an in memory datastore containing a complex FeatureType:
216: * <p>
217: * Schema:
218: *
219: * <pre>
220: * wq_plus
221: * sitename (1..1)
222: * anzlic_no (0..1)
223: * location (0..1)
224: * measurement (0..*)
225: * determinand_description (1..1)
226: * result (1..1)
227: * project_no (0..1)
228: * </pre>
229: *
230: * </p>
231: * <p>
232: * The features created has a variable number of measurement attribute
233: * instances. {@link #NUM_FEATURES} features are created inside an iteration
234: * from 0 to <code>NUM_FEATURES - 1</code>. The iteration number is the
235: * number of measurement instances each feature has.
236: * </p>
237: *
238: * @return
239: */
240: private static MemoryDataAccess complexFixture() throws IOException {
241: MemoryDataAccess md = new MemoryDataAccess();
242: final TypeFactory tf = new TypeFactoryImpl();
243:
244: final FeatureFactory attf = new AttributeFactoryImpl();
245: final GeometryFactory gf = new GeometryFactory();
246: final AttributeBuilder builder = new AttributeBuilder(attf);
247:
248: FeatureType ftype = ComplexTestData
249: .createExample01MultiValuedComplexProperty(tf);
250:
251: // md.createSchema(ftype);
252:
253: wq_plusFeatures = new LinkedList();
254: final String namespaceURI = ftype.getName().getNamespaceURI();
255:
256: builder.setType(ftype);
257: for (int i = 0; i < NUM_FEATURES; i++) {
258:
259: builder.add("sitename_" + i, Types.typeName(namespaceURI,
260: "sitename"));
261:
262: builder.add("anzlic_no_" + i, Types.typeName(namespaceURI,
263: "anzlic_no"));
264:
265: builder.add(gf.createPoint(new Coordinate(i, i)), Types
266: .typeName(namespaceURI, "location"));
267:
268: PropertyDescriptor measurementDescriptor = Types
269: .descriptor(ftype, Types.typeName(namespaceURI,
270: "measurement"));
271:
272: ComplexType mtype = (ComplexType) measurementDescriptor
273: .type();
274:
275: AttributeBuilder mbuilder = new AttributeBuilder(attf);
276:
277: //Collection measurements = new ArrayList();
278:
279: for (int mcount = 0; mcount < i; mcount++) {
280: mbuilder.setType(mtype);
281:
282: mbuilder.add("determinand_description_" + i + "_"
283: + mcount, Types.typeName(namespaceURI,
284: "determinand_description"));
285:
286: mbuilder.add("result_" + i + "_" + mcount, Types
287: .typeName(namespaceURI, "result"));
288:
289: ComplexAttribute measurement = (ComplexAttribute) mbuilder
290: .build();
291: // measurements.add(measurement);
292:
293: builder.add(measurement.getValue(), Types.typeName(
294: namespaceURI, "measurement"));
295: }
296: /*
297: * if (measurements.size() > 0) { builder .add(measurements, new
298: * Name(namespaceURI, "measurement")); }
299: */
300:
301: builder.add("project_no_ " + i, Types.typeName(
302: namespaceURI, "project_no"));
303:
304: String fid = ftype.getName().getLocalPart() + "." + i;
305: Feature f = (Feature) builder.build(fid);
306:
307: wq_plusFeatures.add(f);
308: }
309:
310: md.addFeatures(wq_plusFeatures);
311: return md;
312: }
313: }
|