The goal of
osmextract is to make it easier for people to access OpenStreetMap (OSM) data for reproducible research. OSM data is the premier source of freely available, community created geographic data worldwide. We aim to enable you to extract it for data-driven work in the public interest.
osmextract finds, downloads, converts and imports bulk OSM data hosted by providers such as Geofabrik GmbH and bbbike. For information on alternative providers and how to add them see the providers vignette.
The package answers a common question for researchers who use OSM data: how to get it into a statistical environment, in an appropriate format, as part of a computationally efficient and reproducible workflow? Other packages answer parts of this question.
osmdata, for example, is an R package that provides an R interface to the Overpass API, which is ideal for downloading small OSM datasets. However, the API is rate limited, making it hard to download large datasets. As a case study, try to download all cycleways in England using
library(osmdata) cycleways_england = opq("England") %>% add_osm_feature(key = "highway", value = "cycleway") %>% osmdata_sf() # Error in check_for_error(doc) : General overpass server error; returned: # The data included in this document is from www.openstreetmap.org. The data is made available under ODbL. runtime error: Query timed out in "query" at line 4 after 26 seconds.
The query stops with an error message after around 30 seconds. The same query can be made with
osmextract as follows, which reads-in almost 100k linestrings in less than 10 seconds, after the data has been downloaded in the compressed
.pbf format and converted to the open standard
.gpkg format. The download-and-conversion operation of the OSM extract associated to England takes approximately a few minutes, but this operation must be executed only once. The following code chunk is not evaluated.
library(osmextract) cycleways_england = oe_get( "England", quiet = FALSE, query = "SELECT * FROM 'lines' WHERE highway = 'cycleway'" ) par(mar = rep(0.1, 4)) plot(sf::st_geometry(cycleways_england))
The package is designed to complement
osmdata, which has advantages over
osmextract for small datasets:
osmdata is likely to be quicker for datasets less than a few MB in size, provides up-to-date data and has an intuitive interface.
osmdata can provide data in a range of formats, while
osmextract only returns
osmextract’s niche is that it provides a fast way to download large OSM datasets in the highly compressed
pbf format and read them in via the fast C library GDAL and the popular R package for working with geographic data,
You can install the released version of
osmextract from CRAN with:
You can install the development version from GitHub with:
# install.packages("remotes") remotes::install_github("ropensci/osmextract")
Load the package with:
library(osmextract) #> Data (c) OpenStreetMap contributors, ODbL 1.0. https://www.openstreetmap.org/copyright. #> Check the package website, https://docs.ropensci.org/osmextract/, for more details.
To use alongside functionality in the
sf package, we also recommend attaching this geographic data package as follows:
The functions defined in this package may return a warning message like
if the user is running an old version of GDAL (<= 3.0.0) or PROJ (<= 6.0.0). See here for more details. Nevertheless, every function should still work correctly. Please, raise a new issue if you find any odd behaviour.
osmextract a place name and it will try to find it in a list of names in the specified provider (Geofabrik by default). If the name you give it matches a place, it will download and import the associated data into R. The function
oe_get() downloads (if not already downloaded) and reads-in data from OSM extract providers as an
sf object. By default
oe_get() imports the
lines layer, but any layer can be read-in by changing the
osm_lines = oe_get("Isle of Wight", stringsAsFactors = FALSE, quiet = TRUE) osm_points = oe_get("Isle of Wight", layer = "points", stringsAsFactors = FALSE, quiet = TRUE) nrow(osm_lines) #>  45621 nrow(osm_points) #>  59085 par(mar = rep(0, 4)) plot(st_geometry(osm_lines), xlim = c(-1.59, -1.1), ylim = c(50.5, 50.8)) plot(st_geometry(osm_points), xlim = c(-1.59, -1.1), ylim = c(50.5, 50.8))
The figures above give an insight into the volume and richness of data contained in OSM extracts. Even for a small island such as the Isle of Wight, it contains over 100k features including ferry routes, shops and roads. The column names in the
osm_lines object are as follows:
names(osm_lines) # default variable names #>  "osm_id" "name" "highway" "waterway" "aerialway" #>  "barrier" "man_made" "z_order" "other_tags" "geometry"
Once imported, you can use all functions for data frames in base R and other packages. You can also use functions from the
sf package for spatial analysis and visualisation. Let’s plot all the major, secondary and residential roads, for example:
The same steps can be used to get other OSM datasets (examples not run):
If the input place does not match any of the existing names in the supported providers, then
oe_get() will try to geocode it via Nominatim API, and it will select the smallest OSM extract intersecting the area. For example (not run):
oe_get("Milan") # Warning: It will download more than 400MB of data #> No exact match found for place = Milan and provider = geofabrik. Best match is Iran. #> Checking the other providers. #> No exact match found in any OSM provider data. Searching for the location online. #> ... (extra messages here)
For further details on using the package, see the Introducing osmextract vignette.
The default behaviour of
oe_get() is to save all the files in a temporary directory, which is erased every time you restart your R session. If you want to set a directory that will persist, you can add
OSMEXT_DOWNLOAD_DIRECTORY=/path/for/osm/data in your
.Renviron file, e.g. with:
usethis::edit_r_environ() # Add a line containing: OSMEXT_DOWNLOAD_DIRECTORY=/path/to/save/files
We strongly advise you setting a persistent directory since working with
.pbf files is an expensive operation, that is skipped by
oe_*() functions if they detect that the input
.pbf file was already downloaded.
You can always check the default
download_directory used by
Depending on the
.pbf file selected and your connection speed, you may experience an error stating
Timeout of 60 seconds was reached. If so, before calling
oe_get(), you can adjust the timeout using
options(timeout = 300), choosing an appropriate value. This setting affects all calls to download.file(), so you may need to reset it for the rest of your script.
We hope to make the user interface to the SQL syntax more user friendly. We would love to see more providers added (see the Add new OpenStreetMap providers for details) and see what people can do with OSM datasets of the type provided by this package in a reproducible and open statistical programming environment for the greater good. Any contributions to support this or any other improvements to the package are very welcome via our issue tracker.
We hope this package will provide easy access to OSM data for reproducible research in the public interest, adhering to the condition of the OdBL licence which states that
Any Derivative Database that You Publicly Use must be only under the terms of:
See the Introducing osmextract vignette for more details.
We very much look forward to comments, questions and contributions. If you have any doubt, or if you want to suggest a new approach or add a new OSM provider, feel free to create a new issue in the issue tracker or a new pull request. We always try to build the most intuitive user interface and write the most informative error messages, but if you think that something is not clear and could have been explained better, please let us know.
Please note that this package is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.