1
+ # ' Create geographical extracts from an OSM file
2
+ # '
3
+ # ' Creates geographical extracts from an OSM data file or an OSM history file.
4
+ # ' The geographical extent can be given either as a bounding box or as a
5
+ # ' (multi)polygon.
6
+ # '
7
+ # ' @param input_path A string. The path to the OSM data/history file whose
8
+ # ' extent should be extracted from. Please see [file_formats] for a list of
9
+ # ' supported file formats.
10
+ # ' @param extent Either a `POLYGON` or a `MULTIPOLYGON` `sf` object with only
11
+ # ' one feature or a `bbox` object, created by [sf::st_bbox].
12
+ # ' @param output_path A string. The path to the file where the output should be
13
+ # ' written to. Please see [file_formats] for a list of supported file formats.
14
+ # ' @param overwrite A logical. Whether existing files should be overwritten by
15
+ # ' the output. Defaults to `FALSE`.
16
+ # ' @param echo_cmd A logical. Whether to print the Osmium command generated by
17
+ # ' the function call to the screen. Defaults to `FALSE`.
18
+ # ' @param echo A logical. Whether to print the standard output and error
19
+ # ' generated by the Osmium call to the screen. Defaults to `TRUE`.
20
+ # ' @param spinner A logical. Whether to show a reassuring spinner while the
21
+ # ' Osmium call is being executed. Defaults to `TRUE`.
22
+ # '
23
+ # ' @return The normalized path to the output file.
24
+ # '
25
+ # ' @examplesIf identical(tolower(Sys.getenv("NOT_CRAN")), "true")
26
+ # ' pbf_path <- system.file("extdata/cur.osm.pbf", package = "rosmium")
27
+ # '
28
+ # ' file.size(pbf_path)
29
+ # '
30
+ # ' # buffering the pbf bounding box 4000 meters inward and using the result
31
+ # ' # extent to extract the osm data inside it. transforming the crs because
32
+ # ' # inward buffers only work with project crs
33
+ # '
34
+ # ' lines <- sf::st_read(pbf_path, layer = "lines", quiet = TRUE)
35
+ # ' bbox <- sf::st_bbox(lines)
36
+ # ' bbox_polygon <- sf::st_as_sf(sf::st_as_sfc(bbox))
37
+ # ' smaller_bbox_poly <- sf::st_buffer(
38
+ # ' sf::st_transform(bbox_polygon, 5880),
39
+ # ' -4000
40
+ # ' )
41
+ # ' smaller_bbox_poly <- sf::st_transform(smaller_bbox_poly, 4326)
42
+ # '
43
+ # ' output_path <- extract(
44
+ # ' pbf_path,
45
+ # ' smaller_bbox_poly,
46
+ # ' tempfile(fileext = ".osm.pbf")
47
+ # ' )
48
+ # '
49
+ # ' file.size(output_path)
50
+ # '
51
+ # ' @export
1
52
extract <- function (input_path ,
2
- borders ,
53
+ extent ,
3
54
output_path ,
4
55
overwrite = FALSE ,
5
- echo = TRUE ,
6
56
echo_cmd = FALSE ,
57
+ echo = TRUE ,
7
58
spinner = TRUE ) {
8
59
assert_osmium_is_installed()
9
60
@@ -12,10 +63,10 @@ extract <- function(input_path,
12
63
checkmate :: assert_logical(echo , any.missing = FALSE , len = 1 )
13
64
checkmate :: assert_logical(echo_cmd , any.missing = FALSE , len = 1 )
14
65
checkmate :: assert_logical(spinner , any.missing = FALSE , len = 1 )
15
- assert_borders( borders )
66
+ assert_extent( extent )
16
67
assert_output_path_multi_ext(output_path , overwrite )
17
68
18
- border_arg <- create_border_input(borders )
69
+ border_arg <- create_border_input(extent )
19
70
output_arg <- paste0(" --output=" , output_path )
20
71
overwrite_arg <- if (overwrite ) " --overwrite" else character ()
21
72
@@ -71,20 +122,20 @@ border_input_from_polygon <- function(x) {
71
122
return (input )
72
123
}
73
124
74
- check_borders <- function (borders ) {
75
- multi_class_res <- checkmate :: check_multi_class(borders , c(" sf" , " bbox" ))
125
+ check_extent <- function (extent ) {
126
+ multi_class_res <- checkmate :: check_multi_class(extent , c(" sf" , " bbox" ))
76
127
if (! isTRUE(multi_class_res )) return (multi_class_res )
77
128
78
- if (inherits(borders , " bbox" )) {
129
+ if (inherits(extent , " bbox" )) {
79
130
is_numeric_len_4 <- checkmate :: test_numeric(
80
- borders ,
131
+ extent ,
81
132
finite = TRUE ,
82
133
any.missing = FALSE ,
83
134
len = 4
84
135
)
85
136
86
137
is_correctly_named <- checkmate :: test_subset(
87
- names(borders ),
138
+ names(extent ),
88
139
choices = c(" xmin" , " ymin" , " xmax" , " ymax" )
89
140
)
90
141
@@ -97,7 +148,7 @@ check_borders <- function(borders) {
97
148
)
98
149
}
99
150
} else {
100
- if (nrow(borders ) > 1 ) {
151
+ if (nrow(extent ) > 1 ) {
101
152
return (
102
153
paste0(
103
154
" sf object must contain only one feature. Hint: try using " ,
@@ -106,7 +157,7 @@ check_borders <- function(borders) {
106
157
)
107
158
}
108
159
109
- geometry_type <- as.character(sf :: st_geometry_type(borders ))
160
+ geometry_type <- as.character(sf :: st_geometry_type(extent ))
110
161
if (! geometry_type %in% c(" POLYGON" , " MULTIPOLYGON" )) {
111
162
msg <- paste0(
112
163
" Geometry type of sf object must be either POLYGON or MULTIPOLYGON. " ,
@@ -128,4 +179,4 @@ check_borders <- function(borders) {
128
179
return (TRUE )
129
180
}
130
181
131
- assert_borders <- checkmate :: makeAssertionFunction(check_borders )
182
+ assert_extent <- checkmate :: makeAssertionFunction(check_extent )
0 commit comments