You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
rbindlist() with list of sf objects doesn't combine different geometries #5486
I've check the NEWS/issues/stack overflow but not seen any mentions of rbindlist() in connection with this issue.
I want to make use of data.table's amazing speed :) in combining sf dataframes as in this example here, issue #798
However, when different geometries are involved, an error is returned on different class attributes of the geometry column in the sf dataframe. Here is a simple example:
library(sf)
# create a sf data frame with different geometries
st <- st_as_sf(data.frame(
id = 1:2,
geometry = st_as_sfc(c("LINESTRING(10 5, 9 4, 8 3, 7 2, 6 1)",
"MULTILINESTRING ((0 3, 0 4, 1 5, 2 5), (0.2 3, 0.2 4, 1 4.8, 2 4.8), (0 4.4, 0.6 5))"))))
st
# split into list
st.split <- split(st, ~id)
# attempt to recombine with rbindlist() - error
st2.dt <- data.table::rbindlist(st.split)
# Error in data.table::rbindlist(st.split) :
# Class attribute on column 2 of item 2 does not match with column 2 of item 1.
# rbind does work:
st2.rbind <- do.call(rbind, st.split)
st2.rbind
# Is data table checking these classes?
lapply(st.split, function(x) class(x$geometry))
A workaround can be to use st_cast() to unify geometries (eg to LINESTRING here, or more generally GEOMETRYCOLLECTION) but this is not alway practical.
Can this check on classes be changed for sf objects so that different geometries can be combined?
#Output of sessionInfo()
R version 4.2.1 (2022-06-23 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19044)
Activity
ben-schwen commentedon Oct 13, 2022
This looks like a duplicate to #3911
Cdevenish commentedon Oct 13, 2022
Yes, it's the same issue with a different use case. I'd favour an ignore class / attributes option as well with rbindlist()
ben-schwen commentedon Dec 15, 2023
Actually here comes another thing into play:
sfuses attributes andrbindlistdoes not retain them so it is also related to #5569