【问题标题】:Binding multiple shapefiles results in rownames error绑定多个 shapefile 会导致行名错误
【发布时间】:2020-12-01 09:02:42
【问题描述】:

我有一个包含大约 20 个 shapefile 的列表,我想将它们绑定到一个中。这些 shapefile 有不同数量的字段 - 有些有 1,有些有 2。示例如下所示:

 # 1 field 
 > dput(head(shp[[1]]))
  structure(list(area = c(1.60254096388, 1.40740270051, 0.093933438653, 
  0.609245720277, 22.892748868, 0.0468096597394)), row.names = 0:5, class = "data.frame")

 # 2 fields 
> dput(head(shp[[3]]))
  structure(list(per = c(61, 70, 79, 90, 57, 66), area = c(2218.8, 
  876.414, 2046.94, 1180.21, 1779.12, 122.668)), row.names = c(0:5), class = "data.frame")

我使用下面的代码来绑定它们,它就像我想要的那样工作:

merged<- raster::bind(shp, keepnames= FALSE, variables = area)
writeOGR(merged, './shp', layer= 'area', driver="ESRI Shapefile")

但是,我现在需要对列表中的其中一个 shapefile 进行子集化。我是这样做的:

shp[[3]]@data <- shp[[3]]@data %>% subset(Area >= 50)
names(shp[[3]]@data)[names(shp[[3]]@data) == "Area"] <- "area"

但是,当我运行 bind 命令时,现在这给了我一个错误:

merged<- raster::bind(shp, keepnames= FALSE, variables = area)

Error in `.rowNamesDF<-`(x, value = value) : invalid 'row.names' length
Calls: <Anonymous> ... row.names<- -> row.names<-.data.frame -> .rowNamesDF<-
Execution halted

我不知道为什么会这样。 shapefile 没有改变,它们只是被子集化了。我尝试按如下所示的方式删除行名,但仍然抛出相同的错误。

rownames(shp[[3]]@data) <- NULL

会是什么?

【问题讨论】:

  • 如果没有可重现的示例,很难确切知道发生了什么。如果您愿意从raster 切换,我对使用sf 包的这些类型的操作是多么容易印象深刻。见this答案

标签: r raster shapefile sp


【解决方案1】:

我认为问题在于您对@data(属性)进行了子集化,但您应该对整个对象进行子集化。像这样的

x <- shp[[3]]  # for simplicity
x <- x[x$Area >= 50, ]
names(x)[names(x) == "Area"] <- "area"
shp[[3]] <- x

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-25
    • 1970-01-01
    • 2013-11-29
    • 1970-01-01
    • 2013-09-23
    • 2014-06-07
    • 2021-01-25
    • 1970-01-01
    相关资源
    最近更新 更多