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

Faster way to check if a SpatRaster has at least one non-NA value #1540

Closed
kevinwolz opened this issue Jun 11, 2024 · 1 comment
Closed

Faster way to check if a SpatRaster has at least one non-NA value #1540

kevinwolz opened this issue Jun 11, 2024 · 1 comment

Comments

@kevinwolz
Copy link

To check if a SpatRaster (x) has at least one non-NA value, I am currently using this:

any(as.numeric(terra::global(x, fun = "notNA")[,1]) > 0)

This works on SpatRasters that have one or multiple layers. It works fine, but I think terra::global is checking the entire raster. I don't need to check the entire raster, and, for large rasters, this can take a long time. I'm looking for a method that stops when it finds the first non-NA value. This would save considerable time when checking a large SpatRaster.

Would it be possible to implement something like fun = "anynotNA" for terra::global?

For context, the reason I am doing this check in the first place is because I am using terra::trim() in a situation where the raster may or may not have non-NA values. When applying terra::trim() to a SpatRaster with only NA values, it throws an error, so I must check to ensure there is at least one non-NA value before applying terra::trim(). Perhaps there is a better way to deal with this in this specific example? Either way, I think this feature would be useful in other contexts as well.

@rhijmans
Copy link
Member

I have added functions "anyNA", "anynotNA" that ought to be more efficient.

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