In our previous post Minimal example how to read .osm.pbf file using Python & osmium we investigated how to read a .osm.pbf
file and count all nodes, ways and relations.
Today, we’ll investigate how to filter for specific nodes by tag using osmium working on .osm.pbf
files. In this example, we’ll filter for power
: tower
and count how many nodes we can find
#!/usr/bin/env python3 import osmium as osm class FindPowerTowerHandler(osm.SimpleHandler): def __init__(self): osm.SimpleHandler.__init__(self) self.count = 0 def node(self, node): if node.tags.get("power") == "tower": self.count += 1 osmhandler = FindPowerTowerHandler() osmhandler.apply_file("germany-latest.osm.pbf") print(f'Number of power=tower nodes: {osmhandler.node_count}')
Also see How to filter OpenStreetmap ways by tag from .osm.pbf file using Python & Osmium