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.data.shapefile.indexed;
017:
018: import java.io.IOException;
019: import java.net.URL;
020: import java.util.HashMap;
021: import java.util.Iterator;
022: import java.util.Map;
023: import java.util.Set;
024:
025: import org.geotools.data.DefaultQuery;
026: import org.geotools.data.FeatureStore;
027: import org.geotools.data.Query;
028: import org.geotools.feature.Feature;
029: import org.geotools.feature.FeatureCollection;
030: import org.geotools.feature.FeatureCollections;
031: import org.geotools.feature.FeatureIterator;
032: import org.geotools.feature.FeatureType;
033: import org.geotools.filter.BBoxExpression;
034: import org.geotools.filter.FidFilter;
035: import org.geotools.filter.FilterFactory;
036: import org.geotools.filter.FilterFactoryFinder;
037: import org.geotools.filter.FilterType;
038: import org.geotools.filter.GeometryFilter;
039:
040: import com.vividsolutions.jts.geom.Geometry;
041:
042: public class FidQueryTest extends FIDTestCase {
043: private IndexedShapefileDataStore ds;
044:
045: Map fids = new HashMap();
046:
047: FeatureStore featureStore;
048:
049: private int numFeatures;
050:
051: protected void setUp() throws Exception {
052:
053: super .setUp();
054:
055: URL url = backshp.toURL();
056: ds = new IndexedShapefileDataStore(url, null, false, true,
057: IndexedShapefileDataStore.TREE_QIX);
058: numFeatures = 0;
059: featureStore = (FeatureStore) ds.getFeatureSource();
060: {
061: FeatureIterator features = featureStore.getFeatures()
062: .features();
063: try {
064: while (features.hasNext()) {
065: numFeatures++;
066: Feature feature = features.next();
067: fids.put(feature.getID(), feature);
068: }
069: } finally {
070: if (features != null)
071: features.close();
072: }
073: assertEquals(numFeatures, fids.size());
074: //System.out.println("Number of Features=" + numFeatures);
075: }
076:
077: }
078:
079: public void testGetByFID() throws Exception {
080:
081: assertFidsMatch();
082:
083: }
084:
085: public void testAddFeature() throws Exception {
086: Feature feature = ((Feature) this .fids.values().iterator()
087: .next());
088: FeatureType schema = ds.getSchema();
089: Feature newfeature = schema.create(feature
090: .getAttributes(new Object[feature.getFeatureType()
091: .getAttributeCount()]));
092: FeatureCollection collection = FeatureCollections
093: .newCollection();
094: collection.add(newfeature);
095: Set newFids = featureStore.addFeatures(collection);
096: assertEquals(1, newFids.size());
097: this .assertFidsMatch();
098: FilterFactory fac = FilterFactoryFinder.createFilterFactory();
099:
100: DefaultQuery query = new DefaultQuery(schema.getTypeName());
101: String fid = (String) newFids.iterator().next();
102: query.setFilter(fac.createFidFilter(fid));
103: FeatureIterator features = featureStore.getFeatures(query)
104: .features();
105: try {
106: feature = features.next();
107: for (int i = 0; i < schema.getAttributeCount(); i++) {
108: if (newfeature.getAttribute(i) instanceof Geometry) {
109: assertTrue(((Geometry) newfeature.getAttribute(i))
110: .equals((Geometry) feature.getAttribute(i)));
111: } else {
112: assertEquals(newfeature.getAttribute(i), feature
113: .getAttribute(i));
114: }
115: }
116: assertFalse(features.hasNext());
117: } finally {
118: if (features != null)
119: features.close();
120: }
121: }
122:
123: public void testModifyFeature() throws Exception {
124: Feature feature = ((Feature) this .fids.values().iterator()
125: .next());
126: int newId = 237594123;
127: FilterFactory fac = FilterFactoryFinder.createFilterFactory();
128: FidFilter createFidFilter = fac
129: .createFidFilter(feature.getID());
130: featureStore.modifyFeatures(feature.getFeatureType()
131: .getAttributeType("ID"), new Integer(newId),
132: createFidFilter);
133: FeatureIterator features = featureStore.getFeatures(
134: createFidFilter).features();
135: try {
136: assertFalse(feature.equals(features.next()));
137: } finally {
138: if (features != null)
139: features.close();
140: }
141: feature.setAttribute("ID", new Integer(newId));
142: this .assertFidsMatch();
143: }
144:
145: public void testDeleteFeature() throws Exception {
146: FeatureIterator features = featureStore.getFeatures()
147: .features();
148: Feature feature;
149: try {
150: feature = features.next();
151: } finally {
152: if (features != null)
153: features.close();
154: }
155: FilterFactory fac = FilterFactoryFinder.createFilterFactory();
156: FidFilter createFidFilter = fac
157: .createFidFilter(feature.getID());
158: featureStore.removeFeatures(createFidFilter);
159: fids.remove(feature.getID());
160:
161: assertEquals(fids.size(), featureStore.getCount(Query.ALL));
162:
163: features = featureStore.getFeatures(createFidFilter).features();
164: try {
165: assertFalse(features.hasNext());
166: } finally {
167: if (features != null)
168: features.close();
169: }
170:
171: this .assertFidsMatch();
172:
173: }
174:
175: public void testFIDBBoxQuery() throws Exception {
176: FeatureIterator features = featureStore.getFeatures()
177: .features();
178: Feature feature;
179: try {
180: feature = features.next();
181: feature = features.next();
182: feature = features.next();
183: } finally {
184: if (features != null)
185: features.close();
186: }
187:
188: FilterFactory factory = FilterFactoryFinder
189: .createFilterFactory();
190: BBoxExpression bb = factory.createBBoxExpression(feature
191: .getBounds());
192:
193: GeometryFilter bboxFilter = factory
194: .createGeometryFilter(FilterType.GEOMETRY_INTERSECTS);
195: bboxFilter.addRightGeometry(bb);
196:
197: String geom = ds.getSchema().getDefaultGeometry().getName();
198:
199: bboxFilter.addLeftGeometry(factory
200: .createAttributeExpression(geom));
201:
202: features = featureStore.getFeatures(bboxFilter).features();
203:
204: try {
205: while (features.hasNext()) {
206: Feature newFeature = features.next();
207: assertEquals(newFeature, fids.get(newFeature.getID()));
208: }
209: } finally {
210: if (features != null)
211: features.close();
212: }
213: }
214:
215: private void assertFidsMatch() throws IOException {
216: //long start = System.currentTimeMillis();
217: FilterFactory fac = FilterFactoryFinder.createFilterFactory();
218:
219: DefaultQuery query = new DefaultQuery(featureStore.getSchema()
220: .getTypeName());
221:
222: int i = 0;
223:
224: for (Iterator iter = fids.entrySet().iterator(); iter.hasNext();) {
225: i++;
226: Map.Entry entry = (Map.Entry) iter.next();
227: String fid = (String) entry.getKey();
228: query.setFilter(fac.createFidFilter(fid));
229: FeatureIterator features = featureStore.getFeatures(query)
230: .features();
231: try {
232: Feature feature = features.next();
233: assertFalse(features.hasNext());
234: assertEquals(i + "th feature", entry.getValue(),
235: feature);
236: } finally {
237: if (features != null)
238: features.close();
239: }
240:
241: }
242: long end = System.currentTimeMillis();
243: //System.out.println("Time to search by fid=" + (end - start) + "ms");
244: }
245:
246: }
|