decoupage cuts a territory into contiguous areas whose sizes stay
within bounds. This vignette follows one territory through both scales
of a frame. It first builds pre-census enumeration areas (preEAs) from
roads, buildings and a population raster, then takes a frame of
enumeration areas (EAs) to the primary sampling units (PSUs) of a
survey, orders them for selection and cuts a selected PSU into segments.
The same functions serve other area frames, such as an agricultural area
frame or a forest inventory, and vignette("recipes") shows
both.
library(decoupage)
library(sf)
par(mar = rep(0, 4))
## Different colors for neighboring polygons
paint <- function(x, pal = hcl.colors(6, "Set 2")) {
nb <- st_intersects(x)
col <- integer(nrow(x))
for (i in order(-lengths(nb))) {
col[i] <- setdiff(seq_along(pal), col[setdiff(nb[[i]], i)])[1]
}
pal[col]
}Inputs
decoupage takes plain sf layers and terra
rasters in a projected CRS in meters, and leaves the choice and the
cleaning of inputs to the user. No function downloads or repairs
data.
| Input | Used by | Typical sources |
|---|---|---|
| Boundary units, such as admin units |
build_blocks(),
check_units(boundary = )
|
COD-AB on HDX, geoBoundaries, the statistics office |
| Roads, tracks, paths, streams |
build_blocks(),
unit_graph(visible = )
|
Overture Maps, OpenStreetMap |
| Buildings |
delineate_settlements(),
measure_units(inside = )
|
Overture Maps, Google Open Buildings |
| Population raster | measure_units(raster = ) |
WorldPop, GHS-POP |
| EA boundaries |
group_units(method = "merge"),
split_units()
|
The statistics office |
The package ships a window of 4 by 6 km north of Yako, Burkina Faso,
across the line between two communes, with its roads, buildings, a
population raster and a frame of 21 EAs (see ?yako).
path <- system.file("extdata", "yako.gpkg", package = "decoupage")
communes <- read_sf(path, layer = "communes")
roads <- read_sf(path, layer = "roads")
buildings <- read_sf(path, layer = "buildings")
pop <- terra::rast(system.file("extdata", "yako_pop.tif",
package = "decoupage"))
plot(st_geometry(communes), col = c("#e3ecf3", "#f1ece2"), border = NA)
plot(st_geometry(roads), col = "grey45", add = TRUE)
plot(st_geometry(buildings), pch = 16, cex = 0.12, add = TRUE)
plot(st_geometry(communes), lwd = 2, add = TRUE)
The two communes, roads and buildings of the Yako window.
Yako town fills the bottom of the window, and villages are scattered
across the fields. check_units() lists what a layer breaks
of what the algorithms assume, without changing anything. The communes
are clean.
check_units(communes)
#> [1] check row other area detail
#> <0 rows> (or 0-length row.names)Blocks
Blocks are the smallest areas that roads, tracks and the commune line
enclose. Where no line runs, as between a village and its fields,
delineate_settlements() finds patches of buildings and
build_blocks() draws a zone line around each, so a village
becomes a block of its own. Every block lies in one commune and carries
its columns.
settlements <- delineate_settlements(buildings, cell = 100)
blocks <- build_blocks(communes, roads, settlements = settlements,
smooth = 50)
nrow(blocks)
#> [1] 70
plot(st_geometry(blocks), col = paint(blocks), border = "white")
plot(st_geometry(communes), lwd = 2, add = TRUE)
Blocks, with the commune line in black.
Sizes
measure_units() gives each block its population. Each
cell of the raster sends its count to the buildings it holds, weighted
by their footprint area, so a block with no building gets nothing.
reach = 1 lets a populated cell with no building send its
count to the buildings of the cells around it. The raster total is
kept.
blocks$pop <- measure_units(blocks, raster = pop, inside = buildings,
weight = "area_m2", reach = 1)
c(raster = sum(terra::values(pop), na.rm = TRUE), blocks = sum(blocks$pop))
#> raster blocks
#> 7579.921 7579.921The preEAs here hold 300 to 600 people, near 450. A block above 600
cannot be grouped under that cap, so split_units() cuts it
first, along straight lines that pass between buildings where they can.
Each piece keeps the columns of its block and holds its own share of the
population.
pieces <- split_units(blocks, "pop", max = 600, target = 450,
inside = buildings, weight = "area_m2")
c(blocks = nrow(blocks), pieces = nrow(pieces))
#> blocks pieces
#> 70 79Pre-census EAs
unit_graph() reads the geometry once. It finds which
pieces share a boundary and how long it is, never joins two communes,
keeps the pieces of a settlement together when they fit, and records how
much of each shared boundary lies on a road. group_units()
then works on the graph alone. It cuts a spanning tree into groups under
the cap, and a local search moves pieces between groups to bring sizes
near the target, make shapes compact and draw boundaries along
roads.
g <- unit_graph(pieces, within = "pcode", patch = "settlement",
visible = roads)
pieces$preea <- group_units(g, pieces$pop, min = 300, max = 600,
target = 450, seed = 1)
check_groups(g, pieces$preea, pieces$pop, min = 300, max = 600)
#> check group units size forced detail
#> 1 below_min 1 3 151.7438 TRUE <NA>check_groups() tests that each group is connected, lies
in one commune, stays within the caps and keeps its villages whole. A
group below the minimum is marked forced when no neighbor has room under
the cap, as for a village alone in a corner of a commune.
group_metrics() adds the shape of each group and the share
of its boundary that crosses open ground, which tells which map sheets
need landmarks.
preeas <- dissolve_units(pieces, "preea", size = "pop")
plot(st_geometry(preeas), col = paint(preeas), border = "white")
plot(st_geometry(communes), lwd = 2, add = TRUE)
text(st_coordinates(st_point_on_surface(st_geometry(preeas))),
labels = round(preeas$pop), cex = 0.7)
PreEAs and their population, with the commune line in black.
In the town, a preEA is a few blocks. In the fields, it joins several villages and their land, and covers square kilometers.
From EAs to PSUs
A survey draws its first stage from the census EAs. The window ships
a frame of 21 EAs with their households and an urban or rural stratum.
check_units() checks them against the communes and lists
the EAs below the take of a PSU. None crosses a commune line, and four
hold fewer than 50 households.
eas <- read_sf(path, layer = "eas")
issues <- check_units(eas, size = "households", min = 50,
boundary = communes, boundary_id = "pcode")
issues[, c("check", "row", "detail")]
#> check row detail
#> 1 below_min 1 size 30 below 50
#> 2 below_min 9 size 9 below 50
#> 3 below_min 11 size 38 below 50
#> 4 below_min 15 size 28 below 50Each PSU should hold at least 50 households.
group_units() with method = "merge" joins each
small EA to the neighbor in its stratum it shares the longest boundary
with, and the PSU takes the code of its first EA.
order_units() then ranks the PSUs along a curve that keeps
neighbors close, so a systematic draw spreads over the ground.
eas$psu <- group_units(eas, "households", min = 50, within = "stratum",
id = "ea", method = "merge")
psus <- dissolve_units(eas, "psu", size = "households")
psus$zone_order <- order_units(psus, within = "stratum")
plot(st_geometry(psus), border = "grey30",
col = ifelse(psus$stratum == "urban", "#fdd49e", "#d9f0d3"))
text(st_coordinates(st_point_on_surface(st_geometry(psus))),
labels = psus$zone_order)
PSUs numbered in their order, urban in orange and rural in green.
The PSUs, their strata, sizes and order form the register a design
reads, such as the psu argument of
svyplan::n_alloc() or samplyr::draw() with
method = "pps_systematic". decoupage itself draws nothing
and attaches no probabilities.
register <- st_drop_geometry(psus)[c("psu", "stratum", "households",
"zone_order")]
head(register[order(register$stratum, register$zone_order), ])
#> psu stratum households zone_order
#> 11 EA13 rural 55 1
#> 12 EA14 rural 104 2
#> 2 EA02 rural 75 3
#> 1 EA01 rural 107 4
#> 16 EA19 rural 99 5
#> 7 EA08 rural 95 6Inside a selected PSU
A selected PSU too large to list is cut into segments, and one
segment is drawn by its size. split_units() cuts the
polygon on the buildings inside it, so each segment is an area a team
can find, and its households are the PSU’s times its share of the
buildings. Each segment keeps the columns of its PSU.
large <- psus[psus$households > 80, ]
segments <- split_units(large, "households", max = 80, target = 50,
inside = buildings, weight = "area_m2")
head(st_drop_geometry(segments)[c("psu", "piece", "households")])
#> psu piece households
#> 1 EA01 1 35.74408
#> 2 EA01 2 35.85019
#> 3 EA01 3 35.40573
#> 4 EA04 1 47.13898
#> 5 EA04 2 46.86102
#> 6 EA05 1 47.18600
plot(st_geometry(large), border = NA)
plot(st_geometry(segments), col = paint(segments), border = "white",
add = TRUE)
plot(st_geometry(large), lwd = 2, add = TRUE)
Segments of the PSUs above 80 households, with the PSUs in black.
A three-stage design divides each selected PSU into secondary units
instead, with the same calls: split_units() into small
pieces, then group_units(within = "psu", method = "merge")
up to a minimum size.
Further
vignette("recipes") gathers short recipes, such as
fetching inputs from Overture Maps, an agricultural area frame, a forest
inventory, reconciling a frame edited by hand and running a country in
parallel. The help page of each function details its arguments and its
checks, and ?simulate builds populations with a known truth
to compare frames, as the article The
same population, different frames does.