【发布时间】:2018-12-21 02:46:16
【问题描述】:
我有一个 sf 对象列表,我想对其进行行绑定以创建单个 sf 对象。我正在寻找一个类似于data.table::rbindlist 的函数,它可以有效地堆叠各个对象。
可重现示例的数据:
my_list <- structure(list(structure(list(idhex = 4L, geometry = structure(list(
structure(c(664106.970004623, 6524137.38910266), class = c("XY",
"POINT", "sfg"))), class = c("sfc_POINT", "sfc"), precision = 0, bbox = structure(c(xmin = 664106.970004623,
ymin = 6524137.38910266, xmax = 664106.970004623, ymax = 6524137.38910266
), class = "bbox"), crs = structure(list(epsg = 32633L, proj4string = "+proj=utm +zone=33 +datum=WGS84 +units=m +no_defs"), class = "crs"), n_empty = 0L)), row.names = 1L, class = c("sf",
"data.frame"), sf_column = "geometry", agr = structure(c(idhex = NA_integer_), .Label = c("constant",
"aggregate", "identity"), class = "factor")), structure(list(
idhex = 9, geometry = structure(list(structure(c(665491.220375992,
6525002.7560692), class = c("XY", "POINT", "sfg"))), class = c("sfc_POINT",
"sfc"), precision = 0, bbox = structure(c(xmin = 665491.220375992,
ymin = 6525002.7560692, xmax = 665491.220375992, ymax = 6525002.7560692
), class = "bbox"), crs = structure(list(epsg = 32633L, proj4string = "+proj=utm +zone=33 +datum=WGS84 +units=m +no_defs"), class = "crs"), n_empty = 0L)), row.names = 1L, class = c("sf",
"data.frame"), sf_column = "geometry", agr = structure(c(idhex = NA_integer_), .Label = c("constant",
"aggregate", "identity"), class = "factor"))), .Dim = 1:2, .Dimnames = list(
".", NULL))
请注意,data.table 和 sf 库还不完全兼容。所以rbindlist函数返回了一个不被识别为`sf对象的对象。
single_sf <- rbindlist(my_list)
class(single_sf)
【问题讨论】:
-
虽然您可以轻松转换为 sf(如果 crs 相同)
sf::st_as_sf(data.table::rbindlist(my_list)) -
为了完整起见,@timelyportfolio 还写了
mapedit:::combine_list_of_sf来实现这一点。也可能值得监视github.com/r-spatial/mapedit/issues/46,因为专用的bind_rows_sf可能会在某个阶段在 sf 中实现。
标签: r list data.table rbind sf