取消R中的空间数据帧

时间:2016-05-02 14:11:46

标签: r list spatial

似乎是一个简单的问题,但我找不到解决方案。我试图取消列出空间数据帧,以便将它们写为单独的shapefile

来自以下lapply循环:

gridElide <- lapply(rotatelist, function(x) elide(splines, rotate = x, center=apply(bbox(splines), 1, mean)))

我将此作为输出:

[[1]]
class       : SpatialLinesDataFrame 
features    : 25 
extent      : 491678.9, 491758.6, 1151112, 1151209  (xmin, xmax, ymin, ymax)
coord. ref. : NA 
variables   : 4
names       :                x,                y,  id,             ndvi 
min values  : 491678.744193024, 1151112.05976997,  14, 393.353314621961 
max values  : 491688.194193024, 1151112.05976997, 224, 504.996093530448 

[[2]]
class       : SpatialLinesDataFrame 
features    : 25 
extent      : 491678.8, 491758.6, 1151112, 1151209  (xmin, xmax, ymin, ymax)
coord. ref. : NA 
variables   : 4
names       :                x,                y,  id,             ndvi 
min values  : 491678.744193024, 1151112.05976997,  14, 393.353314621961 
max values  : 491688.194193024, 1151112.05976997, 224, 504.996093530448 

......

然后我做:

#  Get the Lines objects which contain multiple 'lines'
ll0 <- lapply(gridElide , function(x) `@`(x , "lines") )

#  Extract the individual 'lines'
ll1 <- lapply( unlist( ll0 ) , function(y) `@`(y,"Lines") )

#  Combine them into a single SpatialLines object
Sl <- SpatialLines( list( Lines( unlist( ll1 ) , ID = 1)))

然而,数据帧信息丢失,所有功能都被解散为单个功能。我如何取消列出这些SpatialLinesDataframes并将它们作为单独的shapefile写入? 谢谢!

获取空间数据框列表的可重现代码:

library(maptools)

x1 = c(1,2,3,4)
y1 = c(5,6,7,8)
x2 = x1 + 10 * cos(0.9)
y2 = y1 + 10 * sin(0.9)

begin.coord <- cbind(x1, y1)
end.coord <- cbind(x2, y2)


rawlist <- vector("list", nrow(begin.coord))
for (i in seq_along(rawlist)) {
  rawlist[[i]] <- Lines(list(Line(rbind(begin.coord[i, ], end.coord[i,]))), as.character(i))
}

splines <- SpatialLines(rawlist)
data <- as.data.frame(c(1:4))
splinesdf <- SpatialLinesDataFrame(splines, data, match.ID = FALSE)

rotatelist <- list(-0.2, -0.1, 0.1, 0.2)
gridElide <- lapply(rotatelist, function(x) elide(splinesdf, rotate = x, center=apply(bbox(splinesdf), 1, mean)))

1 个答案:

答案 0 :(得分:0)

有点晚了,但是我遇到了同样的问题,并使用此问题(在这里找到解决方案:https://grokbase.com/t/r/r-help/08b6pxyfe0/r-unlist-dataframes):

#'unlist' spatial data frame and save to new vector without 
#losing data.frame information

new_vector <- do.call(rbind, gridElide)