Skip to content

Commit

Permalink
Lint package
Browse files Browse the repository at this point in the history
  • Loading branch information
cforgaci committed Jun 29, 2024
1 parent d36fcc4 commit 9617495
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 34 deletions.
25 changes: 16 additions & 9 deletions R/delineate-corridor.R
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()
}
Expand All @@ -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) |>
Expand All @@ -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
Expand Down Expand Up @@ -101,26 +102,29 @@ 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
#' @param vertices A simple feature geometry set of two points
#'
#' @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(
Expand All @@ -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
Expand All @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion R/preprocess.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 21 additions & 11 deletions R/retreive-osm.R
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#' 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
#'
#' @examples
#' osm_bb("Bucharest")
osm_bb <- function(name) {
bb <- osmdata::getbb(name)
osmdata::getbb(name)
}

#' Retrieve OpenStreetMap data as sf object
Expand All @@ -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
Expand All @@ -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) |>
Expand All @@ -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
}
5 changes: 3 additions & 2 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
3 changes: 2 additions & 1 deletion man/calc_weights.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/cap_corridor.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/create_network.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions man/define_aoi.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/get_corridor_edge.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions man/get_osmdata.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions man/get_osmdata_river_corridor.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/osm_bb.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9617495

Please sign in to comment.