Computes the centroid of a polygon and returns its coordinates as a numeric vector. The attribute-geometry relationship is set to "constant" to suppress sf::st_centroid() warnings.

calc_centroid(poly)

Arguments

poly

An sf or sfc polygon object.

Value

A numeric vector of centroid coordinates (x, y).

Examples

poly <- sf::st_sf(
  id = 1,
  geometry = sf::st_sfc(
    sf::st_polygon(list(rbind(c(0, 0), c(2, 0), c(2, 2), c(0, 2), c(0, 0)))),
    crs = 4326
  )
)
calc_centroid(poly)
#> [1] 1.000000 1.000051