{"library":"osmium","title":"PyOsmium","description":"PyOsmium provides Python bindings for libosmium, a high-performance C++ library designed for processing OpenStreetMap (OSM) data. It enables efficient reading, writing, and manipulation of various OSM file formats (PBF, XML, O5M) and change files, making it suitable for large-scale geospatial data tasks. The library is actively maintained with regular updates, typically aligning with new libosmium releases, and is currently at version 4.3.1.","language":"python","status":"active","last_verified":"Fri Apr 17","install":{"commands":["pip install osmium"],"cli":null},"imports":["from osmium import SimpleHandler","from osmium import FileProcessor","from osmium.osm import Node","from osmium.osm import Location","from osmium import SimpleWriter"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import osmium\nimport osmium.osm\nimport tempfile\nimport os\nfrom datetime import datetime\n\n# Create a dummy PBF file for demonstration purposes\n# In a real scenario, you'd process an existing .osm.pbf file.\ntemp_pbf_file = os.path.join(tempfile.gettempdir(), \"test.osm.pbf\")\nwriter = osmium.SimpleWriter(temp_pbf_file)\nwriter.add_node(osmium.osm.Node(1, location=osmium.osm.Location(1.0, 1.0), user='test_user', timestamp=datetime.now()))\nwriter.add_node(osmium.osm.Node(2, location=osmium.osm.Location(2.0, 2.0), user='test_user', timestamp=datetime.now()))\nwriter.add_way(osmium.osm.Way(3, nodes=[1, 2], user='test_user', timestamp=datetime.now()))\nwriter.add_relation(osmium.osm.Relation(4, user='test_user', timestamp=datetime.now()))\nwriter.close()\n\nclass ElementCounter(osmium.SimpleHandler):\n    def __init__(self):\n        super().__init__()\n        self.nodes = 0\n        self.ways = 0\n        self.relations = 0\n\n    def node(self, n):\n        self.nodes += 1\n\n    def way(self, w):\n        self.ways += 1\n\n    def relation(self, r):\n        self.relations += 1\n\ntry:\n    # Process the (dummy) PBF file using the handler\n    handler = ElementCounter()\n    handler.apply_file(temp_pbf_file)\n\n    print(f\"Nodes: {handler.nodes}\")\n    print(f\"Ways: {handler.ways}\")\n    print(f\"Relations: {handler.relations}\")\nfinally:\n    # Clean up the temporary file\n    if os.path.exists(temp_pbf_file):\n        os.remove(temp_pbf_file)\n","lang":"python","description":"This example demonstrates how to use `osmium.SimpleHandler` to process an OSM PBF file. It creates a temporary dummy PBF file, then defines a handler to count nodes, ways, and relations, and applies it to the file. This pattern is fundamental for reading and iterating over OSM data.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}