-
Notifications
You must be signed in to change notification settings - Fork 89
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
Indexing by other raster returns wrong results #325
Comments
On a similar note, raster allows to keep a set.seed(2017-12-16)
library(raster)
#> Loading required package: sp
elev1 = raster(nrow = 6, ncol = 6, res = 0.5,
xmn = -1.5, xmx = 1.5, ymn = -1.5, ymx = 1.5,
vals = 1:36)
r_mask1 = raster(nrow = 6, ncol = 6, res = 0.5,
xmn = -1.5, xmx = 1.5, ymn = -1.5, ymx = 1.5,
vals = sample(c(NA, TRUE), 36, replace = TRUE))
elev1[r_mask1, drop = FALSE]
#> class : RasterLayer
#> dimensions : 6, 6, 36 (nrow, ncol, ncell)
#> resolution : 0.5, 0.5 (x, y)
#> extent : -1.5, 1.5, -1.5, 1.5 (xmin, xmax, ymin, ymax)
#> crs : +proj=longlat +datum=WGS84 +no_defs
#> source : memory
#> names : layer
#> values : 1, 36 (min, max)
set.seed(2017-12-16)
library(terra)
#> terra version 1.4.2
elev2 = rast(nrow = 6, ncol = 6, resolution = 0.5,
xmin = -1.5, xmax = 1.5, ymin = -1.5, ymax = 1.5,
vals = 1:36)
r_mask2 = rast(nrow = 6, ncol = 6, resolution = 0.5,
xmin = -1.5, xmax = 1.5, ymin = -1.5, ymax = 1.5,
vals = sample(c(NA, TRUE), 36, replace = TRUE))
elev2[r_mask2, drop = FALSE]
#> lyr.1
#> 1 1
#> 2 2
#> 3 3
#> 4 4
#> 5 5
#> 6 6
#> 7 13
#> 8 14
#> 9 15
#> 10 16
#> 11 17
#> 12 18 Created on 2021-09-12 by the reprex package (v2.0.1) |
Thank you. I now get:
|
@rhijmans I found another two cases when set.seed(2017-12-16)
library(terra)
#> terra version 1.4.2
elev = rast(nrow = 6, ncol = 6, resolution = 0.5,
xmin = -1.5, xmax = 1.5, ymin = -1.5, ymax = 1.5,
vals = 1:36)
elev[1:2, drop = FALSE]
#> lyr.1
#> 1 1
#> 2 2
elev[1, 1:2, drop = FALSE]
#> lyr.1
#> 1 1
#> 2 2 Created on 2021-09-13 by the reprex package (v2.0.1) |
I could say that it does work:
But not as you expected. And what you expected is more interesting. Now I get:
And (selecting rows instead of cells)
Selecting columns
Selecting rows and columns
|
@rhijmans - yes, you are right. Results were different from my expectations. I also have another example of that - raster result is different from the terra one: # raster ------------------------------------------------------------------
set.seed(2017-12-16)
library(raster)
#> Loading required package: sp
elev1 = raster(nrow = 6, ncol = 6, res = 0.5,
xmn = -1.5, xmx = 1.5, ymn = -1.5, ymx = 1.5,
vals = 1:36)
r_mask1 = raster(nrow = 6, ncol = 6, res = 0.5,
xmn = -1.5, xmx = 1.5, ymn = -1.5, ymx = 1.5,
vals = sample(c(NA, TRUE), 36, replace = TRUE))
r1 = elev1[r_mask1, drop = FALSE]
plot(r1) # terra -------------------------------------------------------------------
set.seed(2017-12-16)
library(terra)
#> terra version 1.4.2
elev2 = rast(nrow = 6, ncol = 6, resolution = 0.5,
xmin = -1.5, xmax = 1.5, ymin = -1.5, ymax = 1.5,
vals = 1:36)
r_mask2 = rast(nrow = 6, ncol = 6, resolution = 0.5,
xmin = -1.5, xmax = 1.5, ymin = -1.5, ymax = 1.5,
vals = sample(c(NA, TRUE), 36, replace = TRUE))
r2 = elev2[r_mask2, drop = FALSE]
plot(r2) Created on 2021-09-14 by the reprex package (v2.0.1) |
Thank you that is very helpful. |
Oh, wait, but what if the rasters do not align? In that case, it should be crop only, I suppose? |
I would think that the |
@rhijmans @Nowosad, this does not seem entirely fixed yet. While you can replace values based on an index raster with a single value, in terra you can't update the values in a way that depends on the values themselves (e.g. for cells that meet this condition multiply them by 2), while this is possible in raster. I've included an example below and you can see that raster provides the expected results while terra outputs a warning and a different result.
|
I made some changes and now both Alex's and Jakub's example have the same results with raster and terra. I would not be surprised if I broke something else though. |
Also to point out the alternative route with |
Awesome, thanks! |
@rhijmans, it is not quite NA proof yet.
|
Thank you very much Alex. I believe that this last and all the prior examples now work in I would reiterate that this type of notation is nice for quick interactive use, but not for production scripts, and certainly not for functions in a package. They are fragile and not they are not memory safe (will fail with large files). I would use |
@rhijmans, thanks this works now. I did not realize however that this method is not memory safe. I'll update code in my own functions to avoid this syntax. I have also used
|
Yes, that should be memory safe. In the first case, |
Thanks! |
The raster package allowed for extracting values of one raster based on the other with the use of the
[
operator (see the top example).However, the same approach does not correctly work for terra (see the bottom example). Is it a bug, or should the users not use this syntax?
Created on 2021-09-12 by the reprex package (v2.0.1)
The text was updated successfully, but these errors were encountered: