将阴影图层添加到R中的贴图

时间:2015-08-28 15:08:56

标签: r gis maptools

我正在为一个区内的各个学校创建地图,并希望在学校边界周围创建一个阴影区域。我可以使用QGIS手动完成它但是希望能够在R中生成类似的东西。我可以在邻域级别绘制一些人口统计变量,添加学校位置和边界。有一个循环会自动为每个学校创建一个新地图并将其插入到编织报告中。

我无法弄清楚如何创建一个会遮挡边界外区域的图层。这是我用R:

的地图

但是想让它看起来更像这样:

我已经尝试过GISTools的add.masking函数,但它基于点的核密度,而不是多边形。

我的代码看起来像这样(道歉因为它们是安全驱动器上的形状文件而无法重现。

# Load Shape files
proj<- CRS("+init=epsg:32617")

schoolBound <- readShapePoly("J:/GIS/Data/Catchments/ElementaryAreas.shp", proj4string= proj)
schools <- readShapePoints("J:/GIS/Data/Schools/ElementarySchools.shp", proj4string= proj)
roads <- readShapePoints("J:/GIS/Data/Roads/MajorRoads.shp") 
da <- readShapePoly("J:/GIS/Data/Dissemination Areas 2011/da2011.shp", 
                       proj4string = proj) 

SCH <- "School A"

# Subset shape files for area around school

school1Bound <- schoolBoundR [schoolBoundR$SchoolName == paste(SCH),] 
school1BoundBuff <- gBuffer(school1Bound, width = 15) # Adds buffer around geometry
school1Point <- schoolsR [schoolsR$Name == paste(SCH),] 
roadsBound <- roadsR[apply(gIntersects(roadsR, school1BoundBuff, byid = TRUE),2,any),]

# Plot map and add layers

plot(school1BoundBuff) 
plot(da,
     add = TRUE,
     col=colours[findInterval(da$sri, brks,all.inside=TRUE)], # Adds colour palette to Social Risk Index
     axes=F)
plot(school1Point, add = TRUE, pch = 15, col = "blue")
plot(roadsBound, add = TRUE, col = "gray60")
plot(school1Bound, add = TRUE, lwd = 5)

1 个答案:

答案 0 :(得分:0)

我已经通过在此问题中向数据文件添加颜色代码找到了一种方法:R - stuck with plot() - Colouring shapefile polygons based upon a slot value

schoolBound@data$filter <- NA
schoolBound@@data$filter[bound@data$Code == SCH] <- 1
schoolBound@@data$filter[bound@data$Code != SCH] <- 0

schoolBound@data$COLOUR <- "#FFFFFF"
schoolBound@@data$COLOUR[(as.numeric(as.character(bound@data$filter)) %% 10) == 0] <- "white"
schoolBound@@data$COLOUR[(as.numeric(as.character(bound@data$filter)) %% 10) == 1] <- NA

## Add layers to plot
plot(school1BoundBuff) 
plot(da,
     add = TRUE,
     col=colours[findInterval(da$sri, brks,all.inside=TRUE)], # Adds colour palette to Social Risk Index
     axes=F)
plot(school1Point, add = TRUE, pch = 15, col = "blue")
plot(roadsBound, add = TRUE, col = "gray60")
plot(schoolBound, add = TRUE, col = schoolBound@data$COLOUR)