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
Ways to combine categorical rasters [Feature Request] #663
Comments
If I understand you well, I do not think that
But it fails if there are conflicting levels (1 is "forest" in
|
I now see that you are asking for something else. You would like to combine the (active) categories, creating new categories. I will look at that too. |
Now I can do:
And note that with numeric values, you can do
|
@rhijmans, awesome! And thanks for the tip with unique too! |
Hi there, i am currently trying to achieve this myself, and i get the same warning message since i have conflicting categories. Is there a way to work around this? Essentially, i have two rasters, with r1 providing two additional levels of a factor that i would like to add to r2. However, other levels in r1 conflict with those in r2. is there some way i can use the cover function but retain the category labels? |
@Jackson2601 you should be able to adapt the code below. If there are conflicting ID's between levels you can reclassify the values and then add the factor labels back in. library(terra)
#> Warning: package 'terra' was built under R version 4.3.1
#> terra 1.7.39
set.seed(0)
r <- rast(nrows=10, ncols=10)
values(r) <- sample(3, ncell(r), replace=TRUE)
levels(r) <- data.frame(id=1:3, cover=c("forest", "water", "urban"))
r[1:5, ] <- NA
rr <- rast(r)
values(rr) <- sample(1:2, ncell(rr), replace=TRUE)
levels(rr) <- data.frame(id=1:2, cover=c("green", "blue"))
plot(c(r,rr), main=c("r", "rr")) #Loss of factor levels since 1 corresponds to forest and green amd 2 corresponds to both water and blue
a <- cover(r, rr)
#> Warning: [cover] cannot merge categories of layer 1
levels(a)
#> [[1]]
#> [1] ""
rm(a)
# We need to redefine factor ID's
rcl<- matrix(data = c(1,2,4,5), ncol = 2)
rcl #reclassification matrix says to change 1 to 4 and 2 to 5
#> [,1] [,2]
#> [1,] 1 4
#> [2,] 2 5
rr<- classify(rr, rcl) # Change 1 to 4 and 2 to 5
levels(rr) <- data.frame(id=4:5, cover=c("green", "blue")) #remap green and blue to new values
# Green is now 4 and blue is 5
levels(rr)
#> [[1]]
#> id cover
#> 1 4 green
#> 2 5 blue
a <- cover(r, rr)
levels(a)
#> [[1]]
#> id cover
#> 1 1 forest
#> 2 2 water
#> 3 3 urban
#> 4 4 green
#> 5 5 blue
plot(a)
text(a) Created on 2023-07-18 with reprex v2.0.2 |
You've no idea how much i appreciate you taking the time to write this. Thanks so much, you're a life saver! |
I think having some built in ways to combine multiple categorical maps into a single thematic map could be really useful. Possibly by having the
+
operator behave differently if the raster is detected to be a factor or by having a dedicated function. I've included an example below showing how this can be done with data frames, how it can currently be done interra
(if you don't have more than 10 classes; though maybe there's a better way I'm not aware of), as well as some proposed syntax. The example I have is just combining two variables, but could be extended out to any number of factor variables.The text was updated successfully, but these errors were encountered: