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:
dependencies {
implementation("org.openstreetmap.osmosis:osmosis-pbf:0.48.3")
implementation("org.openstreetmap.osmosis:osmosis-areafilter:0.48.3")
}
The code:
import crosby.binary.osmosis.OsmosisReader
import org.openstreetmap.osmosis.areafilter.v0_6.BoundingBoxFilter
import org.openstreetmap.osmosis.core.container.v0_6.*
import org.openstreetmap.osmosis.core.domain.v0_6.*
import org.openstreetmap.osmosis.core.filter.common.IdTrackerType
import org.openstreetmap.osmosis.core.task.v0_6.Sink
import java.io.FileInputStream
class CustomProcessor : Sink {
// Here the results will be stored
val nodes = mutableListOf<Node>()
val ways = mutableListOf<Way>()
val relations = mutableListOf<Relation>()
override fun close() {
// Close all your readers and such
}
override fun initialize(metaData: MutableMap<String, Any>?) {
// Don't know when to use this
}
override fun complete() {
// If you first put data into storages you can process them here after you have all you need
}
override fun process(entityContainer: EntityContainer?) {
val entity = entityContainer?.entity
// Filter for shops
var foundIt = false
if (entity != null) {
for (tag in entity.tags) {
if (tag.key == "shop") {
foundIt = true
break
}
}
}
// If the object is a shop
if (foundIt) {
if (entity is Node) {
nodes.add(entity)
} else if (entity is Way) {
ways.add(entity)
} else if (entity is Relation) {
relations.add(entity)
} else if (entity is Bound) {
// We don't store bounds
} else {
// Shouldn't be possible
}
}
}
}
fun main(args: Array<String>) {
// Set input stream
val inputStream = FileInputStream("path/to/my/file.osm.pbf")
// Create the bounding box filter
val bbFilter = BoundingBoxFilter(
IdTrackerType.Dynamic, // ID tracker used internally. This is the default.
11.5256, // Left lon
11.5004, // Right lon
48.84543, // Top lat
48.8015, // Bottom lat
true, // Remove ways and relation only partly in bounding box?
false, // Include all nodes of ways only partly in bounding box.
false, // Include all nodes and ways of relations only partly in bounding box.
false // Include all relations that reference relation inside bounding box.
)
// Create your processor
val processor = CustomProcessor()
// Define your pipeline
val reader = OsmosisReader(inputStream)
bbFilter.setSink(processor)
reader.setSink(processor)
// Run
reader.run()
// Get your data
val nodes = processor.nodes
val ways = processor.ways
val relations = processor.relations
}
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.