R package for downloading OpenStreetMap data
title: “osmdata, an R package for OpenStreetMap data”
keywords: “open street map, openstreetmap, overpass API, OSM”
output:
rmarkdown::html_vignette:
self_contained: no
knitr::opts_chunk$set (
collapse = TRUE,
warning = TRUE,
message = TRUE,
width = 120,
comment = "#>",
fig.retina = 2,
fig.path = "README-"
)
osmdata
is an R package for accessing the data underlying OpenStreetMap
(OSM), delivered via the Overpass
API. (Other packages such
as
OpenStreetMap
can be used to download raster tiles based on OSM data.)
Overpass is a read-only API that extracts custom
selected parts of OSM data. Data can be returned in a variety of formats,
including as Simple Features (sf
),
Spatial (sp
), or Silicate
(sc
) objects. The package is designed
to allow access to small-to-medium-sized OSM datasets (see
osmextract
for an approach for
reading-in bulk OSM data extracts).
To install latest CRAN version:
install.packages ("osmdata")
Alternatively, install the development version with any one of the following
options:
# install.packages("remotes")
remotes::install_git ("https://git.sr.ht/~mpadge/osmdata")
remotes::install_bitbucket ("mpadge/osmdata")
remotes::install_gitlab ("mpadge/osmdata")
remotes::install_github ("ropensci/osmdata")
To load the package and check the version:
library (osmdata)
packageVersion ("osmdata")
Overpass API queries can be
built from a base query constructed with opq
followed by add_osm_feature
. The
corresponding OSM objects are then downloaded and converted to Simple
Feature (sf
) objects with
osmdata_sf()
, Spatial (sp
)
objects with osmdata_sp()
or Silicate (sc
)
objects with osmdata_sc()
. For example,
x <- opq (bbox = c (-0.27, 51.47, -0.20, 51.50)) %>% # Chiswick Eyot in London, U.K.
add_osm_feature (key = "name", value = "Thames", value_exact = FALSE) %>%
osmdata_sf ()
x
msg <- c (
"Object of class 'osmdata' with:\n",
" $bbox : 51.47,-0.27,51.5,-0.2\n",
" $overpass_call : The call submitted to the overpass API\n",
" $meta : metadata including timestamp and version numbers\n",
" $osm_points : 'sf' Simple Features Collection with 24548 points\n",
" $osm_lines : 'sf' Simple Features Collection with 2219 linestrings\n",
" $osm_polygons : 'sf' Simple Features Collection with 33 polygons\n",
" $osm_multilines : 'sf' Simple Features Collection with 6 multilinestrings\n",
" $osm_multipolygons : 'sf' Simple Features Collection with 3 multipolygons"
)
message (msg)
OSM data can also be downloaded in OSM XML format with osmdata_xml()
and saved
for use with other software.
osmdata_xml(q1, "data.osm")
All osmdata
queries begin with a bounding box defining the area of the query.
The getbb()
function can be used
to extract bounding boxes for specified place names.
getbb ("astana kazakhstan")
The next step is to convert that to an overpass query object with the opq()
function:
q <- opq (getbb ("astana kazakhstan"))
q <- opq ("astana kazakhstan") # identical result
It is also possible to use bounding polygons rather than rectangular boxes:
b <- getbb ("bangalore", format_out = "polygon")
class (b)
head (b [[1]])
The next step is to define features of interest using the add_osm_feature()
function.
This function accepts key
and value
parameters specifying desired features
in the OSM key-vale schema.
Multiple add_osm_feature()
calls may be combined as illustrated below, with
the result being a logical AND operation, thus returning all amenities that
are labelled both as restaurants and also as pubs:
q <- opq ("portsmouth usa") %>%
add_osm_feature (key = "amenity", value = "restaurant") %>%
add_osm_feature (key = "amenity", value = "pub") # There are none of these
Features can also be requested by key only, in which case features with any
values for the specified key will be returned:
q <- opq ("portsmouth usa") %>%
add_osm_feature (key = "amenity")
Such key-only queries can, however, translate into requesting very large data
sets, and should generally be avoided in favour of more precise key-value
specifications.
Negation can also be specified by pre-pending an exclamation mark so that the
following requests all amenities that are NOT labelled as restaurants and that
are not labelled as pubs:
q <- opq ("portsmouth usa") %>%
add_osm_feature (key = "amenity", value = "!restaurant") %>%
add_osm_feature (key = "amenity", value = "!pub") # There are a lot of these
Additional arguments allow for more refined matching, such as the following
request for all pubs with “irish” in the name:
q <- opq ("washington dc") %>%
add_osm_feature (key = "amenity", value = "pub") %>%
add_osm_feature (
key = "name", value = "irish",
value_exact = FALSE, match_case = FALSE
)
Logical OR combinations can be constructed using the separate
add_osm_features()
function.
The first of the above examples requests all features that are both restaurants
AND pubs. The following query will request data on restaurants OR pubs:
q <- opq ("portsmouth usa") %>%
add_osm_features (features = c (
"\"amenity\"=\"restaurant\"",
"\"amenity\"=\"pub\""
))
The vector of features
contains key-value pairs separated by an overpass
“filter”
symbol
such as =
, !=
, or ~
. Each key and value must be enclosed in
escape-delimited quotations as shown above.
Full lists of available features and corresponding tags are available in the functions
?available_features
and
?available_tags
.
An overpass query constructed with the opq()
and add_osm_feature()
functions is then sent to the overpass server to
request data. These data may be returned in a variety of formats, currently
including:
osmdata_xml()
;osmdata_sf()
;osmdata_sp()
;osmdata_sc()
;data.frame
format viaosmdata_data_frame()
.Data may also be trimmed to within a defined polygonal shape with the
trim_osmdata()
function. Full package functionality is described on the
website
citation ("osmdata")
All data that you access using osmdata
is licensed under
OpenStreetMap’s license, the Open Database Licence.
Any derived data and products must also carry the same licence. You should make
sure you understand that licence before publishing any derived datasets.
osmextract is an R package for downloading and importing compressed ‘extracts’ of OSM data covering large areas (e.g. all roads in a country).
The package represents data in sf
format only, and only allows a single “layer” (such as points, lines, or polygons) to be read at one time.
It is nevertheless recommended over osmdata for large queries of single layers, or where relationships between layers are not important.
osmapiR is an R interface to the OpenStreetMap API v0.6 for fetching and saving raw geodata from/to the OpenStreetMap database.
This package allows access to OSM maps data as well as map notes, GPS traces, changelogs, and users data.
osmapiR
enables editing or exploring the history of OSM objects, and is not intended to access OSM map data for other purposes (unlike the osmdata or osmextract packages).
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.
All contributions to this project are gratefully acknowledged using the allcontributors
package following the all-contributors specification. Contributions of any kind are welcome!
sckott |
nsfinkelstein |
gawbul |
edzer |
MAnalytics |
richardellison |
cboettig |
prise6 |
PaoloFrac |
Dris101 |
TomBor |
matkoniecz |
urswilke |
Robsteranium |
assignUser |
rsbivand |