How to calculate centroid of polygon using sf::st_centroid?
All the GEOS functions underlying sf
need projected coordinates to work properly, so you should run st_centroid
on appropriately projected data. I don't know much about Brazil's available CRS's, but EPSG:29101 appears to work fine:
library(tidyverse)d$centroids <- st_transform(d, 29101) %>% st_centroid() %>% # this is the crs from d, which has no EPSG code: st_transform(., '+proj=longlat +ellps=GRS80 +no_defs') %>% # since you want the centroids in a second geometry col: st_geometry()# check withplot(st_geometry(d))plot(d[, 'centroids'], add = T, col = 'red', pch = 19)