Closed Rapsodia86 closed 4 months ago
Hello. I am also facing the same issue. When I crop and mask using terra::crop(spat_raster, shp, snap = "out", mask = T), I have to add overwrite = T and the output files are all converted to 1. The terra version I am using is 1.7.71
That raster with value of 1 looks like it is the rasterized polygon that is used as a mask.
Hello. Just checking if this issue got resolved?
As a workaround, you can try using gdalwarp with, for example, sf::gdal_utils()
:
library("sf")
library("terra")
tmp = rast(ncols = 100, nrows = 100, xmin = 5.74414, xmax = 6.528252,
ymin = 49.44781, ymax = 50.18162, crs = "EPSG:4326")
init(tmp, fun = 1, filename = "test.tif")
f = system.file("ex/lux.shp", package = "terra")
sf::gdal_utils(util = "warp", source = "test.tif", destination = "output.tif",
options = c("-cutline", f, "-crop_to_cutline"))
rast("output.tif")
Rather just do this in two steps (two functions): first crop, then mask the results. At least you will get the same result if the function was working correctly. Using another package may result in a diifferent clipping/masking approach (e.g. you may end up with different pixels along the border) if there is no control about this.
Also running into this issue after updating terra
Hi @rhijmans ! Could you take a look to check what is going on with crop and mask functions when you have a moment? Appreciate very much! Monika
Thank you for your patience. I believe this now also works for SpatRasters that are written to file.
library(terra)
r <- rast(system.file("ex/elev.tif", package="terra"))
v <- vect(system.file("ex/lux.shp", package="terra"))
m <- crop(r, v[9:12,], mask=TRUE, todisk=TRUE)
plot(m)
Thank you very much!
I have recently encountered this problem.
I have recently encountered this problem.
Are you using the most recent version of the package?
I do have a big raster (72439 x 88523 px). When trying to crop with mask=TRUE, all values are turned to 1. It is like, instead of the cropped raster, I was getting the mask (rasterized polygon). If I split the process into two steps (first crop, then mask), everything is ok. If I save it to a file, instead of to memory, the problem persists. With a smaller shapefile everything is ok. However, when wanted to rerun the
crop()
withmask=TRUE
, I got an error:[crop] file exists. You can use 'overwrite=TRUE' to overwrite it
All steps below:
Two-step approach:
Small shapefile scenario:
Finally, when wanted to repeat the process with mask=TRUE, I got the error.
Session info:
Matrix products: default
locale: [1] LC_COLLATE=English_United States.utf8 [2] LC_CTYPE=English_United States.utf8
[3] LC_MONETARY=English_United States.utf8 [4] LC_NUMERIC=C
[5] LC_TIME=English_United States.utf8
attached base packages: [1] stats graphics grDevices utils datasets methods base
other attached packages: [1] terra_1.7-74
loaded via a namespace (and not attached): [1] Rcpp_1.0.12 codetools_0.2-18 fansi_1.0.6 utf8_1.2.4
[5] dplyr_1.1.4 parallelly_1.37.1 plyr_1.8.9 R6_2.5.1
[9] lifecycle_1.0.4 magrittr_2.0.3 pillar_1.9.0 stringi_1.8.3
[13] rlang_1.1.3 cli_3.6.2 reshape2_1.4.4 rstudioapi_0.15.0 [17] vctrs_0.6.5 generics_0.1.3 tools_4.2.2 stringr_1.5.1
[21] glue_1.7.0 parallel_4.2.2 compiler_4.2.2 pkgconfig_2.0.3
[25] tidyselect_1.2.1 tibble_3.2.1