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

add 'snap' option when reading in a raster with a window? #1094

Closed
dfriend21 opened this issue Apr 3, 2023 · 1 comment
Closed

add 'snap' option when reading in a raster with a window? #1094

dfriend21 opened this issue Apr 3, 2023 · 1 comment

Comments

@dfriend21
Copy link
Contributor

When cropping you can use the snap parameter to control how the provided extent aligns to the geometry of the raster being cropped. It'd be nice if a similar option was available when reading in a raster with a set window (via the win parameter). In my particular use case I want to read in only the extent defined by a polygon, and as it's currently implemented it's likely that not all cells touched by the polygon will be included in the window.

Example:

library(terra)
#> terra 1.7.18

r <- rast(matrix(runif(100), nrow = 10, ncol = 10))
poly_mat <- rbind(
  c(1.7, 1.3),
  c(4.2, 8.2),
  c(6.2, 8.2),
  c(8.2, 1.1),
  c(1.7, 1.3)
)
poly <- vect(poly_mat, type = "polygons")

tf <- tempfile(fileext = ".tif")
writeRaster(r, tf)

r2 <- rast(tf, win = ext(poly))

plot_ext <- ext(poly) + .5
plot(r2, xlim = plot_ext[1:2], ylim = plot_ext[3:4])
plot(poly, add = TRUE, lwd = 2)

Created on 2023-04-03 with reprex v2.0.2

@rhijmans
Copy link
Member

rhijmans commented Apr 4, 2023

Thanks. You can now do

r2 <- rast(tf, win = poly, snap="out")
plot(r2)
lines(poly, lwd=2)

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