Skip to contents

Function for finding a centroid and ensuring that it is within the polygon for irregularly shaped polygons This function returns an sf point object.

Usage

st_centroid_within_poly(poly)

Arguments

poly

the sf object containing polygons that you want centroids for

Examples

nc = sf::st_read(system.file("shape/nc.shp", package="sf"))
#> Reading layer `nc' from data source 
#>   `/home/runner/.cache/R/renv/library/kwt-ab773eaf/linux-ubuntu-noble/R-4.5/x86_64-pc-linux-gnu/sf/shape/nc.shp' 
#>   using driver `ESRI Shapefile'
#> Simple feature collection with 100 features and 14 fields
#> Geometry type: MULTIPOLYGON
#> Dimension:     XY
#> Bounding box:  xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
#> Geodetic CRS:  NAD27
nc_centroid = st_centroid_within_poly(nc)
#> Warning: st_centroid assumes attributes are constant over geometries
#> Warning: st_point_on_surface may not give correct results for longitude/latitude data