Open Street Map¶
A good source of roads data is Open Street Map (OSM). (expand what it is, and limitations (completeness?))
You can download OSM data in R using the osmdata package. You can only download a certain amount of data in a single request, so typically you need to a number of requests for adjacent regions and then combine the results. We used the first level administrative boundaries of Tanzania (also downloaded with the geodata package) for this.
library(geodata)
adm <- gadm("TZA", level = 1, path=".")
We want data for primary, secondary and tertiary highways in Tanzania. Below is a helper function to accomplish this.
library(osmdata)
## Warning: package 'osmdata' was built under R version 4.3.1
## Data (c) OpenStreetMap contributors, ODbL 1.0. https://www.openstreetmap.org/copyright
roadtypes <- c("primary", "secondary", "tertiary")
getOSMdata <- function(obj, key, value) {
b <- sp::bbox(obj)
q <- osmdata::opq(b)
q <- osmdata::add_osm_feature(q, key, value)
d <- osmdata::osmdata_sp(q)
d$osm_lines[, key]
}
The below takes a long time to run. It may also fail because of the relative fraility of the OSM servers (or rather then enourmous number of requests that they get).
x <- lapply(seq_along(adm), function(i) getOSMdata(adm[i, ], "highway", roadtypes))
Now put the results together
# combine the results for each area
rd <- do.call(bind, x)
# remove anything outside of Tanzania
rd <- crop(rd, adm)
# aggregate by type, also removing duplicates line parts
roads <- aggregate(rd, "highway")
The OSM data is very detailed. Much more than we need in most analysis.
We simplified it with the rmapshaper
package to store less data and
so that the example runs faster.
library(rmapshaper)
roads <- ms_simplify(roads, keep=0.01)