rspatial/terra

Should cellFromXY() return multiple cell numbers in some cases?

Closed this issue · 1 comments

When a coordinate is exactly in the corner of four cells, should cellFromXY return all four? It is a bit misleading otherwise. I was confused about some results until I realized that cellFromXY was just picking one of the cells and returning it.

library(terra)
r <- rast(ncols=4, nrows=2, xmin=-180, xmax=180, ymin=-90, ymax=90)

cellFromXY(r, matrix(c(0,0),ncol=2)) # Returns 7

But we can see that cellFromXY could have returned cells 1,3,6, or 7.

plot(as.polygons(r))
plot(as.points(r),add=TRUE) #as.points internally (in the cpp code) calls xyFromCell
plot(vect(matrix(c(0,0),ncol=2),crs='EPSG:4326'),col='red',add=TRUE)
plot(vect(matrix(xyFromCell(r, 7),ncol=2),crs='EPSG:4326'),col='blue',add=TRUE)

Perhaps there is a rule that it always returns the highest cell number when there is potentially more than one. In this case, it would be helpful if the documentation reflected this.

Thank you. I have updated the documentation to reflect this rule.