我想将两个 .shp 文件转换为一个数据库,这样我就可以将地图绘制在一起。
另外,有没有办法将 .shp 文件转换为 .csv 文件?我希望能够在 .csv 格式下个性化和添加一些对我来说更容易的数据。如果在地图上添加叠加产量数据和降水数据,我会想到什么。
绘制两个文件的代码:
# 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 中的社区很棒而且很有帮助,尤其是对像 :) 这样的初学者来说,只是想强调一下。