evetion/GeoArrays.jl

KeyError: key Int64 not found on Int Array type

evetion opened this issue · 4 comments

As reported by @vernimmen

julia> ga = GeoArray(rand(Int, (100,100)))
100x100x1 Array{Int64,3} with AffineMap([1.0 0.0; 0.0 1.0], [0.0, 0.0]) and undefined CRS

julia> GeoArrays.write!("test.tif", ga)
ERROR: KeyError: key Int64 not found
Stacktrace:
 [1] getindex at ./dict.jl:477 [inlined]
 [2] #unsafe_create#30 at /Users/evetion/.julia/packages/ArchGDAL/FiLcr/src/dataset.jl:200 [inlined]
 [3] create(::GeoArrays.var"#27#28"{GeoArray{Int64},Int64}, ::String; kwargs::Base.Iterators.Pairs{Symbol,Any,NTuple{6,Symbol},NamedTuple{(:driver, :width, :height, :nbands, :dtype, :options),Tuple{ArchGDAL.Driver,Int64,Int64,Int64,DataType,Array{String,1}}}}) at /Users/evetion/.julia/packages/ArchGDAL/FiLcr/src/context.jl:213
 [4] write!(::String, ::GeoArray{Int64}, ::Nothing, ::String) at /Users/evetion/.julia/packages/GeoArrays/95hSL/src/io.jl:80
 [5] write!(::String, ::GeoArray{Int64}) at /Users/evetion/.julia/packages/GeoArrays/95hSL/src/io.jl:61
 [6] top-level scope at REPL[35]:1

GDAL doesn't support Int64 on GeoTiffs, but it would be good to document/warn against it.

@evetion pardon my silly question, but why can't we just convert the int to float ?

That's not a silly question. I'd say changing data is not what you normally want or expect. And converting to Int32 at least keeps the Integer type.

It seems I already tried my hand on it:
https://github.com/evetion/GeoArrays.jl/blob/master/src/io.jl#L75 and

function cast_to_gdal(A::Array{<:Real,3})

However, I'm happy to accept PRs for expanding the conversion dict here:

const gdt_conversion = Dict{DataType,DataType}(
. We could add Bool types and other common Julia types that do not fit into GDAL.

For now we try to cast to Int32, but this could yield ERROR: InexactError: trunc(Int32, largenumber), then it's up to the user.