Skip to contents

renest_units() carries a partition of units, such as preEAs built from blocks, to a new boundary layer, such as redrawn admin units or management units. Units that a new boundary line crosses are cut along it, groups whose units now fall in more than one boundary unit are partitioned again within the new lines, and every other group is kept as it is, through fixed in group_units(). The result nests exactly in the new layer.

Usage

renest_units(
  units,
  group,
  boundary,
  boundary_id,
  size,
  inside = NULL,
  weight = NULL,
  barrier = NULL,
  barrier_rank = NULL,
  visible = NULL,
  patch = NULL,
  tolerance = 0.01,
  ...
)

Arguments

units

An sf object of the units the partition was built from, in a projected CRS.

group

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

boundary

An sf object of the new boundary units, such as admin units, in the CRS of units, covering them. A unit with more of its area outside them than tolerance allows is an error.

boundary_id

The id of each boundary unit, as a column name of boundary or a vector.

size

The size of each unit, as a column name of units or a nonnegative numeric vector.

inside

Where the size sits within units, as in split_units(), used to share the size of a cut unit among its pieces, or NULL (default) to share it by area.

weight

The weight of each feature of inside, as in split_units(), or NULL.

barrier, barrier_rank, visible

The lines of the graph the partition was built with, as in unit_graph(), or NULL (default) for none. The groups partitioned again keep to them as the first partition did.

patch

The patch of each unit, as a column name of units or a vector, as in unit_graph(), or NULL (default). The pieces of a cut unit keep its patch.

tolerance

How far, in CRS units, a unit may cross a boundary line through the rounding of coordinates. An area up to tolerance times its perimeter may lie across a line without the unit being cut, or outside boundary without an error. The default 0.01 is a centimeter in a metric CRS, the precision of build_blocks().

...

Caps and settings for the groups partitioned again, passed to group_units(): min, max, target, max_area, seed and the others. fixed is set by renest_units(). A value of min, max, target, cross_below, n or n_by given per unit of units goes to every piece of the unit. It must then be the same for every unit of a new boundary unit, which is a part of the regrouping.

Value

An sf object with one row per unit or piece of a cut unit, holding unit (the row of units it comes from), boundary (the id of the new boundary unit holding it), size, previous (its group before), group (its group after) and changed, TRUE for the units of a group partitioned again. Units of groups kept keep their geometry and their group label. group is numeric when the labels of group are numbers, a factor when they are a factor, with the new labels added to its levels, and character otherwise.

Details

A group is kept when all its units lie within one new boundary unit, which is the case for most of a frame when boundary lines move little. The kept groups are locked, so the engine only works inside the groups the new lines cross. A unit is cut only where a new line crosses its interior. The piece of a cut unit on the far side of a new line can be left with nothing, or with less than min, among locked groups that cannot take it in. Such a group unlocks the kept neighbor it shares the longest boundary with, and the engine runs again, so a neighbor is redrawn only when a piece left over cannot stand alone. The groups partitioned again get new labels after the largest previous one when labels are numbers. Otherwise they are labeled "newK", with K counting on from the highest such label already in group, so the output of one run can be re-nested again.

Codes for the new groups, and for the kept ones, come from code_units() with the previous layer, which keeps the code of every group that has not changed.

See also

group_units() and its fixed, code_units(), crosswalk_units().

Other frame update functions: code_units(), crosswalk_units()

Examples

cells <- sf::st_make_grid(
  sf::st_bbox(c(xmin = 0, ymin = 0, xmax = 600, ymax = 400)),
  cellsize = 100
)
blocks <- sf::st_sf(geometry = sf::st_set_crs(cells, 32630))
blocks$pop <- 50
g <- unit_graph(blocks)
ea <- group_units(g, blocks$pop, max = 300, seed = 1)

# A new commune line at x = 250 cuts some EAs
communes <- sf::st_sf(id = c("west", "east"), geometry = sf::st_sfc(
  sf::st_polygon(list(rbind(c(0, 0), c(250, 0), c(250, 400), c(0, 400),
    c(0, 0)))),
  sf::st_polygon(list(rbind(c(250, 0), c(600, 0), c(600, 400),
    c(250, 400), c(250, 0)))),
  crs = 32630
))
out <- renest_units(blocks, ea, communes, "id", blocks$pop, max = 300,
  seed = 1)
table(out$boundary, out$group)
#>       
#>        1 3 4 6 7
#>   east 0 3 6 0 7
#>   west 6 0 0 6 0
sum(out$changed)
#> [1] 13