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

terra::as.points() with the option na.rm=TRUE removes points as soon as a value is missing in any layer of a raster stack #548

Closed
jldupouey opened this issue Feb 20, 2022 · 2 comments

Comments

@jldupouey
Copy link

The behavior of the na.rm=TRUE option in the as.points() and as.polygons() functions when x is a raster stack is not specified in the help page for these functions. It is not clear whether (1) only cells with missing values in all layers are ignored, or (2) a cell is ignored as soon as a value is missing, in any layer of the raster stack. The user has to do a test to know this. It would be more efficient to indicate this behavior in the na.rm entry of the help page.

This is the choice (2) that was made. I wonder now if this is really the best choice. It seems to me that the most common situation is that a user considers any cell with at least one non-NA value in any layer to be worth keeping in the function output.

Moreover, if we want to keep the cells with at least one non-NA value, in the current situation we have first to extract all cells from the raster stack (with the na.rm=FALSE option) and then make the appropriate selection, which is time and volume consuming. Conversely, if the functions as.points() and as.polygons() returned all cells, as soon as they have at least one non-NA value in a layer, it would be quick to then remove the ones that don't have all their values non-NA, if the need arises.

But I guess it's too late to change the behavior of this option, and that the choice has been carefully thought out.

@rhijmans
Copy link
Member

rhijmans commented Apr 6, 2022

I have added a new argument, na.all=FALSE (the current behavior). Setting it to TRUE only removes points (or polygons) if the cell is NA in all layers:

library(terra)
r1 <- rast(ncol=10, nrow=10, vals=1)
r1[1:5] <- NA
r2 <- r1 + 1
r2[1:10] <- NA
r3 <- r1 + 2
r3[1:20] <- NA

x <- c(r1, r2, r3)
p1 = as.points(x, na.rm=T, na.all=TRUE)
p2 = as.points(x, na.rm=T, na.all=FALSE)

x1 = as.polygons(x, dissolve=F, na.rm=T, na.all=TRUE)
x2 = as.polygons(x, dissolve=F, na.rm=T, na.all=FALSE)

plot(r1)
lines(x1, lwd=2)
lines(x2, col="red", lwd=2)
points(p1)
points(p2, col="red")

points

@jldupouey
Copy link
Author

Thanks, it is useful.

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