Skip to content

Commit c62c7fe

Browse files
fnattinocforgaci
andauthored
Test on real data (#46)
Co-authored-by: Claudiu Forgaci <[email protected]>
1 parent e7a4635 commit c62c7fe

File tree

5 files changed

+38
-14
lines changed

5 files changed

+38
-14
lines changed

NEWS.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
- DOI is added to CITATION.cff and README (badge)
1111
- Contributing guidelines are added to the package
12+
- A test on real (packaged) data is included
13+
14+
## Changed
15+
16+
- Data resized to city boundary (buffer is dropped)
1217

1318
## Fixed
1419

R/data.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
#'
33
#' Data extracted from OpenStreetMap for testing the rcoins package.
44
#'
5-
#' @source OpenStreetMap
5+
#' @source <https://www.openstreetmap.org/about>
66
"bucharest"

data-raw/bucharest.R

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,24 +51,16 @@ get_osm_river <- function(river_name, bb, crs) {
5151
river_centerline <- osmdata_as_sf("waterway", "river", bb)
5252
river_centerline <- river_centerline$osm_multilines |>
5353
filter(.data$name == river_name) |>
54+
st_filter(st_as_sfc(bb), .predicate = st_intersects) |>
5455
st_transform(crs) |>
5556
st_geometry()
5657

5758
return(river_centerline)
5859
}
5960

60-
get_osmdata <- function(city_name, river_name, crs, buffer = NULL) {
61+
get_osmdata <- function(city_name, river_name, crs) {
6162
bb <- get_osm_bb(city_name)
6263

63-
if (!is.null(buffer)) {
64-
bb <- bb |>
65-
st_as_sfc() |>
66-
st_transform(crs = crs) |>
67-
st_buffer(buffer) |>
68-
st_transform(crs = 4326) |>
69-
st_bbox()
70-
}
71-
7264
streets <- get_osm_streets(bb, crs)
7365
river <- get_osm_river(river_name, bb, crs)
7466

@@ -86,14 +78,12 @@ get_osmdata <- function(city_name, river_name, crs, buffer = NULL) {
8678
city_name <- "Bucharest"
8779
river_name <- "Dâmbovița"
8880
epsg_code <- 32635
89-
bbox_buffer <- 2000 # m
9081

9182
# Fetch the data
9283
bucharest <- get_osmdata(
9384
city_name,
9485
river_name,
95-
crs = epsg_code,
96-
buffer = bbox_buffer
86+
crs = epsg_code
9787
)
9888

9989
# Fix encoding issue in the WKT string of city boundary

data/bucharest.rda

-69.8 KB
Binary file not shown.

tests/testthat/test-stroke.R

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,32 @@ test_that("a ring is recognized when from_edge is specified", {
195195
actual <- stroke(sfc, from_edge = 1)
196196
expect_setequal(actual, expected)
197197
})
198+
199+
test_that("flow mode does not break edges on a real dataset", {
200+
edges <- sf::st_geometry(bucharest$streets)
201+
202+
strokes <- rcoins::stroke(edges, flow_mode = TRUE)
203+
204+
# find out which of the initial edges are contained in each of the strokes
205+
# NOTE: edges included in self-intersecting strokes can be missed by the
206+
# following command, if the test fails double check the input!
207+
contains <- sf::st_contains(strokes, edges)
208+
209+
# merge the groups of edges in (multi)linestrings
210+
merge_edges <- function(idx) {
211+
union <- sf::st_union(edges[idx])
212+
if (sf::st_geometry_type(union) == "LINESTRING") {
213+
return(union)
214+
} else {
215+
return(sf::st_line_merge(union))
216+
}
217+
}
218+
edges_merged <- sf::st_sfc(sapply(contains, merge_edges),
219+
crs = sf::st_crs(edges))
220+
221+
# compare the grouped edges to the strokes: if identical, this means that
222+
# the strokes contain full edges, i.e. flow_mode is respected
223+
# NOTE: element-wise comparison works even if "strokes" consists of only
224+
# linestrings, while "edges_merged" includes some multilinestrings
225+
expect_true(all(strokes == edges_merged))
226+
})

0 commit comments

Comments
 (0)