代码之家  ›  专栏  ›  技术社区  ›  Ben Bolker

为什么rbind()和bind_row()在组合这些sf对象时不起作用?

  •  0
  • Ben Bolker  · 技术社区  · 4 年前

    我有一些 sf 我想合并的对象。我把它们分成一个多边形( rtmp rtmp2 )为了这个问题的目的。 rbind() do.call(rbind,...) 看起来工作不错,但是 bind_rows() 不。显然我可以解决这个问题,但我缺少什么?

    这些工作:

    library(dplyr)  ## version 0.8.3
    library(sf)     ## version 0.8.0
    r1 <- rbind(rtmp,rtmp2)  ## works
    r2 <- do.call(rbind,list(rtmp,rtmp2)) ## works
    
    identical(r1,r2) ## TRUE
    all.equal(r1,r2) ## fails??
    

    错误:无法在“geometry”x“geometry”上联接,因为类型不兼容(sfc_POLYGON/sfc/sfc_POLYGON/sfc)

    这失败了:

    bind_rows(list(rtmp,rtmp2))
    

    尝试在get1index中选择少于一个元素
    1: 在绑定行中(x,.id): 矢量化“sfc_POLYGON”元素可能无法保留其属性 矢量化“sfc_POLYGON”元素可能无法保留其属性

    rtmp <- structure(list(PROVCODE = "ON", geometry = structure(list(structure(list(
        structure(c(7201954.77714286, 7206422.6, 7206659.96857143, 
        7206183.55714286, 7206456.25714286, 7206918.87142857, 7207300.78857143, 
        7207337.03428571, 7207488.91714286, 7207400.89428571, 7208110.25714286, 
        7208360.52, 7208664.28571429, 7208710.88571429, 7208819.62285714, 
        7209207.96, 7209308.06285714, 7209665.33428571, 7209425.42857143, 
        7209568.68285714, 7209522.08285714, 7209870.72285714, 7210022.60571429, 
        7210017.42857143, 7210321.19428571, 7210507.59714286, 7210630.14, 
        7210407.49142857, 7210535.21142857, 7210378.15142857, 7210921.82285714, 
        7210906.87428571, 7211255.58285714, 7211310.45142857, 7211675.00857143, 
        7212183.14285714, 7208783.07714286, 7209760.22, 7208297.23714286, 
        7207629.9, 7201954.77714286, 935407.037142857, 937804.568571429, 
        937788.902857143, 937063.325714286, 936599.088571429, 936431.897142857, 
        936498.582857143, 936341.52, 936410.56, 936134.408571429, 
        936103.34, 935840.997142857, 935891.048571429, 935715.002857143, 
        935972.168571429, 936172.38, 935989.428571429, 935885.871428571, 
        935349.102857143, 935278.337142857, 934976.297142857, 934831.317142857, 
        934964.217142857, 934770.911428571, 934691.517142857, 934978.022857143, 
        934460.24, 934237.594285714, 933921.745714286, 933302.131428571, 
        933074.305714286, 932540.708571429, 932384.642857143, 931995.7, 
        932099.337142857, 931815.214285714, 929976.52, 928202.56, 
        927651.188571429, 927094.268571429, 935407.037142857), .Dim = c(41L, 
        2L))), class = c("XY", "POLYGON", "sfg"))), class = c("sfc_POLYGON", 
    "sfc"), precision = 0, bbox = structure(c(xmin = 7201954.77714286, 
    ymin = 927094.268571429, xmax = 7212183.14285714, ymax = 937804.568571429
    ), class = "bbox"), crs = structure(list(epsg = NA_integer_, 
        proj4string = NA_character_), class = "crs"), n_empty = 0L)), row.names = c(NA, 
    -1L), sf_column = "geometry", agr = structure(c(PROVCODE = NA_integer_), .Label = c("constant", 
    "aggregate", "identity"), class = "factor"), class = c("sf", 
    "tbl_df", "tbl", "data.frame"))
    
    rtmp2 <- structure(list(PROVCODE = "ON", geometry = structure(list(structure(list(
        structure(c(6914891.13617828, 7022510.44138576, 7130129.74659325, 
        7130129.74659325, 7022510.44138576, 6914891.13617828, 6914891.13617828, 
        896361.565655993, 958495.600487533, 896361.565655993, 772093.495992912, 
        709959.461161371, 772093.495992912, 896361.565655993), .Dim = c(7L, 
        2L), .Dimnames = list(NULL, c("x", "y")))), class = c("XY", 
    "POLYGON", "sfg"))), class = c("sfc_POLYGON", "sfc"), precision = 0, bbox = structure(c(xmin = 6914891.13617828, 
    ymin = 709959.461161371, xmax = 7130129.74659325, ymax = 958495.600487533
    ), class = "bbox"), crs = structure(list(epsg = NA_integer_, 
        proj4string = NA_character_), class = "crs"), n_empty = 0L)), row.names = 277L, class = c("sf", 
    "data.frame"), sf_column = "geometry", agr = structure(c(PROVCODE = NA_integer_), .Label = c("constant", 
    "aggregate", "identity"), class = "factor"))
    
    0 回复  |  直到 4 年前
        1
  •  4
  •   Chuan    4 年前

    我以前也遇到过同样的问题 rbindlist sf 对象使用 st_as_sf()

    这对我有效:

    p <- data.table::rbindlist(list(rtmp,rtmp2),
                               use.names = TRUE,
                               fill = TRUE,
                               idcol = NULL)
    st_as_sf(p) 
    
    Simple feature collection with 2 features and 1 field
    geometry type:  POLYGON
    dimension:      XY
    bbox:           xmin: 7201955 ymin: 927094.3 xmax: 7212183 ymax: 937804.6
    epsg (SRID):    NA
    proj4string:    NA
      PROVCODE                       geometry
    1       ON POLYGON ((7201955 935407, 7...
    2       ON POLYGON ((6914891 896361.6,...