首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用sf::st_centroid计算多边形的质心?

如何用sf::st_centroid计算多边形的质心?
EN

Stack Overflow用户
提问于 2017-09-12 20:40:45
回答 1查看 16.3K关注 0票数 13

我正在尝试使用新的"sf“包来操纵R中的一些巴西人口普查数据。我可以导入数据,但在尝试创建原始面的质心时出现错误

代码语言:javascript
复制
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) 

现在我尝试创建一个包含列" geometry“的质心的新几何列,但得到一个错误:

代码语言:javascript
复制
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

我该如何解决这个问题呢?

EN

回答 1

Stack Overflow用户

发布于 2017-10-04 15:15:35

所有sf底层的GEOS函数都需要投影坐标才能正常工作,因此您应该对适当的投影数据运行st_centroid。我对巴西可用的CRS了解不多,但EPSG:29101似乎工作得很好:

代码语言:javascript
复制
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)
票数 18
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46176660

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档