pyrosm.OSM.to_pbf

Contents

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_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 is cropped by its envelope (bounding rectangle), matching how OSM() itself filters by a polygon bounding box, so a cropped-and-reread file matches reading the source with the same bounding_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; when False no relations are written.

  • workers (int) – Number of worker processes for the CPU-heavy per-block work. 1 (default) runs sequentially; >1 uses 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. 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; the re-pack write is sequential, though workers still parallelizes the selection. 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