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

fix error in set.names() #1333

Merged
merged 1 commit into from Nov 11, 2023
Merged

Conversation

dfriend21
Copy link
Contributor

set.names() wasn't working properly for SpatRasterDataset or SpatRasterCollection - here's a example testing set.names with the different Spat* classes:

library(terra)

# SpatRaster ----
r <- c(rast(), rast())
set.names(r, c("a", "b"))
names(r)
#> [1] "a" "b"

# SpatRasterDataset ----
r <- sds(rast(), rast())
set.names(r, c("a", "b"))
#> Warning in 1:nlyr(x): numerical expression has 2 elements: only the first used
#> Error in if (length(value) != nlyr(x)) {: the condition has length > 1
names(r)
#> [1] "" ""

# SpatRasterCollection ----
r <- sprc(rast(), rast())
set.names(r, c("a", "b"))
#> Warning in 1:nlyr(x): numerical expression has 2 elements: only the first used
#> Error in if (length(value) != nlyr(x)) {: the condition has length > 1
names(r)
#> [1] "" ""

# SpatVector ----
v <- vect(cbind(0,0), atts = data.frame(x = 1, y = 2))
set.names(v, c("a", "b"))
names(v)
#> [1] "a" "b"

.raster_names_check() was being used to check the names, which uses nlyr to make sure the length is correct - this was causing an error for a SpatRasterDataset and SpatRasterCollection. I made a more generic .names_check function that accepts a function (lengthfx) for determining the length of the object - and then for SpatRasterDataset and SpatRasterCollection I passed length to lengthfx. With these changes, the above example works for all classes.

@rhijmans rhijmans merged commit 1ccadec into rspatial:master Nov 11, 2023
@rhijmans
Copy link
Member

Thank you, that looks very good.

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

Successfully merging this pull request may close these issues.

None yet

2 participants