knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

Install and Load Package

devtools::install_github("ITSleeds/OSMtools")
library(OSMtools)

Download from Geofabrik

osmt_geofabrik_dl(region = "europe/great-britain/england/isle-of-wight", destfile = "iow.pbf")

You can also use the geofabrik package

Convert from one format to another

osmt_convert(file = "iow.pbf", format_out = "o5m")

Formats supported are:

Note on file format choice

The native format of OSm is the the .osm. But .pbf is the prefered format for transfering data as it is the most compact. It is also acceptable and an input for many pieces of sofotware e.g. sf package and opentripplanner. But it is not supported by osmt_filter.

You could convert your files from pbf to osm and then use osmt_filter and finally read them into R. But osmt_filter also accepts .o5m files and will process them faster. So a typical workflow for large files is:

  1. Download pbf files
  2. Convert to o5m
  3. Filter as required
  4. Convert to pbf
  5. Import into R or OpenTripPlanner etc

Filtering objects

You can keep or remove (drop) objects based on their tags. For example if you only wanted to keep the roads you could use keep = "highway=" this would only keep objects that had any value for the highway tag.

osmt_filter(file = "iow.o5m", path_out = "cycling.o5m", keep = "highway=")

Filtering tags

You can keep or remove (drop) tags from objects. This example removed the cycleway tag from all objects

osmt_filter(file = "cycling.o5m", path_out = "driving.o5m", drop_tags = "cycleway")

Modifying Tags

To modify tags use the form "key=tag1 to =tag2" you can do multiple modifications at once. This example changes all the objects with the tags highway = primary or highway = secondary to highway = tertiary.

osmt_filter(file = "cycling.o5m", path_out = "smallstreets.o5m", 
            modify_tags = "highway=primary to =tertiary highway=secondary to =tertiary")

Reading into R

The SF package supports reading .osm and .pbf files but not .o5m files.

lines <- sf::st_read("iow.pbf", layer = "lines") # Works
lines2 <- sf::st_read("cycling.o5m", layer = "lines") # Fails
osmt_convert(file = "cycling.o5m", format_out = "pbf")
lines3 <- sf::st_read("cycling.pbf", layer = "lines") # Works


ITSLeeds/OSMtools documentation built on Oct. 28, 2021, 1:53 a.m.