Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何使用sf::st_质心计算多边形的质心?_R_Polygon_Centroid_Sf - Fatal编程技术网

R 如何使用sf::st_质心计算多边形的质心?

R 如何使用sf::st_质心计算多边形的质心?,r,polygon,centroid,sf,R,Polygon,Centroid,Sf,我试图使用新的“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_ce

我试图使用新的“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) 
库(sf)
#不加载数据

文件路径所有作为
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)
库(tidyverse)
d$质心%
st_形心()%>%
#这是d的crs,没有EPSG代码:
st_变换(,'+proj=longlat+ellps=GRS80+no_defs')%>%
#由于需要第二个几何体中的质心:
st_几何()
#核对
绘图(st_几何(d))
绘图(d[,'质心'],加=T,列=红色,pch=19)

这不是错误,而是警告。这些值已创建。第二个plot()调用给出“不支持打印列表列”。我也无法在列中获得任何质心,不确定答案是否过时(它们经常更改sf包)。Hi@Deleet,看起来在现在绘图之前必须激活辅助几何列,例如
绘图(st_set_geometry(d,'质心')[,0],add=t,col='red',pch=19)
。我正在Windows环境中使用
sf
0.8-0。
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)