Skip to content

Commit

Permalink
Merge 2544e4d into 89de822
Browse files Browse the repository at this point in the history
  • Loading branch information
msbarry authored Jan 2, 2023
2 parents 89de822 + 2544e4d commit 0cd8280
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ public class ShapefileReader extends SimpleReader<SimpleFeature> {
private final FeatureCollection<SimpleFeatureType, org.opengis.feature.simple.SimpleFeature> inputSource;
private final String[] attributeNames;
private final ShapefileDataStore dataStore;
private final String layer;
private MathTransform transformToLatLon;

public ShapefileReader(String sourceProjection, String sourceName, Path input) {
super(sourceName);
this.layer = input.getFileName().toString().replaceAll("\\.shp$", "");
dataStore = open(input);
try {
String typeName = dataStore.getTypeNames()[0];
Expand Down Expand Up @@ -119,7 +121,7 @@ public void readFeatures(Consumer<SimpleFeature> next) throws TransformException
}
if (latLonGeometry != null) {
SimpleFeature geom = SimpleFeature.create(latLonGeometry, new HashMap<>(attributeNames.length),
sourceName, null, ++id);
sourceName, layer, ++id);
for (int i = 1; i < attributeNames.length; i++) {
geom.setTag(attributeNames[i], feature.getAttribute(i));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1697,7 +1697,8 @@ void testPlanetilerRunnerShapefile() throws Exception {
public void processFeature(SourceFeature source, FeatureCollector features) {
features.point("stations")
.setZoomRange(0, 14)
.setAttr("source", source.getSource());
.setAttr("source", source.getSource())
.setAttr("layer", source.getSourceLayer());
}
})
// Match *.shp within [shapefile.zip, shapefile-copy.zip]
Expand All @@ -1715,7 +1716,7 @@ public void processFeature(SourceFeature source, FeatureCollector features) {
for (var tile : tileMap.values()) {
for (var feature : tile) {
feature.geometry().validate();

assertEquals("stations", feature.attrs().get("layer"));
switch ((String) feature.attrs().get("source")) {
case "shapefile" -> fileCount++;
case "shapefile-glob" -> globCount++;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.onthegomap.planetiler.reader;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.onthegomap.planetiler.TestUtils;
Expand Down Expand Up @@ -47,29 +46,30 @@ void testReadShapefileUnzipped() throws IOException {
}

private static void testReadShapefile(Path path) {
var reader = new ShapefileReader(null, "test", path);
try (var reader = new ShapefileReader(null, "test", path)) {

for (int i = 1; i <= 2; i++) {
assertEquals(86, reader.getFeatureCount());
List<Geometry> points = new ArrayList<>();
List<String> names = new ArrayList<>();
WorkerPipeline.start("test", Stats.inMemory())
.readFromTiny("files", List.of(path))
.addWorker("reader", 1, (IterableOnce<Path> p, Consumer<SimpleFeature> next) -> reader.readFeatures(next))
.addBuffer("reader_queue", 100, 1)
.sinkToConsumer("counter", 1, elem -> {
assertTrue(elem.getTag("name") instanceof String);
assertEquals("test", elem.getSource());
assertNull(elem.getSourceLayer());
points.add(elem.latLonGeometry());
names.add(elem.getTag("name").toString());
}).await();
assertEquals(86, points.size());
assertTrue(names.contains("Van Dörn Street"));
var gc = GeoUtils.JTS_FACTORY.createGeometryCollection(points.toArray(new Geometry[0]));
var centroid = gc.getCentroid();
assertEquals(-77.0297995, centroid.getX(), 5, "iter " + i);
assertEquals(38.9119684, centroid.getY(), 5, "iter " + i);
for (int i = 1; i <= 2; i++) {
assertEquals(86, reader.getFeatureCount());
List<Geometry> points = new ArrayList<>();
List<String> names = new ArrayList<>();
WorkerPipeline.start("test", Stats.inMemory())
.readFromTiny("files", List.of(path))
.addWorker("reader", 1, (IterableOnce<Path> p, Consumer<SimpleFeature> next) -> reader.readFeatures(next))
.addBuffer("reader_queue", 100, 1)
.sinkToConsumer("counter", 1, elem -> {
assertTrue(elem.getTag("name") instanceof String);
assertEquals("test", elem.getSource());
assertEquals("stations", elem.getSourceLayer());
points.add(elem.latLonGeometry());
names.add(elem.getTag("name").toString());
}).await();
assertEquals(86, points.size());
assertTrue(names.contains("Van Dörn Street"));
var gc = GeoUtils.JTS_FACTORY.createGeometryCollection(points.toArray(new Geometry[0]));
var centroid = gc.getCentroid();
assertEquals(-77.0297995, centroid.getX(), 5, "iter " + i);
assertEquals(38.9119684, centroid.getY(), 5, "iter " + i);
}
}
}
}

0 comments on commit 0cd8280

Please sign in to comment.