Skip to contents

split_units() cuts each unit whose size or area is above the caps into pieces that fit under them, and returns every other unit whole. It works at any level, on blocks that no line crosses before they are grouped, on selected PSUs cut into segments, or on the blocks of a PSU cut into SSUs. Each piece carries the columns of its unit, so the id of a PSU or an EA follows its segments.

Where the size sits inside a unit comes from inside, a layer of weighted points (buildings, households, or raster cells turned into points). A unit with no such point, or of size 0, is treated as evenly filled, so max_area cuts it by area.

Usage

split_units(
  x,
  size,
  max = NULL,
  target = NULL,
  max_area = NULL,
  inside = NULL,
  weight = NULL,
  method = c("bisect", "quadtree"),
  directions = 8L,
  tolerance = 0.05,
  lines = NULL
)

Arguments

x

An sf object (or sfc) of polygons in a projected CRS.

size

The size of each unit, as a column name of x or a nonnegative numeric vector. It is spread over the unit's points in proportion to their weights, so the pieces of a unit always sum to its size.

max

The largest size a piece may have, or NULL.

target

For method = "bisect", the size pieces should be near. A unit of size S is cut into ceiling(S / target) pieces. NULL (default) uses max, which gives the fewest pieces.

max_area

The largest area a piece may have, in the squared units of the CRS, or NULL. At least one of max and max_area is required.

inside

Where the size sits within units, as an sf or sfc of points or of polygons such as building footprints, in the CRS of x, or NULL. Each feature belongs to the unit assign_points() gives it. Footprints also keep cuts out of buildings (see Details).

weight

The weight of each feature of inside, as a column name or a numeric vector, or NULL (default) for a weight of 1 each. A weight is the quantity a feature stands for, not a survey weight.

method

"bisect" (default) or "quadtree". See Details.

directions

For method = "bisect", the number of cut directions tried at each cut, evenly spaced over a half turn. The default 8 tries one every 22.5 degrees.

tolerance

For method = "bisect" with footprints, how far a cut may move to pass between buildings, as a share of the size of the piece being cut, or of its load with max_area (see Details). The default 0.05 lets a half hold 5 percentage points more or less than its due.

lines

Lines field staff can find inside the units, such as tracks, field edges and canals left out of build_blocks(), as an sf or sfc object of lines in the CRS of x, or NULL (default). A unit above the caps is first cut along them, and its pieces are made of the faces they enclose (see Details). For method = "bisect".

Value

An sf object with one row per piece. Each piece holds the columns of the unit it comes from, then unit (the row of x), piece (its number within the unit), size, share (its share of the unit's size, or of its area for a unit of size 0), area, crosses (the count of buildings its cuts pass through, NA without footprints), and visible (the share of its cut boundary that runs along lines, NA without lines or for a unit not cut). When size names a column, that column holds the size of the piece and no size column is added, and naming another computed column is an error. Any other count copied from the unit, such as persons when size is households, is the unit's total, and its value for the piece is that total times share. A column of x with the name of a computed column is replaced. A unit within the caps is one piece of share 1 with its own geometry.

Details

method = "bisect" is recursive bisection weighted by size, the kd-tree idea applied to sizes. A unit to be cut into k pieces is cut once into two parts holding floor(k / 2) and the rest of the k shares, and each part is cut again the same way. Each cut is a straight line at the size quantile along one of directions directions. The shortest line that leaves both parts in one piece is kept, as in the shortest-splitline method for districting. Where every direction leaves a side in several parts, as across the teeth of a comb, each part becomes a piece of its own, and a part above the caps is cut again. With footprints, the line moves within tolerance to a gap between buildings when one exists. Pieces come out near S / k each and follow the shape of the unit. With max_area, k also covers the area over max_area, and each line is placed by the load of the two parts, the larger of a part's size over target (or max) and its area over max_area. The line then follows the size where size is the tighter cap and the area where area is, so a unit whose size sits at one end gives small pieces there and pieces near max_area elsewhere.

method = "quadtree" cuts the square around the unit into four equal squares, and each square again, until the part of the unit inside each square is within the caps. It is the method of the WorldPop preEA tool. Its squares do not depend on where people are, so a unit just above the cap gives four pieces, some of them empty. A square that cuts the unit into several parts gives one piece per part, so every piece is contiguous.

With lines, a unit above the caps is cut along the lines into faces, as build_blocks() cuts a unit, with faces below a square meter or narrower than a meter on average joined to a neighbor. Each point goes to the face holding it. A face above the caps is bisected as above, and the faces are then grouped by group_units() into pieces within the caps and near target, with boundaries drawn along the lines. A cut runs off the lines only inside a face too large for a piece, so the denser the lines, the more of each cut field staff can see. Faces fit together less closely than straight cuts, so a unit may give more and smaller pieces than without lines. A footprint counts in crosses when its vertices fall both in the piece and elsewhere in the unit.

A piece above max remains only when its size sits on one point heavier than max, which no straight cut can divide. Area can always be divided, so pieces meet max_area. Both caps are met to a relative 1e-9, the rounding of computed areas and sums, so a unit of area k * max_area gives k pieces rather than one more.

References

Bentley, J. L. (1975). Multidimensional binary search trees used for associative searching. Communications of the ACM, 18(9), 509-517. doi:10.1145/361002.361007

Finkel, R. A., and Bentley, J. L. (1974). Quad trees: A data structure for retrieval on composite keys. Acta Informatica, 4(1), 1-9. doi:10.1007/BF00288933

See also

group_units(), which merges the pieces with their neighbors afterwards.

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

Examples

block <- sf::st_sfc(
  sf::st_polygon(list(rbind(
    c(0, 0), c(600, 0), c(600, 300), c(0, 300), c(0, 0)
  ))),
  crs = 32630
)
set.seed(1)
houses <- sf::st_sample(block, 400)

pieces <- split_units(block, size = 400, max = 150, inside = houses)
pieces
#> Simple feature collection with 3 features and 7 fields
#> Geometry type: MULTIPOLYGON
#> Dimension:     XY
#> Bounding box:  xmin: 0 ymin: 0 xmax: 600 ymax: 300
#> Projected CRS: WGS 84 / UTM zone 30N
#>   unit piece size  share     area crosses visible
#> 1    1     1  134 0.3350 60011.89      NA      NA
#> 2    1     2  133 0.3325 56211.94      NA      NA
#> 3    1     3  133 0.3325 63776.16      NA      NA
#>                         geometry
#> 1 MULTIPOLYGON (((0 0, 0 300,...
#> 2 MULTIPOLYGON (((200.0396 0,...
#> 3 MULTIPOLYGON (((600 0, 387....

quads <- split_units(block, size = 400, max = 150, inside = houses,
  method = "quadtree"
)
nrow(quads)
#> [1] 8