check_units() lists, without changing anything, the defects of a
layer of units, such as blocks, enumeration areas or PSUs, that the
algorithms assume absent or that make a poor frame. It always checks
the geometry. With boundary, it also checks that units nest in a
boundary layer, such as admin units or management units, and cover it,
and with size, that every unit has a size within
the bounds. With id and groups, it checks the units against a
table of records, such as the EAs of a census by admin unit, before
the units are cut. Each finding is a row the user can look up in a
GIS.
It is a diagnostic, not a certificate. A layer with no finding can still be a poor frame, and the responsibility for inputs that are clean enough stays with the user.
Usage
check_units(
x,
size = NULL,
min = NULL,
max = NULL,
boundary = NULL,
boundary_id = NULL,
graph = NULL,
id = NULL,
groups = NULL,
sliver = 0.01,
tolerance = 0.01
)Arguments
- x
An
sfobject (orsfc) of polygons.- size
The size of each unit, as a column name of
xor a nonnegative numeric vector, orNULL(default) to skip the size checks.- min, max
The smallest and largest size a unit should have, or
NULL(default) for no bound. They needsize.- boundary
An
sfobject (orsfc) of boundary units, such as admin units, in the CRS ofx, the unitsxshould nest in and cover, orNULL(default) to skip the boundary checks.- boundary_id
The id of each boundary unit, as a column name of
boundaryor a vector, orNULL(default) for its row number.- graph
The
unit_graph()ofx, orNULL(default). With it, units with no neighbor are reported. It must be built fromxitself, in the same row order. Only its number of units is checked, so a graph of a reordered or edited layer gives wrong results without an error.- id
The code of each unit, as a column name of
xor a vector, matched againstgroups$part, orNULL(default). It needsgroups.- groups
A data frame of records with a column
part, such as the table ofgroup_units()with one row per census EA or forest compartment, orNULL(default) to skip the record checks. Onlypartis read, so the table given togroup_units()can be passed as it is. It needsid.- sliver
The share of a unit outside its boundary unit, or of a boundary unit outside every unit, below which nothing is reported. It leaves out the slivers between lines drawn from different sources, and is the share of an enclosed unit allowed outside the unit around it. The default is
0.01.- tolerance
The mean width, in CRS units, below which an overlap or a gap is rounding along a shared line and is not reported. The mean width is the area over half the perimeter. The default
0.01is a centimeter in a metric CRS, the precision ofbuild_blocks().
Value
A data frame with one row per finding, holding check, row
(the row of x, NA for the layer as a whole, a gap or a boundary
unit), other (the second row of a pair, NA otherwise), area (of
an overlap, a gap or the uncovered part of a boundary unit, in the
squared units of the CRS) and detail. The checks, in order, are
"crs", a CRS in longitude and latitude, or none."type", a geometry that is not a polygon."empty", an empty geometry."invalid", an invalid polygon, with the reason GEOS gives."duplicate", two units with the same footprint."overlap", two units whose interiors overlap, on more than a millionth of the smaller one and wider thantolerance, as smaller or thinner overlaps are rounding noise along a shared border."gap", a hole in the union of the units that no unit fills, wider thantolerance, with a unit on its edge inrow. Withboundary, a hole of the boundary layer is not a gap (see Details)."isolated", a unit with no neighbor ingraph."crosses_boundary", a unit with more thansliverof its area outside its main boundary unit, whichdetailnames with the share outside."outside_boundary", a unit outside every boundary unit."uncovered", a boundary unit with more thansliverof its area in no unit, whichdetailnames with that share."no_unit", a part ofgroupsthat no unit has, withNAinrow.detailgives the part and its number of rows."no_groups", a unit whose code no row ofgroupshas, with its size indetailwhensizeis given."enclosed", a unit with no rows that lies, but forsliverof its area, within the outline of another unit, given inother, as a town inside its rural district. The two need not share a line, and of several such units, the smallest is given."zero_size", a unit of size 0."below_min"and"over_max", a unit whose size is out of the bounds, given indetail.
Details
Overlaps and gaps are common in enumeration area layers digitized one
area at a time. unit_graph() links overlapping units by default, so
grouping still works, but their areas count twice, and a gap becomes a
hole no group covers. A gap can also be real, such as a lake or an
enclave left out of the layer, which only the user can tell. With
boundary, the boundary layer tells: a hole that lies in a hole of the
boundary layer, or outside it, by all but sliver of its area, is not
reported, so a lake cut out of the admin units is not a gap of the
frame. A gap along the outer edge of the layer is no hole, so only
boundary finds it,
as uncovered territory.
Invalid polygons are repaired with sf::st_make_valid() for the later
checks only. The layer itself is not changed. Overlaps, gaps, enclosed
units and the boundary checks are measured in planar coordinates, so for
a layer in longitude and latitude, or with no CRS, they are skipped.
Transform it first, for example to the local UTM zone.
A table of records, such as the EAs of a census with their admin
codes, rarely matches the admin polygons at hand. A part of the table
may have no polygon, after a split or a recoding, and a polygon may
have no records, such as a park, a lake or a town whose records the
table codes in the unit around it. group_units() stops at the first
such part, so id and groups list them all at once, on the admin
layer before build_blocks(), where they are cheap to fix. With
size, such as building counts from measure_units(), "no_groups"
gives what the frame would leave out. The checks only report.
Merging an enclosed unit into the unit around it, or dropping a unit
with no records, is the user's step.
A frame's sizes can be compared with an independent estimate, such as a
population raster, in one call to measure_units() (see Examples).
See also
unit_graph() and its overlap argument, check_groups()
for a grouping, renest_units() to carry groups to new
boundaries.
Other checks:
audit_field(),
check_groups(),
graph_lines(),
group_metrics()
Examples
sq <- function(x0, y0, s) {
sf::st_polygon(list(rbind(c(x0, y0), c(x0 + s, y0), c(x0 + s, y0 + s),
c(x0, y0 + s), c(x0, y0))))
}
x <- sf::st_sfc(sq(0, 0, 100), sq(98, 0, 100), sq(200, 0, 100),
sq(300, 10, 100),
crs = 32630
)
check_units(x)
#> check row other area detail
#> 1 overlap 1 2 200 <NA>
# A frame against two communes whose line at x = 250 cuts two EAs
frame <- sf::st_sf(households = c(80, 95, 0, 110, 70, 90, 85, 100),
geometry = sf::st_set_crs(sf::st_make_grid(
sf::st_bbox(c(xmin = 0, ymin = 0, xmax = 400, ymax = 200)),
cellsize = 100
), 32630)
)
rect <- function(x0, x1) {
sf::st_polygon(list(rbind(c(x0, 0), c(x1, 0), c(x1, 200), c(x0, 200),
c(x0, 0))))
}
communes <- sf::st_sf(id = c("west", "east"),
geometry = sf::st_sfc(rect(0, 250), rect(250, 420), crs = 32630)
)
check_units(frame, size = "households", min = 50, boundary = communes,
boundary_id = "id")
#> check row other area detail
#> 1 crosses_boundary 3 NA NA 50.0% outside west
#> 2 crosses_boundary 7 NA NA 50.0% outside west
#> 3 uncovered NA NA 4000 11.8% of east
#> 4 zero_size 3 NA NA <NA>
# The communes against a census table, which codes the EAs of a town
# inside the east commune under that commune, and has a third commune
town <- sf::st_polygon(list(rbind(c(300, 50), c(350, 50), c(350, 100),
c(300, 100), c(300, 50))))
east <- sf::st_difference(rect(250, 420), town)
areas <- sf::st_sf(code = c("west", "east", "town"),
geometry = sf::st_sfc(rect(0, 250), east, town, crs = 32630)
)
census <- data.frame(part = c("west", "west", "east", "north"),
size = c(120, 95, 140, 80))
check_units(areas, id = "code", groups = census)
#> check row other area detail
#> 1 no_unit NA NA NA part north with 1 row of 'groups'
#> 2 no_groups 3 NA NA part town
#> 3 enclosed 3 2 NA part town inside east
# Sizes against a population raster, five persons per household
if (requireNamespace("terra", quietly = TRUE) &&
requireNamespace("exactextractr", quietly = TRUE)) {
pop <- terra::rast(xmin = 0, xmax = 400, ymin = 0, ymax = 200,
resolution = 100, crs = "EPSG:32630",
vals = c(350, 450, 425, 1000, 400, 475, 40, 550)
)
ratio <- frame$households / (measure_units(frame, raster = pop) / 5)
round(ratio, 2)
}
#> [1] 1.0 1.0 0.0 1.0 1.0 1.0 1.0 0.5