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

Implement segmentation as part of the package #41

Merged
merged 36 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
4a31bf9
add rcoins dependency
fnattino Nov 18, 2024
d936da4
add dbscan dependency
fnattino Nov 18, 2024
4b63d98
implement strokes function
fnattino Nov 18, 2024
fa6049a
first functions on segmentation
fnattino Nov 20, 2024
83780fa
Merge branch 'main' into 37-segmentation-fn
fnattino Nov 21, 2024
f208d0d
Merge branch 'main' into 37-segmentation-fn
fnattino Nov 21, 2024
865b801
fix rcoins syntax
fnattino Nov 28, 2024
cbc0ae3
generalize function
fnattino Nov 28, 2024
57aea19
include angle threshold as input arg
fnattino Nov 28, 2024
73697be
Add segment implementation
fnattino Dec 15, 2024
0ee2cd0
Merge branch 'main' into 37-segmentation-fn
fnattino Dec 15, 2024
1ff7d8d
fix typo
fnattino Dec 15, 2024
1ebf1ee
Merge branch '37-segmentation-fn' of github.com:CityRiverSpaces/CRiSp…
fnattino Dec 15, 2024
2065702
update man file
fnattino Dec 15, 2024
fb0c434
update unrelated data files
fnattino Dec 15, 2024
a27f63b
fix API
fnattino Dec 17, 2024
254036a
fix docstring
fnattino Dec 17, 2024
746ff4d
update vignette
fnattino Dec 17, 2024
c9431e9
add segments tests
fnattino Dec 18, 2024
f91b398
remove strokes interface to rcoins
fnattino Dec 18, 2024
c476d1e
Update R/segments.R
fnattino Dec 19, 2024
8b70481
how -> method
fnattino Dec 19, 2024
760b0bb
Update R/segments.R
fnattino Dec 19, 2024
146b83e
reorder auxiliary functions according to call order
fnattino Dec 19, 2024
40d6bc6
idx -> index
fnattino Dec 19, 2024
7ed97f0
Update vignettes/corridor-segmentation.Rmd
fnattino Dec 19, 2024
20aa494
Update vignettes/corridor-segmentation.Rmd
fnattino Dec 19, 2024
eb2f637
Update vignettes/corridor-segmentation.Rmd
fnattino Dec 19, 2024
8bf643d
corridor will be added to bucharest_delineation packaged data
fnattino Dec 19, 2024
da626af
reorder according to call order
fnattino Dec 19, 2024
7f77b1e
Merge branch '37-segmentation-fn' of github.com:CityRiverSpaces/CRiSp…
fnattino Dec 19, 2024
f8619c6
small refactoring
fnattino Dec 19, 2024
51ff182
fix tests segments
fnattino Dec 19, 2024
19f5585
update docs
fnattino Dec 19, 2024
5b6f9e5
fix link in docstring
fnattino Dec 19, 2024
18d53e4
fix lint issues
fnattino Dec 19, 2024
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
12 changes: 8 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ Description: CRiSp (City River Spaces) provides tools to automate the
License: Apache License (>= 2)
URL: https://cityriverspaces.github.io/CRiSp/
BugReports: https://github.com/CityRiverSpaces/crisp/issues
Depends:
Depends:
R (>= 2.10)
Imports:
Imports:
dbscan,
dplyr,
lwgeom,
osmdata,
rcoins,
rlang,
rstac,
sf,
Expand All @@ -29,17 +31,19 @@ Imports:
tibble,
tidygraph,
units
Suggests:
Suggests:
ggplot2,
gridExtra,
knitr,
purrr,
rmarkdown,
testthat (>= 3.0.0)
VignetteBuilder:
VignetteBuilder:
knitr
Config/testthat/edition: 3
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.2
Remotes:
github::CityRiverSpaces/rcoins
4 changes: 2 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ export(add_weights)
export(as_bbox)
export(as_network)
export(clean_network)
export(corridor)
export(delineate_corridor)
export(delineate_riverspace)
export(delineate_segments)
export(dem_to_cog)
export(flatten_network)
export(get_cd_char)
export(get_corridor)
export(get_cost_distance)
export(get_dem)
export(get_osm_bb)
Expand All @@ -19,6 +18,7 @@ export(get_osm_railways)
export(get_osm_river)
export(get_osm_streets)
export(get_osmdata)
export(get_segments)
export(get_slope)
export(get_stac_asset_urls)
export(get_utm_zone)
Expand Down
14 changes: 11 additions & 3 deletions R/corridor.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#'
#' @return A simple feature geometry representing the river corridor
#' @export
corridor <- function(
get_corridor <- function(
network, river_centerline, river_surface, bbox, initial_method = "buffer",
capping_method = "direct"
) {
Expand Down Expand Up @@ -133,11 +133,19 @@ split_aoi <- function(bbox, river) {
#'
#' @param geometry Geometry to split
#' @param line Dividing (multi)linestring
#' @param boundary Whether to return the split boundary instead of the regions
#'
#' @return A simple feature object
split <- function(geometry, line) {
lwgeom::st_split(geometry, line) |>
split <- function(geometry, line, boundary = FALSE) {
regions <- lwgeom::st_split(geometry, line) |>
sf::st_collection_extract()
if (!boundary) {
return(regions)
} else {
boundaries <- sf::st_boundary(regions)
split_boundary <- sf::st_difference(boundaries, line)
return(split_boundary)
}
}

#' Identify the initial edges of the river corridor
Expand Down
28 changes: 15 additions & 13 deletions R/delineate.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@
#' @param capping_method The method employed to connect the corridor edge end
#' points (i.e. to "cap" the corridor). See [cap_corridor()] for
#' the available methods
#' @param angle_threshold Only network edges forming angles above this threshold
#' (in degrees) are considered when forming segment edges. See
#' `[get_segments()]` and [strokes()]. Only used if `segments` is TRUE.
#' @param segments Whether to carry out the corridor segmentation
#' @param riverspace Whether to carry out the riverspace delineation
#'
#' @return A simple feature geometry
#' @export
delineate_corridor <- function(
city_name, river_name, crs = NULL, bbox_buffer = NULL,
initial_method = "buffer", capping_method = "direct", segments = FALSE,
riverspace = FALSE
initial_method = "buffer", capping_method = "direct", angle_threshold = 90,
segments = FALSE, riverspace = FALSE
) {
# Retrieve all relevant OSM datasets using the extended bounding box
osm_data <- get_osmdata(city_name, river_name, crs, bbox_buffer)
Expand All @@ -32,25 +35,24 @@ delineate_corridor <- function(
network <- as_network(network_edges)

# Run the corridor delineation on the spatial network
corridor <- corridor(
corridor <- get_corridor(
network, osm_data$river_centerline, osm_data$river_surface, bbox,
initial_method, capping_method
)

if (segments) delineate_segments()
if (segments) {
# Select the relevant part of the network
buffer_corridor <- 100 # TODO should this be an additional input parameter?
corridor_buffer <- sf::st_buffer(corridor, buffer_corridor)
network_filtered <- filter_network(network, corridor_buffer)

corridor <- get_segments(corridor, network_filtered,
osm_data$river_centerline, angle_threshold)
}
if (riverspace) delineate_riverspace()
return(corridor)
}


fnattino marked this conversation as resolved.
Show resolved Hide resolved
#' Delineate segments of a river corridor.
#'
#' @return A simple feature geometry
#' @export
delineate_segments <- function() {
stop("Segmentation not yet implemented.")
}

#' Delinate the riverspace.
#'
#' @return A simple feature geometry
Expand Down
24 changes: 22 additions & 2 deletions R/network.R
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,26 @@ filter_network <- function(network, target) {
tidygraph::filter(sfnetworks::node_intersects(target))
}

strokes <- function() {
stop("`strokes` not yet implemented.")
#' Identify naturally continuos lines in a spatial network.
#'
#' Extend the specified network edges along the spatial network to form longer
#' strokes. See [`rcoins::stroke()`] for more details.
#'
#' @param network A spatial network object
#' @param from_edge The indices of the network edges to be continued
#' @param angle_threshold Consecutive line segments are considered part of the
#' same continuous line if the internal angle they form is larger than
#' `angle_threshold`` (in degrees). Should be in the range [0, 180) degrees
#' @param flow_mode If TRUE, line segments that belong to the same edge are not
#' split across strokes (even if they form internal angles smaller than
#' `angle_threshold`).
#'
#' @return Stroke geometries as a simple feature object
strokes <- function(
network, from_edge, angle_threshold = 0., flow_mode = FALSE
) {
rcoins::stroke(
network, angle_threshold = angle_threshold, attributes = FALSE,
flow_mode = flow_mode, from_edge = from_edge
)
}
Loading
Loading