-
Notifications
You must be signed in to change notification settings - Fork 998
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refresh attributes of sfc column when subsetting from data.table #4217
Comments
I'm having a similar problem with a custom class that extends |
This comment has been minimized.
This comment has been minimized.
library(data.table)
abc_index <- function(x) {
x <- as.character(x)
first <- tolower(substr(x, 1, 1))
index <- match(first, letters)
attr(index, "original") <- first
class(index) <- c("abc_index", "integer")
index
}
`[.abc_index` <- function(x, i) {
abc_index(attr(x, "original")[i])
}
DF <- data.frame(a = abc_index(c("p", "k", "?")))
DT <- data.table(a = abc_index(c("p", "k", "?")))
all.equal(DF[, "a"], DT[, a])
# [1] TRUE
attr(DF[2:3, "a"], "original")
# [1] "k" "?"
attr(DT[2:3, a], "original")
# [1] "p" "k" "?"
|
This may be a little late and not necessarily useful, but I wrote a short function that uses your helpful workaround @jplecavalier. library(data.table)
library(sf)
#> Linking to GEOS 3.9.0, GDAL 3.2.1, PROJ 7.2.1
set.seed(20200130)
data <- data.table(
id = 1:5,
point = st_sfc(replicate(5, st_point(c(runif(1), runif(1))), simplify = FALSE))
)
subset <- data[1:2]
st_identity <- function(x) {
idx <- x[, which(unlist(lapply(.SD, function(x) "sfc" %in% class(x))))]
if (length(idx) == 0) stop("No geometry column is present.", call. = F)
crs <- lapply(idx, function(y) st_crs(x[, y, with = F][[1]]))
names(crs) <- as.character(idx)
for (i in idx) x[, (i) := st_sfc(lapply(x[, i, with = F][[1]], identity), crs = crs[[as.character(i)]])]
}
attributes(data$point)
#> $precision
#> [1] 0
#>
#> $bbox
#> xmin ymin xmax ymax
#> 0.09302893 0.07300738 0.96152443 0.71613790
#>
#> $crs
#> Coordinate Reference System: NA
#>
#> $n_empty
#> [1] 0
#>
#> $class
#> [1] "sfc_POINT" "sfc"
st_identity(subset)
attributes(subset$point)
#> $class
#> [1] "sfc_POINT" "sfc"
#>
#> $precision
#> [1] 0
#>
#> $bbox
#> xmin ymin xmax ymax
#> 0.09302893 0.65609871 0.43876379 0.71613790
#>
#> $crs
#> Coordinate Reference System: NA
#>
#> $n_empty
#> [1] 0 Created on 2021-04-17 by the reprex package (v2.0.0) |
Just filed r-spatial/sf#1660, which I think is the same issue |
I like to use the
sf
package anddata.table
together. Instead of using thesf
class, which is built on top of thedata.frame
class, I use thedata.table
class with a column of classsfc
which contains a vector of simple features with some metadata kept in the object's attributes.However, when I subset some rows of my
data.table
, those attributes are not automatically updated, which can cause some problems. When subsetting from ansf
, those attributes are automatically updated.I have a non-elegant way to get around the problem by manually updating the attributes of the
sfc
column, but I'm wondering if there is a way to make it under the hood.Let's say I have the following
Let's look at the attributes of the
point
column.If I subset some elements, the attributes won't update.
However, if I use an
sf
object, they will silently update (you can see it easily with thebbox
attribute).I don't understand everything behind how
data.table
works, but I'm wondering if there is a way you could update the attributes of anysfc
column present in adata.table
. An easy way of doing so would be to wrap a generalization of something like this.We would then achieve the same result than when using an
sf
object.Like I said, I'm not familiar with the development of
data.table
, I'm really just a user who don't like to use any other data structure thandata.table
. You will excuse me if I'm missing something on why it is impossible to implement such feature.The text was updated successfully, but these errors were encountered: