2017-09-12 15 views
1

새로운 "sf"패키지를 사용하여 R에서 브라질 인구 센서 데이터를 조작하려고합니다. 나는 데이터를 가져올 수 있어요,하지만 난sf :: st_centroid를 사용하여 다각형의 중심을 계산하는 방법?

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 

어떻게 해결할 수 있습니까?

+1

이것은 오류가 아닙니다, 그것은 경고입니다. 값이 작성됩니다. –

답변

2

sf의 모든 GEOS 함수는 올바르게 작동하려면 투영 좌표가 필요하므로 적절하게 투영 된 데이터에서 st_centroid을 실행해야합니다. 나는 브라질의 사용 가능한 CRS의의에 대해 잘 모르겠지만, EPSG가 : 29,101 잘 작동이 나타납니다 :

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)