I'm after the centroid of a group of points. I expected st_centroid
to return the centroid, but it appears to just return the original set of points. It seems the solution to this should be easy to find, but cannot find answer in a good hunt around stack overflow.
How can I get st_centroid
to return the centroid - therefore a single point in the centre of a cluster of points.
library(dplyr)
library(sf)
df <- read.table(header=TRUE, text= "site lat long
site1 41.21 -115.11
site2 45.3 -112.31
site3 41.15 -115.15
site4 41.12 -115.19")
df_sf <- st_as_sf(df, coords = c('long', 'lat'))
st_centroid(df_sf)
Are you after 1 centroid of your whole dataset?
If so, consider combining all your points to one feature using a sf::st_combine()
call before calculating the centroid.
Because an example is more than words:
library(dplyr)
library(sf)
df <- read.table(header=TRUE, text= "site lat long
site1 41.21 -115.11
site2 45.3 -112.31
site3 41.15 -115.15
site4 41.12 -115.19")
df_sf <- st_as_sf(df, coords = c('long', 'lat'))
st_centroid(df_sf) # your original code; not quite what we want...
# Simple feature collection with 4 features and 1 field
# Geometry type: POINT
# Dimension: XY
# Bounding box: xmin: -115.19 ymin: 41.12 xmax: -112.31 ymax: 45.3
# CRS: NA
# site geometry
# 1 site1 POINT (-115.11 41.21)
# 2 site2 POINT (-112.31 45.3)
# 3 site3 POINT (-115.15 41.15)
# 4 site4 POINT (-115.19 41.12)
df_mod <- df_sf %>%
st_combine()
st_centroid(df_mod) # would this fit your expectations better?
# Geometry set for 1 feature
# Geometry type: POINT
# Dimension: XY
# Bounding box: xmin: -114.44 ymin: 42.195 xmax: -114.44 ymax: 42.195
# CRS: NA
# POINT (-114.44 42.195)