[英]Do the values returned by rgeos::gCentroid() and sf::st_centroid() differ?
[英]How to calculate centroid of polygon using sf::st_centroid?
我試圖使用新的“sf”包來操縱R中的一些巴西人口普查數據。 我能夠導入數據,但是當我嘗試創建原始多邊形的質心時出現錯誤
library(sf)
#Donwload data
filepath <- 'ftp://geoftp.ibge.gov.br/organizacao_do_territorio/malhas_territoriais/malhas_de_setores_censitarios__divisoes_intramunicipais/censo_2010/setores_censitarios_shp/ac/ac_setores_censitarios.zip'
download.file(filepath,'ac_setores_censitarios.zip')
unzip('ac_setores_censitarios.zip')
d <- st_read('12SEE250GC_SIR.shp',stringsAsFactors = F)
現在我嘗試創建一個包含“幾何體”列的質心的新幾何列,但是會收到錯誤:
d$centroid <- st_centroid(d$geometry)
Warning message:
In st_centroid.sfc(d$geometry) :
st_centroid does not give correct centroids for longitude/latitude data
我怎么解決這個問題?
基礎sf
所有GEOS函數都需要投影坐標才能正常工作,因此您應該在適當投影的數據上運行st_centroid
。 我對巴西現有的CRS不太了解,但EPSG:29101似乎運行良好:
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 with
plot(st_geometry(d))
plot(d[, 'centroids'], add = T, col = 'red', pch = 19)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.