Skip to content

Commit

Permalink
Update st_repair_geometry() (#34)
Browse files Browse the repository at this point in the history
- update`st_repair_geometry()` to avoid uneeded geometry duplication.
- this reduces memory requirements for`crate_spp_info_data()`.
  • Loading branch information
jeffreyhanson authored Oct 3, 2022
1 parent ccd2176 commit 15770bf
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: aoh
Type: Package
Version: 0.0.1.3
Version: 0.0.1.4
Title: Create Area of Habitat Data
Description: Create Area of Habitat data to characterize species distributions.
Data are produced following procedures outlined by Brooks et al. (2019)
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# aoh 0.0.1.4

- Update`st_repair_geometry()` to avoid uneeded geometry duplication.
This reduces memory requirements for`crate_spp_info_data()`.

# aoh 0.0.1.3

- Fix issues with using GDAL engine for processing data on Windows.
Expand Down
17 changes: 17 additions & 0 deletions R/st_repair_geometry.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,23 @@ st_repair_geometry <- function(x, geometry_precision = 1e5) {
# extract polygons (if needed)
x2 <- suppressWarnings(sf::st_collection_extract(x2, "POLYGON"))

# dissolve by repair id
if (!identical(anyDuplicated(x2[["_repair_id"]]), 0L)) {
x2 <- split(x2, x2[["_repair_id"]])
x2_df <- tibble::tibble(`_repair_id` = as.integer(names(x2)))
x2 <- lapply(x2, sf::st_geometry)
x2 <- lapply(x2, sf::st_union)
x2 <- do.call(c, x2)
x_df <- match(x2_df[["_repair_id"]], x[["_repair_id"]])
x_df <- sf::st_drop_geometry(x)[x_df, , drop = FALSE]
x_df <- x_df[, setdiff(names(x_df), "_repair_id"), drop = FALSE]
x2_df <- tibble::as_tibble(cbind(x2_df, x_df))
x2_df <- x2_df[, names(sf::st_drop_geometry(x)), , drop = FALSE]
x2_df$geometry <- x2
x2 <- sf::st_sf(x2_df)
rm(x_df, x2_df)
}

# detect if any invalid geometries persist
## subset repaired polygons
x_sub <- x[match(x2[["_repair_id"]], x[["_repair_id"]]), , drop = FALSE]
Expand Down

0 comments on commit 15770bf

Please sign in to comment.