2 つの .shp ファイルを 1 つのデータベースに変換して、マップを一緒に描画できるようにしたいと考えています。
また、.shp ファイルを .csv ファイルに変換する方法はありますか? .csv 形式で簡単にデータをパーソナライズして追加できるようにしたいと考えています。地図にオーバーレイ収量データと降水量データを追加する場合、私が念頭に置いていること。
2 つのファイルをプロットするコード:
# This is code for mapping of CGE_Morocco results
# Loading administrative coordinates for Morocco maps
library(sp)
library(maptools)
library(mapdata)
# Loading shape files
Mor <- readShapeSpatial("F:/Purdue University/RA_Position/PhD_ResearchandDissert/PhD_Draft/Country-CGE/MAR_adm1.shp")
Sah <- readShapeSpatial("F:/Purdue University/RA_Position/PhD_ResearchandDissert/PhD_Draft/Country-CGE/ESH_adm1.shp")
# Ploting the maps (raw)
png("Morocco.png")
Morocco <- readShapePoly("F:/Purdue University/RA_Position/PhD_ResearchandDissert/PhD_Draft/Country-CGE/MAR_adm1.shp")
plot(Morocco)
dev.off()
png("WesternSahara.png")
WesternSahara <- readShapePoly("F:/Purdue University/RA_Position/PhD_ResearchandDissert/PhD_Draft/Country-CGE/ESH_adm1.shp")
plot(WesternSahara)
dev.off()
@AriBFriedman と @PaulHiemstra からの提案を調べ、その後 .shp ファイルをマージする方法を見つけた後、次のコードとデータを使用して次のマップを作成することができました (.shp データについては、上記のリンクを参照してください)。
コード:
# Merging Mor and Sah .shp files into one .shp file
MoroccoData <- rbind(Mor@data,Sah@data) # First, 'stack' the attribute list rows using rbind()
MoroccoPolys <- c(Mor@polygons,Sah@polygons) # Next, combine the two polygon lists into a single list using c()
summary(MoroccoData)
summary(MoroccoPolys)
offset <- length(MoroccoPolys) # Next, generate a new polygon ID for the new SpatialPolygonDataFrame object
browser()
for (i in 1: offset)
{
sNew = as.character(i)
MoroccoPolys[[i]]@ID = sNew
}
ID <- c(as.character(1:length(MoroccoPolys))) # Create an identical ID field and append it to the merged Data component
MoroccoDataWithID <- cbind(ID,MoroccoData)
MoroccoPolysSP <- SpatialPolygons(MoroccoPolys,proj4string=CRS(proj4string(Sah))) # Promote the merged list to a SpatialPolygons data object
Morocco <- SpatialPolygonsDataFrame(MoroccoPolysSP,data = MoroccoDataWithID,match.ID = FALSE) # Combine the merged Data and Polygon components into a new SpatialPolygonsDataFrame.
Morocco@data$id <- rownames(Morocco@data)
Morocco.fort <- fortify(Morocco, region='id')
Morocco.fort <- Morocco.fort[order(Morocco.fort$order), ]
MoroccoMap <- ggplot(data=Morocco.fort, aes(long, lat, group=group)) +
geom_polygon(colour='black',fill='white') +
theme_bw()
結果:
新しい質問:
1- マップを半分にカットする境界データを削除する方法は?
2- .shp ファイル内で異なるリージョンを結合する方法は?
ありがとうございます。
PS: stackoverflow.com のコミュニティは素晴らしく、非常に役に立ちます。特に :) のような初心者向けです。