ここ数日、マッピングに R を使用することに真っ先に夢中になりました。私はモデリングなどでRを広く使用していますが、この種の作業はこれまでにありませんでした。シェープファイル、その読み取り方法などに関して、いくつか質問と問題があります。
オーストラリア統計局から形状ファイルをダウンロードしました。州の境界、郵便番号、都市などを含む多数のファイルがあります。シェープ ファイルは膨大で、オーストラリアの州境には約 180 万の座標ポイントがあり、私が試したもう 1 つのファイルは、800 万を超える統計エリアです。このファイルは R のセットアップには大きすぎるため、何もしませんでした。
で形状ファイルを読み込んで、そのreadShapePoly
ように変換しました
AUS@data$id = rownames(AUS@data)
AUS.points = fortify(AUS, region="id")
AUS.df = join(AUS.points, AUS@data, by="id")
州境の形状ファイルをSpatialPolygonsDataFrame
通常のデータフレームに変換したら、正常にプロットできましたが、時間がかかりすぎて詳細が大きすぎました。thinnedSpatialPoly
単純化するために使用することを考えましたが、エラーが発生します:
Error in stopifnot(length(Sr@polygons) == nrow(data)) :trying to get slot "polygons" from an object of a basic class ("NULL") with no slots
どのグーグルが私を助けることができません。
私の次の戦略は、それを SAS に読み込み、これを使用proc greduce
してファイルを取得し、密度フィールドを作成することでした。ポリゴンの密度を選択できます。
proc mapimport out=states datafile='\Digital Boundaries\States\Shape file\STE_2011_AUST.shp';
id ste_code11; run;
proc greduce data = states out = reduced_states;
id ste_code11; run;
SASにはくだらないグラフィックがあり、私のために物事をプロットすることさえできなかったので、データセットをエクスポートし、データフレームをサブセット化してプロットで使用したい新しい密度フィールドでRに読み戻しました。
私の問題は、Rでプロットすると、これが得られることです
ggplot(data=states.df, aes(X, Y, group=SEGMENT)) +
geom_polygon(colour='black', fill='white') + theme_bw()
ポリゴンが乱れているのか壊れているのか?この関数を使用してポリゴンを再結合しようとしましたが、まだうまくいきません
RegroupElements <- function(df, longcol, idcol){
g <- rep(1, length(df[,longcol]))
if (diff(range(df[,longcol])) > 300) { # check if longitude within group differs more than 300 deg, ie if element was split
d <- df[,longcol] > mean(range(df[,longcol])) # we use the mean to help us separate the extreme values
g[!d] <- 1 # some marker for parts that stay in place (we cheat here a little, as we do not take into account concave polygons)
g[d] <- 2 # parts that are moved
}
g <- paste(df[, idcol], g, sep=".") # attach to id to create unique group variable for the dataset
df$group.regroup <- g
df
}
### Function to close regrouped polygons
# Takes dataframe, checks if 1st and last longitude value are the same, if not, inserts first as last and reassigns order variable
ClosePolygons <- function(df, longcol, ordercol){
if (df[1,longcol] != df[nrow(df),longcol]) {
tmp <- df[1,]
df <- rbind(df,tmp)
}
o <- c(1: nrow(df)) # rassign the order variable
df[,ordercol] <- o
df
}
それでは、最後に質問です!大規模で過度に詳細な形状ファイルをどのように扱うのでしょうか? なぜ、thinnedspatialpoly が機能しなかったのですか (可能であれば SAS を避けたいのですが)? プロットががらくたのように見えないようにするにはどうすればよいですか?
最後に私のR仕様:
R version 2.15.1 (2012-06-22)
Platform: x86_64-pc-mingw32/x64 (64-bit)
locale:
[1] LC_COLLATE=English_Australia.1252 LC_CTYPE=English_Australia.1252
[3] LC_MONETARY=English_Australia.1252 LC_NUMERIC=C
[5] LC_TIME=English_Australia.1252
attached base packages:
[1] grid stats graphics grDevices utils datasets methods
[8] base
other attached packages:
[1] gridExtra_0.9 gpclib_1.5-1 ggmap_2.1 maptools_0.8-16
[5] lattice_0.20-6 rgeos_0.2-7 plyr_1.7.1 stringr_0.6
[9] ggplot2_0.9.1 sp_0.9-99 shapefiles_0.6 foreign_0.8-50
[13] fastshp_0.1-0
loaded via a namespace (and not attached):
[1] colorspace_1.1-1 dichromat_1.2-4 digest_0.5.2 labeling_0.1
[5] MASS_7.3-18 memoise_0.1 munsell_0.3 png_0.1-4
[9] proto_0.3-9.2 RColorBrewer_1.0-5 reshape2_1.2.1 RgoogleMaps_1.2.0
[13] rjson_0.2.8 scales_0.2.1 tools_2.15.1