pyrosm.OSM.to_pbf#
- OSM.to_pbf(output_path=None, keep_relations=True, workers=1, 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_boxis cropped by its envelope (bounding rectangle), matching howOSM()itself filters by a polygon bounding box, so a cropped-and-reread file matches reading the source with the samebounding_box.- 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.workers (int) – Number of worker processes for the CPU-heavy per-block work.
1(default) runs sequentially;>1uses a multiprocessing pool and produces a byte-identical output.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; the re-pack write is sequential, thoughworkersstill parallelizes the selection. 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