pyrosm.OSM.to_pbf

Contents

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_box and write a valid, re-readable *.osm.pbf to 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/MultiPolygon bounding_box crops to the polygon itself, as OSM() reads by it, so reading the cropped file returns the features that reading the source with the same bounding_box does.

The crop shows its progress as this object’s progress says: one bar, or progress(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; when False no relations are written. With complete_relations=True (see OSM), 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) the workers of this OSM object; 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"); 1 runs sequentially. Every count produces a byte-identical output. With PYROSM_DECODE_BACKEND=processes they are worker processes, one by default; when those cannot run (on macOS and Windows: a script without an if __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. When True each 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 True the kept elements are re-chunked into canonical, densely packed blocks (as osmium/Osmosis produce), giving the smallest output at the cost of speed. workers threads 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 with PYROSM_DECODE_BACKEND=processes). Re-packed blocks already have minimal string tables, so compact is ignored when repack=True. The written OSM data is identical to repack=False. Default False keeps the current (faster, slightly larger) in-place crop.

Returns:

The path of the written PBF file.

Return type:

str