R package for downloading OpenStreetMap data
1#library (osmdata)
2
3benchmark <- function (times = 100) {
4
5 devtools::load_all (".", export_all = FALSE)
6 q0 <- opq (bbox = c(-0.27, 51.47, -0.20, 51.50))
7 q1 <- add_osm_feature (q0, key = "name", value = "Thames")
8 # contains both multipolygons and multilinestrings
9 doc <- osmdata_xml (q1, "export.osm")
10
11 objs <- c ("points", "lines", "multilinestrings", "multipolygons",
12 "other_relations")
13 mt_sf <- size_sf <- NULL
14 for (i in seq (objs)) {
15 mb <- microbenchmark::microbenchmark (
16 dat <- sf::st_read ("export.osm", layer = objs [i], quiet = TRUE),
17 times = times)
18 size_sf <- c (size_sf, object.size (dat))
19 mt_sf <- c (mt_sf, median (mb$time))
20 cat ("\r", i, " / ", length (objs))
21 }
22 mt_sf <- mt_sf / 1e6 # nano-seconds to milli-seconds
23 cat ("\rSF: Median times (in ms) for (", paste (objs), "):\n")
24 cat ("\t(", mt_sf, "); total = ", sum (mt_sf), "\n")
25
26 mb <- microbenchmark::microbenchmark (x <- osmdata_sf (q1, doc),
27 times = times)
28 #mb <- microbenchmark::microbenchmark ( x <- osmdata_sf (q1, "export.osm"),
29 # times = 10L)
30 mt <- median (mb$time / 1e6)
31 cat ("osmdata: Median time = ", mt, " ms\n")
32 size_od <- object.size (x)
33
34 cat ("\nosmdata took ", mt / sum (mt_sf), " times longer to extract ",
35 size_od / sum (size_sf), " times as much data\n")
36}