【问题标题】:GIS in R - Show only part of area that crosses with other areaR中的GIS - 仅显示与其他区域交叉的部分区域
【发布时间】:2014-02-18 12:47:43
【问题描述】:

我正在尝试绘制乔治亚州的奥塞特斯地区。这工作正常,以下代码:

setwd("/my/path/")

library(sp)
library(maps)
library(maptools)
library(ggplot2)
library(rgdal)

greg <- readOGR("../GlobalData/GREG/", "GREG", 
                verbose = TRUE, stringsAsFactors = FALSE)

borders <- readOGR("/path/to/cshapes/", "cshapes", 
                   verbose = TRUE, stringsAsFactors = FALSE)

georgia <- borders[borders$COWCODE == 372,]
ossetes <- greg[greg$G1ID == 849,]

georgia.df <- fortify(georgia)
ossetes.df <- fortify(ossetes)

tblisi <- georgia@data # access data from the shapefile from here
tblisi["group"] <- 91.1

p <- ggplot(georgia.df, aes(x = long, y = lat, group = group)) + 
  geom_polygon(aes(x=long,y=lat,group=group), fill="white", colour="grey") +
  # Add capital city
  geom_point(data=tblisi,aes(CAPLONG,CAPLAT),colour="black",size=4) +
  geom_text(data=tblisi, aes(CAPLONG, CAPLAT, label=CAPNAME),hjust=1, vjust=-0.6) +
  # Add Ossetes
  geom_path(data = ossetes.df, aes(x=long,y=lat,group=group), fill="white", colour="grey") +
  # Styling
  labs(x=" ", y=" ") + 
  theme_bw() + 
  theme(panel.grid.minor=element_blank(), panel.grid.major=element_blank()) + 
  theme(axis.ticks = element_blank(), axis.text.x = element_blank(), axis.text.y = element_blank()) + 
  theme(panel.border = element_blank())

p

返回下图:

我想做的只是显示佐治亚州的 Ossetes 地区。 但我目前不知道如何压制国家边界以外的区域(从第比利斯直上的部分,与国家边界分开)。

关于如何做到这一点的任何想法?

【问题讨论】:

标签: r mapping gis geospatial


【解决方案1】:

正如@JoshObrien 所说,gIntersection(...) 是正确的选择。

library(sp)
library(ggplot2)
library(rgdal)
library(rgeos)
setwd("<directory with shapefiles...>")

greg <- readOGR(dsn=".", layer="GREG", verbose = TRUE, stringsAsFactors = FALSE)
borders <- readOGR(dsn=".", layer="GEO_adm0", verbose = TRUE, stringsAsFactors = FALSE)
ossetes <- greg[greg$G1ID == 849,]
ossetes.df <- fortify(ossetes)
georgia.df <- fortify(borders)

intersect <- gIntersection(borders,ossetes)
intersect.df <- fortify(intersect)
ggplot()+
  geom_path(data=intersect.df, aes(x=long,y=lat,group=group), colour="blue")+
  geom_path(data=georgia.df, aes(x=long,y=lat,group=group), colour="red")+
  coord_fixed()

顺便说一句:以后请提供指向您的 shapefile 的链接。 GREGGeorgia

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-19
    • 1970-01-01
    • 2022-10-17
    • 2012-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多