/
ShapefileDataStoreTest.java
1578 lines (1337 loc) · 59.4 KB
/
ShapefileDataStoreTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* GeoTools - The Open Source Java GIS Toolkit
* http://geotools.org
*
* (C) 2002-2008, Open Source Geospatial Foundation (OSGeo)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
package org.geotools.data.shapefile;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URI;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;
import java.util.TreeSet;
import java.util.UUID;
import org.geotools.TestData;
import org.geotools.data.DataStore;
import org.geotools.data.DataUtilities;
import org.geotools.data.DefaultTransaction;
import org.geotools.data.FeatureReader;
import org.geotools.data.FeatureWriter;
import org.geotools.data.Query;
import org.geotools.data.Transaction;
import org.geotools.data.shapefile.files.ShpFileType;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.data.simple.SimpleFeatureStore;
import org.geotools.factory.CommonFactoryFinder;
import org.geotools.factory.FactoryRegistryException;
import org.geotools.feature.DefaultFeatureCollection;
import org.geotools.feature.FeatureTypes;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.feature.type.BasicFeatureTypes;
import org.geotools.filter.IllegalFilterException;
import org.geotools.geometry.jts.ReferencedEnvelope;
import org.geotools.referencing.CRS;
import org.junit.After;
import org.junit.Test;
import org.opengis.feature.Feature;
import org.opengis.feature.FeatureVisitor;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.feature.type.AttributeDescriptor;
import org.opengis.feature.type.FeatureType;
import org.opengis.filter.Filter;
import org.opengis.filter.FilterFactory;
import org.opengis.filter.FilterFactory2;
import org.opengis.filter.Id;
import org.opengis.filter.identity.FeatureId;
import org.opengis.filter.identity.Identifier;
import org.opengis.geometry.BoundingBox;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Envelope;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryCollection;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.LineString;
import com.vividsolutions.jts.geom.LinearRing;
import com.vividsolutions.jts.geom.MultiLineString;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.geom.Polygon;
/**
*
*
*
* @source $URL$
* @version $Id$
* @author Ian Schneider
*/
public class ShapefileDataStoreTest extends TestCaseSupport {
final static String STATE_POP = "shapes/statepop.shp";
final static String STREAM = "shapes/stream.shp";
final static String DANISH = "shapes/danish_point.shp";
final static String CHINESE = "shapes/chinese_poly.shp";
final static String RUSSIAN = "shapes/rus-windows-1251.shp";
final static String UTF8 = "shapes/wgs1snt.shp";
final static FilterFactory2 ff = CommonFactoryFinder
.getFilterFactory2(null);
private ShapefileDataStore store;
@After
public void tearDown() throws Exception {
if(store != null) {
store.dispose();
}
System.clearProperty("org.geotools.shapefile.datetime");
super.tearDown();
}
protected SimpleFeatureCollection loadFeatures(String resource, Query query)
throws Exception {
assertNotNull(query);
URL url = TestData.url(resource);
store = new ShapefileDataStore(url);
SimpleFeatureSource fs = store.getFeatureSource(store.getTypeNames()[0]);
return fs.getFeatures(query);
}
protected SimpleFeatureCollection loadLocalFeaturesM2() throws IOException {
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";
URL url = new URL(target);
ShapefileDataStore s = new ShapefileDataStore(url);
SimpleFeatureSource fs = s.getFeatureSource(s.getTypeNames()[0]);
return fs.getFeatures();
}
protected SimpleFeatureCollection loadFeatures(String resource, Charset charset,
Query q) throws Exception {
if (q == null)
q = new Query();
URL url = TestData.url(resource);
store = new ShapefileDataStore(url);
store.setCharset(charset);
SimpleFeatureSource fs = store.getFeatureSource(store.getTypeNames()[0]);
return fs.getFeatures(q);
}
protected SimpleFeatureCollection loadFeatures(ShapefileDataStore s)
throws Exception {
return s.getFeatureSource(s.getTypeNames()[0]).getFeatures();
}
@Test
public void testLoad() throws Exception {
loadFeatures(STATE_POP, Query.ALL);
}
@Test
public void testNullReproject() throws Exception {
// try to reproject to the target CRS, used to fail due to a missing check in
// ContentFeatureSource
CoordinateReferenceSystem targetCRS = loadFeatures(STATE_POP, Query.ALL).getSchema().getCoordinateReferenceSystem();
Query q = new Query(Query.ALL);
q.setCoordinateSystemReproject(targetCRS);
// used to get an exception here
loadFeatures(STATE_POP, q);
}
@Test
public void testLoadDanishChars() throws Exception {
SimpleFeatureCollection fc = loadFeatures(DANISH, Query.ALL);
SimpleFeature first = firstFeature(fc);
// Charlotte (but with the o is stroked)
assertEquals("Charl\u00F8tte", first.getAttribute("TEKST1"));
}
@Test
public void testLoadChineseChars() throws Exception {
try {
SimpleFeatureCollection fc = loadFeatures(CHINESE, Charset
.forName("GB18030"), null);
SimpleFeature first = firstFeature(fc);
String s = (String) first.getAttribute("NAME");
assertEquals("\u9ed1\u9f99\u6c5f\u7701", s);
} catch (UnsupportedCharsetException notInstalledInJRE) {
// this just means you have not installed
// chinese support into your JRE
// (as such it represents a bad configuration
// rather than a test failure)
// we only wanted to ensure that if you have Chinese support
// available - GeoTools can use it
}
}
@Test
public void testLoadRussianChars() throws Exception {
try {
SimpleFeatureCollection fc = loadFeatures(RUSSIAN, Charset
.forName("CP1251"), null);
SimpleFeatureIterator features = fc.features();
SimpleFeature f = features.next();
assertEquals("\u041A\u0438\u0440\u0438\u043B\u043B\u0438\u0446\u0430", f.getAttribute("TEXT"));
f = features.next();
assertEquals("\u0421\u043C\u0435\u0448\u0430\u043D\u044B\u0439 12345", f.getAttribute("TEXT"));
features.close();
} catch (UnsupportedCharsetException notInstalledInJRE) {
// this just means you have not installed Russian support into your JRE
// (as such it represents a bad configuration rather than a test failure)
}
}
/**
* This is just another approach to open the shapefile and pass the Charset
* If the Shape is opened with "ISO-8859-1", the single UTF-8 encoded is shown as two ugly
* chars. As expected, because it's UTF8 two-byte.
*/
@Test
public void testLoadingAndReadingUTF8Wrongly() throws Exception {
SimpleFeatureCollection features = loadFeatures(UTF8, Charset
.forName("ISO-8859-1"), null);
SimpleFeatureIterator iterator = features.features();
assertTrue ( iterator.hasNext() );
assertEquals(4, features.size());
SimpleFeature f = iterator.next();
iterator.close();
// GEOM, NAME,C,100 VISUAL,C,3 NUM1,N,5 NUM2,N,5
assertEquals(5, f.getAttributeCount());
String nameAttribute = (String) f.getAttribute("NAME");
// We expect that the UTF8 is not understood here and there will be one extra char for the misinterpreted special char
assertEquals("Iconfee Stra\u00dfe".length()+1, nameAttribute.length());
}
/**
* This is just another approach to open the shapefile and pass the Charset
* Now we open the shape with UTF8 charset and expect the attribute to be correctly retuned with
* 4 chars and including the german special character.
*/
@Test
public void testLoadingAndReadingUTF8Correctly() throws Exception {
SimpleFeatureCollection features = loadFeatures(UTF8, Charset
.forName("UTF8"), null);
SimpleFeatureIterator iterator = features.features();
assertTrue(iterator.hasNext());
assertEquals(4, features.size());
SimpleFeature f = iterator.next();
iterator.close();
// GEOM, NAME,C,100 VISUAL,C,3 NUM1,N,5 NUM2,N,5
assertEquals(5, f.getAttributeCount());
String nameAttribute = (String) f.getAttribute("NAME");
// We expect that the UTF8 is not understood here
assertEquals("Iconfee Stra\u00dfe".length(), nameAttribute.length());
assertEquals("Iconfee Stra\u00dfe", nameAttribute);
}
@Test
public void testNamespace() throws Exception {
ShapefileDataStoreFactory factory = new ShapefileDataStoreFactory();
Map map = new HashMap();
URI namespace = new URI("http://jesse.com");
map.put(ShapefileDataStoreFactory.NAMESPACEP.key, namespace);
map.put(ShapefileDataStoreFactory.URLP.key, TestData.url(STATE_POP));
ShapefileDataStore store = (ShapefileDataStore) factory.createDataStore(map);
FeatureType schema = store.getSchema();
assertEquals(namespace.toString(), schema.getName().getNamespaceURI());
store.dispose();
}
@Test
public void testSchema() throws Exception {
URL url = TestData.url(STATE_POP);
ShapefileDataStore shapeDataStore = new ShapefileDataStore(url);
String typeName = shapeDataStore.getTypeNames()[0];
SimpleFeatureType schema = shapeDataStore.getSchema(typeName);
List<AttributeDescriptor> attributes = schema.getAttributeDescriptors();
assertEquals("Number of Attributes", 253, attributes.size());
shapeDataStore.dispose();
}
@Test
public void testSpacesInPath() throws Exception {
URL u = TestData.url(TestCaseSupport.class, "folder with spaces/pointtest.shp");
File f = DataUtilities.urlToFile(u);
assertTrue(f.exists());
ShapefileDataStore s = new ShapefileDataStore(u);
loadFeatures(s);
s.dispose();
}
/**
* Test envelope versus old DataSource
*/
@Test
public void testEnvelope() throws Exception {
SimpleFeatureCollection features = loadFeatures(STATE_POP, Query.ALL);
ShapefileDataStore s = new ShapefileDataStore(TestData.url(STATE_POP));
String typeName = s.getTypeNames()[0];
SimpleFeatureCollection all = s.getFeatureSource(typeName).getFeatures();
assertEquals(features.getBounds(), all.getBounds());
s.dispose();
}
@Test
public void testCreateAndReadQIX() throws Exception {
File shpFile = copyShapefiles(STATE_POP);
URL url = shpFile.toURI().toURL();
String name = shpFile.getName();
File file = new File(shpFile.getParent(), name.substring(0, name.lastIndexOf('.')) + ".qix");
if (file.exists()) {
file.delete();
}
file.deleteOnExit();
ShapefileDataStore ds = new ShapefileDataStore(url);
SimpleFeatureCollection features = ds.getFeatureSource().getFeatures();
SimpleFeatureIterator indexIter = features.features();
GeometryFactory factory = new GeometryFactory();
double area = Double.MAX_VALUE;
SimpleFeature smallestFeature = null;
while (indexIter.hasNext()) {
SimpleFeature newFeature = indexIter.next();
BoundingBox bounds = newFeature.getBounds();
Geometry geometry = factory.toGeometry(new ReferencedEnvelope(
bounds));
double newArea = geometry.getArea();
if (smallestFeature == null || newArea < area) {
smallestFeature = newFeature;
area = newArea;
}
}
indexIter.close();
ShapefileDataStore ds2 = new ShapefileDataStore(url);
ds2.setIndexed(false);
// reduce the bounds, thus making the store use the spatial index
Envelope newBounds = ds.getFeatureSource().getBounds(Query.ALL);
double dx = newBounds.getWidth() / 4;
double dy = newBounds.getHeight() / 4;
newBounds = new Envelope(newBounds.getMinX() + dx, newBounds.getMaxX()
- dx, newBounds.getMinY() + dy, newBounds.getMaxY() - dy);
CoordinateReferenceSystem crs = features.getSchema().getCoordinateReferenceSystem();
performQueryComparison(ds, ds2, new ReferencedEnvelope(newBounds, crs));
performQueryComparison(ds, ds2, new ReferencedEnvelope(smallestFeature
.getBounds()));
assertTrue(file.exists());
ds.dispose();
ds2.dispose();
}
@Test
public void testSelectionQuery() throws Exception {
File shpFile = copyShapefiles(STATE_POP);
URL url = shpFile.toURI().toURL();
ShapefileDataStore ds = new ShapefileDataStore(url);
ds.setIndexed(false);
SimpleFeatureSource featureSource = ds.getFeatureSource();
SimpleFeatureType schema = featureSource.getSchema();
Query query = new Query( schema.getTypeName() );
query.setPropertyNames(new String[0]);
SimpleFeatureCollection features = featureSource.getFeatures( query );
assertNotNull( "selection query worked", features );
assertFalse( "selection non empty", features.isEmpty() );
assertTrue( "selection non empty", features.size() > 0 );
ReferencedEnvelope bounds = features.getBounds();
final Set<FeatureId> selection = new LinkedHashSet<FeatureId>();
features.accepts( new FeatureVisitor() {
public void visit(Feature feature) {
selection.add( feature.getIdentifier() );
}
}, null );
assertFalse( selection.isEmpty() );
// try with filter and no attributes
FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2(null);
String geomName = schema.getGeometryDescriptor().getName().getLocalPart();
query.setFilter( ff.bbox( ff.property(geomName), bounds) );
features = featureSource.getFeatures( query );
assertNotNull("selection query worked", features );
assertTrue( "selection non empty", !features.isEmpty() );
assertEquals(selection.size(), features.size());
ds.dispose();
}
@Test
public void testQueryBboxNonGeomAttributes() throws Exception {
File shpFile = copyShapefiles(STATE_POP);
URL url = shpFile.toURI().toURL();
ShapefileDataStore ds = new ShapefileDataStore(url);
SimpleFeatureSource fs = ds.getFeatureSource();
// build a query that extracts no geom but uses a bbox filter
FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2(null);
Query q = new Query();
q.setPropertyNames(new String[] {"STATE_NAME", "PERSONS"});
ReferencedEnvelope queryBounds = new ReferencedEnvelope(-75.102613, -72.361859, 40.212597,
41.512517, null);
q.setFilter(ff.bbox(ff.property(""), queryBounds));
// grab the features
SimpleFeatureCollection fc = fs.getFeatures(q);
assertTrue(fc.size() > 0);
ds.dispose();
}
@Test
public void testFidFilter() throws Exception {
File shpFile = copyShapefiles(STATE_POP);
URL url = shpFile.toURI().toURL();
ShapefileDataStore ds = new ShapefileDataStore(url);
SimpleFeatureSource featureSource = ds.getFeatureSource();
SimpleFeatureCollection features = featureSource.getFeatures();
SimpleFeatureIterator indexIter = features.features();
Set<String> expectedFids = new LinkedHashSet<String>();
final Filter fidFilter;
try {
FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2(null);
Set<FeatureId> fids = new HashSet<FeatureId>();
while (indexIter.hasNext()) {
SimpleFeature newFeature = indexIter.next();
String id = newFeature.getID();
expectedFids.add(id);
fids.add(ff.featureId(id));
}
fidFilter = ff.id(fids);
} finally {
indexIter.close();
}
Set<String> actualFids = new HashSet<String>();
{
features = featureSource.getFeatures(fidFilter);
try {
indexIter = features.features();
while (indexIter.hasNext()) {
SimpleFeature next = indexIter.next();
String id = next.getID();
actualFids.add(id);
}
} finally {
if(indexIter != null) {
indexIter.close();
}
}
}
TreeSet<String> lackingFids = new TreeSet<String>(expectedFids);
lackingFids.removeAll(actualFids);
TreeSet<String> unexpectedFids = new TreeSet<String>(actualFids);
unexpectedFids.removeAll(expectedFids);
String lacking = String.valueOf(lackingFids);
String unexpected = String.valueOf(unexpectedFids);
String failureMsg = "lacking fids: " + lacking + ". Unexpected ones: " + unexpected;
assertEquals(failureMsg, expectedFids.size(), actualFids.size());
assertEquals(failureMsg, expectedFids, actualFids);
ds.dispose();
}
private ArrayList performQueryComparison(
ShapefileDataStore indexedDS,
ShapefileDataStore baselineDS, ReferencedEnvelope newBounds)
throws FactoryRegistryException, IllegalFilterException,
IOException {
SimpleFeatureCollection features;
SimpleFeatureIterator indexIter;
FilterFactory2 fac = CommonFactoryFinder.getFilterFactory2(null);
String geometryName = indexedDS.getSchema().getGeometryDescriptor()
.getLocalName();
Filter filter = fac.bbox(fac.property(geometryName), newBounds);
features = indexedDS.getFeatureSource().getFeatures(filter);
SimpleFeatureCollection features2 = baselineDS.getFeatureSource()
.getFeatures(filter);
SimpleFeatureIterator baselineIter = features2.features();
indexIter = features.features();
ArrayList baselineFeatures = new ArrayList();
ArrayList indexedFeatures = new ArrayList();
try {
while (baselineIter.hasNext()) {
baselineFeatures.add(baselineIter.next());
}
while (indexIter.hasNext()) {
indexedFeatures.add(indexIter.next());
}
assertFalse(indexIter.hasNext());
assertFalse(baselineIter.hasNext());
assertEquals(baselineFeatures.size(), indexedFeatures.size());
for (Iterator it = baselineFeatures.iterator(); it.hasNext();) {
SimpleFeature f = (SimpleFeature) it.next();
assertTrue(f.getID() + ((Geometry) f.getDefaultGeometry()).getEnvelopeInternal(), indexedFeatures.contains(f));
}
} finally {
indexIter.close();
baselineIter.close();
}
return indexedFeatures;
}
@Test
public void testLoadAndVerify() throws Exception {
SimpleFeatureCollection features = loadFeatures(STATE_POP, Query.ALL);
// SimpleFeatureCollection features = loadFeaturesM2();
int count = features.size();
assertTrue("Have features", count > 0);
// assertEquals("Number of Features loaded",49,features.size()); // FILE
// (correct value)
// assertEquals("Number of Features loaded",3, count); // JAR
SimpleFeature firstFeature = firstFeature(features);
SimpleFeatureType schema = firstFeature.getFeatureType();
assertNotNull(schema.getGeometryDescriptor());
assertEquals("Number of Attributes", 253, schema.getAttributeCount());
assertEquals("Value of statename is wrong", "Illinois", firstFeature
.getAttribute("STATE_NAME"));
assertEquals("Value of land area is wrong", 143986.61,
((Double) firstFeature.getAttribute("LAND_KM")).doubleValue(),
0.001);
}
@Test
public void testLoadAndCheckParentTypeIsPolygon() throws Exception {
SimpleFeatureCollection features = loadFeatures(STATE_POP, Query.ALL);
SimpleFeatureType schema = firstFeature(features).getFeatureType();
assertTrue(FeatureTypes.isDecendedFrom(schema,
BasicFeatureTypes.POLYGON));
assertTrue(FeatureTypes.isDecendedFrom(schema,
BasicFeatureTypes.POLYGON));
assertTrue(FeatureTypes.isDecendedFrom(schema,
FeatureTypes.DEFAULT_NAMESPACE, "polygonFeature"));
}
@Test
public void testCreateSchemaWithEmptyCRS() throws Exception {
File file = new File("test.shp");
URL toURL = file.toURI().toURL();
ShapefileDataStore ds = new ShapefileDataStore(toURL);
ds.createSchema(DataUtilities.createType("test", "geom:MultiPolygon"));
assertEquals("test", ds.getSchema().getTypeName());
ds.dispose();
file.deleteOnExit();
file = new File("test.dbf");
file.deleteOnExit();
file = new File("test.shp");
file.deleteOnExit();
file = new File("test.prj");
if (file.exists())
file.deleteOnExit();
file = new File("test.shx");
if (file.exists()){
file.deleteOnExit();
}
}
@Test
public void testCreateSchemaWithCRS() throws Exception {
File file = new File("test.shp");
URL toURL = file.toURI().toURL();
ShapefileDataStore ds = new ShapefileDataStore(toURL);
SimpleFeatureType featureType = DataUtilities.createType("test", "geom:MultiPolygon:srid=32615");
CoordinateReferenceSystem crs = featureType.getGeometryDescriptor().getCoordinateReferenceSystem();
assertNotNull( crs );
ds.createSchema(featureType);
assertEquals("test", ds.getSchema().getTypeName());
CoordinateReferenceSystem crs2 = ds.getSchema().getGeometryDescriptor().getCoordinateReferenceSystem();
assertNotNull( crs2 );
assertTrue(CRS.equalsIgnoreMetadata(crs, crs2));
ds.dispose();
file.deleteOnExit();
file = new File("test.dbf");
file.deleteOnExit();
file = new File("test.shp");
file.deleteOnExit();
file = new File("test.prj");
if (file.exists())
file.deleteOnExit();
file = new File("test.shx");
if (file.exists()){
file.deleteOnExit();
}
file = new File("test.prj");
if( file.exists()){
file.deleteOnExit();
}
}
@Test
public void testForceCRS() throws Exception {
File file = new File("test.shp");
URL toURL = file.toURI().toURL();
ShapefileDataStore ds = new ShapefileDataStore(toURL);
ds.createSchema(DataUtilities.createType("test", "geom:MultiPolygon"));
FeatureType before = ds.getSchema();
ds.forceSchemaCRS(CRS.decode("EPSG:3005"));
FeatureType after = ds.getSchema();
ds.dispose();
assertNotSame(before, after);
assertNull("4326", before.getCoordinateReferenceSystem());
assertEquals("NAD83 / BC Albers", after.getCoordinateReferenceSystem().getName().getCode());
file.deleteOnExit();
file = new File("test.dbf");
file.deleteOnExit();
file = new File("test.shp");
file.deleteOnExit();
file = new File("test.prj");
if (file.exists())
file.deleteOnExit();
file = new File("test.shx");
if (file.exists())
file.deleteOnExit();
}
private ShapefileDataStore createDataStore(File f) throws Exception {
SimpleFeatureCollection fc = createFeatureCollection();
ShapefileDataStore sds = new ShapefileDataStore(f.toURI().toURL());
writeFeatures(sds, fc);
return sds;
}
private ShapefileDataStore createDataStore() throws Exception {
return createDataStore(getTempFile());
}
/**
* Create a set of features, then remove every other one, updating the
* remaining. Test for removal and proper update after reloading...
*/
@Test
public void testUpdating() throws Throwable {
ShapefileDataStore sds = createDataStore();
loadFeatures(sds);
FeatureWriter<SimpleFeatureType, SimpleFeature> writer = null;
try {
writer = sds.getFeatureWriter(sds.getTypeNames()[0],
Filter.INCLUDE, Transaction.AUTO_COMMIT);
while (writer.hasNext()) {
SimpleFeature feat = writer.next();
Integer b = (Integer) feat.getAttribute(1);
if (b.byteValue() % 2 == 0) {
writer.remove();
} else {
feat.setAttribute(1, new Byte((byte) -1));
}
}
} finally {
if (writer != null)
writer.close();
}
SimpleFeatureCollection fc = loadFeatures(sds);
assertEquals(10, fc.size());
SimpleFeatureIterator features = null;
try {
features = fc.features();
for (SimpleFeatureIterator i = features; i.hasNext();) {
assertEquals(-1, ((Integer) i.next().getAttribute(1)).byteValue());
}
} finally {
if(features != null) {
features.close();
}
}
sds.dispose();
}
/**
* Create a test file, then continue removing the first entry until there
* are no features left.
*/
@Test
public void testRemoveFromFrontAndClose() throws Throwable {
ShapefileDataStore sds = createDataStore();
int idx = loadFeatures(sds).size();
while (idx > 0) {
FeatureWriter<SimpleFeatureType, SimpleFeature> writer = null;
try {
writer = sds.getFeatureWriter(sds.getTypeNames()[0],
Filter.INCLUDE, Transaction.AUTO_COMMIT);
SimpleFeature feature = writer.next();
System.out.println(feature);
writer.remove();
} finally {
if (writer != null) {
writer.close();
writer = null;
}
}
assertEquals(--idx, loadFeatures(sds).size());
}
sds.dispose();
}
/**
* Create a test file, then continue removing the first entry until there
* are no features left.
*/
@Test
public void testRemoveFromFrontAndCloseTransaction() throws Throwable {
ShapefileDataStore sds = createDataStore();
int idx = loadFeatures(sds).size();
while (idx > 0) {
Transaction t = new DefaultTransaction();
FeatureWriter<SimpleFeatureType, SimpleFeature> writer = null;
try {
writer = sds.getFeatureWriter(sds.getTypeNames()[0],
Filter.INCLUDE, t);
writer.next();
writer.remove();
} finally {
if (writer != null) {
writer.close();
writer = null;
}
}
t.commit();
t.close();
assertEquals(--idx, loadFeatures(sds).size());
}
sds.dispose();
}
/**
* Create a test file, then continue removing the last entry until there are
* no features left.
*/
@Test
public void testRemoveFromBackAndClose() throws Throwable {
ShapefileDataStore sds = createDataStore();
int idx = loadFeatures(sds).size();
while (idx > 0) {
FeatureWriter<SimpleFeatureType, SimpleFeature> writer = null;
try {
writer = sds.getFeatureWriter(sds.getTypeNames()[0],
Filter.INCLUDE, Transaction.AUTO_COMMIT);
while (writer.hasNext()) {
writer.next();
}
writer.remove();
} finally {
if (writer != null) {
writer.close();
writer = null;
}
}
assertEquals(--idx, loadFeatures(sds).size());
}
sds.dispose();
}
@Test
public void testWriteShapefileWithNoRecords() throws Exception {
SimpleFeatureType featureType = DataUtilities.createType("whatever",
"a:Polygon,b:String");
File tempFile = getTempFile();
ShapefileDataStore shapefileDataStore = new ShapefileDataStore(tempFile
.toURI().toURL());
shapefileDataStore.createSchema(featureType);
FeatureWriter<SimpleFeatureType, SimpleFeature> featureWriter = shapefileDataStore.getFeatureWriter(
shapefileDataStore.getTypeNames()[0], Transaction.AUTO_COMMIT);
// don't add any features to the data store....
// this should create a shapefile with no records. Not sure about the
// semantics of this,
// but it's meant to be used in the context of a FeatureCollection
// iteration,
// where the SimpleFeatureCollection has nothing in it.
featureWriter.close();
shapefileDataStore.dispose();
}
@Test
public void testTransaction() throws Exception {
ShapefileDataStore sds = createDataStore();
int idx = sds.getFeatureSource().getCount(Query.ALL);
SimpleFeatureStore store = (SimpleFeatureStore) sds.getFeatureSource(sds
.getTypeNames()[0]);
Transaction transaction = new DefaultTransaction();
store.setTransaction(transaction);
SimpleFeature[] newFeatures1 = new SimpleFeature[1];
SimpleFeature[] newFeatures2 = new SimpleFeature[2];
GeometryFactory fac = new GeometryFactory();
newFeatures1[0] = DataUtilities.template(sds.getSchema());
newFeatures1[0].setDefaultGeometry(fac
.createPoint(new Coordinate(0, 0)));
newFeatures2[0] = DataUtilities.template(sds.getSchema());
newFeatures2[0].setDefaultGeometry(fac
.createPoint(new Coordinate(0, 0)));
newFeatures2[1] = DataUtilities.template(sds.getSchema());
newFeatures2[1].setDefaultGeometry(fac
.createPoint(new Coordinate(0, 0)));
store.addFeatures(DataUtilities.collection(newFeatures1));
store.addFeatures(DataUtilities.collection(newFeatures2));
transaction.commit();
transaction.close();
assertEquals(idx + 3, sds.getCount(Query.ALL));
sds.dispose();
}
/**
* Creates feature collection with all the stuff we care about from simple
* types, to Geometry and date.
* <p>
* As we care about supporting more stuff please add on to the end of this
* list...
*
* @return SimpleFeatureCollection For use in testing.
* @throws Exception
*/
private SimpleFeatureCollection createFeatureCollection() throws Exception {
SimpleFeatureType featureType = createExampleSchema();
SimpleFeatureBuilder build = new SimpleFeatureBuilder(featureType);
DefaultFeatureCollection features = new DefaultFeatureCollection();
for (int i = 0, ii = 20; i < ii; i++) {
build.add(new GeometryFactory().createPoint(new Coordinate(1, -1)));
build.add(new Byte((byte) i));
build.add(new Short((short) i));
build.add(new Double(i));
build.add(new Float(i));
build.add(new String(i + " "));
build.add(new Date(i));
build.add(new Boolean(true));
build.add(new Integer(22));
build.add(new Long(1234567890123456789L));
build.add(new BigDecimal(new BigInteger(
"12345678901234567890123456789"), 2));
build.add(new BigInteger("12345678901234567890123456789"));
GregorianCalendar calendar = new GregorianCalendar();
calendar.setTimeInMillis(i);
build.add(calendar);
build.add(UUID.randomUUID());
SimpleFeature feature = build.buildFeature(null);
features.add(feature);
}
return features;
}
private SimpleFeatureType createExampleSchema() {
SimpleFeatureTypeBuilder build = new SimpleFeatureTypeBuilder();
build.setName("junk");
build.add("a", Point.class);
build.add("b", Byte.class);
build.add("c", Short.class);
build.add("d", Double.class);
build.add("e", Float.class);
build.add("f", String.class);
build.add("g", Date.class);
build.add("h", Boolean.class);
build.add("i", Number.class);
build.add("j", Long.class);
build.add("k", BigDecimal.class);
build.add("l", BigInteger.class);
build.add("m", Calendar.class);
build.add("n", UUID.class);
return build.buildFeatureType();
}
@Test
public void testAttributesWriting() throws Exception {
SimpleFeatureCollection features = createFeatureCollection();
File tmpFile = getTempFile();
tmpFile.createNewFile();
ShapefileDataStore s = new ShapefileDataStore(tmpFile.toURI().toURL());
writeFeatures(s, features);
s.dispose();
}
@Test
public void testWriteReadBigNumbers() throws Exception {
// create feature type
SimpleFeatureType type = DataUtilities.createType("junk",
"a:Point,b:java.math.BigDecimal,c:java.math.BigInteger");
DefaultFeatureCollection features = new DefaultFeatureCollection();
BigInteger bigInteger = new BigInteger("1234567890123456789");
BigDecimal bigDecimal = new BigDecimal(bigInteger, 2);
SimpleFeatureBuilder build = new SimpleFeatureBuilder(type);
build.add(new GeometryFactory().createPoint(new Coordinate(1, -1)));
build.add(bigDecimal);
build.add(bigInteger);
SimpleFeature feature = build.buildFeature(null);
features.add(feature);
// store features
File tmpFile = getTempFile();
tmpFile.createNewFile();
ShapefileDataStore s = new ShapefileDataStore(tmpFile.toURI().toURL());
writeFeatures(s, features);
// read them back
FeatureReader<SimpleFeatureType, SimpleFeature> reader = s.getFeatureReader();
try {
SimpleFeature f = reader.next();
assertEquals("big decimal", bigDecimal.doubleValue(), ((Number) f
.getAttribute("b")).doubleValue(), 0.00001);
assertEquals("big integer", bigInteger.longValue(), ((Number) f
.getAttribute("c")).longValue(), 0.00001);
} finally {
reader.close();
}
s.dispose();
}
@Test
public void testGeometriesWriting() throws Exception {
// String[] wktResources = new String[] { "point", "multipoint", "line",
// "multiline", "polygon", "multipolygon" };
String[] wktResources = new String[] { "line",