-
-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
212 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
planetiler-core/src/main/java/com/onthegomap/planetiler/reader/GeoJsonReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package com.onthegomap.planetiler.reader; | ||
|
||
import com.onthegomap.planetiler.Profile; | ||
import com.onthegomap.planetiler.collection.FeatureGroup; | ||
import com.onthegomap.planetiler.config.PlanetilerConfig; | ||
import com.onthegomap.planetiler.stats.Stats; | ||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.function.Consumer; | ||
import org.geotools.data.geojson.store.GeoJSONDataStore; | ||
import org.geotools.data.simple.SimpleFeatureCollection; | ||
import org.locationtech.jts.geom.Geometry; | ||
|
||
/** | ||
* Utility that reads {@link SourceFeature SourceFeatures} from the vector geometries contained in a GeoJSON file. | ||
*/ | ||
public class GeoJsonReader extends SimpleReader<SimpleFeature> { | ||
|
||
private final GeoJSONDataStore store; | ||
private final String layer; | ||
|
||
GeoJsonReader(String sourceName, Path input) { | ||
super(sourceName); | ||
store = new GeoJSONDataStore(input.toFile()); | ||
layer = input.getFileName().toString().replaceFirst("\\.[^.]+$", ""); // remove file extention. | ||
} | ||
|
||
/** | ||
* Renders map features for all elements from an GeoJSON on the mapping logic defined in {@code | ||
* profile}. | ||
* | ||
* @param sourceName string ID for this reader to use in logs and stats | ||
* @param sourcePaths paths to the {@code .geojson} files on disk | ||
* @param writer consumer for rendered features | ||
* @param config user-defined parameters controlling number of threads and log interval | ||
* @param profile logic that defines what map features to emit for each source feature | ||
* @param stats to keep track of counters and timings | ||
* @throws IllegalArgumentException if a problem occurs reading the input file | ||
*/ | ||
public static void process(String sourceName, List<Path> sourcePaths, FeatureGroup writer, PlanetilerConfig config, | ||
Profile profile, Stats stats) { | ||
SourceFeatureProcessor.processFiles( | ||
sourceName, | ||
sourcePaths, | ||
path -> new GeoJsonReader(sourceName, path), | ||
writer, config, profile, stats | ||
); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
store.dispose(); | ||
} | ||
|
||
@Override | ||
public long getFeatureCount() { | ||
String typeName; | ||
try { | ||
typeName = store.getTypeNames()[0]; | ||
SimpleFeatureCollection features = store.getFeatureSource(typeName).getFeatures(); | ||
return Long.valueOf(features.size()); | ||
} catch (IOException e) { | ||
return 0; | ||
} | ||
} | ||
|
||
@Override | ||
public void readFeatures(Consumer<SimpleFeature> next) throws Exception { | ||
long id = 0; | ||
String typeName = store.getTypeNames()[0]; | ||
SimpleFeatureCollection features = store.getFeatureSource(typeName).getFeatures(); | ||
|
||
try (var iter = features.features()) { | ||
while (iter.hasNext()) { | ||
var feature = iter.next(); | ||
var properties = feature.getProperties(); | ||
SimpleFeature simpleFeature = SimpleFeature.create((Geometry) feature.getDefaultGeometry(), HashMap.newHashMap(properties.size()), | ||
sourceName, layer, id++); | ||
properties.forEach(property -> simpleFeature.setTag(property.getName().toString(), property.getValue())); | ||
next.accept(simpleFeature); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
planetiler-core/src/test/java/com/onthegomap/planetiler/reader/GeoJsonReaderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.onthegomap.planetiler.reader; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import com.onthegomap.planetiler.TestUtils; | ||
import com.onthegomap.planetiler.geo.GeoUtils; | ||
import com.onthegomap.planetiler.stats.Stats; | ||
import com.onthegomap.planetiler.worker.WorkerPipeline; | ||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
import java.util.concurrent.CopyOnWriteArrayList; | ||
import org.junit.jupiter.api.Test; | ||
import org.locationtech.jts.geom.Geometry; | ||
|
||
public class GeoJsonReaderTest { | ||
@Test | ||
void testReadGeoJson() throws IOException { | ||
Path path = TestUtils.pathToResource("geojson.geojson"); | ||
try (var reader = new GeoJsonReader("test", path)) { | ||
assertEquals(3, reader.getFeatureCount()); | ||
List<Geometry> points = new CopyOnWriteArrayList<>(); | ||
List<String> names = new CopyOnWriteArrayList<>(); | ||
WorkerPipeline.start("test", Stats.inMemory()) | ||
.fromGenerator("source", reader::readFeatures) | ||
.addBuffer("reader_queue", 100, 1) | ||
.sinkToConsumer("counter", 1, elem -> { | ||
assertTrue(elem.getTag("name") instanceof String); | ||
assertEquals("test", elem.getSource()); | ||
assertEquals("geojson", elem.getSourceLayer()); | ||
points.add(elem.latLonGeometry()); | ||
names.add(elem.getTag("name").toString()); | ||
}).await(); | ||
assertEquals(3, points.size()); | ||
assertTrue(names.contains("line")); | ||
assertTrue(names.contains("point")); | ||
assertTrue(names.contains("polygon")); | ||
var gc = GeoUtils.JTS_FACTORY.createGeometryCollection(points.toArray(new Geometry[0])); | ||
var centroid = gc.getCentroid(); | ||
assertEquals(100.5, centroid.getX(), 1e-5); | ||
assertEquals(0.5, centroid.getY(), 1e-5); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
{ | ||
"type": "FeatureCollection", | ||
"features": [{ | ||
"type": "Feature", | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [102.0, 0.5] | ||
}, | ||
"properties": { | ||
"name": "point" | ||
} | ||
}, { | ||
"type": "Feature", | ||
"geometry": { | ||
"type": "LineString", | ||
"coordinates": [[102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0]] | ||
}, | ||
"properties": { | ||
"name": "line", | ||
"prop1": 0.0 | ||
} | ||
}, { | ||
"type": "Feature", | ||
"geometry": { | ||
"type": "Polygon", | ||
"coordinates": [ | ||
[ | ||
[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], | ||
[100.0, 1.0], [100.0, 0.0] | ||
] | ||
] | ||
}, | ||
"properties": { | ||
"name": "polygon" | ||
} | ||
}] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,7 +41,8 @@ | |
"enum": [ | ||
"osm", | ||
"shapefile", | ||
"geopackage" | ||
"geopackage", | ||
"geojson" | ||
] | ||
}, | ||
"url": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters