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.File;
019: import java.io.IOException;
020: import java.net.MalformedURLException;
021: import java.net.URL;
022: import java.util.HashMap;
023: import java.util.Map;
024:
025: import junit.framework.AssertionFailedError;
026:
027: import org.geotools.TestData;
028: import org.geotools.data.DataStore;
029: import org.geotools.data.DefaultQuery;
030: import org.geotools.data.FeatureReader;
031: import org.geotools.data.FeatureSource;
032: import org.geotools.data.FeatureStore;
033: import org.geotools.data.Query;
034: import org.geotools.feature.Feature;
035: import org.geotools.feature.FeatureCollection;
036: import org.geotools.feature.FeatureIterator;
037: import org.geotools.feature.FeatureType;
038: import org.geotools.filter.FidFilter;
039: import org.geotools.filter.FilterFactory;
040: import org.geotools.filter.FilterFactoryFinder;
041:
042: import com.vividsolutions.jts.geom.Envelope;
043: import com.vividsolutions.jts.geom.Geometry;
044:
045: /**
046: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/plugin/shapefile/src/test/java/org/geotools/data/shapefile/indexed/ShapefileQuadTreeReadWriteTest.java $
047: * @version $Id: ShapefileQuadTreeReadWriteTest.java 26186 2007-07-10 02:18:59Z jdeolive $
048: * @author Ian Schneider
049: */
050: public class ShapefileQuadTreeReadWriteTest extends TestCaseSupport {
051: final String[] files = { "shapes/statepop.shp",
052: "shapes/polygontest.shp", "shapes/pointtest.shp",
053: "shapes/holeTouchEdge.shp", "shapes/stream.shp" };
054: boolean readStarted = false;
055: Exception exception = null;
056:
057: /**
058: * Creates a new instance of ShapefileReadWriteTest
059: */
060: public ShapefileQuadTreeReadWriteTest(String name)
061: throws IOException {
062: super (name);
063: }
064:
065: public void testAll() throws Throwable {
066: StringBuffer errors = new StringBuffer();
067: Exception bad = null;
068:
069: for (int i = 0, ii = files.length; i < ii; i++) {
070: try {
071: test(files[i]);
072: } catch (Exception e) {
073: e.printStackTrace();
074: errors.append("\nFile " + files[i] + " : "
075: + e.getMessage());
076: bad = e;
077: }
078: }
079:
080: if (errors.length() > 0) {
081: fail(errors.toString(), bad);
082: }
083: }
084:
085: public void fail(String message, Throwable cause) throws Throwable {
086: Throwable fail = new AssertionFailedError(message);
087: fail.initCause(cause);
088: throw fail;
089: }
090:
091: public void testWriteTwice() throws Exception {
092: copyShapefiles("shapes/stream.shp");
093: IndexedShapefileDataStoreFactory fac = new IndexedShapefileDataStoreFactory();
094: DataStore s1 = createDataStore(fac, TestData.url(this ,
095: "shapes/stream.shp"), true);
096: String typeName = s1.getTypeNames()[0];
097: FeatureSource source = s1.getFeatureSource(typeName);
098: FeatureType type = source.getSchema();
099: FeatureCollection one = source.getFeatures();
100:
101: IndexedShapefileDataStoreFactory maker = new IndexedShapefileDataStoreFactory();
102:
103: doubleWrite(type, one, getTempFile(), maker, false);
104: doubleWrite(type, one, getTempFile(), maker, true);
105: }
106:
107: private DataStore createDataStore(
108: IndexedShapefileDataStoreFactory fac, URL url,
109: boolean memoryMapped) throws IOException {
110: Map params = new HashMap();
111: params.put(IndexedShapefileDataStoreFactory.URLP.key, url);
112: params
113: .put(
114: IndexedShapefileDataStoreFactory.CREATE_SPATIAL_INDEX.key,
115: new Boolean(true));
116: DataStore createDataStore = fac.createDataStore(params);
117: return createDataStore;
118: }
119:
120: private void doubleWrite(FeatureType type, FeatureCollection one,
121: File tmp, IndexedShapefileDataStoreFactory maker,
122: boolean memorymapped) throws IOException,
123: MalformedURLException {
124: DataStore s;
125: s = createDataStore(maker, tmp.toURL(), memorymapped);
126:
127: s.createSchema(type);
128: FeatureStore store = (FeatureStore) s.getFeatureSource(type
129: .getTypeName());
130:
131: store.addFeatures(one);
132: store.addFeatures(one);
133:
134: s = createDataStore(maker, tmp.toURL(), true);
135: assertEquals(one.size() * 2, store.getCount(Query.ALL));
136: }
137:
138: void test(String f) throws Exception {
139: File file = copyShapefiles(f); // Work on File rather than URL from JAR.
140: DataStore s = createDataStore(
141: new IndexedShapefileDataStoreFactory(), file.toURL(),
142: true);
143: String typeName = s.getTypeNames()[0];
144: FeatureSource source = s.getFeatureSource(typeName);
145: FeatureType type = source.getSchema();
146: FeatureCollection one = source.getFeatures();
147:
148: IndexedShapefileDataStoreFactory maker = new IndexedShapefileDataStoreFactory();
149: test(type, one, getTempFile(), maker, false);
150: test(type, one, getTempFile(), maker, true);
151: }
152:
153: private void test(FeatureType type, FeatureCollection one,
154: File tmp, IndexedShapefileDataStoreFactory maker,
155: boolean memorymapped) throws IOException,
156: MalformedURLException, Exception {
157: DataStore s;
158: String typeName;
159: s = createDataStore(maker, tmp.toURL(), memorymapped);
160:
161: s.createSchema(type);
162:
163: FeatureStore store = (FeatureStore) s.getFeatureSource(type
164: .getTypeName());
165:
166: store.addFeatures(one);
167:
168: s = createDataStore(new IndexedShapefileDataStoreFactory(), tmp
169: .toURL(), true);
170: typeName = s.getTypeNames()[0];
171:
172: FeatureCollection two = s.getFeatureSource(typeName)
173: .getFeatures();
174:
175: compare(one.features(), two.features());
176: }
177:
178: static void compare(FeatureIterator fs1, FeatureIterator fs2)
179: throws Exception {
180:
181: int i = 0;
182:
183: while (fs1.hasNext()) {
184: Feature f1 = fs1.next();
185: Feature f2 = fs2.next();
186:
187: if ((i++ % 50) == 0) {
188: if (verbose) {
189: System.out.print("*");
190: }
191: }
192:
193: compare(f1, f2);
194: }
195: }
196:
197: static void compare(Feature f1, Feature f2) throws Exception {
198: if (f1.getNumberOfAttributes() != f2.getNumberOfAttributes()) {
199: throw new Exception("Unequal number of attributes");
200: }
201:
202: for (int i = 0; i < f1.getNumberOfAttributes(); i++) {
203: Object att1 = f1.getAttribute(i);
204: Object att2 = f2.getAttribute(i);
205:
206: if (att1 instanceof Geometry && att2 instanceof Geometry) {
207: Geometry g1 = ((Geometry) att1);
208: Geometry g2 = ((Geometry) att2);
209: g1.normalize();
210: g2.normalize();
211:
212: if (!g1.equalsExact(g2)) {
213: throw new Exception("Different geometries (" + i
214: + "):\n" + g1 + "\n" + g2);
215: }
216: } else {
217: if (!att1.equals(att2)) {
218: throw new Exception("Different attribute (" + i
219: + "): [" + att1 + "] - [" + att2 + "]");
220: }
221: }
222: }
223: }
224:
225: /**
226: * Test optimized getBounds().
227: *
228: * Testing when filter is a bbox filter and a fidfilter
229: *
230: * @throws Exception
231: */
232: public void testGetBoundsQuery() throws Exception {
233: File file = copyShapefiles("shapes/streams.shp");
234:
235: IndexedShapefileDataStoreFactory fac = new IndexedShapefileDataStoreFactory();
236:
237: Map params = new HashMap();
238: params.put(IndexedShapefileDataStoreFactory.URLP.key, file
239: .toURL());
240: params
241: .put(
242: IndexedShapefileDataStoreFactory.CREATE_SPATIAL_INDEX.key,
243: new Boolean(true));
244: IndexedShapefileDataStore ds = (IndexedShapefileDataStore) fac
245: .createDataStore(params);
246:
247: FilterFactory ff = FilterFactoryFinder.createFilterFactory();
248:
249: FeatureCollection features = ds.getFeatureSource().getFeatures(
250: ff.createFidFilter("streams.84"));
251: FeatureIterator iter = features.features();
252: Envelope bounds;
253: try {
254: bounds = iter.next().getBounds();
255: } finally {
256: iter.close();
257: }
258:
259: FidFilter filter = ff.createFidFilter("streams.84");
260: Query query = new DefaultQuery(ds.getCurrentTypeName(), filter);
261:
262: Envelope result = ds.getFeatureSource().getBounds(query);
263:
264: //assertEquals(bounds, result);
265: assertTrue(result.equals(bounds));
266: }
267:
268: public static final void main(String[] args) throws Exception {
269: verbose = true;
270: junit.textui.TestRunner
271: .run(suite(ShapefileQuadTreeReadWriteTest.class));
272: }
273: }
|