Skip to content

Commit

Permalink
Update functions and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
cforgaci committed Jun 17, 2024
1 parent 91edd01 commit 92e9f2f
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 27 deletions.
31 changes: 21 additions & 10 deletions R/delineate-corridor.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
#'
#' @return An area of interest as a simple feature geometry
#' @export
define_aoi <- function(bb, crs) {
define_aoi <- function(bb, crs, buffer_dist = 0) {
bbox <- as.vector(bb)
names(bbox) <- c("xmin", "ymin", "xmax", "ymax")
aoi <- sf::st_bbox(bbox, crs = sf::st_crs(4326)) |>
sf::st_as_sfc() |>
sf::st_transform(crs)

if (buffer_dist != 0) aoi <- sf::st_buffer(aoi, buffer_dist) else aoi
}

#' Split the area of interest (AoI) by a river.
Expand Down Expand Up @@ -133,6 +135,23 @@ get_corridor_edge <- function(net, area, vertices){
edges[edge_path]
}

cap_corridor <- function(edges, cap = "city", crs = NULL) {
if (cap == "city") {
cap <- CRiSp::osmdata_as_sf("place", "city", bb)
cap <- cap$osm_multipolygons |>
sf::st_geometry()
} else {
cap <- cap
}
if (!is.null(crs)) cap <- cap |> sf::st_transform(crs)

capped_corridor <- cap |>
lwgeom::st_split(corridor_edges) |>
sf::st_collection_extract("POLYGON") |>
sf::st_as_sf() |>
sf::st_filter(river, .predicate = sf::st_intersects)
}

#' Delineate a corridor around a river.
#'
#' @param place A place name as a string
Expand All @@ -143,10 +162,6 @@ get_corridor_edge <- function(net, area, vertices){
#' @export
delineate_corridor <- function(place, river, crs = NULL) {
bb <- CRiSp::osm_bb(place)
cap <- CRiSp::osmdata_as_sf("place", "city", bb)
cap <- cap$osm_multipolygons |>
sf::st_geometry()
if (!is.null(crs)) cap <- cap |> sf::st_transform(crs)

highways_value <- c("motorway", "primary", "secondary", "tertiary")
net <- CRiSp::osmdata_as_sf("highway", highways_value, bb) |>
Expand All @@ -161,9 +176,5 @@ delineate_corridor <- function(place, river, crs = NULL) {
corridor_edge_2 <- CRiSp::get_corridor_edge(net, areas[2], vertices)
corridor_edges <- sf::st_union(corridor_edge_1, corridor_edge_2)

corridor <- cap |>
lwgeom::st_split(corridor_edges) |>
sf::st_collection_extract("POLYGON") |>
sf::st_as_sf() |>
sf::st_filter(river, .predicate = sf::st_intersects)
cap_corridor(corridor_edges, cap)
}
1 change: 0 additions & 1 deletion R/preprocess.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ merge_streets <- function(highways) {
#' @export
create_network <- function(data, crs = NULL) {
net <- data |>
dplyr::select("highway") |>
sfnetworks::as_sfnetwork(directed = FALSE)

if (!is.null(crs)) {
Expand Down
10 changes: 6 additions & 4 deletions R/retreive-osm.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ osm_bb <- function(name) {

#' Retrieve OpenStreetMap data as sf object
#'
#' Query the Overpass API for a key:value pair within a given 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
#' @param bb A list with the bounding box
Expand Down Expand Up @@ -51,18 +53,18 @@ get_osmdata <- function(name, key, value) {
#'
#' @return An sf object with the river corridor
#' @export
get_osmdata_river_corridor <- function(name, river_name, epsg_code, buffer) {
get_osmdata_river_corridor <- function(city_name, river_name, epsg_code, buffer_dist) {
key = "waterway"
value = "river"
waterways <- CRiSp::get_osmdata(name, key, value)
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"
water <- CRiSp::get_osmdata(name, key, value)
water <- CRiSp::get_osmdata(city_name, key, value)

waterbody <- dplyr::bind_rows(water$osm_polygons, water$osm_multipolygons) |>
sf::st_transform(epsg_code) |>
Expand All @@ -71,6 +73,6 @@ get_osmdata_river_corridor <- function(name, river_name, epsg_code, buffer) {
sf::st_union()

corridor_initial <- c(waterway, waterbody) |>
sf::st_buffer(buffer) |>
sf::st_buffer(buffer_dist) |>
sf::st_union()
}
8 changes: 5 additions & 3 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#' Get the latitude and longitude of an sf object
#' Add latitude and longitude information to an sf object
#'
#' @param x Object of sf class sf, sfc or sfg
#' Add latitude and longitude information to an sf object (WGS84)
#'
#' @return An sf object with the latitude and longitude
#' @param x Object of class sf, sfc or sfg
#'
#' @return An sf object with latitude and longitude
#' @export
get_latlon <- function(x) {
sf::st_transform(x, 4326) |>
Expand Down
2 changes: 1 addition & 1 deletion man/define_aoi.Rd

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

8 changes: 4 additions & 4 deletions man/get_latlon.Rd

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

6 changes: 3 additions & 3 deletions man/get_osmdata_river_corridor.Rd

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

2 changes: 1 addition & 1 deletion man/osmdata_as_sf.Rd

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

0 comments on commit 92e9f2f

Please sign in to comment.