-
Notifications
You must be signed in to change notification settings - Fork 95
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
Incorrect values & weird blockiness from terra::project when projecting SpatRaster with 100s of layers #1327
Comments
I suspect this problem may be due to processing raster in blocks from disk. If the raster is too large to be loaded into memory, it is processed sequentially in blocks. Check |
Here's what I get when I tried your suggestion:
Or is the It starts using 2 chunks at n≥307, and uses 3 at n≥615, but this blockiness behaviour started sooner when I was reprojecting ≥~200 layers. If I had to guess, maybe this is cause it's using additional memory to do the reprojecting, and that's what's taking it over the 60% memory allotment? I hadn't seen this blockiness issue when using
|
|
I think I can reproduce this and this is some bug in library("terra") # 1.7.55
gdal(lib = "")
#> gdal proj geos
#> "3.6.2" "9.2.0" "3.11.2"
set.seed(1)
n = 300
nlyr = 300
v = sample(1:5, size = n ^ 2, replace = TRUE)
v = rep(v, times = nlyr)
r = rast(nrows = n, ncols = n, nlyrs = nlyr, vals = v, crs = "EPSG:4326")
rr_1 = project(r[[1]], "EPSG:3857", method = "bilinear") # one layer
rr_300 = project(r, "EPSG:3857", method = "bilinear") # all layers
par(mfrow = c(1, 2))
plot(rr_1, legend = FALSE, axes = FALSE)
plot(rr_300[[nlyr]], legend = FALSE, axes = FALSE) |
@kadyb it's not sensible to blithely reproject a global longlat raster to EPSG:3857 - it's natural to get strange shapes like that when the bounds of the projection are asymptotic - please use a target grid not just a string, like the OP has done @nafreymueller can you please provide a reproducible example, enough would be one layer from the dataset your are projecting and the one you are projecting to - this information is not in the OP, there's more than one "pan-Arctic projection" I'm chiming in here because I think the mem and projection to Mercator diversions are unhelpful, and ultimately this is GDAL doing the reprojecting so I'd like to see if it's reproducible there - there's been a lot of anti-meridian related fixes in the GDAL warper recently, and more are still needed - it would be enough if you could share one of your source files and the specifications of |
@mdsumner, I just wanted to create some reprex. In my example, for identical inputs, shouldn't the result be identical regardless of the number of layers? I would expect that the result should be the same. |
oh I see, I'm sorry you're right |
Thanks @mdsumner and @kadyb for your insights. Here's a worked example with one raster (example rasters in zip file attached at the bottom):
When I reproject a SpatRaster with 1 layer, I get this, which seems reasonable: But when I reproject a SpatRaster with 680 layers, I get this block pattern: It's worth noting that I now get this warming:
Maybe this indicates an underlying GDAL issue after all? Is there a single fix within |
Update (no solution yet). I see this happening with > 110 layers
|
I am pretty certain this is a GDAL issue, will link a bug report. |
ok it's about the working memory (increase it with |
Hi,
I came across this issue where incorrect values are returned in this weird blocky pattern when I run
terra::project
on a SpatRaster, but this only starts to happen after that SpatRaster starts having more than ~200 layers. The issue persists even after restarting R.The actual code is just to illustrate the issue - it'd be cumbersome to attach the entire NetCDF file with 100s of layers.
Now I run the following code where I project a SpatRaster with multiple layers.
I do this four times, changing n each time (
n <- 1:10
;n <- 1:200
;n <- 1:250
;n <- 1:680
).I then plot
icec[[2]]
four times with the four different n's.icec[[2]]
is the exact same layer asa2.p
from earlier. The only difference is that it was in a SpatRaster that had multiple layers when it was reprojected:Everything seems fine for
n <- 1:10
andn <- 1:200
. But when I repeat withn <- 1:250
, these weird blocky patterns emerge in various locations (the Bering Sea is the most severe). This progressively worsens with increasing numbers of layers in the SpatRaster, terminating at 680, which is currently the maximum number of layers I'm working with. Changingn <- 1:450
(not shown) resulted in a blockiness level that was intermediate betweenn <- 1:250
andn <- 1:680
. When I played around with other values, >200 layers seemed to be the cutoff where this behaviour started.I should be able to work around this for now by just running a for loop where I filter the SpatLayer to a single layer (or only a few layers), reproject, write to disk, and can then load it in later. However, I know that I will likely need to be working with SpatRasters with 1000s of layers soon, and a for loop just won't scale well at that point.
Any thoughts/insights as to why this is happening? I initially thought it might be a wrapping-around-the-antimeridian issue, but this blockiness persists elsewhere (e.g., Sea of Okhotsk, Baffin Bay, south of Svalbard, etc.).
Cheers,
Nick
The text was updated successfully, but these errors were encountered: