I have (1) an OpenStreetMap file of a large area in .osm.pbf format that I would like to clip with (2) a shapefile .shp of a smaller area using R.

The output output I would like to get is a .osm.pbf file of the intersection between (1) and (2). Any ideas on how to do this in R ?

Reproducible example:


I'm not sure about how best to do this with R, but if you don't mind using the osmconvert tool (http://wiki.openstreetmap.org/wiki/Osmconvert - binaries available for Linux/Windows/Mac), there are some options.

Clip using a rectangle:

osmconvert input-data.osm.pbf -b=10.5,49,11.5,50 -o=my-output-file.pbf

(south-west and north-eastern corners, lng/lat)

Clip using a polygon:

osmconvert input-data.osm.pbf -B=clip-polygon.poly -o=my-output-file.pbf

The '.poly' file is in the Osmosis polygon filter file format (http://wiki.openstreetmap.org/wiki/Osmosis/Polygon_Filter_File_Format), that page shows you how to create one manually or there are some suggested tools for converting from other formats.

  • 1
    You can't do that with R, at least not without compiling protobuffers for R, as "*.pbf" is a serialized OpenStreetMap standard. In other words, you can export internal OSM format to PBF (smaller), or to XML (larger, but more compatible). As "tom" suggested above, use osmconvert. It's a reliable and fast tool (written in C) – Michał Leon May 6 '18 at 19:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.