Skip to contents

Dissolves a polygon layer by a grouping column, returning one feature per group. Single-member groups short-circuit to their own geometry, so the cost is paid only on groups that genuinely need unioning.

Usage

union_polygons(poly, ID)

Arguments

poly

An `sf` POLYGON/MULTIPOLYGON object with an attribute column used for grouping.

ID

A string naming the column over which to union geometries.

Value

An `sf` polygon layer unioned by `ID` (column preserved).

Notes

If any resulting geometries are geometry collections, they are extracted to POLYGON using `sf::st_collection_extract()`.

The dissolve is **area-conserving and cannot introduce overlap**. Two earlier behaviors are deliberately gone:

* It no longer casts the result to `POLYGON` and keeps only the largest part per group. A group whose members are genuinely disjoint is a multipart catchment, not an error, and discarding the smaller parts silently deleted ground. Output is `MULTIPOLYGON`, which is lossless. * It no longer routes geometry through `terra::makeValid()` before aggregating. That round-trip perturbed shared boundaries enough that neighboring groups came out overlapping: on one CONUS VPU the summed area of the result exceeded the summed area of its inputs by 20 km2, all of it overlap between adjacent groups, which downstream cleanup then had to remove. Grouped `sf::st_union()` of already-valid inputs preserves the input tiling exactly.

Invalid input geometry is repaired per group with `sf::st_make_valid()` only when `sf::st_is_valid()` says it is needed, so valid inputs are untouched.

Examples

if (FALSE) { # \dontrun{
out <- union_polygons(counties_sf, "state_fips")
} # }