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) containingPOINTs,MULTIPOINTs,POLYGONs orMULTIPOLYGONs. 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 ofweightsor from regular point sampling (seereg_threshold).- weights
A spatial raster (
SpatRasterorstars) 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 = 0computes unweighted centroids0 < power < 1reduces the impact of weightingpower = 1computes normal weighted centroidspower > 1increases the impact of weighting
- reg_threshold
If
weightsis a vector dataset, determines the number of data points inweights(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_thresholdis greater than 0, specifies approximately how many points the regular grid should consist of.- of_largest_polygon
If
TRUEandxcontainsMULTIPOLYGONs, computes centroids only of the largest polygons. Mirrors the behavior of the same argument inst_centroid.- parallel
Whether to use parallelized centroid computation. If
TRUE, uses thefuture.applypackage to compute centroids asynchronously for each geometry. Can be useful for datasets with many (i.e. 500 or more) polygons. Defaults toFALSE.
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
powerargument (\(\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)
