Skip to contents

measure_units() returns one size per unit, from a raster of counts such as a gridded population, or from points such as census localities or buildings. The result is the size that group_units() and split_units() take.

With a raster, each cell's count is shared among the units that cover it. When inside gives where the count sits, such as building footprints, a cell's count goes to the units holding those features, in proportion to their weight, so blocks without buildings get nothing (dasymetric allocation). Cells with no feature fall back to the area share, or send their count to the features of nearby cells within reach.

Usage

measure_units(
  x,
  raster = NULL,
  points = NULL,
  inside = NULL,
  weight = NULL,
  reach = 0L
)

Arguments

x

An sf object (or sfc) of polygons in a projected CRS.

raster

A terra::SpatRaster with one layer, or the path to a raster file. It may be in any CRS, since x is transformed to it. Missing values count as 0.

points

An sf or sfc of points in the CRS of x, used instead of raster. Polygons such as footprints are accepted, and each goes to the unit holding most of its area.

inside

For raster, the features that say where the count of each cell sits, such as building footprints, the fields under a cropland raster or the forest under a stock raster, as an sf or sfc of polygons or of points in the CRS of x, or NULL (default) for the area share. points holds what is counted, and inside where the count of a raster cell sits, as in split_units().

weight

The weight of each feature of points or inside, as a column name or a numeric vector, or NULL (default). NULL gives each feature of points a weight of 1, polygons included, so each footprint counts once. For inside, it gives each point a weight of 1 and each polygon its area. Building points that carry their footprint area, as in Google Open Buildings, take that column. A weight is the quantity a feature stands for, not a survey weight.

reach

For inside, how many rings of cells the count of a cell with no feature may move to reach features, a nonnegative integer. The default 0 leaves it in place, shared by area. With 100 m cells, reach = 1 moves it by at most 141 m, from center to center.

Value

A numeric vector with one value per unit of x, in the units of the raster or of weight. Sizes from a raster are estimates, not counts, and are not rounded.

Details

The area share gives each unit the count of every cell it covers times the share of the cell's area it covers, as in the WorldPop preEA method. It conserves cell totals, so admin totals add up, but it puts people on parks, markets and fields wherever blocks are smaller than cells, as in town centers with 100 m cells.

With inside, such as buildings, the count of a cell that holds buildings is shared among the buildings by weight, and each unit receives the shares of the buildings it holds, a building cut by a unit line going to the unit holding most of its area. Buildings outside x keep their share, so a cell on the edge of x sends outside the part of its count that sits on buildings outside. Cell totals are conserved in both cases. This is a dasymetric allocation (Mennis 2003) with inside as the ancillary layer.

A cell can hold a count and no building when the raster and the buildings come from different sources, as when WorldPop masked its raster with an older building layer. Its count is shared by area, which puts it on whatever units cover the cell. With reach = k, it goes instead to the buildings of the nearest cells that hold any, within k rings, shared by weight as above. Only a cell with no building within k rings falls back to the area share. Totals are conserved either way.

With points, each unit receives the weights of the points it holds. A point on the line between two units counts once, in the unit assign_points() gives it, whatever the row order. Points outside x are left out.

Sizes are sums. A raster of densities, such as persons per square kilometer, gives counts once multiplied by the area of its cells, as in pop * terra::cellSize(pop, unit = "km"). A class raster, such as the Degree of Urbanisation, is carried to units with exactextractr::exact_extract(raster, x, "majority") after transforming x to the raster's CRS.

With inside, the raster is read into memory over the bounding box of x. Where that box covers at least half of the raster's cells, the whole raster is read, so a national raster is best measured admin unit by admin unit, each call reading only its own box.

raster needs the terra and exactextractr packages.

References

Mennis, J. (2003). Generating surface models of population using dasymetric mapping. The Professional Geographer, 55(1), 31-42. doi:10.1111/0033-0124.10042

See also

split_units() for where the size sits within units, and the flexurba package for the Degree of Urbanisation grid.

Other input functions: assign_points(), assign_units(), build_blocks(), delineate_settlements()

Examples

grid <- sf::st_make_grid(
  sf::st_as_sfc(sf::st_bbox(c(xmin = 0, ymin = 0, xmax = 200, ymax = 100),
    crs = 32630
  )),
  n = c(2, 1)
)
houses <- sf::st_sfc(
  sf::st_point(c(20, 30)), sf::st_point(c(60, 50)),
  sf::st_point(c(150, 80)),
  crs = 32630
)
measure_units(grid, points = houses)
#> [1] 2 1
measure_units(grid, points = houses, weight = c(5, 3, 4))
#> [1] 8 4

if (requireNamespace("terra", quietly = TRUE) &&
  requireNamespace("exactextractr", quietly = TRUE)) {
  ## One cell of 90 people over both units
  pop <- terra::rast(xmin = 0, xmax = 200, ymin = 0, ymax = 100,
    resolution = c(200, 100), crs = "EPSG:32630", vals = 90
  )
  measure_units(grid, raster = pop)
  measure_units(grid, raster = pop, inside = houses)
}
#> [1] 60 30