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

Intended behaviour "crop" with extend=TRUE? #1114

Closed
jvandoninck opened this issue Apr 13, 2023 · 2 comments
Closed

Intended behaviour "crop" with extend=TRUE? #1114

jvandoninck opened this issue Apr 13, 2023 · 2 comments

Comments

@jvandoninck
Copy link

When using crop with a raster and an extend that goes beyond the raster, it returns the original raster when extend=TRUE.

library(terra)
r <- rast(nrow=100, ncol=100, extent=ext(-100, 100, -45, 45))
e <- ext(0,150, 0, 30)
crop(r,e)
crop(r,e,extend=TRUE)

I would have expected that it extends the cropped raster to have the extent of e, as with:

extend(crop(r,e), e)

Is this the intended behaviour or a bug?

@rhijmans
Copy link
Member

rhijmans commented Apr 13, 2023

Thank you, that is a bug. The function did not expand rasters with no cell values.
I now see:

library(terra)
#terra 1.7.24
r <- rast(nrow=10, ncol=10, extent=ext(0, 10, 0, 10))

e <- ext(-10, 15, -10, 15) 
ext(crop(r, e))
#SpatExtent : 0, 10, 0, 10 (xmin, xmax, ymin, ymax)
ext(crop(r, e, extend=TRUE))
#SpatExtent : -10, 15, -10, 15 (xmin, xmax, ymin, ymax)
 
e <- ext(-10, 5, -10, 5)
ext(crop(r, e))
#SpatExtent : 0, 5, 0, 5 (xmin, xmax, ymin, ymax)
ext(crop(r, e, extend=TRUE))
#SpatExtent : -10, 5, -10, 5 (xmin, xmax, ymin, ymax)
 
e <- ext(0, 5, 0, 5)
ext(crop(r, e))
#SpatExtent : 0, 5, 0, 5 (xmin, xmax, ymin, ymax)
ext(crop(r, e, extend=TRUE))
#SpatExtent : 0, 5, 0, 5 (xmin, xmax, ymin, ymax)

@jvandoninck
Copy link
Author

Thank you.
Out of curiosity, has it been considered changing the error message when extents of x and y to not overlap, but extend=TRUE, into returning an empty raster with ext=y? Perhaps with a warning message.

library(terra)
#terra 1.7.24

r <- rast(nrow=10, ncol=10, extent=ext(0, 10, 0, 10))
e <- ext(-10, -5, -10, -5)

crop(r, e, extend=TRUE)
#Error: [crop] extents do not overlap

I can imagine a few cases where this behaviour would be useful, though it can obviously easily be circumvented using a prior check based on intersect(ext(r), e), or simply using crop(extend(r,e),e), though the latter is potentially very inefficient in cases with a large distance between ext(r) and e.

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