Skip to content

Commit 17a0ebc

Browse files
author
Josh Devins
committed
Better handling of shapefiles
1 parent 2124979 commit 17a0ebc

File tree

1 file changed

+77
-54
lines changed

1 file changed

+77
-54
lines changed

src/test/java/net/joshdevins/gis/geocoder/ShapefileIndexerTest.java

+77-54
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@
33
import java.io.File;
44
import java.util.Arrays;
55
import java.util.Date;
6-
import java.util.Formatter;
6+
import java.util.HashMap;
7+
import java.util.HashSet;
78
import java.util.List;
9+
import java.util.Map;
10+
import java.util.Set;
811

9-
import org.geotools.data.shapefile.ShapefileDataStore;
12+
import org.geotools.data.DataStore;
13+
import org.geotools.data.DataStoreFinder;
1014
import org.geotools.data.simple.SimpleFeatureIterator;
1115
import org.geotools.data.simple.SimpleFeatureSource;
16+
import org.junit.Assert;
1217
import org.junit.Test;
1318
import org.opengis.feature.simple.SimpleFeature;
1419
import org.opengis.feature.simple.SimpleFeatureType;
@@ -27,7 +32,9 @@ public class ShapefileIndexerTest {
2732
public void testBasicIndexing() throws Exception {
2833

2934
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);
3138

3239
String name = store.getTypeNames()[0];
3340
SimpleFeatureSource source = store.getFeatureSource(name);
@@ -36,54 +43,42 @@ public void testBasicIndexing() throws Exception {
3643
STRtree index = new STRtree();
3744

3845
long start = new Date().getTime();
39-
while (features.hasNext()) {
46+
try {
47+
while (features.hasNext()) {
4048

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();
4452

45-
index.insert(envelope, feature);
53+
index.insert(envelope, feature);
54+
}
55+
} finally {
56+
features.close();
57+
store.dispose();
4658
}
4759

4860
System.out.printf("Index built in: %.2f seconds\n", (new Date().getTime() - start) / (float) 1000);
4961

5062
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);
6163

62-
@SuppressWarnings("unchecked")
63-
List<Object> results = index.query(searchEnv);
64+
Assert.assertTrue(getWOEID(0, 0, index, geometryFactory).isEmpty());
6465

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));
6670

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();
8072
}
8173

8274
@Test
8375
public void testPrintShapefile() throws Exception {
8476

8577
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);
8782

8883
String name = store.getTypeNames()[0];
8984
System.out.println("TypeNames: " + Arrays.toString(store.getTypeNames()));
@@ -101,38 +96,66 @@ public void testPrintShapefile() throws Exception {
10196

10297
System.out.println();
10398

104-
// now print out the feature contents (every non geometric attribute)
10599
SimpleFeatureIterator features = source.getFeatures().features();
100+
SimpleFeatureIterator features2 = source.getFeatures().features();
106101

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();
110107

111-
System.out.print(feature.getID() + "\t");
108+
System.out.print(feature.getID() + "\t");
112109

113-
for (Object attribute : feature.getAttributes()) {
110+
for (Object attribute : feature.getAttributes()) {
114111

115-
if (!(attribute instanceof Geometry)) {
116-
System.out.print(attribute + "\t");
112+
if (!(attribute instanceof Geometry)) {
113+
System.out.print(attribute + "\t");
114+
}
117115
}
116+
117+
System.out.println();
118+
i++;
118119
}
119120

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();
122136
}
137+
}
123138

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) {
128141

129-
SimpleFeature feature = features.next();
142+
Coordinate coordinate = new Coordinate(lon, lat);
143+
Envelope searchEnv = new Envelope(coordinate);
130144

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+
}
134157
}
135158

136-
store.dispose();
159+
return rtn;
137160
}
138161
}

0 commit comments

Comments
 (0)