Skip to contents

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 sf object (or sfc) 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 x or 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 sf or sfc object in the CRS of x. 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 barrier or 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, and group_units() opens the lowest first where barriers enclose too little (its cross_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 sf or sfc object in the CRS of x. Usually the lines given to build_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 x or a vector, NA for units in none, such as the settlement of each block from build_blocks() or the stand of each piece of a forest stand. Units with one label that touch form a patch, and group_units() keeps a patch in one group when it fits under the caps. NULL (default) for none.

min_shared

The smallest shared boundary that makes two units adjacent, as a share of the shorter of their two perimeters. The default 0.02 drops 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.001 is 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 from build_blocks(). See Details.

between

Whether to record the pairs of units of different parts of within that share a boundary, FALSE (default) or TRUE. 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 barrier or visible line when its midpoint is within this distance of the line, in CRS units. The default 0.1 is ten centimeters in a metric CRS, which covers the rounding of coordinates by build_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 within part, such as land across a creek, is linked to the nearest unit of the rest. 0 (default) links nothing, and Inf links 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 of x, in the same order, holding area, perimeter, the centroid cx and cy, within, patch, outer, the length of boundary shared with no unit of the same part (a within line or the edge of the layer), and barrier, the length shared with units across a barrier.

  • edges, a data frame with one row per adjacent pair, holding from and to (row numbers of x, from < to), shared, the shared boundary length, and visible, the part of it on a visible line (NA without visible lines).

  • between, with between = TRUE, the pairs of units of different parts that share a boundary, with from, to, shared, barrier (whether a barrier line runs along it) and rank, and NULL otherwise.

  • crossings, the pairs removed because their shared boundary lies on a barrier, with from, to and shared as in edges, rank, the highest barrier rank along it, and gap, NA for these. With gap, the links across gaps follow, with a shared of 0, a rank of Inf and the distance in gap. 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, as check_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, tolerance and crs.

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