3
3
import java .io .File ;
4
4
import java .util .Arrays ;
5
5
import java .util .Date ;
6
- import java .util .Formatter ;
6
+ import java .util .HashMap ;
7
+ import java .util .HashSet ;
7
8
import java .util .List ;
9
+ import java .util .Map ;
10
+ import java .util .Set ;
8
11
9
- import org .geotools .data .shapefile .ShapefileDataStore ;
12
+ import org .geotools .data .DataStore ;
13
+ import org .geotools .data .DataStoreFinder ;
10
14
import org .geotools .data .simple .SimpleFeatureIterator ;
11
15
import org .geotools .data .simple .SimpleFeatureSource ;
16
+ import org .junit .Assert ;
12
17
import org .junit .Test ;
13
18
import org .opengis .feature .simple .SimpleFeature ;
14
19
import org .opengis .feature .simple .SimpleFeatureType ;
@@ -27,7 +32,9 @@ public class ShapefileIndexerTest {
27
32
public void testBasicIndexing () throws Exception {
28
33
29
34
File shapefile = new File ("data/build/flickr-shapes/neighbourhoods.shp" );
30
- ShapefileDataStore store = new ShapefileDataStore (shapefile .toURI ().toURL ());
35
+ Map <String , Object > map = new HashMap <String , Object >();
36
+ map .put ("url" , shapefile .toURI ().toURL ());
37
+ DataStore store = DataStoreFinder .getDataStore (map );
31
38
32
39
String name = store .getTypeNames ()[0 ];
33
40
SimpleFeatureSource source = store .getFeatureSource (name );
@@ -36,54 +43,42 @@ public void testBasicIndexing() throws Exception {
36
43
STRtree index = new STRtree ();
37
44
38
45
long start = new Date ().getTime ();
39
- while (features .hasNext ()) {
46
+ try {
47
+ while (features .hasNext ()) {
40
48
41
- SimpleFeature feature = features .next ();
42
- MultiPolygon geom = (MultiPolygon ) feature .getDefaultGeometry ();
43
- Envelope envelope = geom .getEnvelopeInternal ();
49
+ SimpleFeature feature = features .next ();
50
+ MultiPolygon geom = (MultiPolygon ) feature .getDefaultGeometry ();
51
+ Envelope envelope = geom .getEnvelopeInternal ();
44
52
45
- index .insert (envelope , feature );
53
+ index .insert (envelope , feature );
54
+ }
55
+ } finally {
56
+ features .close ();
57
+ store .dispose ();
46
58
}
47
59
48
60
System .out .printf ("Index built in: %.2f seconds\n " , (new Date ().getTime () - start ) / (float ) 1000 );
49
61
50
62
GeometryFactory geometryFactory = new GeometryFactory ();
51
- new File ("target/test/output/flickr-shapes" ).mkdirs ();
52
- File output = new File ("target/test/output/flickr-shapes/neighbourhoods.txt" );
53
- Formatter formatter = new Formatter (output );
54
-
55
- start = new Date ().getTime ();
56
- for (float lon = -180F ; lon <= 180F ; lon += 0.01F ) {
57
- for (float lat = -90F ; lat <= 90F ; lat += 0.01F ) {
58
-
59
- Coordinate coordinate = new Coordinate (lon , lat );
60
- Envelope searchEnv = new Envelope (coordinate );
61
63
62
- @ SuppressWarnings ("unchecked" )
63
- List <Object > results = index .query (searchEnv );
64
+ Assert .assertTrue (getWOEID (0 , 0 , index , geometryFactory ).isEmpty ());
64
65
65
- for (Object item : results ) {
66
+ Set <Long > woeids = getWOEID (52.5135 , 13.3535 , index , geometryFactory );
67
+ Assert .assertEquals (2 , woeids .size ());
68
+ Assert .assertTrue (woeids .contains (675695L ));
69
+ Assert .assertTrue (woeids .contains (29352065L ));
66
70
67
- SimpleFeature feature = (SimpleFeature ) item ;
68
- MultiPolygon geom = (MultiPolygon ) feature .getDefaultGeometry ();
69
-
70
- if (geom .contains (geometryFactory .createPoint (coordinate ))) {
71
- formatter .format ("%.2f,%.2f\t %s\n " , lat , lon , feature .getAttribute ("woe_id" ));
72
- }
73
- }
74
- }
75
- }
76
-
77
- formatter .close ();
78
-
79
- System .out .printf ("Full index query in: %d seconds\n " , (new Date ().getTime () - start ) / 1000 );
71
+ store .dispose ();
80
72
}
81
73
82
74
@ Test
83
75
public void testPrintShapefile () throws Exception {
84
76
85
77
File shapefile = new File ("data/build/flickr-shapes/counties.shp" );
86
- ShapefileDataStore store = new ShapefileDataStore (shapefile .toURI ().toURL ());
78
+
79
+ Map <String , Object > map = new HashMap <String , Object >();
80
+ map .put ("url" , shapefile .toURI ().toURL ());
81
+ DataStore store = DataStoreFinder .getDataStore (map );
87
82
88
83
String name = store .getTypeNames ()[0 ];
89
84
System .out .println ("TypeNames: " + Arrays .toString (store .getTypeNames ()));
@@ -101,38 +96,66 @@ public void testPrintShapefile() throws Exception {
101
96
102
97
System .out .println ();
103
98
104
- // now print out the feature contents (every non geometric attribute)
105
99
SimpleFeatureIterator features = source .getFeatures ().features ();
100
+ SimpleFeatureIterator features2 = source .getFeatures ().features ();
106
101
107
- int i = 0 ;
108
- while (features .hasNext () && i < 10 ) {
109
- SimpleFeature feature = features .next ();
102
+ try {
103
+ // now print out the feature contents (every non geometric attribute)
104
+ int i = 0 ;
105
+ while (features .hasNext () && i < 10 ) {
106
+ SimpleFeature feature = features .next ();
110
107
111
- System .out .print (feature .getID () + "\t " );
108
+ System .out .print (feature .getID () + "\t " );
112
109
113
- for (Object attribute : feature .getAttributes ()) {
110
+ for (Object attribute : feature .getAttributes ()) {
114
111
115
- if (!(attribute instanceof Geometry )) {
116
- System .out .print (attribute + "\t " );
112
+ if (!(attribute instanceof Geometry )) {
113
+ System .out .print (attribute + "\t " );
114
+ }
117
115
}
116
+
117
+ System .out .println ();
118
+ i ++;
118
119
}
119
120
120
- System .out .println ();
121
- i ++;
121
+ // and finally print out every geometry in wkt format
122
+ i = 0 ;
123
+ while (features .hasNext () && i < 10 ) {
124
+
125
+ SimpleFeature feature = features .next ();
126
+
127
+ System .out .print (feature .getID () + "\t " );
128
+ System .out .println (feature .getDefaultGeometry ());
129
+ i ++;
130
+ }
131
+
132
+ } finally {
133
+ features .close ();
134
+ features2 .close ();
135
+ store .dispose ();
122
136
}
137
+ }
123
138
124
- // and finally print out every geometry in wkt format
125
- features = source .getFeatures ().features ();
126
- i = 0 ;
127
- while (features .hasNext () && i < 10 ) {
139
+ private Set <Long > getWOEID (final double lat , final double lon , final STRtree index ,
140
+ final GeometryFactory geometryFactory ) {
128
141
129
- SimpleFeature feature = features .next ();
142
+ Coordinate coordinate = new Coordinate (lon , lat );
143
+ Envelope searchEnv = new Envelope (coordinate );
130
144
131
- System .out .print (feature .getID () + "\t " );
132
- System .out .println (feature .getDefaultGeometry ());
133
- i ++;
145
+ @ SuppressWarnings ("unchecked" )
146
+ List <Object > results = index .query (searchEnv );
147
+ Set <Long > rtn = new HashSet <Long >(results .size ());
148
+
149
+ for (Object item : results ) {
150
+
151
+ SimpleFeature feature = (SimpleFeature ) item ;
152
+ MultiPolygon geom = (MultiPolygon ) feature .getDefaultGeometry ();
153
+
154
+ if (geom .contains (geometryFactory .createPoint (coordinate ))) {
155
+ rtn .add ((Long ) feature .getAttribute ("woe_id" ));
156
+ }
134
157
}
135
158
136
- store . dispose () ;
159
+ return rtn ;
137
160
}
138
161
}
0 commit comments