Skip to contents

dissolve_units() merges the polygons of the units of each group into one polygon, sums their sizes, and keeps the columns that are the same for every unit of a group, such as the stratum of a PSU. Geometry is dissolved once, at the end, after group_units() has worked on the graph alone.

Usage

dissolve_units(x, group, size = NULL, is_coverage = TRUE)

Arguments

x

An sf object (or sfc) of polygons, the layer the groups were built from.

group

The group of each unit, as a column name of x or an atomic vector, with no NA.

size

The size of each unit, summed per group, as a column name of x or a numeric vector, or NULL (default) for none. Several column names, such as c("households", "cropland"), sum several sizes.

is_coverage

Whether the units form a coverage, that is, they do not overlap and neighbors share their vertices, as units cut from the same lines do. TRUE (default) uses the faster coverage union of GEOS, and falls back to the full union for any group where it fails or returns an invalid polygon, as where pieces of split_units() meet a neighbor that lacks their vertices. Use FALSE for a layer whose units were digitized separately, which unit_graph() reports as pairs found by geometric intersection or as overlapping pairs, and check_units() as overlaps.

Value

An sf object with one row per group, in order of first appearance, in the CRS of x. It holds the group label, the columns of x that are constant within every group, units (the count of units), the summed sizes when size is given, and area. The label and the sizes keep the names of their columns when given by name, and are group and size otherwise. A label or a size named units, area or geometry, or a size named like the label, is an error. A column of x that varies within a group, or that has the name of a computed column, is left out.

See also

group_units() for the groups.

Other grouping and cutting functions: group_graph(), group_units(), order_units(), split_units(), unit_graph()

Examples

cells <- sf::st_make_grid(
  sf::st_bbox(c(xmin = 0, ymin = 0, xmax = 400, ymax = 300)),
  cellsize = 100
)
cells <- sf::st_sf(geometry = cells, crs = 32630)
cells$pop <- rep(c(50, 120, 80, 30), 3)

cells$commune <- rep(c("west", "east"), each = 6)

g <- unit_graph(cells, within = "commune")
cells$ea <- group_units(g, cells$pop, max = 300, seed = 1)
dissolve_units(cells, "ea", size = "pop")
#> Simple feature collection with 4 features and 5 fields
#> Geometry type: MULTIPOLYGON
#> Dimension:     XY
#> Bounding box:  xmin: 0 ymin: 0 xmax: 400 ymax: 300
#> Projected CRS: WGS 84 / UTM zone 30N
#>   ea commune units pop  area                       geometry
#> 1  1    west     4 280 40000 MULTIPOLYGON (((0 0, 0 100,...
#> 2  2    west     2 170 20000 MULTIPOLYGON (((0 100, 0 20...
#> 3  3    east     4 220 40000 MULTIPOLYGON (((200 100, 20...
#> 4  4    east     2 170 20000 MULTIPOLYGON (((0 200, 0 30...