-
Notifications
You must be signed in to change notification settings - Fork 2
/
osmium-extract.R
389 lines (324 loc) · 11.6 KB
/
osmium-extract.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#' Extract network data with osmium, convert to \pkg{sc} format, and collate all
#' results into a single \pkg{osmdata} object.
#'
#' @param city Name of city (used to name resultant files).
#' @param path Path to a local '.pbf' or '.bz2' file with OpenStreetMap data for
#' nominated city.
#' @param bbox Optional bounding box within which data are to be extracted. If
#' not given, result includes the entire network within the nominated OSM
#' file. `bbox` can be either a matrix obtained from the \pkg{osmdata} function,
#' `getbb` (or equivalent), or an object from which a bounding box can be
#' extracted. Objects currently recognised are matrices or arrays, which should
#' have two columns (x and y / longitude and latitude), or an \pkg{sf} object
#' from which a bounding box can be extracted. Alternatively, `bbox` can be a
#' local path to a 'geojson' file containing a single polygonal outline to be
#' used to trim the OSM data.
#' @param bbox_expand A proportional amount by which to extend the limits of the
#' bounding box defined by the `bbox` argument, defaulting to 5%.
#' @param osm_id In lieu of a bounding box, the ID of an Open Street Map object
#' (generally a relation) can be used to provide the boundary to trim the OSM
#' file.
#' @export
ua_extract_osm <- function (city, path, bbox = NULL, bbox_expand = 0.05,
osm_id = NULL) {
requireNamespace ("fs")
requireNamespace ("withr")
checkmate::assert_character (city, min.len = 1L, max.len = 1L)
city <- tolower (city)
checkmate::assert_file_exists (path)
if (!fs::path_ext (path) %in% c ("pbf", "bz2")) {
stop ("'path' must be to a '.pbf' or '.bz2' file")
}
checkmate::assert_numeric (
bbox_expand,
lower = -1,
upper = 1,
min.len = 1L,
max.len = 1L
)
if (!is.null (osm_id)) {
path_to_pbf <- trim_to_osm_id (city, path, osm_id)
} else if (!is.null (bbox)) {
path_to_pbf <- trim_osm_to_bbox (city, path, bbox, bbox_expand)
} else {
path_to_pbf <- convert_bz2_to_pbf (city, path)
}
extract_osm_keys (path_to_pbf)
ua_m4ra_parking_extraction (path_to_pbf)
ua_osm_schools (path_to_pbf)
ua_osm_nature (path_to_pbf)
}
#' Trim OSM file to an 'osm_id' and return converted 'pbf' output.
#'
#' @param city Directly from 'ua_extract_osm'
#' @param path The 'path' param from 'ua_extract_osm'
#' @param osm_id Directly from 'ua_extract_osm'
#' @return Full path and file name of newly created '.pbf' file.
#' @noRd
trim_to_osm_id <- function (city, path, osm_id) {
pars <- get_osmium_convert_args (city, path)
if (pars$f_exists) {
return (fs::path (pars$osm_dir, pars$f))
}
cli::cli_h3 (cli::col_green ("Reducing file to specified 'bbox':"))
# Get .osm file for specified 'osm_id':
ftmp <- fs::path (fs::path_temp (), "boundary.osm")
cmd <- paste ("osmium getid -r -t ", path, paste0 ("r", osm_id), "-o", ftmp)
system (cmd)
cmd <- paste ("osmium extract -p", ftmp, pars$f0, "-o", pars$f)
withr::with_dir (pars$osm_dir, system (cmd))
return (fs::path (pars$osm_dir, pars$f))
}
#' Trim OSM file to 'bbox' and return converted 'pbf' output.
#'
#' @param city Directly from 'ua_extract_osm'
#' @param path The 'path' param from 'ua_extract_osm'
#' @param bbox Directly from 'ua_extract_osm'
#' @param bbox_expand Directly from 'ua_extract_osm'
#' @return Full path and file name of newly created '.pbf' file.
#' @noRd
trim_osm_to_bbox <- function (city, path, bbox, bbox_expand) {
bbox <- get_ua_bbox (bbox, bbox_expand = bbox_expand)
pars <- get_osmium_convert_args (city, path)
if (pars$f_exists) {
return (pars$f)
} else if (!fs::file_exists (pars$f)) {
# No city file yet; use country file
pars$f <- path
}
cli::cli_h3 (cli::col_green ("Reducing file to specified 'bbox':"))
pb_arg <- ifelse (methods::is (bbox, "character"), "-p", "-b")
cmd <- paste (
"osmium extract",
pb_arg,
paste0 (bbox, collapse = ","),
pars$f,
"-o",
pars$f0
)
withr::with_dir (pars$osm_dir, system (cmd))
return (fs::path (pars$osm_dir, pars$f0))
}
#' Convert various form of 'bbox' parameter to an actual 2-by-2 matrix.
#'
#' @param bbox Directly from 'ua_extract_osm'
#' @param bbox_expand Directly from 'ua_extract_osm'
#' @return bbox as 2-by-2 matrix.
#' @noRd
get_ua_bbox <- function (bbox = NULL, bbox_expand = 0.05) {
if (methods::is (bbox, "character")) {
checkmate::assert_character (bbox, len = 1L)
checkmate::assert_file_exists (bbox)
return (bbox)
}
if (inherits (bbox, "sf")) {
bbox <- sf::st_transform (bbox, 4326)
xy <- sf::st_coordinates (bbox) [, 1:2]
} else if (ncol (bbox) != 2L) {
stop ("'bbox' must have 2 columns only.", call. = FALSE)
} else if (!inherits (bbox, "matrix")) {
stop ("bbox must be a matrix-like object.", call. = FALSE)
} else if (nrow (bbox) < 2L) {
stop ("bbox must have at least 2 rows.", call. = FALSE)
}
if (nrow (bbox) == 2L) {
return (bbox)
}
res <- t (apply (xy, 2, range))
rownames (res) <- c ("x", "y")
colnames (res) <- c ("min", "max")
# expand:
for (i in 1:2) {
res [i, ] <- mean (range (res [i, ])) +
c (-0.5, 0.5) * diff (range (res [i, ])) * (1 + bbox_expand)
}
return (res)
}
#' Convert '.bz2' directly to 'pbf' without trimming.
#'
#' This function is called when no `bbox` parameter is passed to main
#' 'ua_extract_osm()` function.
#'
#' @param city Directly from 'ua_extract_osm'
#' @param path The 'path' param from 'ua_extract_osm'
#' @return Full path and file name of newly created '.pbf' file.
#' @noRd
convert_bz2_to_pbf <- function (city, path) {
pars <- get_osmium_convert_args (city, path)
if (!pars$f_exists) {
cmd <- paste ("osmium cat ", pars$f0, "-o", pars$f)
withr::with_dir (pars$osm_dir, system (cmd))
}
return (fs::path (pars$osm_dir, pars$f))
}
#' Helper function to return arguments used for 'osmium' calls, mostly by
#' removing paths.
#' @noRd
get_osmium_convert_args <- function (city, path) {
osm_dir <- fs::path_dir (path)
f <- paste0 (city, ".osm.pbf")
f_exists <- FALSE
if (!fs::file_exists (fs::path (osm_dir, f)) &&
fs::path_file (osm_dir) != "city") {
osm_dir <- fs::path (osm_dir, city)
if (!fs::dir_exists (osm_dir)) {
fs::dir_create (osm_dir)
}
}
if (fs::file_exists (fs::path (osm_dir, f))) {
cli::cli_alert_info (cli::col_blue (
"File '",
fs::path (osm_dir, f),
" already exists and will not be over-written."
))
f_exists <- TRUE
}
f0 <- f
f <- fs::path (osm_dir, f)
list (osm_dir = osm_dir, f = f, f0 = f0, f_exists = f_exists)
}
#' Extract smaller files for a series of OSM keys and convert to '.osm' format
#'
#' @param path Path to single '.pbf' file returned from either
#' 'convert_bz2_to_pbf' or 'trim_osm_to_bbox'.
#' @return Nothing.
#' @noRd
extract_osm_keys <- function (path) {
path_dir <- fs::path_dir (path)
f <- fs::path_file (path)
ptn <- "\\.osm\\.(pbf|bz2)$"
if (grepl ("\\-latest", f)) {
ptn <- paste0 ("\\-latest", ptn)
}
ft <- paste0 (gsub (ptn, "", f), "-network.osm")
ft_full <- fs::path (path_dir, ft)
cli::cli_h3 (cli::col_green ("Extracting OSM tags:"))
if (fs::file_exists (ft_full)) {
cli::cli_alert_info (cli::col_blue (
"File '",
ft_full,
"' already exists."
))
} else {
tags <- c (
"highway", "restriction", "access", "foot", "motorcar",
"motor_vehicle", "vehicle", "toll", "bicycle",
"cycleway", "cycleway:left", "cycleway:right"
)
tags <- paste0 (paste0 ("wr/", tags), collapse = " ")
cmd <- paste ("osmium tags-filter", f, tags, "-o", ft)
withr::with_dir (path_dir, system (cmd))
}
}
#' Modified version of \pkg{m4ra} functions to extract OSM data used for parking
#' analyses.
#'
#' This version uses 'osmium' instead of \pkg{osmdata} used there.
#'
#' @param path The `path_to_pbf` parameter generated in \link{ua_extract_osm}.
#' @return Path of a '.osm' file containing information needed for \pkg{m4ra}
#' parking analyses.
#' @noRd
ua_m4ra_parking_extraction <- function (path) {
tags <- c (
"nwr/parking",
"nwr/amentiy=parking",
"nwr/building=garage",
"nwr/building=garages",
"nwr/parking:lane:left",
"nwr/parking:lane:right",
"nwr/parking:lane:both",
"nwr/amentiy=parking",
"nwr/building=garage",
"nwr/building=garages",
"nwr/parking:lane:left",
"nwr/parking:lane:right",
"nwr/parking:lane:both"
)
path_dir <- fs::path_dir (path)
f <- fs::path_file (path)
cli::cli_h3 (cli::col_green ("Extracting parking data:"))
ptn <- "\\.osm\\.(pbf|bz2)$"
if (grepl ("\\-latest", f)) {
ptn <- paste0 ("\\-latest", ptn)
}
ft <- paste0 (gsub (ptn, "", f), "-parking.osm")
ft_full <- fs::path (path_dir, ft)
if (fs::file_exists (ft_full)) {
cli::cli_alert_info (cli::col_blue (
"File '",
ft_full,
"' already exists."
))
} else {
cmd <- paste ("osmium tags-filter", f, paste0 (tags, collapse = " "), "-o", ft)
withr::with_dir (path_dir, system (cmd))
}
return (ft_full)
}
#' Extract data on schools using osmium
#'
#' @param path The `path_to_pbf` parameter generated in \link{ua_extract_osm}.
#' @return Path of a '.osm' file containing information needed for \pkg{m4ra}
#' parking analyses.
#' @noRd
ua_osm_schools <- function (path) {
tags <- c (
"nwr/amenity=school",
"nwr/school=elementary,primary,secondary"
)
path_dir <- fs::path_dir (path)
f <- fs::path_file (path)
cli::cli_h3 (cli::col_green ("Extracting school data:"))
ptn <- "\\.osm\\.(pbf|bz2)$"
if (grepl ("\\-latest", f)) {
ptn <- paste0 ("\\-latest", ptn)
}
ft <- paste0 (gsub (ptn, "", f), "-schools.osm")
ft_full <- fs::path (path_dir, ft)
if (fs::file_exists (ft_full)) {
cli::cli_alert_info (cli::col_blue (
"File '",
ft_full,
"' already exists."
))
} else {
cmd <- paste ("osmium tags-filter", f, paste0 (tags, collapse = " "), "-o", ft)
withr::with_dir (path_dir, system (cmd))
}
return (ft_full)
}
#' Extract data on natural spaces using osmium
#'
#' @param path The `path_to_pbf` parameter generated in \link{ua_extract_osm}.
#' @return Path of a '.osm' file containing information needed for \pkg{m4ra}
#' parking analyses.
#' @noRd
ua_osm_nature <- function (path) {
tags <- c (
"w/leisure=garden,park,nature_reserve,playground",
"w/surface=grass",
"w/landuse=forest,meadow,recreation_ground,village_green",
"wr/natural"
)
path_dir <- fs::path_dir (path)
f <- fs::path_file (path)
cli::cli_h3 (cli::col_green ("Extracting natural spaces data:"))
ptn <- "\\.osm\\.(pbf|bz2)$"
if (grepl ("\\-latest", f)) {
ptn <- paste0 ("\\-latest", ptn)
}
ft <- paste0 (gsub (ptn, "", f), "-natural.osm")
ft_full <- fs::path (path_dir, ft)
if (fs::file_exists (ft_full)) {
cli::cli_alert_info (cli::col_blue (
"File '",
ft_full,
"' already exists."
))
} else {
cmd <- paste ("osmium tags-filter", f, paste0 (tags, collapse = " "), "-o", ft)
withr::with_dir (path_dir, system (cmd))
}
return (ft_full)
}