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

Lingering error from compareGeom gets printed later #1207

Closed
see24 opened this issue Jun 27, 2023 · 3 comments
Closed

Lingering error from compareGeom gets printed later #1207

see24 opened this issue Jun 27, 2023 · 3 comments

Comments

@see24
Copy link
Contributor

see24 commented Jun 27, 2023

I admit that the only way I could reproduce this was to copy a weird and potentially inadvisable use case but it is possible it could come up in other situations and was quite tricky to track down.
What happened was I have a list of rasters and a reference raster, they can have different resolution but need to have the same crs and origin. I was using do.call to pass the raster list into compareGeom and I inlcuded the reference raster (lc below) twice to cover a case when the list was empty. The compareGeom call returns TRUE and my code continues until awhile later I try to change one of the rasters to a factor and get an error about the resolution. I figured out that this was because there was an error hanging on from the compareGeom call in the SpatRaster object.

Ideally the error would surface in the compareGeom call and then I would at least know that my do.call approach was not working and opt for something simpler.

library(terra)

lc <- rast(xmin = 0, xmax = 25000, ymin = 0, ymax = 25000,
                  resolution = 250, crs = "EPSG:5070")
lc[] <- 1:ncell(lc)

lc500 <- rast(lc) |> `res<-`(c(500, 500))
lc500[] <- 1:ncell(lc500)
lc1000 <- rast(lc) |> `res<-`(c(1000, 1000))
lc1000[] <- 1:ncell(lc1000)

rastLst <- list(lc500, lc1000)

do.call(compareGeom, c(rastLst, list(lc, lc, crs = TRUE, res = FALSE, ext = FALSE, 
                          rowcol = FALSE, stopOnError = FALSE)))

# lc@ptr$getError()
# lc500@ptr$getError()
# lc1000@ptr$getError()

# Later on an unrelated call fails with a confusing error message
as.factor(lc500)

My probably better approach that did not return errors was

lapply(rastLst, terra::compareGeom,
       y = lc, crs = TRUE, res = FALSE, ext = FALSE, 
       rowcol = FALSE, stopOnError = FALSE) |>
  unlist() |> all()
@rhijmans
Copy link
Member

I would use sapply:

library(terra)
lc <- rast(xmin = 0, xmax = 25000, ymin = 0, ymax = 25000,
                  resolution = 250, crs = "EPSG:5070")
lc500 <- rast(lc) |> `res<-`(c(500, 500))
lc1000 <- rast(lc) |> `res<-`(c(1000, 1000))
rastLst <- list(lc500, lc, lc1000)

sapply(rastLst, \(r) terra::compareGeom(lc, r, stopOnError = FALSE))
#[1] FALSE  TRUE FALSE

# or 

sapply(rastLst, terra::compareGeom, y=lc, stopOnError = FALSE)
#[1] FALSE  TRUE FALSE

Your example use of compareGeom with do.call is a bit odd as you have rowcol=FALSE. With that argument the rasters have indeed the same geometry (extent and crs); as "res" is now ignored (for better or worse, but, either way, you also have "res=FALSE").

@rhijmans
Copy link
Member

I have added a compareGeom<SpatRaster,list> method: 1d8b22a

So that you can do

compareGeom(lc, rastLst, stopOnError = FALSE)

(That does not explain the delayed message)

@rhijmans
Copy link
Member

rhijmans commented Jul 3, 2023

Sorry for letting this linger a bit, but I think it works now. I can do

do.call(compareGeom, c(rastLst, list(lc, lc, stopOnError = FALSE)))
[1] FALSE
> as.factor(lc500)
class       : SpatRaster 
dimensions  : 50, 50, 1  (nrow, ncol, nlyr)
resolution  : 500, 500  (x, y)
extent      : 0, 25000, 0, 25000  (xmin, xmax, ymin, ymax)
coord. ref. : NAD83 / Conus Albers (EPSG:5070) 
source(s)   : memory
categories  : lyr.1 
name        : lyr.1 
min value   :     1 
max value   :  2500 

or

sapply(rastLst, terra::compareGeom, y = lc, stopOnError = T)
# FALSE FALSE

Alternatively, you can now use a list as argument (as well as SpatRasterCollections)

compareGeom(lc, rastLst, stopOnError = FALSE)
[1] FALSE

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