【问题标题】:How to append a shp file to modify map boundaries?如何附加 shp 文件来修改地图边界?
【发布时间】:2013-04-30 15:01:03
【问题描述】:

感谢@AriBFriedman 和@PaulHiemstra 的建议以及随后弄清楚如何合并 .shp 文件,我设法使用以下代码和数据生成了以下地图:

数据:

MoroccoWestern Sahara

代码:

    # 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 individually
    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()

    # Merged map of Morocco and Western Sahara

    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()

结果:

个人地图

合并地图:

问题:

我想消除穿过地图中间的边界?有人有任何建议和/或帮助吗?

谢谢大家

【问题讨论】:

    标签: r maps ggplot2 shapefile


    【解决方案1】:

    我确实实现了通过使用QGIS软件消除了合并后将地图一分为二的额外多边形层,该软件可在此链接免费下载QGIS

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-12
      • 1970-01-01
      • 2015-04-03
      • 1970-01-01
      • 2016-09-01
      相关资源
      最近更新 更多