diff --git a/R/delineate-corridor.R b/R/delineate-corridor.R index a2418ba..31c380a 100644 --- a/R/delineate-corridor.R +++ b/R/delineate-corridor.R @@ -1,4 +1,5 @@ -#' Define an area of interest (AoI) from a bounding box and a coordinate reference system (CRS). +#' Define an area of interest (AoI) from a bounding box and a +#' coordinate reference system (CRS). #' #' @param bb A bounding box as a matrix with 4 elements: xmin, ymin, xmax, ymax #' @param crs A coordinate reference system as an epsg code, e.g. 4326 for WGS84 @@ -24,7 +25,7 @@ define_aoi <- function(bb, crs, buffer_dist = 0) { #' @return A simple feature geometry set of two areas of interest #' @export split_aoi <- function(aoi, river) { - areas <- aoi |> + aoi |> lwgeom::st_split(river) |> sf::st_collection_extract() } @@ -37,7 +38,7 @@ split_aoi <- function(aoi, river) { #' #' @return A network object #' @export -trim_network <- function(net, area, river_corridor){ +trim_network <- function(net, area, river_corridor) { net |> sfnetworks::activate("nodes") |> sf::st_filter(area, .predicate = sf::st_intersects) |> @@ -51,7 +52,7 @@ trim_network <- function(net, area, river_corridor){ #' @return A simplifed network object #' @export simplify_network <- function(net) { - net|> + net |> sfnetworks::activate("edges") |> # TODO incorporate this comment in the function description # reorder the edges so that the shortest is kept @@ -101,18 +102,21 @@ get_vertices <- function(aoi, corridor_initial) { #' #' @return A simple feature geometry set of two points #' @export -get_target_points <- function(vertices, area, threshold = 0.001){ +get_target_points <- function(vertices, area, threshold = 0.001) { vertices |> sf::st_as_sf() |> # TODO incorporate this comment into the function description # keep threshold to check which points intersect the polygons - sf::st_filter(area, .predicate = sf::st_is_within_distance, dist = threshold) |> + sf::st_filter(area, + .predicate = sf::st_is_within_distance, + dist = threshold) |> sf::st_geometry() } #' Determine the corridor edge on the network. #' -#' Find the corridor edge on one side of the river by using a shortest path algorithm. +#' Find the corridor edge on one side of the river by using a +#' shortest path algorithm. #' #' @param net A network object #' @param area An area of interest as a simple feature @@ -120,7 +124,7 @@ get_target_points <- function(vertices, area, threshold = 0.001){ #' #' @return A simple feature geometry #' @export -get_corridor_edge <- function(net, area, vertices){ +get_corridor_edge <- function(net, area, vertices) { target_points <- CRiSp::get_target_points(vertices, area) paths <- sfnetworks::st_network_paths( @@ -142,7 +146,8 @@ get_corridor_edge <- function(net, area, vertices){ #' @param river River centerline as a simple feature #' @param crs A coordinate reference system as an epsg code, e.g. 4326 for WGS84 #' @param bb A bounding box as a matrix with 4 elements: xmin, ymin, xmax, ymax -#' @param cap Character string with the type of cap to be used. Default is "city" +#' @param cap Character string with the type of cap to be used. +#' Default is "city" #' #' @return A simple feature geometry #' @export @@ -162,6 +167,8 @@ cap_corridor <- function(corridor_edges, river, crs, bb, cap = "city") { sf::st_collection_extract("POLYGON") |> sf::st_as_sf() |> sf::st_filter(river, .predicate = sf::st_intersects) + + capped_corridor } #' Delineate a corridor around a river. diff --git a/R/preprocess.R b/R/preprocess.R index b50deb1..7748038 100644 --- a/R/preprocess.R +++ b/R/preprocess.R @@ -10,11 +10,13 @@ merge_streets <- function(highways) { sf::st_cast("LINESTRING") highways_lines <- highways$osm_lines |> dplyr::bind_rows(poly_to_lines) + highways_lines } #' Create a network from a line strings #' -#' @param data A data frame with a column named 'highway' containing line strings +#' @param data A data frame with a column named 'highway' containing +#' line strings #' @param crs A coordinate reference system as an epsg code, e.g. 4326 for WGS84 #' #' @return A network object diff --git a/R/retreive-osm.R b/R/retreive-osm.R index 3d22c00..5e47ad0 100644 --- a/R/retreive-osm.R +++ b/R/retreive-osm.R @@ -1,6 +1,7 @@ #' Retrieve bounding box from OpenStreetMap #' -#' @param name A character string with the name of the place to retrieve the bounding box +#' @param name A character string with the name of the place to retrieve +#' the bounding box #' #' @return A list with the bounding box #' @export @@ -8,7 +9,7 @@ #' @examples #' osm_bb("Bucharest") osm_bb <- function(name) { - bb <- osmdata::getbb(name) + osmdata::getbb(name) } #' Retrieve OpenStreetMap data as sf object @@ -30,11 +31,13 @@ osmdata_as_sf <- function(key, value, bb) { #' Retrieve OpenStreetMap data for a given location #' -#' @param name A character string with the name of the place to retrieve the bounding box +#' @param name A character string with the name of the place to retrieve +#' the bounding box #' @param key A character string with the key to filter the data #' @param value A character string with the value to filter the data #' -#' @return An sf object with the retrieved OpenStreetMap data for the given location +#' @return An sf object with the retrieved OpenStreetMap data for the +#' given location #' @export #' #' @examples @@ -47,24 +50,29 @@ get_osmdata <- function(name, key, value) { name <- NULL #' Get OpenStreetMap data for a river corridor #' -#' @param city_name A character string with the name of the place to retrieve the bounding box +#' @param city_name A character string with the name of the place to retrieve +#' the bounding box #' @param river_name A character string with the name of the river #' @param epsg_code An integer with the EPSG code for the projection -#' @param buffer_dist A numeric with the buffer distance in meters from the water stream +#' @param buffer_dist A numeric with the buffer distance in meters from the +#' water stream #' #' @return An sf object with the river corridor #' @export -get_osmdata_river_corridor <- function(city_name, river_name, epsg_code, buffer_dist) { - key = "waterway" - value = "river" +get_osmdata_river_corridor <- function(city_name, + river_name, + epsg_code, + buffer_dist) { + key <- "waterway" + value <- "river" waterways <- CRiSp::get_osmdata(city_name, key, value) waterway <- waterways$osm_multilines |> dplyr::filter(name == river_name) |> sf::st_transform(epsg_code) |> sf::st_geometry() - key = "natural" - value = "water" + key <- "natural" + value <- "water" water <- CRiSp::get_osmdata(city_name, key, value) waterbody <- dplyr::bind_rows(water$osm_polygons, water$osm_multipolygons) |> @@ -76,4 +84,6 @@ get_osmdata_river_corridor <- function(city_name, river_name, epsg_code, buffer_ corridor_initial <- c(waterway, waterbody) |> sf::st_buffer(buffer_dist) |> sf::st_union() + + corridor_initial } diff --git a/R/utils.R b/R/utils.R index ff882f9..4c326aa 100644 --- a/R/utils.R +++ b/R/utils.R @@ -26,9 +26,10 @@ not_intersects <- function(x, y) { #' #' @param net A network object #' -#' @return A network object with a new column 'weight' containing the length of the edges +#' @return A network object with a new column 'weight' containing the length of +#' the edges #' @export -calc_weights <- function(net){ +calc_weights <- function(net) { net |> sfnetworks::activate("edges") |> dplyr::mutate(weight = sfnetworks::edge_length()) diff --git a/man/calc_weights.Rd b/man/calc_weights.Rd index 4fa5930..368682d 100644 --- a/man/calc_weights.Rd +++ b/man/calc_weights.Rd @@ -10,7 +10,8 @@ calc_weights(net) \item{net}{A network object} } \value{ -A network object with a new column 'weight' containing the length of the edges +A network object with a new column 'weight' containing the length of +the edges } \description{ Calculate the weights of the edges of a network based on their length diff --git a/man/cap_corridor.Rd b/man/cap_corridor.Rd index fa1428e..2447d5f 100644 --- a/man/cap_corridor.Rd +++ b/man/cap_corridor.Rd @@ -15,7 +15,8 @@ cap_corridor(corridor_edges, river, crs, bb, cap = "city") \item{bb}{A bounding box as a matrix with 4 elements: xmin, ymin, xmax, ymax} -\item{cap}{Character string with the type of cap to be used. Default is "city"} +\item{cap}{Character string with the type of cap to be used. +Default is "city"} } \value{ A simple feature geometry diff --git a/man/create_network.Rd b/man/create_network.Rd index c14c749..c8eee5c 100644 --- a/man/create_network.Rd +++ b/man/create_network.Rd @@ -7,7 +7,8 @@ create_network(data, crs = NULL) } \arguments{ -\item{data}{A data frame with a column named 'highway' containing line strings} +\item{data}{A data frame with a column named 'highway' containing +line strings} \item{crs}{A coordinate reference system as an epsg code, e.g. 4326 for WGS84} } diff --git a/man/define_aoi.Rd b/man/define_aoi.Rd index 4088fd8..8448697 100644 --- a/man/define_aoi.Rd +++ b/man/define_aoi.Rd @@ -2,7 +2,8 @@ % Please edit documentation in R/delineate-corridor.R \name{define_aoi} \alias{define_aoi} -\title{Define an area of interest (AoI) from a bounding box and a coordinate reference system (CRS).} +\title{Define an area of interest (AoI) from a bounding box and a +coordinate reference system (CRS).} \usage{ define_aoi(bb, crs, buffer_dist = 0) } @@ -17,5 +18,6 @@ define_aoi(bb, crs, buffer_dist = 0) An area of interest as a simple feature geometry } \description{ -Define an area of interest (AoI) from a bounding box and a coordinate reference system (CRS). +Define an area of interest (AoI) from a bounding box and a +coordinate reference system (CRS). } diff --git a/man/get_corridor_edge.Rd b/man/get_corridor_edge.Rd index ee34648..b7d8968 100644 --- a/man/get_corridor_edge.Rd +++ b/man/get_corridor_edge.Rd @@ -17,5 +17,6 @@ get_corridor_edge(net, area, vertices) A simple feature geometry } \description{ -Find the corridor edge on one side of the river by using a shortest path algorithm. +Find the corridor edge on one side of the river by using a +shortest path algorithm. } diff --git a/man/get_osmdata.Rd b/man/get_osmdata.Rd index 17fd662..2e0d8aa 100644 --- a/man/get_osmdata.Rd +++ b/man/get_osmdata.Rd @@ -7,14 +7,16 @@ get_osmdata(name, key, value) } \arguments{ -\item{name}{A character string with the name of the place to retrieve the bounding box} +\item{name}{A character string with the name of the place to retrieve +the bounding box} \item{key}{A character string with the key to filter the data} \item{value}{A character string with the value to filter the data} } \value{ -An sf object with the retrieved OpenStreetMap data for the given location +An sf object with the retrieved OpenStreetMap data for the +given location } \description{ Retrieve OpenStreetMap data for a given location diff --git a/man/get_osmdata_river_corridor.Rd b/man/get_osmdata_river_corridor.Rd index 89ddc5f..e62e9ff 100644 --- a/man/get_osmdata_river_corridor.Rd +++ b/man/get_osmdata_river_corridor.Rd @@ -7,13 +7,15 @@ get_osmdata_river_corridor(city_name, river_name, epsg_code, buffer_dist) } \arguments{ -\item{city_name}{A character string with the name of the place to retrieve the bounding box} +\item{city_name}{A character string with the name of the place to retrieve +the bounding box} \item{river_name}{A character string with the name of the river} \item{epsg_code}{An integer with the EPSG code for the projection} -\item{buffer_dist}{A numeric with the buffer distance in meters from the water stream} +\item{buffer_dist}{A numeric with the buffer distance in meters from the +water stream} } \value{ An sf object with the river corridor diff --git a/man/osm_bb.Rd b/man/osm_bb.Rd index 5282a43..6e55c40 100644 --- a/man/osm_bb.Rd +++ b/man/osm_bb.Rd @@ -7,7 +7,8 @@ osm_bb(name) } \arguments{ -\item{name}{A character string with the name of the place to retrieve the bounding box} +\item{name}{A character string with the name of the place to retrieve +the bounding box} } \value{ A list with the bounding box