2015-08-28 105 views
0

我正在爲區域內的各個學校創建地圖,並且想要在學校邊界周圍創建陰影區域。我可以使用QGIS手動完成,但是希望能夠在R中生成類似的東西。我現在可以在地圖上繪製一些人口統計變量,添加學校位置和邊界。有一個循環會自動爲每所學校創建一張新地圖,並將其插入到knitr報告中。將陰影圖層添加到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) 
+0

這將是困難的,你這個節目沒有你的shape文件,但嘗試http://stackoverflow.com/questions/29624895/how-to-add-a-hole-跟着一起在空間多邊形數據框中爲空間切出一個洞然後使用'gUnaryUnion'這樣的東西來分解被遮擋的多邊形。 – hrbrmstr

回答

0

我已經從這個添加顏色代碼到數據文件想出一個辦法問題:R - stuck with plot() - Colouring shapefile polygons based upon a slot value

[email protected]$filter <- NA 
[email protected]@data$filter[[email protected]$Code == SCH] <- 1 
[email protected]@data$filter[[email protected]$Code != SCH] <- 0 

[email protected]$COLOUR <- "#FFFFFF" 
[email protected]@data$COLOUR[(as.numeric(as.character([email protected]$filter)) %% 10) == 0] <- "white" 
[email protected]@data$COLOUR[(as.numeric(as.character([email protected]$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 = [email protected]$COLOUR)