unit_graph() finds which units share a boundary, and how long that
boundary is, once for a layer. group_units() and
check_groups() then reuse the graph, so the geometry is read only
here.
Two units are adjacent when they share a stretch of boundary, not a
point, and that stretch is at least min_shared of the shorter of the
two perimeters. No edge joins units of different parts of within,
and no edge crosses a barrier line. With visible lines, each edge
also records how much of its shared boundary can be seen on the
ground, which group_units() uses to draw group boundaries along those
lines.
Usage
unit_graph(
x,
within = NULL,
barrier = NULL,
barrier_rank = NULL,
visible = NULL,
patch = NULL,
min_shared = 0.02,
tolerance = 0.001,
overlap = c("link", "error", "ignore"),
between = FALSE,
line_dist = 0.1,
gap = 0
)Arguments
- x
An
sfobject (orsfc) of polygons in a projected coordinate reference system. Lengths and areas are taken in its units.- within
The part of the layer each unit lies in, such as its admin unit or stratum, as a column name of
xor a vector. Units of different parts are never adjacent, so no group built from the graph crosses from one part to another.NULL(default) makes the layer one part.- barrier
Lines that no group may cross, such as major roads and rivers, as an
sforsfcobject in the CRS ofx. An edge is removed when any part of the boundary its two units share lies on a barrier. The units must already be cut by these lines, as blocks built from them are.NULL(default) for none.- barrier_rank
How hard each barrier line is to cross, as a column name of
barrieror a numeric vector, higher for harder, such as 2 for rivers and trunk roads and 1 for tertiary roads. Each removed pair records the highest rank along its shared boundary, andgroup_units()opens the lowest first where barriers enclose too little (itscross_below).NULL(default) gives every line rank 1.- visible
Lines field staff can find on the ground, such as roads, tracks, paths and streams for enumerators and listers, or forest roads and field edges for survey crews, as an
sforsfcobject in the CRS ofx. Usually thelinesgiven tobuild_blocks(). The part of each shared boundary that lies on them is recorded on the edge.NULL(default) for none.- patch
The patch each unit belongs to, as a column name of
xor a vector,NAfor units in none, such as the settlement of each block frombuild_blocks()or the stand of each piece of a forest stand. Units with one label that touch form a patch, andgroup_units()keeps a patch in one group when it fits under the caps.NULL(default) for none.The smallest shared boundary that makes two units adjacent, as a share of the shorter of their two perimeters. The default
0.02drops contacts at a corner or along a few meters, while a sliver along a long boundary keeps its neighbor.- tolerance
Coordinates closer than this, in CRS units, are treated as the same point when boundaries are matched. The default
0.001is a millimeter in a metric CRS.- overlap
What to do with units whose interiors overlap, as when neighboring enumeration areas were digitized separately.
"link"(default) makes them adjacent,"error"stops and names the first overlapping pair, for a layer that must be a coverage, and"ignore"skips the check, which saves its time on a layer known to be one, such as blocks frombuild_blocks(). See Details.- between
Whether to record the pairs of units of different parts of
withinthat share a boundary,FALSE(default) orTRUE.group_graph()needs them to build a level above whose groups span several parts, such as supervision areas over several communes.- line_dist
A stretch of shared boundary lies on a
barrierorvisibleline when its midpoint is within this distance of the line, in CRS units. The default0.1is ten centimeters in a metric CRS, which covers the rounding of coordinates bybuild_blocks().- gap
The largest distance, in CRS units, at which a part of the graph that no edge or barrier crossing joins to the rest of its
withinpart, such as land across a creek, is linked to the nearest unit of the rest.0(default) links nothing, andInflinks every such part whatever the distance, as for islands. See Details.
Value
An object of class unit_graph, a list with
units, a data frame with one row per unit ofx, in the same order, holdingarea,perimeter, the centroidcxandcy,within,patch,outer, the length of boundary shared with no unit of the same part (awithinline or the edge of the layer), andbarrier, the length shared with units across a barrier.edges, a data frame with one row per adjacent pair, holdingfromandto(row numbers ofx,from < to),shared, the shared boundary length, andvisible, the part of it on a visible line (NAwithoutvisiblelines).between, withbetween = TRUE, the pairs of units of different parts that share a boundary, withfrom,to,shared,barrier(whether a barrier line runs along it) andrank, andNULLotherwise.crossings, the pairs removed because their shared boundary lies on a barrier, withfrom,toandsharedas inedges,rank, the highest barrier rank along it, andgap,NAfor these. Withgap, the links across gaps follow, with asharedof 0, arankofInfand the distance ingap.group_units()opens some of them where barriers or gaps enclose too little.dropped, the count of pairs removed by each rule.collinear, the count of pairs found by matching segments along shared lines, where one unit has a vertex its neighbor lacks.fallback, the count of pairs found by geometric intersection.overlap, the count of pairs linked because they overlap, ascheck_units()reports overlaps at its default tolerance.slivers, the count of pairs linked across thinner overlaps, which are rounding along a shared border.min_shared,toleranceandcrs.
Details
Shared boundaries are found by matching identical boundary segments between units, which is exact and fast on a coverage whose units share vertices, as units cut from the same lines do. Segments left unmatched are then matched along the lines they lie on, which finds the shared boundary where one unit has a vertex its neighbor lacks, as after a unit is split. A unit whose boundary is still partly unmatched, because the layer was digitized piece by piece, is checked last with a geometric intersection against its touching neighbors. The graph is then complete for any layer whose units do not overlap.
Units that overlap may share no boundary, so matching may find no edge
between them, and a unit inside another, or overlapping all its
neighbors, would have none. With overlap = "link", every pair of
units of one part whose interiors overlap is adjacent, and half the
perimeter of the overlap is added to their shared boundary, which is
the length of a thin overlap along a common border. An overlap that a
barrier line crosses counts as a crossing of that barrier, and no
overlap counts as visible.
Only units whose boundary is partly unmatched, or matched with a unit
lying on the same side of it, as a duplicate does, are tested, so the
check costs little on a layer with few overlaps. The overlaps are not
removed.
The units keep their geometry, and their areas still count twice where
they overlap.
Units are matched within one part at a time, since no edge crosses parts, so each geometric match works on one part. The segments of every unit boundary are tabled for the whole layer first, so memory still grows with the size of the layer.
With gap, the parts of the graph within each part of within are
found from the edges and the barrier crossings together, so land
behind a barrier already has a way out. Each part but the largest by
area is linked to the nearest unit of another part and to the nearest
unit of the largest part, when they lie within gap. The links are
crossings of a rank above every barrier, which group_units() opens
with its cross_below, so land across a creek joins the nearest group
when it holds nothing, or less than cross_below, and a road is
crossed before a creek is jumped. A group joined across a gap is in
several pieces on the ground, which group_metrics() counts.
A link is only a way across, and group_units() decides whether a
group takes it. Its cross_below opens the links around land that
holds too little, and its n opens those a count needs. A
wider gap, or Inf, therefore joins nothing by itself. It lets a
part of islands reach its count, where a short gap leaves the islands
as groups of their own and group_units() warns.
A stretch of shared boundary is tested against the lines at its midpoint. On blocks cut from the same lines, every stretch lies either along a line or off it, so the test is exact there. On units drawn independently of the lines, a long stretch that only partly follows a line is counted whole or not at all.
See also
group_units() to group the units, check_groups() to
test a grouping against the graph, group_metrics() to measure
the boundary of each group by what it follows.
Other grouping and cutting functions:
dissolve_units(),
group_graph(),
group_units(),
order_units(),
split_units()
Examples
cells <- sf::st_make_grid(
sf::st_bbox(c(xmin = 0, ymin = 0, xmax = 500, ymax = 400)),
cellsize = 100
)
cells <- sf::st_sf(geometry = cells, crs = 32630)
cells$commune <- ifelse(sf::st_coordinates(sf::st_centroid(cells))[, 1] < 300,
"A", "B"
)
g <- unit_graph(cells, within = "commune")
g
#> <unit_graph>
#> 20 units, 27 edges, 2 parts of 'within'
#> 2 connected components, 0 units with no neighbor
#> pairs dropped: 0 on a barrier, 0 below min_shared = 0.02
head(g$edges)
#> from to shared visible
#> 1 1 2 100 NA
#> 2 1 6 100 NA
#> 3 2 3 100 NA
#> 4 2 7 100 NA
#> 5 3 8 100 NA
#> 6 4 5 100 NA