Skip to content
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

spatvectorcollection does not behave as a list #1515

Closed
jedgroev opened this issue May 22, 2024 · 2 comments
Closed

spatvectorcollection does not behave as a list #1515

jedgroev opened this issue May 22, 2024 · 2 comments

Comments

@jedgroev
Copy link

I do like that there is a class of spatvectorcollection, however, the functionality is not exactly what I expected. I would expect it to behave as a list (and thus similar to a spatraster with multiple bands). There are two issues:

(ISSUE 1): list names are not inherited and added automatically to the collection (minor)
(ISSUE 2): spatvectorcollection cannot be filtered using the names of the datasets

I am using terra 1.7.71, the most recent version available on CRAN.

Here is an example:

library(terra)
terra 1.7.71

# sample data
f <- system.file("ex/lux.shp", package="terra")
v <- vect(f)
# create two vector datasets
v <- vect(f, query="SELECT NAME_1, NAME_2, ID_2 FROM lux WHERE ID_2 < 4") 
v2 <- vect(f, query="SELECT NAME_1, NAME_2, ID_2 FROM lux WHERE ID_2 > 5")

# make list of the two vector datasets 
v_list <- list(v,v2)
names(v_list) <- c('A0','A1')

# make collection 
collection <- svc(v_list)

# (ISSUE 1) I would have expected that the names would be inherited from the names given to each item in the list
# add names to collection 
names(collection) <- names(v_list)

# (ISSUE 2) It is not possible to subset a collection by using the names of the datasets within the collection
collection$A1
Error in collection$A1 : $ operator not defined for this S4 class

names(collection)
[1] "A0" "A1"
@rhijmans
Copy link
Member

Thanks, I now get

library(terra)
#terra 1.7.79
f <- system.file("ex/lux.shp", package="terra")
v1 <- vect(f, query="SELECT NAME_1, NAME_2, ID_2 FROM lux WHERE ID_2 < 4") 
v2 <- vect(f, query="SELECT NAME_1, NAME_2, ID_2 FROM lux WHERE ID_2 > 5")
v_list <- list(A0=v1, A1=v2)
collection <- svc(v_list)

names(collection)
#[1] "A0" "A1"
collection$A1
# class       : SpatVector 
# geometry    : polygons 
# dimensions  : 7, 3  (geometries, attributes)
# extent      : 5.810482, 6.528252, 49.44781, 49.85403  (xmin, xmax, ymin, ymax)
# source      : lux.shp
# coord. ref. : lon/lat WGS 84 (EPSG:4326) 
# names       :       NAME_1       NAME_2  ID_2
# type        :        <chr>        <chr> <num>
# values      : Grevenmacher   Echternach     6
#               Grevenmacher       Remich     7
#               Grevenmacher Grevenmacher    12

@jedgroev
Copy link
Author

I just noticed that plotting the collection is also not possible:

library(terra)
#terra 1.7.79
f <- system.file("ex/lux.shp", package="terra")
v1 <- vect(f, query="SELECT NAME_1, NAME_2, ID_2 FROM lux WHERE ID_2 < 4") 
v2 <- vect(f, query="SELECT NAME_1, NAME_2, ID_2 FROM lux WHERE ID_2 > 5")
v_list <- list(A0=v1, A1=v2)
collection <- svc(v_list)
terra::plot(collection)
Error in as.double(y) : 
  cannot coerce type 'S4' to vector of type 'double'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants