Skip to contents

Computes a weighted centroid of a polygon or a set of points. The location of a weighted centroid depends on the attributes of an underlying spatial weights dataset.

Usage

wt_centroid(x, weights, ...)

# S3 method for class 'SpatVector'
wt_centroid(x, weights, ...)

# S3 method for class 'sf'
wt_centroid(x, weights, ...)

# S3 method for class 'sfc'
wt_centroid(
  x,
  weights,
  power = 1,
  reg_threshold = 10,
  reg_n = 100,
  of_largest_polygon = TRUE,
  parallel = FALSE
)

Arguments

x

A spatial vector dataset (sf, sfc, SpatVector) containing POINTs, MULTIPOINTs, POLYGONs or MULTIPOLYGONs. If point geometries are provided, computes a single centroid for the entire area. If polygons are provided, computes a centroid for each polygon area. In this case, points are generated either from the unweighted centroids of cell centers of weights or from regular point sampling (see reg_threshold).

weights

A spatial raster (SpatRaster or stars) or spatial vector dataset ( sf, SpatVector) containing weights to determine the location of weighted centroids. The first layer or column is interpreted as the weight feature. Note that it is recommended to use stars objects to optimize performance.

power

Numeric value that scales the impact of weighting.

  • power = 0 computes unweighted centroids

  • 0 < power < 1 reduces the impact of weighting

  • power = 1 computes normal weighted centroids

  • power > 1 increases the impact of weighting

reg_threshold

If weights is a vector dataset, determines the number of data points in weights (per polygon) below which to replace the existing points with a regularized point grid. This is useful if the polygons are too large to be reasonably represented by a centroid. If 0, disables regularization.

reg_n

If reg_threshold is greater than 0, specifies approximately how many points the regular grid should consist of.

of_largest_polygon

If TRUE and x contains MULTIPOLYGONs, computes centroids only of the largest polygons. Mirrors the behavior of the same argument in st_centroid.

parallel

Whether to use parallelized centroid computation. If TRUE, uses the future.apply package to compute centroids asynchronously for each geometry. Can be useful for datasets with many (i.e. 500 or more) polygons. Defaults to FALSE.

Value

An object of class sfc with the same length as x. If an sf or SpatVector object is provided, only replaces the geometries and returns the otherwise original dataset.

Details

The equation used to compute weighted centroids is as follows:

$$ C_x = \frac{\sum_{i = 1}^{n}{w_{i}^\alpha x_i}}{\sum_{i = 1}^{n} {w_{i}^\alpha}} $$

$$ C_y = \frac{\sum_{i = 1}^{n}{w_{i}^\alpha y_i}}{\sum_{i = 1}^{n} {w_{i}^\alpha}} $$

where:

  • \(C_x\) and \(C_y\) are the x and y coordinates of the weighted centroids

  • \(x_i\) and \(y_i\) are the x and y coordinates of each input point in a region

  • \(w_i\) is the weight assigned to each coordinate pair \(x_i, y_i\) (\(w_i > 0\))

  • \(\alpha\) is the power argument (\(\alpha >= 0\))

  • \(n\) is the number of points in a region

If sf::st_is_longlat(x), the centroid is calculated assuming a spherical earth. This approach is taken from the centr package, which in turn is analogous to the ArcGIS Pro tool "Mean Center".

Progress bars

This function implements the progressr standard for progress bars. You can wrap the call using with_progress to show a progress bar. This works for all methods except random_cpp.

Examples

germany <- giscoR::gisco_get_nuts(country = "DE", nuts_level = 2)
popgrid <- wp_data("DEU")

cent <- wt_centroid(germany, popgrid)
terra::plot(popgrid)
plot(sf::st_centroid(germany)$geometry, pch = 16, col = "red", add = TRUE)
#> Warning: st_centroid assumes attributes are constant over geometries
plot(cent$geometry, pch = 16, col = "green", add = TRUE)