pyrosm.OSM.write_pbf

Contents

pyrosm.OSM.write_pbf#

OSM.write_pbf(data, output_path, subset_only=False)#

Write the OSM data this object holds back to a valid, re-readable *.osm.pbf, applying attribute/tag edits from a (modified) GeoDataFrame.

The whole cached dataset (all nodes/ways/relations read from the source) is written. Each row of data updates the tags of the matching element (by osm_type + id); rows whose id is not in the source are added as new elements synthesized from their geometry (with negative ids). Topology and coordinates come from the data pyrosm read, so the output is faithful and re-readable (e.g. by pyrosm, osmium, GDAL and r5py/R5).

Pass subset_only=True to instead write a PBF containing only the elements in data (matched by osm_type + id) plus the references they need to stay valid – e.g. export just the buildings or just the road network to a new file. Multiple layers can be combined by passing a list ([buildings, network]); the union of their elements is written.

Typical use is to modify attributes in pandas and save them back:

osm = OSM("data.osm.pbf")
edges = osm.get_network("driving")
edges["maxspeed"] = edges["maxspeed"].fillna(50)
edges["travel_time"] = edges["length"] / (edges["maxspeed"] / 3.6)
osm.write_pbf(edges, "modified.osm.pbf")
Parameters:
  • data (GeoDataFrame or list of GeoDataFrame) – The (possibly modified) feature frame(s) whose tag columns are written onto the matching elements. New rows (ids not in the source) are added from their geometry: Point -> node, LineString -> way, hole-less Polygon -> closed way. Polygons with holes, MultiPolygon and MultiLineString geometries are not supported and raise ValueError.

  • output_path (str) – Where to write the PBF.

  • subset_only (bool (default False)) – If True, write only the elements present in data (and the nodes and relation members they reference) instead of the whole cached dataset.

Returns:

The path of the written PBF file.

Return type:

str

Notes

v1 applies edits and additions, not deletions: with the default subset_only=False the whole cached dataset is the base set, so dropping rows from data does not remove elements. Use subset_only=True to limit the output to the elements in data.