R - crop LIST of Least Cost Paths intersected with SpatialPolygonsDataFrame objects

心已入冬 提交于 2020-01-16 08:13:06

问题


I have a series of steps I need to complete on a LIST of SpatialLinesDataFrame (=least cost paths flowing 'downslope' created using ] gdistance; 'LCPs' herein) objects based on their relationships with individual features within a MULTI-FEATURE SpatialPolygonsDataFrame ('polygons') object.

To summarize, each LCP element in the list originates inside a single polygon feature, and may or may not pass through one or more other polygon features as it 'flows' downhill. I want to create new SpatialLinesDataFrame element to connect origin polygons to the first point of contact for each individual polygon intersected by the LCP element. So, each LCP element may become multiple new line features (n=number of intersected polygons). Ideally each new line feature would be saved as an element in a new list.

I would like to do this efficiently as my LCPs and polygon features are numerous. I am new to R and not a programmer so, apart from a good solution from the step-wise procedure above, I would like to know how to reference each LIST element and polygon FEATURE within the code.

fyi I asked a similar question but simplified my example data too much. A great solution using the sf pkg was provided, but I need something a little different given my LCPs are not straight lines. Original question with for-loop code attempt and solution is here: R - nested loop for list of SpatialLinesDataFrame intersected with SpatialPolygonsDataFrame objects.

library(rgdal)
library(raster)
library(rgeos)
library(sp) 
library(gdistance)

#Reproducible example data prep:
#'RDCO Regional Parks' data can be downloaded here: https://data-rdco.opendata.arcgis.com/datasets?group_ids=1950175c56c24073bb5cef3900e19460 
parks <- readOGR("/Users/rachelfield/Documents/UBC/Data/Regional Districts/RDCO/RDCO_Regional_Parks/RDCO_Regional_Parks.shp")

#DEM data downloaded here: https://pub.data.gov.bc.ca/datasets/175624/82e/ (files for '082e14_w.dem')
dem <- raster("/path/to/example/data/082e14_w.dem")
demproj <- "+proj=longlat +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +no_defs"

#reproject parks to match dem
p <- spTransform(parks, demproj)

#subset of parks data to reduce for example
e <- extent(dem)
p_crop <- crop(p, e)
p_sub <- p_crop[p_crop@data$Shapearea > 100000,]
p_sub2 <- p_sub[p_sub@data$CommonName != "Mission Creek Greenway Regional Park",]
#fyi I delete polygon [7,] because violates rules of my actual data (polygons do not touch)

#create polygon centroids and convert to SpatialPointsDataFrame using data from each 'origin' polygon data.frame
p_cent <- gCentroid(p_sub, byid = TRUE)
p_centdf <- SpatialPointsDataFrame(p_cent, data = data.frame(p_sub), match.ID = FALSE)

#manually create approx location of lowest elevation cell
lowest <- SpatialPoints(coords = cbind(-119.47,49.86), proj4string = CRS("+proj=longlat +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +no_defs"))

#find LCPs from 'origin' polygon centroids to lowest elevation cell
tr <- transition(dem, transitionFunction=function(x) 1/min(x), directions=8) 
trCost <- geoCorrection(tr, type="c")
#run shortestPath (LCP) analysis for all polygon centroids
lcp_list <- list()
for(i in 1:nrow(p_centdf)){
  origin <- p_centdf[i,]
  #find LCP from each centroid
  lcp <- shortestPath(trCost, origin, goal = lowest, output="SpatialLines")
  #convert LCP SpatialLines to SpatialLinesDataFrame object type, and preserve ID from original centroid 
  lcp_list[[i]] <- SpatialLinesDataFrame(lcp, data = data.frame(p_centdf[i,]), match.ID = FALSE)
}

#plot example data
plot(dem)
plot(p_sub2,add=T, border = 'green')
plot(p_cent, add=T, pch = 19)
plot(lowest, add=T, pch=18, cex=2)
lapply(lcp_list, plot, add=T, lty=3)
legend(-119.24, 49.96, legend=c("centroid", "lowest", "LCP"), pch=c(19,18,NA), lty=c(NA,NA,3), cex=1)

Below is a figure showing A) an LCP (i.e., from centroid of origin polygon to lowest elevation cell = triangle); and B) what I want the LCP to be split into after the above steps are completed. In this figure, the result would be two SpatialLinesDataFrames features; one in RED from the border of the origin polygon to the first point of contact with the first intersected polygon; the second in GREEN from the border of the origin polygon to the first point of contact with the second intersected polygon (an so on if more polygons were intersected in another example).

来源:https://stackoverflow.com/questions/53952083/r-crop-list-of-least-cost-paths-intersected-with-spatialpolygonsdataframe-obje

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!