rafaqz / Rasters.jl

Raster manipulation for the Julia language
MIT License
214 stars 36 forks source link

Error when writing .tif files with rasters reduced from 3 dimensions. #703

Closed BG-AIMS closed 3 months ago

BG-AIMS commented 3 months ago

I'm experiencing an issue when reducing a raster from 3 to 2 dimensions with mean(), the reduced dimension becomes a reference dimension. Then when trying to write this to .tif file it gives the error MethodError: no method matching lookup(::Nothing)

Here is an example:

A = Raster(rand(10,10,10), (:x, :y, :t))
Xt = dropdims(mean(A, dims=:t), dims=:t)
write("test.tif", Xt)

Same error when not using dropdims() as well.

felixcremer commented 3 months ago

Could you please copy the whole stacktrace?

felixcremer commented 3 months ago

The problem is you are using symbols for the dimensions instead of the dimension types like X, Y and Ti see this part of the DimensionalData documentation: https://rafaqz.github.io/DimensionalData.jl/dev/dimensions

If you replace your dimensions with X, Y and Ti the writing of the tif file works as expected.

using Rasters, ArchGDAL
A = Raster(rand(10,10,10), (X, Y, Ti))
       xt = dropdims(mean(A, dims=Ti), dims=Ti)
       write("test.tif", Xt)
rafaqz commented 3 months ago

You can also use Symbols, but they have to be uppercase - those will be converted to the types X and Y. We need to know which dimensions relate to gdals X and Y dimensions and uppercase X/Y is our convention.

@dim x XDim
@dim y YDim

May help too, but may not work everywhere.

BG-AIMS commented 3 months ago

Thanks for the help. It works after renaming x and y to uppercase dimensions and avoiding symbols.