R-如何使用不同的空间数据编写嵌套的for循环

时间:2019-03-31 22:04:53

标签: r for-loop geospatial nested-loops

我需要遍历多功能SpatialPolygonsDataFrame(在此为SPDF),并删除每个多边形与单功能SpatialLinesDataFrames(SLDF)列表中包含的SpatialLines相交的位置,并保存更新的'已将SLDF删除到新列表。如果一条线与两个不同的多边形要素相交,我希望创建两个更新的“擦除” SLDF并将其添加到新的SLDF列表中。在下面我提供的示例中,SLDF仅与一个SPDF相交,除了其中一个SLDF与两个不同的SPDF多边形要素相交。因此,更新后的列表应包含一个附加的SLDF元素。

但是,当我运行一个嵌套的for循环时,生成的“已擦除” SLDF列表包含与原始SLDF列表相同数量的元素。我认为我的循环结构有问题,但是我无法弄清楚。

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

#my nested for-loop attempt to get resulting SLDF list 
line_erasel <- list()
#iterate thru all SPDF features
for (i in seq_along(p_sub2)) {
#iterate thru all SLDF list elements
  for (j in seq_along(lcp_list)) {
#if a SLDF intersects with a SPDF feature, execute erase function
    if (tryCatch(!is.null(raster::intersect(lcp_list[[j]], p_sub2[i,])), 
error=function(e) return(FALSE)) == 'TRUE'){
      #erase part of line overlapped by intersected polygon and add to new list
      line_erasel[[i]] <- erase(lcp_list[[j]],p_sub2[i,])
}}

0 个答案:

没有答案