Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test on real data #46

Merged
merged 6 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- DOI is added to CITATION.cff and README (badge)
- Contributing guidelines are added to the package
- A test on real (packaged) data is included

## Changed

- Data resized to city boundary (buffer is dropped)

## Fixed

Expand Down
2 changes: 1 addition & 1 deletion R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
#'
#' Data extracted from OpenStreetMap for testing the rcoins package.
#'
#' @source OpenStreetMap
#' @source <https://www.openstreetmap.org/about>
"bucharest"
16 changes: 3 additions & 13 deletions data-raw/bucharest.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,16 @@ get_osm_river <- function(river_name, bb, crs) {
river_centerline <- osmdata_as_sf("waterway", "river", bb)
river_centerline <- river_centerline$osm_multilines |>
filter(.data$name == river_name) |>
st_filter(sf::st_as_sfc(bb), .predicate = sf::st_intersects) |>
fnattino marked this conversation as resolved.
Show resolved Hide resolved
fnattino marked this conversation as resolved.
Show resolved Hide resolved
st_transform(crs) |>
st_geometry()

return(river_centerline)
}

get_osmdata <- function(city_name, river_name, crs, buffer = NULL) {
get_osmdata <- function(city_name, river_name, crs) {
bb <- get_osm_bb(city_name)

if (!is.null(buffer)) {
bb <- bb |>
st_as_sfc() |>
st_transform(crs = crs) |>
st_buffer(buffer) |>
st_transform(crs = 4326) |>
st_bbox()
}

streets <- get_osm_streets(bb, crs)
river <- get_osm_river(river_name, bb, crs)

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

# Fetch the data
bucharest <- get_osmdata(
city_name,
river_name,
crs = epsg_code,
buffer = bbox_buffer
crs = epsg_code
)

# Fix encoding issue in the WKT string of city boundary
Expand Down
Binary file modified data/bucharest.rda
Binary file not shown.
29 changes: 29 additions & 0 deletions tests/testthat/test-stroke.R
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,32 @@ test_that("a ring is recognized when from_edge is specified", {
actual <- stroke(sfc, from_edge = 1)
expect_setequal(actual, expected)
})

test_that("flow mode does not break edges on a real dataset", {
edges <- sf::st_geometry(bucharest$streets)

strokes <- rcoins::stroke(edges, flow_mode = TRUE)

# find out which of the initial edges are contained in each of the strokes
# NOTE: edges included in self-interesecting strokes can be missed by the
fnattino marked this conversation as resolved.
Show resolved Hide resolved
# following command, if the test fails double check the input!
contains <- sf::st_contains(strokes, edges)

# merge the groups of edges in (multi)linestrings
merge_edges <- function(idx) {
union <- sf::st_union(edges[idx])
if (sf::st_geometry_type(union) == "LINESTRING") {
return(union)
} else {
return(sf::st_line_merge(union))
}
}
edges_merged <- sf::st_sfc(sapply(contains, merge_edges),
crs = sf::st_crs(edges))

# compare the grouped edges to the strokes: if identical, this means that
# the strokes contain full edges, i.e. flow_mode is respected
# NOTE: elemet-wise comparison works even if "strokes" consists of only
fnattino marked this conversation as resolved.
Show resolved Hide resolved
# linestrings, while "edges_merged" includes some multilinestrings
expect_true(all(strokes == edges_merged))
})
Loading