code_units() gives each unit a code made of the codes of the admin
units that hold it and a number within the lowest of them, assigned in
a geographic order so that consecutive numbers are neighbors. With the
previous version of the layer, an unchanged unit keeps its code, the
pieces of a split unit get child codes of their parent, and new units
are numbered after the highest number already used.
Usage
code_units(
x,
levels,
digits = 3,
sep = "",
order = c("hilbert", "serpentine"),
previous = NULL,
previous_code = "code",
child_sep = "-",
same = 0.99
)Arguments
- x
An
sfobject of polygons in a projected CRS.- levels
The columns of
xholding the codes of the admin levels, from the top down, such asc("region", "commune"). Units are numbered within each combination of them.- digits
The width of the unit number, padded with zeros.
- sep
The separator between the parts of the code,
""(default) for none.- order
"hilbert"(default) or"serpentine", the path along which units are numbered, as inorder_units().- previous
The previous version of the layer, an
sfobject in the CRS ofxwith its codes, orNULL(default). Its codes are read withdigits,sepandchild_sep, which must be those that made them.- previous_code
The column of
previousholding the codes.- child_sep
The separator between a parent code and the number of one of its pieces.
- same
The share of area two units must hold in common, each way, to count as the same unit, and the share of a new unit that must lie in an old one for it to count as its piece.
Details
Without previous, units are numbered 1, 2, ... within each
combination of levels, along order.
With previous, each unit is matched to the previous ones by
crosswalk_units(). A unit that holds at least same of a previous
unit's area, and lies at least same within it, is that unit, and keeps
its code when its admin codes are unchanged. A unit that lies within a
previous unit now cut into several is one of its pieces, and gets the
parent's code, child_sep and its number among the pieces. Every other
unit, merged from several or moved to another admin unit, is new and is
numbered after the highest number used in its admin unit by the
previous codes, so no code is ever given to two different areas. The
previous codes are split into their parts with the current digits,
sep and child_sep, so a call with other values reads them wrongly
and gives unchanged units new codes. The format of a frame changes only
with a coding from scratch, without previous.
NSOs differ in their formats (digits per level, separators, whether
numbers restart in each commune). Numbers restart within the lowest
level given, so a national sequence is levels set to a column with
one value. There are no built-in national formats.
A frame edited by hand, as cartographers do before fieldwork, is
reconciled with its previous version in three calls. check_units()
lists its defects and sizes out of bounds, code_units() with
previous codes it, and crosswalk_units() matches it with the
previous version (see Examples).
See also
crosswalk_units(), order_units(), renest_units(),
check_units().
Other frame update functions:
crosswalk_units(),
renest_units()
Examples
cells <- sf::st_make_grid(
sf::st_bbox(c(xmin = 0, ymin = 0, xmax = 400, ymax = 200)),
cellsize = 100
)
x <- sf::st_sf(region = "07", commune = rep(c("01", "02"), each = 4),
geometry = sf::st_set_crs(cells, 32630)[c(1, 2, 5, 6, 3, 4, 7, 8)])
x$code <- code_units(x, c("region", "commune"), digits = 3)
x$code
#> [1] "0701001" "0701004" "0701002" "0701003" "0702001" "0702004" "0702002"
#> [8] "0702003"
# Split the first unit in two: the pieces become children
halves <- sf::st_make_grid(sf::st_geometry(x)[1], n = c(2, 1))
y <- rbind(x[-1, c("region", "commune")],
sf::st_sf(region = "07", commune = "01", geometry = halves))
code_units(y, c("region", "commune"), digits = 3, previous = x)
#> [1] "0701004" "0701002" "0701003" "0702001" "0702004" "0702002"
#> [7] "0702003" "0701001-1" "0701001-2"
# Reconcile the edited frame: check it, code it, match it
set.seed(1)
buildings <- sf::st_sample(sf::st_union(x), 120)
y$size <- measure_units(y, points = buildings)
check_units(y, size = "size", min = 10, max = 20)
#> check row other area detail
#> 1 below_min 8 NA NA size 6 below 10
#> 2 below_min 9 NA NA size 7 below 10
#> 3 over_max 3 NA NA size 22 above 20
y$code <- code_units(y, c("region", "commune"), digits = 3, previous = x)
crosswalk_units(x, y, "code", "code", points = buildings)
#> old new area area_old area_new size size_old size_new
#> 1 0701001 0701001-1 5000 0.5 1 6 0.4615385 1
#> 2 0701001 0701001-2 5000 0.5 1 7 0.5384615 1
#> 3 0701004 0701004 10000 1.0 1 16 1.0000000 1
#> 4 0701002 0701002 10000 1.0 1 12 1.0000000 1
#> 5 0701003 0701003 10000 1.0 1 22 1.0000000 1
#> 6 0702001 0702001 10000 1.0 1 13 1.0000000 1
#> 7 0702004 0702004 10000 1.0 1 14 1.0000000 1
#> 8 0702002 0702002 10000 1.0 1 14 1.0000000 1
#> 9 0702003 0702003 10000 1.0 1 16 1.0000000 1