Skip to content

Commit

Permalink
Merge pull request #14 from anirudhgovind/dev
Browse files Browse the repository at this point in the history
edit: clean up functions
  • Loading branch information
anirudhgovind authored Apr 19, 2024
2 parents c4ba8af + eeecab4 commit 1954031
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 66 deletions.
55 changes: 2 additions & 53 deletions R/st_explode.R
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 0 additions & 3 deletions R/st_nest_coordinates.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#' @return A `sf` object containing `LINESTRING` geometries **without** a CRS.
#' @export
#'
#' @examples
st_nest_coordinates <- function(x) {

# Housekeeping
Expand Down Expand Up @@ -60,8 +59,6 @@ st_nest_coordinates <- function(x) {
#' coordinates for a line.
#'
#' @export
#'
#' @examples
st_unnest_coordinates <- function(x) {

# Housekeeping
Expand Down
16 changes: 10 additions & 6 deletions R/st_orientation.R
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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)

Expand Down
4 changes: 3 additions & 1 deletion man/st_orientation.Rd

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

7 changes: 4 additions & 3 deletions vignettes/articles/1-Spatial-Units.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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`.)

Expand All @@ -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
Expand Down

0 comments on commit 1954031

Please sign in to comment.