CLI GuideChannel: osm pbfgeojson

OSM PBF to GeoJSON Converter

Extract OpenStreetMap binary data to web formats.

OSM PBF is a highly compressed binary format for storing OpenStreetMap data. Converting these immense databases into GeoJSON requires careful filtering using tools like Osmosis or GDAL, as direct conversions of entire regions are too heavy for memory-limited environments.

Indirect Mapping Execution Required

Because database connection attributes or local attributes parsing is required, direct web streams are disabled. Use the GDAL CLI script below for precise terminal conversions.

Specification Rules

  • Extract local road maps or building footprints from OpenStreetMap dumps
  • Format open geographic assets for use in web applications
  • Isolate specific landmark groups like parks or transit routes
  • PBF packages represent millions of geographic objects
  • Converting complete cities can deplete standard server memory buffers
  • Requires custom filtration parameters to select specific assets

GDAL Direct Equivalent Terminal Command

sh — gdalready
$ogr2ogr -f GeoJSON output.geojson input.osm.pbf -sql "SELECT * FROM lines WHERE highway IS NOT NULL"

When to use this conversion

Use this workflow when you need a slice of OpenStreetMap data (roads, buildings, points of interest) for a specific region or theme and want the result as GeoJSON for web display. Always filter — converting a whole continent's .osm.pbf to GeoJSON produces an unusable multi-gigabyte file.

Technical Details

OSM PBF is a Protocol Buffers binary encoding of OSM's node-way-relation data model. GDAL's OSM driver reads the file and exposes five layers (points, lines, multilinestrings, multipolygons, other_relations) controlled by an osmconf.ini configuration that maps OSM tags to attribute columns. SQL filters at extraction time avoid loading entire planets.
Output file size

GeoJSON is typically 5–10× the size of the source PBF because the binary Protobuf encoding is much denser than text JSON. A 50 MB PBF can produce a 300–500 MB GeoJSON, depending on how much of the data is included.

Common Errors & Fixes

  • Out-of-memory on whole-country PBF: GDAL tries to materialise the full multipolygons layer. Fix: pre-filter with osmium-tool or use a -sql query that selects a small tag set.
  • Tags missing from output: osmconf.ini didn't list the keys you need. Fix: copy GDAL's default osmconf.ini, add the keys under the relevant layer, and pass it via --config OSM_CONFIG_FILE.
  • Relations broken: multipolygon assembly fails when a relation references ways missing from the file. Fix: ensure your input includes complete relations (use osmium's -s complete-ways flag).
  • Output too large: GeoJSON of a city's roads is hundreds of MB. Fix: simplify with mapshaper, switch to vector tiles via tippecanoe, or filter to a narrower tag set.

Alternative Tools

  • osmium-tool (osmium tags-filter, osmium extract) for fast region/tag filtering
  • osmfilter / osmconvert for legacy OSM XML processing
  • Overpass Turbo (overpass-turbo.eu) for small-area extracts via web interface
  • Geofabrik regional extracts as smaller starting points than planet.osm.pbf

Frequently Asked Questions

How do I avoid out-of-memory errors with PBF files?

Apply precise SQL spatial or attribute queries inside your extraction script to filter for only the specific roads, buildings, or polygons required.

Why does my output lack some geographic attributes?

OSM uses a key-value tag structure. Ensure your conversion command includes all required keys by configuring the 'osmconf.ini' definitions file.

Are relations and multipolygons preserved?

Yes, GDAL reconstructs relations into multi-part geometries based on the OSM configuration rules.

Related Transformations Map