pyrosm.OSM.to_pbf#
- OSM.to_pbf(output_path=None, keep_relations=True, workers=None, compact=False, repack=False)#
Crop the source PBF by this object’s
bounding_boxand write a valid, re-readable*.osm.pbfto disk.Cropping is “complete ways”: a way is kept when at least one of its nodes falls inside the bounding box, and the kept way retains its full node list so geometries are not cut at the box edge. The crop streams the source file blob-by-blob and never loads it fully into memory.
A
Polygon/MultiPolygonbounding_boxcrops to the polygon itself, asOSM()reads by it, so reading the cropped file returns the features that reading the source with the samebounding_boxdoes.The crop shows its progress as this object’s
progresssays: one bar, orprogress(done, 100)calls, from 0 to 100 % over the whole crop.- Parameters:
output_path (str, optional) – Where to write the cropped PBF. When
None(default) a temporary file is created in the system temp directory and its path returned.keep_relations (bool) – When
True(default) relations referencing a kept node or way are written; whenFalseno relations are written. Withcomplete_relations=True(seeOSM), a relation with a way member in the area is written with all its way members and their nodes, so the cropped file reads back with its complete geometry.workers (int | str) – Number of threads for the CPU-heavy per-block work. By default (
None) theworkersof thisOSMobject; when that is not set either, the count is chosen as an out-of-core read does for a file of this size (as"auto");1runs sequentially. Every count produces a byte-identical output. WithPYROSM_DECODE_BACKEND=processesthey are worker processes, one by default; when those cannot run (on macOS and Windows: a script without anif __name__ == "__main__":guard, or one read from stdin), the crop runs in one process and warns.compact (bool) – When
False(default) each output block keeps its source block’s full string table, which is the fastest crop but leaves strings used only by dropped elements in the file. WhenTrueeach output block’s string table is pruned to only the strings its kept elements reference, producing a smaller file at the cost of some extra per-block work. The written OSM data is identical either way.repack (bool) – When
Truethe kept elements are re-chunked into canonical, densely packed blocks (asosmium/Osmosis produce), giving the smallest output at the cost of speed.workersthreads select the elements, read and decode the source blocks of the write and encode the re-packed blocks, which are written in order (the whole write runs in one process withPYROSM_DECODE_BACKEND=processes). Re-packed blocks already have minimal string tables, socompactis ignored whenrepack=True. The written OSM data is identical torepack=False. DefaultFalsekeeps the current (faster, slightly larger) in-place crop.
- Returns:
The path of the written PBF file.
- Return type:
str