From eeecab4f509ce3abb496bb3754c2ab5f95b22683 Mon Sep 17 00:00:00 2001 From: Anirudh Govind Date: Fri, 19 Apr 2024 15:51:59 +0200 Subject: [PATCH] edit: clean up functions --- R/st_explode.R | 55 +------------------------- R/st_nest_coordinates.R | 3 -- R/st_orientation.R | 16 +++++--- man/st_orientation.Rd | 4 +- vignettes/articles/1-Spatial-Units.Rmd | 7 ++-- 5 files changed, 19 insertions(+), 66 deletions(-) diff --git a/R/st_explode.R b/R/st_explode.R index c8fb103..153b4e4 100644 --- a/R/st_explode.R +++ b/R/st_explode.R @@ -11,64 +11,13 @@ #' exploded_geom st_explode <- function(x) { - # Housekeeping: Bind values to col names used further on. - - id <- x1 <- y1 <- NULL - - # Check types and convert to linestrings. - - x_lines <- process_linestrings(x) - # Keep track of CRS x_crs <- sf::st_crs(x) - # Get coordinates - - x_coords <- as.data.frame(sf::st_coordinates(x_lines)) - - # Set colnames - - colnames(x_coords) <- c("x1", "y1", "id") - - # Group by id. Mutate x2 and y2 as lead values. - # This should result in a number of NAs. - - # Add x2 and y2 - - x_coords_grouped <- x_coords |> - dplyr::group_by(id) |> - dplyr::mutate(x2 = dplyr::lead(x1, - n = 1, - order_by = id), - y2 = dplyr::lead(y1, - n = 1, - order_by = id)) - - # Ungroup - - x_coords <- dplyr::ungroup(x_coords_grouped) - - # Drop missing values - - x_coords <- stats::na.omit(x_coords) - - # Create linestrings + x_coords <- st_unnest_coordinates(x) - x_expl_lines <- - sf::st_sf(data.frame(geometry = sf::st_as_sfc( - paste0( - "LINESTRING (", - x_coords$x1, - " ", - x_coords$y1, - ", ", - x_coords$x2, - " ", - x_coords$y2, - ")" - ) - ))) + x_expl_lines <- st_nest_coordinates(x_coords) # Add CRS diff --git a/R/st_nest_coordinates.R b/R/st_nest_coordinates.R index ab6bbe8..220083c 100644 --- a/R/st_nest_coordinates.R +++ b/R/st_nest_coordinates.R @@ -6,7 +6,6 @@ #' @return A `sf` object containing `LINESTRING` geometries **without** a CRS. #' @export #' -#' @examples st_nest_coordinates <- function(x) { # Housekeeping @@ -60,8 +59,6 @@ st_nest_coordinates <- function(x) { #' coordinates for a line. #' #' @export -#' -#' @examples st_unnest_coordinates <- function(x) { # Housekeeping diff --git a/R/st_orientation.R b/R/st_orientation.R index 5f5f787..49cd34a 100644 --- a/R/st_orientation.R +++ b/R/st_orientation.R @@ -1,13 +1,13 @@ #' Determine the interior angle between a `LINESTRING` and the horizontal. #' #' @param x a `sf` object containing `LINESTRING` geometries. +#' @param nest logical; If `TRUE`, `LINESTRING` geometries will be recreated. #' #' @return An unnested `sf` object containing the orientation of each line #' segment making up the original `LINESTRING` geometries. #' @export -#' -#' @examples -st_orientation <- function(x) { +st_orientation <- function(x, + nest = TRUE) { # Housekeeping @@ -43,11 +43,15 @@ st_orientation <- function(x) { # Nest the columns to form line strings - x_orientation <- st_nest_coordinates(x_orientation) + if (nest == TRUE) { + + x_orientation <- st_nest_coordinates(x_orientation) + + # Add the crs back in - # Add the crs back in + sf::st_crs(x_orientation) <- x_crs - sf::st_crs(x_orientation) <- x_crs + } return(x_orientation) diff --git a/man/st_orientation.Rd b/man/st_orientation.Rd index b60e442..e26a8e6 100644 --- a/man/st_orientation.Rd +++ b/man/st_orientation.Rd @@ -4,10 +4,12 @@ \alias{st_orientation} \title{Determine the interior angle between a \code{LINESTRING} and the horizontal.} \usage{ -st_orientation(x) +st_orientation(x, nest = TRUE) } \arguments{ \item{x}{a \code{sf} object containing \code{LINESTRING} geometries.} + +\item{nest}{logical; If \code{TRUE}, \code{LINESTRING} geometries will be recreated.} } \value{ An unnested \code{sf} object containing the orientation of each line diff --git a/vignettes/articles/1-Spatial-Units.Rmd b/vignettes/articles/1-Spatial-Units.Rmd index c452df4..9587019 100644 --- a/vignettes/articles/1-Spatial-Units.Rmd +++ b/vignettes/articles/1-Spatial-Units.Rmd @@ -105,7 +105,8 @@ The `st_merge_spatialunits()` function uses an iterative merging process. Please bangalore_streetblocks_merged <- multi::st_merge_spatialunits(x = bangalore_streetblocks, merge_threshold = 4050, - merge_type = "max_shared_boundary") + merge_type = "max_shared_boundary", + verbose = FALSE) bangalore_streetblocks_merged @@ -135,7 +136,7 @@ bangalore_buildings <- osmdata::opq(bbox = sf::st_bbox(bangalore_boundary)) |> sf::st_make_valid() ``` -As before, this example data is included with the package. The code chunk below uses these datasets to draw MTs. We use default values for `segment_length`, `shrink_extent`, `merge_type`, and `contiguity`. We do however define a merge threshold of 100 m^2. Fair warning, this function (currently) takes longer to execute. +As before, this example data is included with the package. The code chunk below uses these datasets to draw MTs. We use default values for `segment_length`, `shrink_extent`, and `contiguity`. We skip the merging process for now. (Please refer to Fleischmann et al., 2020 for details on `segment_length` and `shrink_extent`.) @@ -144,7 +145,7 @@ As before, this example data is included with the package. The code chunk below bangalore_mtus <- multi::st_create_tessellations(x = bangalore_buildings, boundary = bangalore_boundary, - merge_threshold = 100, + merge_threshold = NULL, verbose = FALSE) bangalore_mtus