R-从行迭代删除空间多边形数据框并保存到新列表

时间:2019-04-01 20:01:34

标签: r geospatial nested-loops sp spatial-data-frame

original SLDF are thick grey (n=4), and the red and black lines (n=5) are what I want to end up with in the new list

我需要遍历多功能SpatialPolygonsDataFrame(此处为SPDF),并删除每个多边形与单功能SpatialLinesDataFrames(SLDF)列表中包含的SpatialLines相交的位置,并保存更新的'将“ SLDF”删除到新列表,同时将每个功能的一些信息添加到每个列表元素。在我包括的图中,原始的SLDF是厚灰色(n = 4),红线和黑线(n = 5)是我要在新列表中添加的内容。

#example data prep:
library(sp)

#create  multi-feature SpatialPolygonDataFrame
p <- 
SpatialPolygons(list(Polygons(list(Polygon(cbind(c(2,4,3,2),c(2,2,4,2)))), 
"1"),

Polygons(list(Polygon(cbind(c(12,14,13,12),c(0,0,2,0)))), "2"),

Polygons(list(Polygon(cbind(c(15,14,12,15),c(12,13,12,12)))), "3"),

Polygons(list(Polygon(cbind(c(0,2,1,0),c(12,12,14,12)))), "4"))) 
# Create a dataframe and display default rownames
p.df <- data.frame( ID=1:length(p)) 
rownames(p.df)
# Extract polygon ID's
pid <- sapply(slot(p, "polygons"), function(x) slot(x, "ID")) 
# Create dataframe with correct rownames
p.df <- data.frame( ID=1:length(p), row.names = pid)   
# coersion and check class
p <- SpatialPolygonsDataFrame(p, p.df)
class(p) 

#create list of single-feature SpatialLineDataFrame
l1 <- cbind(c(0,3), c(0,3))
l2 <- cbind(c(0, 13), c(0, 1))
l3 <- cbind(c(0, 14), c(0,12.5))
l4 <- cbind(c(0, 1), c(0,13))

Sl1 <- Line(l1)
Sl2 <- Line(l2)
Sl3 <- Line(l3)
Sl4 <- Line(l4)

Sl1 <- Lines(list(Sl1), ID = "1")
Sl2 <- Lines(list(Sl2), ID = "2")
Sl3 <- Lines(list(Sl3), ID = "3")
Sl4 <- Lines(list(Sl4), ID = "4")

Sl <- SpatialLines(list(Sl1, Sl2, Sl3, Sl4))

# Create a dataframe and display default rownames
Sl.df <- data.frame( ID=1:length(Sl)) 
rownames(Sl.df)
# Extract polygon ID's
pidl <- sapply(slot(Sl, "lines"), function(x) slot(x, "ID")) 
# Create dataframe with correct rownames
Sl.df <- data.frame( ID=1:length(Sl), row.names = pidl)   
# Try coersion again and check class
Sldf <- SpatialLinesDataFrame(Sl, Sl.df)

#convert multipart SpatialLineDataFrame feature to individual features in 
list
linel <- list()
linel[[1]] <- Sldf[1,]
linel[[2]] <- Sldf[2,]
linel[[3]] <- Sldf[3,]
linel[[4]] <- Sldf[4,]

#for-loop attempt
line_erasel <- list()
for (i in seq_along(p)) {
  for (j in seq_along(linel)) {
#for the LCP lines that do not intersect with a specific polygon, do nothing 
#and move to next step, i.e, else...
    if (tryCatch(!is.null(raster::intersect(linel[[j]], p[i,])), 
    error=function(e) return(FALSE)) == 'TRUE'){
#erase part of line overlapped by intersected polygon
      line_erasel[[i]] <- erase(linel[[j]],p[i,])
#add original START/PARENT polygon id back to new list
      line_erasel[[i]]@data["id.parent"] <- linel[[j]]@data$ID
#add intersected polygon ID to the line attribute table 
      line_erasel[[i]]@data["id.intersect"] <- p[i,]@data$ID  }
  }}

如果一条线与两个(或多个)不同的多边形要素相交,我想结束时要创建两个(或=相交的多边形数)更新的“擦除” SLDF,并将其添加到新的SLDF列表中。在下面我提供的示例中,SLDF仅与一个SPDF相交,除了其中一个SLDF与两个不同的SPDF多边形要素相交。因此,更新后的列表应包含一个附加的SLDF元素。

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

昨天我发布了一个非常类似的问题,其中包含更复杂的示例数据(此处:R - how to write nested for-loop with different spatial data.frames),但这是可复制粘贴运行的数据集。仍然会导致相同的问题。

0 个答案:

没有答案