I am importing a osm.pbf file into my java application using Osmosis like this:
data class MapObject (
val lat: Double,
val lon: Double,
val id: Long
)
class OsmReader : Sink {
val data = mutableListOf<MapObject>()
override fun close() {}
override fun complete() {}
override fun initialize(metaData: MutableMap<String, Any>?) {}
override fun process(entityContainer: EntityContainer?) {
if (entityContainer is NodeContainer) {
val node = entityContainer.entity
data.add( MapObject(node.latitude, node.longitude, node.id) )
}
}
}
fun readOSM(pathToPBF: String) {
val inputStream = FileInputStream(pathToPBF)
// read from osm pbf file:
val custom = OsmReader()
val reader = OsmosisReader(inputStream)
reader.setSink(custom)
// initial parsing of the .pbf file:
reader.run()
println("Break")
}
I would like to only read the nodes in a given bounding box. Then using Osmosis from the command line you can use --bounding-box, but how would you do the same thing in code?
Currently I intersect each node with the bounding box using the JTS, however that is rather slow.
I found out the answer now:
Here is an example of how you would get all the shops in a specific bouding box and store them in lists for the nodes, ways, and relations in lists.
I use kotllin with gradle.
In the gradle.kts:
The code:
If you want the geometries of ways and relations you will also need to store the nodes they consist of. These are not tagged with "shop". Storing them in lists is quite RAM intensive so it is best to use the org.openstreetmap.osmosis.core.store.SimpleObjectStore class.