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 have no clue about doing something in R myself, but you can browse the OSM wiki about Frameworks and OSM Scientific Tools ... and do a search there for "osmar". It seems that there is some tool for processing OSM data, but I was not able to figure out whether you can do some clipping.
    – stephan75
    Commented Mar 21, 2016 at 19:40
  • crosspost: stackoverflow.com/questions/36104643/…
    – scai
    Commented Mar 29, 2016 at 13:00

1 Answer 1


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) Commented May 6, 2018 at 19:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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