@@ -290,35 +290,35 @@ summarise.sf <- function(.data, ..., .dots, do_union = TRUE, is_coverage = FALSE
290
290
precision = st_precision(.data )
291
291
crs = st_crs(.data )
292
292
geom = st_geometry(.data )
293
+
294
+ if (! requireNamespace(" dplyr" , quietly = TRUE ))
295
+ stop(" dplyr required: install that first" ) # nocov
293
296
class(.data ) = setdiff(class(.data ), " sf" )
294
- ret = NextMethod()
295
- if (! missing(do_union ))
296
- ret $ do_union = NULL
297
- if (! missing(is_coverage ))
298
- ret $ is_coverage = NULL
297
+ .data $ .rows = vctrs :: vec_seq_along(.data )
298
+ ret = dplyr :: summarise(
299
+ .data = .data ,
300
+ ... ,
301
+ .rows = list (.rows )
302
+ )
303
+ .rows = ret $ .rows
304
+ ret = ret [names(ret ) != " .rows" ]
299
305
300
306
if (! any(sapply(ret , inherits , what = " sfc" ))) {
301
- geom = if (inherits(.data , " grouped_df" ) || inherits(.data , " grouped_dt" )) {
302
- if (! requireNamespace(" dplyr" , quietly = TRUE ))
303
- stop(" dplyr required: install that first" ) # nocov
304
- i = dplyr :: group_indices(.data )
305
- # geom = st_geometry(.data)
307
+ geom = if (nrow(ret ) > 1 ) {
306
308
geom = if (do_union )
307
- lapply(sort(unique( i )) , function (x ) {
308
- if (x == 1 )
309
- st_union(geom [i == x ], is_coverage = is_coverage )
309
+ lapply(.rows , function (x ) {
310
+ if (1 %in% x )
311
+ st_union(geom [x ], is_coverage = is_coverage )
310
312
else
311
- suppressMessages(st_union(geom [i == x ], is_coverage = is_coverage ))
313
+ suppressMessages(st_union(geom [x ], is_coverage = is_coverage ))
312
314
})
313
315
else
314
- lapply(sort(unique( i )) , function (x ) st_combine(geom [i == x ]))
316
+ lapply(.rows , function (x ) st_combine(geom [x ]))
315
317
geom = unlist(geom , recursive = FALSE )
316
318
if (is.null(geom ))
317
319
geom = list () # 676 #nocov
318
320
do.call(st_sfc , c(geom , crs = list (crs ), precision = precision ))
319
321
} else { # single group:
320
- if (nrow(ret ) > 1 )
321
- stop(paste0(" when using .by, also add across(" , sf_column , " , st_union) as argument" )) # https://github.com/r-spatial/sf/issues/2207
322
322
if (do_union )
323
323
st_union(geom , is_coverage = is_coverage )
324
324
else
0 commit comments