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

Suggestion - makeTiles() using SpatVector #920

Closed
tgoodbody opened this issue Dec 1, 2022 · 3 comments
Closed

Suggestion - makeTiles() using SpatVector #920

tgoodbody opened this issue Dec 1, 2022 · 3 comments

Comments

@tgoodbody
Copy link

Hi Robert,

I am currently working on a project where raster processing requires a bit of overlap between tiles (processing is spatially contextual and edges cause artifacts) that will eventually be mosaiced. I noticed in the makeTiles documentation that y - SpatRaster or SpatVector but gives the error Error: [makeTiles] y must be a SpatRaster when called. Not sure if this is intended or not.

With that being said, it would be really useful to be able to leverage spatVectors (or have an option to buffer tile extents from the y spatRaster) to generate overlapping tiles like the theoretical example below. I know I could write a loop to crop() the raster over each feature, but figured that could be avoided with the proposed functionality. If there is already a method for this or it has been discussed before I apologize 😄 .

Thanks ahead of time and much appreciated for developing such a great package.

Proposed example reprex

#--- define raster to be tiled ---#
r <- rast(ncols=100, nrows=100)
values(r) <- 1:ncell(r)

#--- define desired tiling extents ---#
x <- rast(ncols=2, nrows=2)

#--- convert desired tile extent to spatVector and buffer by 200 units ---#
xp <- as.polygons(x) |> buffer(200)

#--- specify output file names ---#
filename <- paste0(tempfile(), "_.tif")

#--- tile by feature in xp and write outputs using overlapping spatRaster polygons ---#
ff <- makeTiles(x=r, y=xp, filename)
@rhijmans
Copy link
Member

rhijmans commented Dec 1, 2022

Thanks for that suggestion. I think the documentation suggesting that you can use a SpatVector was an error (I do not think I intended to implement that).

You can now do:

library(terra)
r <- rast(ncols=100, nrows=100, xmin=0, xmax=10, ymin=0, ymax=10, crs="+proj=utm +zone=1")
values(r) <- 1:ncell(r)
x <- rast(ncols=2, nrows=2, xmin=0, xmax=10, ymin=0, ymax=10, crs="+proj=utm +zone=1")
xp <- as.polygons(x) |> buffer(1)
filename <- paste0(tempfile(), "_.tif")
ff <- makeTiles(x=r, y=xp, filename)

plot(xp)
plot(rast(ff[1]), add=T, legend=F, col="red")
plot(rast(ff[4]), add=T, legend=F, col="blue")
lines(xp)

Another option that to consider could be add to add an argument that would allow you to specify certain amount of expansion, in terms of rows and columns, when y is a SpatRaster.

@tgoodbody
Copy link
Author

This is excellent, thanks so much!

@Rapsodia86
Copy link

How about yet another option to use only a number of columns and rows to make tiles? A way without specifically creating/using another SpatRaster with a lower resolution?
Something like:
makeTIles(x, c(nx,ny), ...) where:
"nx" is a number of columns
"ny" is a number of rows

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

3 participants