Skip to content

Commit

Permalink
refactor: use sf_use_s2 to decide how to summarise polygons 🚧
Browse files Browse the repository at this point in the history
  • Loading branch information
loreabad6 committed Sep 19, 2024
1 parent ec56ccc commit fe69fad
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ importFrom(rlang,`%||%`)
importFrom(rlang,sym)
importFrom(sf,`st_crs<-`)
importFrom(sf,`st_geometry<-`)
importFrom(sf,sf_use_s2)
importFrom(sf,st_agr)
importFrom(sf,st_as_sf)
importFrom(sf,st_as_sfc)
Expand Down
7 changes: 4 additions & 3 deletions R/summarise_geometry.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ summarize_geometry_union = summarise_geometry_union
#' @describeIn summarise_geometry computes the geometry summary as the centroid
#' of the union and dissolve of the changing geometries
#' @inheritParams summarise_geometry_union
#' @importFrom sf st_centroid
#' @importFrom sf st_centroid sf_use_s2
#' @export
summarise_geometry_centroid = function(x,
group_id = NULL,
Expand All @@ -70,7 +70,7 @@ summarise_geometry_centroid = function(x,
x_unioned = st_summarise_polys(x,
group_id = group_id,
sf_column_name = sf_column_name,
do_union = TRUE,
do_union = sf::sf_use_s2(),
.checks = .checks)
x_centroid = sf::st_centroid(x_unioned)
x_centroid
Expand Down Expand Up @@ -108,6 +108,7 @@ summarise_geometry_bbox = function(x,
}

#' @rdname summarise_geometry
#' @importFrom sf st_convex_hull sf_use_s2
#' @export
summarize_geometry_bbox = summarise_geometry_bbox

Expand All @@ -123,7 +124,7 @@ summarise_geometry_convex_hull = function(x,
x_unioned = st_summarise_polys(x,
group_id = group_id,
sf_column_name = sf_column_name,
do_union = TRUE,
do_union = sf::sf_use_s2(),
.checks = .checks)
x_conv_hull = sf::st_convex_hull(x_unioned)
x_conv_hull
Expand Down
7 changes: 6 additions & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ st_bbox_by_feature = function(x) {
#' @noRd
#' @importFrom sf st_make_valid st_cast st_union
st_summarise_polys = function(x, group_id, sf_column_name,
do_union = TRUE, .checks = TRUE) {
do_union = sf::sf_use_s2(),
.checks = TRUE) {

if(.checks){
# group_id: Defaults to the first column of x, if not sfc or temporal class
Expand All @@ -66,6 +67,10 @@ st_summarise_polys = function(x, group_id, sf_column_name,
}

x_groupped = split(x[[sf_column_name]], x[[group_id]])
# Combining all the polygon features into one single polygon is a much
# cheaper operation than doing a union operation.
# However the validity of the multipolygon will throw errors with s2 geometry
# so the default is to union when s2 is active in sf
if(do_union) {
do.call(
"c",
Expand Down

0 comments on commit fe69fad

Please sign in to comment.