Order units so that neighbors on the ground stay close in the order
Source:R/order-units.R
order_units.Rdorder_units() returns a rank for every unit along a path that visits
the units in space, so that units next to each other in the order are
near each other on the ground. Ranks restart in each part of within,
such as a stratum. Sorting a frame by
it is the implicit stratification that systematic selection with
probability proportional to size relies on, and it is the order in
which svyplan::n_alloc() cuts zones (psu$zone_order).
Usage
order_units(x, within = NULL, nest = NULL, method = c("hilbert", "serpentine"))Arguments
- x
An
sfobject (orsfc) of polygons in a projected CRS.- within
The part of the layer each unit lies in, such as its stratum, its admin unit, or its PSU for SSUs, as a column name of
xor a vector, orNULL(default) for one part. Ranks restart at 1 in each part.- nest
Units to keep together in the order, such as districts, as a column name or a vector, or
NULL(default). The groups are ordered by their centers, then the units within each group.- method
"hilbert"(default) or"serpentine". See Details.
Details
method = "hilbert" sorts units by the position of their point on
surface along a Hilbert curve, which fills the plane by visiting each
quarter of a square in turn, recursively. Two points close on the curve
are close in space, and the curve keeps a part's units in compact
runs, so consecutive units form compact zones (Bartholdi and Platzman
1988).
method = "serpentine" cuts each part into as many horizontal bands
as the square root of its unit count, each with the same count, from
north to south, and runs west to east in the first band, east to west in
the next, and so on. It is the order of many census listing manuals.
Both read only the positions of the units, not their adjacency, so two consecutive units are near each other but may not share a boundary.
References
Bartholdi, J. J., and Platzman, L. K. (1988). Heuristics based on spacefilling curves for combinatorial problems in Euclidean space. Management Science, 34(3), 291-305. doi:10.1287/mnsc.34.3.291
See also
group_units() with method = "merge" for PSUs to order.
Other grouping and cutting functions:
dissolve_units(),
group_graph(),
group_units(),
split_units(),
unit_graph()
Examples
cells <- sf::st_make_grid(
sf::st_bbox(c(xmin = 0, ymin = 0, xmax = 400, ymax = 400)),
cellsize = 100
)
cells <- sf::st_set_crs(cells, 32630)
rank <- order_units(cells)
matrix(rank, 4, 4, byrow = TRUE)[4:1, ]
#> [,1] [,2] [,3] [,4]
#> [1,] 6 7 10 11
#> [2,] 5 8 9 12
#> [3,] 4 3 14 13
#> [4,] 1 2 15 16
matrix(order_units(cells, method = "serpentine"), 4, 4, byrow = TRUE)[4:1, ]
#> [,1] [,2] [,3] [,4]
#> [1,] 1 2 3 4
#> [2,] 8 7 6 5
#> [3,] 9 10 11 12
#> [4,] 16 15 14 13