2017-09-12 9 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: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) 
関連する問題