Skip to content

Commit 3be72f3

Browse files
committed
extract() documentation and tests
1 parent 1e94c8d commit 3be72f3

File tree

4 files changed

+175
-24
lines changed

4 files changed

+175
-24
lines changed

NAMESPACE

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
# Generated by roxygen2: do not edit by hand
22

33
export(assert_osmium_is_installed)
4+
export(extract)

R/extract.R

+63-12
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,60 @@
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
152
extract <- function(input_path,
2-
borders,
53+
extent,
354
output_path,
455
overwrite = FALSE,
5-
echo = TRUE,
656
echo_cmd = FALSE,
57+
echo = TRUE,
758
spinner = TRUE) {
859
assert_osmium_is_installed()
960

@@ -12,10 +63,10 @@ extract <- function(input_path,
1263
checkmate::assert_logical(echo, any.missing = FALSE, len = 1)
1364
checkmate::assert_logical(echo_cmd, any.missing = FALSE, len = 1)
1465
checkmate::assert_logical(spinner, any.missing = FALSE, len = 1)
15-
assert_borders(borders)
66+
assert_extent(extent)
1667
assert_output_path_multi_ext(output_path, overwrite)
1768

18-
border_arg <- create_border_input(borders)
69+
border_arg <- create_border_input(extent)
1970
output_arg <- paste0("--output=", output_path)
2071
overwrite_arg <- if (overwrite) "--overwrite" else character()
2172

@@ -71,20 +122,20 @@ border_input_from_polygon <- function(x) {
71122
return(input)
72123
}
73124

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"))
76127
if (!isTRUE(multi_class_res)) return(multi_class_res)
77128

78-
if (inherits(borders, "bbox")) {
129+
if (inherits(extent, "bbox")) {
79130
is_numeric_len_4 <- checkmate::test_numeric(
80-
borders,
131+
extent,
81132
finite = TRUE,
82133
any.missing = FALSE,
83134
len = 4
84135
)
85136

86137
is_correctly_named <- checkmate::test_subset(
87-
names(borders),
138+
names(extent),
88139
choices = c("xmin", "ymin", "xmax", "ymax")
89140
)
90141

@@ -97,7 +148,7 @@ check_borders <- function(borders) {
97148
)
98149
}
99150
} else {
100-
if (nrow(borders) > 1) {
151+
if (nrow(extent) > 1) {
101152
return(
102153
paste0(
103154
"sf object must contain only one feature. Hint: try using ",
@@ -106,7 +157,7 @@ check_borders <- function(borders) {
106157
)
107158
}
108159

109-
geometry_type <- as.character(sf::st_geometry_type(borders))
160+
geometry_type <- as.character(sf::st_geometry_type(extent))
110161
if (! geometry_type %in% c("POLYGON", "MULTIPOLYGON")) {
111162
msg <- paste0(
112163
"Geometry type of sf object must be either POLYGON or MULTIPOLYGON. ",
@@ -128,4 +179,4 @@ check_borders <- function(borders) {
128179
return(TRUE)
129180
}
130181

131-
assert_borders <- checkmate::makeAssertionFunction(check_borders)
182+
assert_extent <- checkmate::makeAssertionFunction(check_extent)

man/extract.Rd

+75
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-extract.R

+36-12
Original file line numberDiff line numberDiff line change
@@ -13,36 +13,36 @@ geomcollection <- sf::st_as_sf(
1313
)
1414

1515
tester <- function(input_path = pbf_path,
16-
borders = smaller_bbox_poly,
16+
extent = smaller_bbox_poly,
1717
output_path = tempfile(fileext = ".osm.pbf"),
1818
overwrite = FALSE,
19-
echo = TRUE,
2019
echo_cmd = FALSE,
20+
echo = TRUE,
2121
spinner = TRUE) {
22-
extract(input_path, borders, output_path, overwrite, echo, echo_cmd, spinner)
22+
extract(input_path, extent, output_path, overwrite, echo_cmd, echo, spinner)
2323
}
2424

2525
test_that("input should be correct", {
2626
expect_error(tester(1))
2727
expect_error(tester("a.osm.pbf"))
2828

29-
expect_error(tester(borders = unclass(bbox)))
29+
expect_error(tester(extent = unclass(bbox)))
3030

3131
bad_bbox <- bbox
3232
bad_bbox[4] <- Inf
33-
expect_error(tester(borders = bad_bbox))
33+
expect_error(tester(extent = bad_bbox))
3434
bad_bbox[4] <- NA
35-
expect_error(tester(borders = bad_bbox))
35+
expect_error(tester(extent = bad_bbox))
3636
bad_bbox[4] <- 25
3737
names(bad_bbox)[4] <- "oi"
38-
expect_error(tester(borders = bad_bbox))
38+
expect_error(tester(extent = bad_bbox))
3939
names(bad_bbox)[4] <- "ymax"
4040
bad_bbox[5] <- 12
41-
expect_error(tester(borders = bad_bbox))
41+
expect_error(tester(extent = bad_bbox))
4242

43-
expect_error(tester(borders = rbind(bbox_polygon, bbox_polygon)))
44-
expect_error(tester(borders = linestring))
45-
expect_error(tester(borders = geomcollection))
43+
expect_error(tester(extent = rbind(bbox_polygon, bbox_polygon)))
44+
expect_error(tester(extent = linestring))
45+
expect_error(tester(extent = geomcollection))
4646

4747
tmpfile <- tempfile(fileext = ".osm.pbf")
4848
file.create(tmpfile)
@@ -79,9 +79,33 @@ test_that("works with bbox and results in same output as with equiv poly", {
7979
tmpfile <- tempfile(fileext = ".osm.pbf")
8080
smaller_bbox <- sf::st_bbox(smaller_bbox_poly)
8181

82-
result <- tester(borders = smaller_bbox, output_path = tmpfile)
82+
result <- tester(extent = smaller_bbox, output_path = tmpfile)
8383
expect_identical(result, normalizePath(tmpfile))
8484

8585
# same snapshot as the above test, which was generated with the polygon
8686
expect_snapshot_file(tmpfile, name = "tester_default_output")
8787
})
88+
89+
test_that("overwrite arguments works", {
90+
tmpfile <- tempfile(fileext = ".osm.pbf")
91+
92+
result <- tester(output_path = tmpfile)
93+
94+
expect_error(tester(output_path = tmpfile, overwrite = FALSE))
95+
expect_no_error(tester(output_path = tmpfile, overwrite = TRUE))
96+
})
97+
98+
test_that("echo_cmd argument works", {
99+
# using spinner = FALSE to make sure it doesn't mess up with the test
100+
expect_output(
101+
a <- tester(echo_cmd = TRUE, spinner = FALSE),
102+
regexp = "^Running osmium extract"
103+
)
104+
105+
output <- capture.output(a <- tester(echo_cmd = FALSE, spinner = FALSE))
106+
expect_identical(output, character(0))
107+
})
108+
109+
# spinner doesn't work on non-interactive sessions and none of the queries calls
110+
# we have generated output anything to stdout/stderr, so we're skipping the
111+
# 'spinner' and 'echo' argument tests

0 commit comments

Comments
 (0)