osmxml is an R package useful to download, read, prepare and
(rudimentarily) visualise OpenStreetMap (OSM) XML files like the
map.osm
file you would get from the OSM website’s “Export”
page.
This package might not be what you’re after: please see below the section “Other OSM-related R packages” to find more powerful tools.
You can install the development version of osmexport from GitHub with:
# install.packages("devtools")
devtools::install_github("stragu/osmxml")
ox_download()
: download an OSM export by defining its bounding box, save it to file.ox_read()
: read a.osm
file as an object of classosm
ox_separate_tags()
: separate the tags contained in theother_tags
column. This is done by default when importing the data withox_read()
The osm
S3 class – very much subject to changing in name and
properties – has print()
and plot()
methods. It is made of
sf
(“simple features”) spatial
objects.
The functions available are designed to be pipeable. For example, to download and read the area around Te Kura Tatauranga, Waipapa Taumata Rau:
library(osmxml)
#> All data, downloaded or included as an example, is © OpenStreetMap contributors, and the conditions of its reuse are defined by the ODbL licence.
#> Find out more on the OSM website: https://www.openstreetmap.org/copyright
TKT <- c(174.76598, -36.85440, 174.77019, -36.85129) |>
ox_download() |>
ox_read()
#> Using cached .osm file with same bbox, which was last modified on 2022-08-06 00:09:17
# see what the object contains
TKT
#> OSM data object made of 5 simple feature collections: points, lines, multilinestrings, multipolygons, other_relations
#> The corresponding sf geometry types are:
#> points lines multilinestrings multipolygons
#> POINT LINESTRING MULTILINESTRING MULTIPOLYGON
#> other_relations
#> GEOMETRYCOLLECTION
We can now use the default plot method to have a glimpse at the data:
plot(TKT)
Because the parts of the object are of class sf
, they can be processed
with sf and well as dplyr functions:
library(dplyr)
library(sf)
# most common "building" values in polygons
TKT$multipolygons |>
st_drop_geometry() |> # remove geometry column
filter(!is.na(building)) |> # only keep buildings
count(building, sort = TRUE) # most commons values at the top
#> building n
#> 1 university 31
#> 2 yes 6
#> 3 roof 5
#> 4 apartments 2
#> 5 church 1
#> 6 civic 1
#> 7 garages 1
#> 8 gazebo 1
#> 9 retail 1
#> 10 ruins 1
Contributions are welcome and appreciated. You can contribute to this package by:
- Testing it and reporting issues you encounter
- Suggesting a change with a pull request (but please discuss your idea in an issue beforehand)
- Writing a new vignette with an interesting worked example
Note that all contributions to the codebase will be released under the GPL, and submitting your contribution to this repository is an implicit agreement.
We expect contributors to respect UQRUG’s Code of Conduct.
If a conversation or comment does not belong in a public issue report, please contact the maintainer listed in the package description.
OSM data included in this package (in the ./inst
directory) and
downloaded with the ox_download()
function is © OpenStreetMap
contributors, and the conditions of its reuse are defined by the ODbL
licence. You likely need to
include this information on anything derived from it. Find out more on
the OSM website.
There are many other OSM-related R packages that might be more suitable for what you are hoping to achieve. Notable packages available on CRAN are:
- osmadata uses the Overpass API to download datasets based on location and tags. Use this for a more targeted download.
- osmextract uses different providers to download chunks of large data dumps, for specific regions and dates. Some providers offer data already prepared for particular uses (e.g. data relevant to cycling).
- osmplotr downloads OSM data with the Overpass API and provides many tools to render the data.
- osmar accesses OSM data from different sources and converts it to various formats. Note that it hasn’t been updated since 2013.
Transport-specific packages:
- stplanr provides tools for sustainable transport planning, making use of the osrm package.
- osrm bridges R with the OSRM API (routing service based on OpenStreetMap data).
- opentripplanner provides an interface to OpenTripPlanner (OTP), a routing service that relies on OpenStreetMap data.
To acquire basemaps:
- OpenStreetMap allows downloading rendered OSM data from various servers.
- OSMscale plots spatial data with OSM basemaps and corresponding scale bars.
Other interactive visualisation packages automatically fetch rendered OSM data as basemaps, like leaflet and tmap.