001: /*
002: * Geotools2 - 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: */
017: package org.geotools.data.shapefile;
018:
019: import java.io.File;
020: import java.io.IOException;
021: import java.math.BigDecimal;
022: import java.math.BigInteger;
023: import java.net.URI;
024: import java.net.URL;
025: import java.net.URLDecoder;
026: import java.nio.charset.Charset;
027: import java.util.Arrays;
028: import java.util.Date;
029: import java.util.HashMap;
030: import java.util.Map;
031:
032: import org.geotools.TestData;
033: import org.geotools.data.DataStore;
034: import org.geotools.data.DataUtilities;
035: import org.geotools.data.DefaultQuery;
036: import org.geotools.data.FeatureReader;
037: import org.geotools.data.FeatureSource;
038: import org.geotools.data.FeatureWriter;
039: import org.geotools.data.Query;
040: import org.geotools.data.Transaction;
041: import org.geotools.feature.AttributeType;
042: import org.geotools.feature.AttributeTypeFactory;
043: import org.geotools.feature.Feature;
044: import org.geotools.feature.FeatureCollection;
045: import org.geotools.feature.FeatureCollections;
046: import org.geotools.feature.FeatureIterator;
047: import org.geotools.feature.FeatureType;
048: import org.geotools.feature.FeatureTypeFactory;
049: import org.geotools.feature.FeatureTypes;
050: import org.geotools.feature.SimpleFeature;
051: import org.geotools.feature.type.BasicFeatureTypes;
052: import org.geotools.filter.CompareFilter;
053: import org.geotools.filter.Filter;
054: import org.geotools.filter.FilterFactory;
055: import org.geotools.filter.FilterFactoryFinder;
056: import org.geotools.filter.GeometryFilter;
057: import org.geotools.referencing.CRS;
058: import org.opengis.filter.expression.Expression;
059:
060: import com.vividsolutions.jts.geom.Coordinate;
061: import com.vividsolutions.jts.geom.Geometry;
062: import com.vividsolutions.jts.geom.GeometryCollection;
063: import com.vividsolutions.jts.geom.GeometryFactory;
064: import com.vividsolutions.jts.geom.LinearRing;
065: import com.vividsolutions.jts.geom.Polygon;
066: import com.vividsolutions.jts.geom.PrecisionModel;
067:
068: /**
069: *
070: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/plugin/shapefile/src/test/java/org/geotools/data/shapefile/ShapefileDataStoreTest.java $
071: * @version $Id: ShapefileDataStoreTest.java 26119 2007-07-02 17:41:05Z aaime $
072: * @author Ian Schneider
073: */
074: public class ShapefileDataStoreTest extends TestCaseSupport {
075:
076: final static String STATE_POP = "shapes/statepop.shp";
077: final static String STREAM = "shapes/stream.shp";
078: final static String DANISH = "shapes/danish_point.shp";
079: final static String CHINESE = "shapes/chinese_poly.shp";
080:
081: public ShapefileDataStoreTest(String testName) throws IOException {
082: super (testName);
083: }
084:
085: protected FeatureCollection loadFeatures(String resource,
086: Query query) throws Exception {
087: assertNotNull(query);
088:
089: URL url = TestData.url(resource);
090: ShapefileDataStore s = new ShapefileDataStore(url);
091: FeatureSource fs = s.getFeatureSource(s.getTypeNames()[0]);
092: return fs.getFeatures(query);
093: }
094:
095: protected FeatureCollection loadLocalFeaturesM2()
096: throws IOException {
097: String target = "jar:file:/C:/Documents and Settings/jgarnett/.m2/repository/org/geotools/gt2-sample-data/2.4-SNAPSHOT/gt2-sample-data-2.4-SNAPSHOT.jar!/org/geotools/test-data/shapes/statepop.shp";
098: URL url = new URL(target);
099: ShapefileDataStore s = new ShapefileDataStore(url);
100: FeatureSource fs = s.getFeatureSource(s.getTypeNames()[0]);
101: return fs.getFeatures();
102: }
103:
104: protected FeatureCollection loadFeatures(String resource,
105: Charset charset, Query q) throws Exception {
106: if (q == null)
107: q = new DefaultQuery();
108: URL url = TestData.url(resource);
109: ShapefileDataStore s = new ShapefileDataStore(url, false,
110: charset);
111: FeatureSource fs = s.getFeatureSource(s.getTypeNames()[0]);
112: return fs.getFeatures(q);
113: }
114:
115: protected FeatureCollection loadFeatures(ShapefileDataStore s)
116: throws Exception {
117: return s.getFeatureSource(s.getTypeNames()[0]).getFeatures();
118: }
119:
120: public void testLoad() throws Exception {
121: loadFeatures(STATE_POP, Query.ALL);
122: }
123:
124: public void testLoadDanishChars() throws Exception {
125: FeatureCollection fc = loadFeatures(DANISH, Query.ALL);
126: Feature first = fc.features().next();
127: // Charløtte, if you can read it with your OS charset
128: assertEquals("Charl\u00F8tte", first.getAttribute("TEKST1"));
129: }
130:
131: public void testLoadChineseChars() throws Exception {
132: FeatureCollection fc = loadFeatures(CHINESE, Charset
133: .forName("GB18030"), null);
134: Feature first = fc.features().next();
135: String s = (String) first.getAttribute("NAME");
136: assertEquals("\u9ed1\u9f99\u6c5f\u7701", first
137: .getAttribute("NAME"));
138: }
139:
140: public void testNamespace() throws Exception {
141: ShapefileDataStoreFactory factory = new ShapefileDataStoreFactory();
142: Map map = new HashMap();
143: URI namespace = new URI("http://jesse.com");
144: map.put(ShapefileDataStoreFactory.NAMESPACEP.key, namespace);
145: map.put(ShapefileDataStoreFactory.URLP.key, TestData
146: .url(STATE_POP));
147: DataStore store = factory.createDataStore(map);
148: assertEquals(namespace, store.getSchema(
149: STATE_POP.substring(STATE_POP.lastIndexOf('/') + 1,
150: STATE_POP.lastIndexOf('.'))).getNamespace());
151: }
152:
153: public void testSchema() throws Exception {
154: URL url = TestData.url(STATE_POP);
155: ShapefileDataStore s = new ShapefileDataStore(url);
156: FeatureType schema = s.getSchema(s.getTypeNames()[0]);
157: AttributeType[] types = schema.getAttributeTypes();
158: assertEquals("Number of Attributes", 253, types.length);
159: }
160:
161: public void testSpacesInPath() throws Exception {
162: URL u = TestData.url(this , "folder with spaces/pointtest.shp");
163: File f = new File(URLDecoder.decode(u.getFile(), "UTF-8"));
164: assertTrue(f.exists());
165: ShapefileDataStore s = new ShapefileDataStore(u);
166: loadFeatures(s);
167: }
168:
169: /**
170: * Test envelope versus old DataSource
171: */
172: public void testEnvelope() throws Exception {
173: FeatureCollection features = loadFeatures(STATE_POP, Query.ALL);
174: ShapefileDataStore s = new ShapefileDataStore(TestData
175: .url(STATE_POP));
176: String typeName = s.getTypeNames()[0];
177: FeatureCollection all = s.getFeatureSource(typeName)
178: .getFeatures();
179:
180: assertEquals(features.getBounds(), all.getBounds());
181: }
182:
183: public void testLoadAndVerify() throws Exception {
184: FeatureCollection features = loadFeatures(STATE_POP, Query.ALL);
185: //FeatureCollection features = loadFeaturesM2();
186: int count = features.size();
187:
188: assertTrue("Have features", count > 0);
189: //assertEquals("Number of Features loaded",49,features.size()); // FILE (correct value)
190: //assertEquals("Number of Features loaded",3, count); // JAR
191:
192: FeatureType schema = firstFeature(features).getFeatureType();
193: assertNotNull(schema.getDefaultGeometry());
194: assertEquals("Number of Attributes", 253, schema
195: .getAttributeTypes().length);
196: assertEquals("Value of statename is wrong", firstFeature(
197: features).getAttribute("STATE_NAME"), "Illinois");
198: assertEquals("Value of land area is wrong",
199: ((Double) firstFeature(features)
200: .getAttribute("LAND_KM")).doubleValue(),
201: 143986.61, 0.001);
202: }
203:
204: public void testLoadAndCheckParentTypeIsPolygon() throws Exception {
205: FeatureCollection features = loadFeatures(STATE_POP, Query.ALL);
206: FeatureType schema = firstFeature(features).getFeatureType();
207: assertTrue(schema.isDescendedFrom(BasicFeatureTypes.POLYGON));
208: assertTrue(schema.isDescendedFrom(
209: FeatureTypes.DEFAULT_NAMESPACE, "polygonFeature"));
210: }
211:
212: public void testCreateSchema() throws Exception {
213: File file = new File("test.shp");
214: URL toURL = file.toURL();
215: ShapefileDataStore ds = new ShapefileDataStore(toURL);
216: ds.createSchema(DataUtilities.createType("test",
217: "geom:MultiPolygon"));
218:
219: // ds = new ShapefileDataStore(toURL); this is not needed?
220: assertEquals("test", ds.getSchema().getTypeName());
221:
222: file.deleteOnExit();
223: file = new File("test.dbf");
224: file.deleteOnExit();
225: file = new File("test.shp");
226: file.deleteOnExit();
227:
228: file = new File("test.prj");
229: if (file.exists())
230: file.deleteOnExit();
231:
232: file = new File("test.shx");
233: if (file.exists())
234: file.deleteOnExit();
235: }
236:
237: public void testForceCRS() throws Exception {
238: File file = new File("test.shp");
239: URL toURL = file.toURL();
240:
241: ShapefileDataStore ds = new ShapefileDataStore(toURL);
242: ds.createSchema(DataUtilities.createType("test",
243: "geom:MultiPolygon"));
244: FeatureType before = ds.getSchema();
245:
246: ds.forceSchemaCRS(CRS.decode("EPSG:3005"));
247: FeatureType after = ds.getSchema();
248:
249: assertNotSame(before, after);
250: assertNull("4326", before.getDefaultGeometry()
251: .getCoordinateSystem());
252: assertEquals("NAD83 / BC Albers", after.getDefaultGeometry()
253: .getCoordinateSystem().getName().getCode());
254:
255: file.deleteOnExit();
256: file = new File("test.dbf");
257: file.deleteOnExit();
258: file = new File("test.shp");
259: file.deleteOnExit();
260:
261: file = new File("test.prj");
262:
263: if (file.exists())
264: file.deleteOnExit();
265:
266: file = new File("test.shx");
267: if (file.exists())
268: file.deleteOnExit();
269: }
270:
271: private ShapefileDataStore createDataStore(File f) throws Exception {
272: FeatureCollection fc = createFeatureCollection();
273: ShapefileDataStore sds = new ShapefileDataStore(f.toURL());
274: writeFeatures(sds, fc);
275: return sds;
276: }
277:
278: private ShapefileDataStore createDataStore() throws Exception {
279: return createDataStore(getTempFile());
280: }
281:
282: /**
283: * Create a set of features, then remove every other one, updating the
284: * remaining. Test for removal and proper update after reloading...
285: */
286: public void testUpdating() throws Throwable {
287: try {
288: ShapefileDataStore sds = createDataStore();
289: loadFeatures(sds);
290:
291: FeatureWriter writer = null;
292: try {
293: writer = sds.getFeatureWriter(sds.getTypeNames()[0],
294: Filter.INCLUDE, Transaction.AUTO_COMMIT);
295: while (writer.hasNext()) {
296: Feature feat = writer.next();
297: Byte b = (Byte) feat.getAttribute(1);
298: if (b.byteValue() % 2 == 0) {
299: writer.remove();
300: } else {
301: feat.setAttribute(1, new Byte((byte) -1));
302: }
303: }
304: } finally {
305: if (writer != null)
306: writer.close();
307: }
308: FeatureCollection fc = loadFeatures(sds);
309:
310: assertEquals(10, fc.size());
311: for (FeatureIterator i = fc.features(); i.hasNext();) {
312: assertEquals(-1, ((Byte) i.next().getAttribute(1))
313: .byteValue());
314: }
315: } catch (Throwable t) {
316: if (System.getProperty("os.name").startsWith("Windows")) {
317: System.out.println("Ignore " + t
318: + " because you are on windows");
319: return;
320: } else {
321: throw t;
322: }
323: }
324: }
325:
326: /**
327: * Create a test file, then continue removing the first entry until
328: * there are no features left.
329: */
330: public void testRemoveFromFrontAndClose() throws Throwable {
331: try {
332: ShapefileDataStore sds = createDataStore();
333:
334: int idx = loadFeatures(sds).size();
335:
336: while (idx > 0) {
337: FeatureWriter writer = null;
338:
339: try {
340: writer = sds.getFeatureWriter(
341: sds.getTypeNames()[0], Filter.INCLUDE,
342: Transaction.AUTO_COMMIT);
343: writer.next();
344: writer.remove();
345: } finally {
346: if (writer != null) {
347: writer.close();
348: writer = null;
349: }
350: }
351: assertEquals(--idx, loadFeatures(sds).size());
352: }
353: } catch (Throwable t) {
354: if (System.getProperty("os.name").startsWith("Windows")) {
355: System.out.println("Ignore " + t
356: + " because you are on windows");
357: return;
358: } else {
359: throw t;
360: }
361: }
362:
363: }
364:
365: /**
366: * Create a test file, then continue removing the last entry until
367: * there are no features left.
368: */
369: public void testRemoveFromBackAndClose() throws Throwable {
370: try {
371: ShapefileDataStore sds = createDataStore();
372:
373: int idx = loadFeatures(sds).size();
374:
375: while (idx > 0) {
376: FeatureWriter writer = null;
377: try {
378: writer = sds.getFeatureWriter(
379: sds.getTypeNames()[0], Filter.INCLUDE,
380: Transaction.AUTO_COMMIT);
381: while (writer.hasNext()) {
382: writer.next();
383: }
384: writer.remove();
385: } finally {
386: if (writer != null) {
387: writer.close();
388: writer = null;
389: }
390: }
391: assertEquals(--idx, loadFeatures(sds).size());
392: }
393: } catch (Throwable t) {
394: if (System.getProperty("os.name").startsWith("Windows")) {
395: System.out.println("Ignore " + t
396: + " because you are on windows");
397: return;
398: } else {
399: throw t;
400: }
401: }
402: }
403:
404: public void testWriteShapefileWithNoRecords() throws Exception {
405: //create a FeatureType
406: AttributeType thePolygon = AttributeTypeFactory
407: .newAttributeType("a", Polygon.class);
408: AttributeType attributeB = AttributeTypeFactory
409: .newAttributeType("b", String.class);
410:
411: FeatureType featureType = FeatureTypeFactory.newFeatureType(
412: new AttributeType[] { thePolygon, attributeB },
413: "whatever");
414:
415: File tempFile = getTempFile();
416: ShapefileDataStore shapefileDataStore = new ShapefileDataStore(
417: tempFile.toURL());
418: shapefileDataStore.createSchema(featureType);
419: FeatureWriter featureWriter = shapefileDataStore
420: .getFeatureWriter(shapefileDataStore.getTypeNames()[0],
421: Transaction.AUTO_COMMIT);
422:
423: //don't add any features to the data store....
424:
425: //this should create a shapefile with no records. Not sure about the semantics of this,
426: //but it's meant to be used in the context of a FeatureCollection iteration,
427: //where the FeatureCollection has nothing in it.
428: featureWriter.close();
429: }
430:
431: private FeatureCollection createFeatureCollection()
432: throws Exception {
433: FeatureTypeFactory factory = FeatureTypeFactory
434: .newInstance("junk");
435: factory.addType(AttributeTypeFactory.newAttributeType("a",
436: Geometry.class));
437: factory.addType(AttributeTypeFactory.newAttributeType("b",
438: Byte.class));
439: factory.addType(AttributeTypeFactory.newAttributeType("c",
440: Short.class));
441: factory.addType(AttributeTypeFactory.newAttributeType("d",
442: Double.class));
443: factory.addType(AttributeTypeFactory.newAttributeType("e",
444: Float.class));
445: factory.addType(AttributeTypeFactory.newAttributeType("f",
446: String.class));
447: factory.addType(AttributeTypeFactory.newAttributeType("g",
448: Date.class));
449: factory.addType(AttributeTypeFactory.newAttributeType("h",
450: Boolean.class));
451: factory.addType(AttributeTypeFactory.newAttributeType("i",
452: Number.class));
453: factory.addType(AttributeTypeFactory.newAttributeType("j",
454: Long.class));
455: factory.addType(AttributeTypeFactory.newAttributeType("k",
456: BigDecimal.class));
457: factory.addType(AttributeTypeFactory.newAttributeType("l",
458: BigInteger.class));
459: FeatureType type = factory.getFeatureType();
460: FeatureCollection features = FeatureCollections.newCollection();
461: for (int i = 0, ii = 20; i < ii; i++) {
462: features.add(type.create(new Object[] {
463: new GeometryFactory().createPoint(new Coordinate(1,
464: -1)),
465: new Byte((byte) i),
466: new Short((short) i),
467: new Double(i),
468: new Float(i),
469: new String(i + " "),
470: new Date(i),
471: new Boolean(true),
472: new Integer(22),
473: new Long(1234567890123456789L),
474: new BigDecimal(new BigInteger(
475: "12345678901234567890123456789"), 2),
476: new BigInteger("12345678901234567890123456789") }));
477: }
478: return features;
479: }
480:
481: public void testAttributesWriting() throws Exception {
482: FeatureCollection features = createFeatureCollection();
483: File tmpFile = getTempFile();
484: tmpFile.createNewFile();
485: ShapefileDataStore s = new ShapefileDataStore(tmpFile.toURL());
486: writeFeatures(s, features);
487: }
488:
489: public void testWriteReadBigNumbers() throws Exception {
490: // create feature type
491: FeatureTypeFactory factory = FeatureTypeFactory
492: .newInstance("junk");
493: factory.addType(AttributeTypeFactory.newAttributeType("a",
494: Geometry.class));
495: factory.addType(AttributeTypeFactory.newAttributeType("b",
496: BigDecimal.class));
497: factory.addType(AttributeTypeFactory.newAttributeType("c",
498: BigInteger.class));
499: FeatureType type = factory.getFeatureType();
500: FeatureCollection features = FeatureCollections.newCollection();
501: BigInteger bi = new BigInteger("1234567890123456789");
502: BigDecimal bd = new BigDecimal(bi, 2);
503: features.add(type.create(new Object[] {
504: new GeometryFactory()
505: .createPoint(new Coordinate(1, -1)), bd, bi }));
506:
507: // store features
508: File tmpFile = getTempFile();
509: tmpFile.createNewFile();
510: ShapefileDataStore s = new ShapefileDataStore(tmpFile.toURL());
511: writeFeatures(s, features);
512:
513: // read them back
514: FeatureReader fr = s.getFeatureReader("junk");
515: Feature f = fr.next();
516:
517: // check attribute values (type won't be preserved)
518: assertEquals(((Number) f.getAttribute("b")).doubleValue(), bd
519: .doubleValue(), 0.000001);
520: assertEquals(((Number) f.getAttribute("c")).longValue(), bi
521: .longValue());
522: }
523:
524: public void testGeometriesWriting() throws Exception {
525:
526: String[] wktResources = new String[] { "point", "multipoint",
527: "line", "multiline", "polygon", "multipolygon" };
528:
529: PrecisionModel pm = new PrecisionModel();
530: for (int i = 0; i < wktResources.length; i++) {
531: Geometry geom = readGeometry(wktResources[i]);
532: String testName = wktResources[i];
533: try {
534:
535: runWriteReadTest(geom, false);
536: make3D(geom);
537: testName += "3d";
538: runWriteReadTest(geom, true);
539: } catch (Throwable e) {
540: e.printStackTrace();
541: throw new Exception("Error in " + testName, e);
542: }
543:
544: }
545:
546: }
547:
548: private void make3D(Geometry g) {
549: Coordinate[] c = g.getCoordinates();
550: for (int i = 0, ii = c.length; i < ii; i++) {
551: c[i].z = 42 + i;
552: }
553: }
554:
555: private void writeFeatures(ShapefileDataStore s,
556: FeatureCollection fc) throws Exception {
557: s.createSchema(fc.features().next().getFeatureType());
558: FeatureWriter fw = s.getFeatureWriter(s.getTypeNames()[0],
559: Transaction.AUTO_COMMIT);
560: FeatureIterator it = fc.features();
561: while (it.hasNext()) {
562: ((SimpleFeature) fw.next()).setAttributes(it.next()
563: .getAttributes(null));
564: fw.write();
565: }
566: fw.close();
567: }
568:
569: private void runWriteReadTest(Geometry geom, boolean d3)
570: throws Exception {
571: // make features
572: FeatureTypeFactory factory = FeatureTypeFactory
573: .newInstance("junk");
574: factory.addType(AttributeTypeFactory.newAttributeType("a",
575: Geometry.class));
576: FeatureType type = factory.getFeatureType();
577: FeatureCollection features = FeatureCollections.newCollection();
578: for (int i = 0, ii = 20; i < ii; i++) {
579: features.add(type.create(new Object[] { geom.clone() }));
580: }
581:
582: // set up file
583: File tmpFile = getTempFile();
584: tmpFile.delete();
585:
586: // write features
587: ShapefileDataStore s = new ShapefileDataStore(tmpFile.toURL());
588: s.createSchema(type);
589: writeFeatures(s, features);
590:
591: // read features
592: s = new ShapefileDataStore(tmpFile.toURL());
593: FeatureCollection fc = loadFeatures(s);
594: FeatureIterator fci = fc.features();
595: // verify
596: while (fci.hasNext()) {
597: Feature f = fci.next();
598: Geometry fromShape = f.getDefaultGeometry();
599:
600: if (fromShape instanceof GeometryCollection) {
601: if (!(geom instanceof GeometryCollection)) {
602: fromShape = ((GeometryCollection) fromShape)
603: .getGeometryN(0);
604: }
605: }
606: try {
607: Coordinate[] c1 = geom.getCoordinates();
608: Coordinate[] c2 = fromShape.getCoordinates();
609: for (int cc = 0, ccc = c1.length; cc < ccc; cc++) {
610: if (d3)
611: assertTrue(c1[cc].equals3D(c2[cc]));
612: else
613: assertTrue(c1[cc].equals2D(c2[cc]));
614: }
615: } catch (Throwable t) {
616: fail("Bogus : " + Arrays.asList(geom.getCoordinates())
617: + " : "
618: + Arrays.asList(fromShape.getCoordinates()));
619: }
620: }
621: tmpFile.delete();
622: }
623:
624: public void testGetCount() throws Exception {
625: assertTrue(copyShapefiles(STREAM).canRead()); // The following test seems to fail in the URL point into the JAR file.
626: ShapefileDataStore store = (ShapefileDataStore) new ShapefileDataStoreFactory()
627: .createDataStore(TestData.url(this , STREAM));
628: int count = 0;
629: FeatureReader reader = store.getFeatureReader();
630: try {
631: while (reader.hasNext()) {
632: count++;
633: reader.next();
634: }
635: assertEquals(count, store.getCount(Query.ALL));
636: } finally {
637: reader.close();
638: }
639: }
640:
641: /**
642: * Checks if feature reading optimizations still allow to execute the queries or not
643: * @throws Exception
644: */
645: public void testGetReaderOptimizations() throws Exception {
646: URL url = TestData.url(STATE_POP);
647: ShapefileDataStore s = new ShapefileDataStore(url);
648:
649: // attributes other than geometry can be ignored here
650: Query query = new DefaultQuery(s.getSchema().getTypeName(),
651: Filter.INCLUDE, new String[] { "the_geom" });
652: FeatureReader reader = s.getFeatureReader(s.getSchema()
653: .getTypeName(), query);
654: assertEquals(1, reader.getFeatureType().getAttributeCount());
655: assertEquals("the_geom", reader.getFeatureType()
656: .getAttributeTypes()[0].getName());
657:
658: // here too, the filter is using the geometry only
659: FilterFactory ff = FilterFactoryFinder.createFilterFactory();
660: GeometryFactory gc = new GeometryFactory();
661: LinearRing ring = gc.createLinearRing(new Coordinate[] {
662: new Coordinate(0, 0), new Coordinate(10, 0),
663: new Coordinate(10, 10), new Coordinate(0, 10),
664: new Coordinate(0, 0) });
665: Polygon polygon = gc.createPolygon(ring, null);
666: GeometryFilter gf = ff
667: .createGeometryFilter(Filter.GEOMETRY_BBOX);
668: gf.addLeftGeometry(ff.createAttributeExpression("the_geom"));
669: gf.addRightGeometry(ff.createLiteralExpression(polygon));
670: query = new DefaultQuery(s.getSchema().getTypeName(), gf,
671: new String[] { "the_geom" });
672: reader = s.getFeatureReader(s.getSchema().getTypeName(), query);
673: assertEquals(1, reader.getFeatureType().getAttributeCount());
674: assertEquals("the_geom", reader.getFeatureType()
675: .getAttributeTypes()[0].getName());
676:
677: // here not, we need state_name in the feature type, so open the dbf file please
678: CompareFilter cf = ff
679: .createCompareFilter(Filter.COMPARE_EQUALS);
680: cf.addLeftValue(ff.createAttributeExpression("STATE_NAME"));
681: cf.addRightValue(ff.createLiteralExpression("Illinois"));
682: query = new DefaultQuery(s.getSchema().getTypeName(), cf,
683: new String[] { "the_geom" });
684: reader = s.getFeatureReader(s.getSchema().getTypeName(), query);
685: assertEquals(s.getSchema(), reader.getFeatureType());
686: }
687:
688: /**
689: * This is useful to dump a UTF16 character to an UT16 escape sequence, basically
690: * the only way to represent the chars we don't have on the keyboard (such as chinese ones :))
691: * @param c
692: * @return
693: */
694: static public String charToHex(char c) {
695: // Returns hex String representation of char c
696: byte hi = (byte) (c >>> 8);
697: byte lo = (byte) (c & 0xff);
698: return byteToHex(hi) + byteToHex(lo);
699: }
700:
701: static public String byteToHex(byte b) {
702: // Returns hex String representation of byte b
703: char[] hexDigit = { '0', '1', '2', '3', '4', '5', '6', '7',
704: '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
705: char[] array = { hexDigit[(b >> 4) & 0x0f], hexDigit[b & 0x0f] };
706: return new String(array);
707: }
708: }
|