yeesian / ArchGDAL.jl

A high level API for GDAL - Geospatial Data Abstraction Library
https://yeesian.github.io/ArchGDAL.jl/stable/
Other
137 stars 25 forks source link

`AG.read("nonexisting.tif") do r` should give hint that file does not exist #428

Open felixcremer opened 2 weeks ago

felixcremer commented 2 weeks ago

When I am using AG.read("nonexisting.tif") do r with some ArchGDAL functions I get a confusing GDALError about some Pointer being NULL. I would have expected an error indicating that the there does not exist a file in that path which happens on a plain AG.read.


julia> AG.read("nonexisting.tif") do r
       AG.nraster(r)
       end
ERROR: GDALError (CE_Failure, code 10):
        Pointer 'hDS' is NULL in 'GDALGetRasterCount'.

Stacktrace:
 [1] maybe_throw
   @ ~/.julia/packages/GDAL/hNyuz/src/error.jl:42 [inlined]
 [2] aftercare
   @ ~/.julia/packages/GDAL/hNyuz/src/error.jl:59 [inlined]
 [3] gdalgetrastercount
   @ ~/.julia/packages/GDAL/hNyuz/src/libgdal.jl:7260 [inlined]
 [4] nraster
   @ ~/.julia/packages/ArchGDAL/xZUPv/src/dataset.jl:662 [inlined]
 [5] #13
   @ ./REPL[33]:2 [inlined]
 [6] read(f::var"#13#14", args::String; kwargs::@Kwargs{})
   @ ArchGDAL ~/.julia/packages/ArchGDAL/xZUPv/src/context.jl:268
 [7] read(f::Function, args::String)
   @ ArchGDAL ~/.julia/packages/ArchGDAL/xZUPv/src/context.jl:265
 [8] top-level scope
   @ REPL[33]:1

I would like to get an error like this also for the do block.

julia> AG.read("nonexisting.tif")
ERROR: GDALError (CE_Failure, code 4):
        nonexisting.tif: No such file or directory

Stacktrace:
 [1] maybe_throw
   @ ~/.julia/packages/GDAL/hNyuz/src/error.jl:42 [inlined]
 [2] aftercare
   @ ~/.julia/packages/GDAL/hNyuz/src/error.jl:59 [inlined]
 [3] gdalopenex(pszFilename::String, nOpenFlags::Int64, papszAllowedDrivers::Ptr{…}, papszOpenOptions::Ptr{…}, papszSiblingFiles::Ptr{…})
   @ GDAL ~/.julia/packages/GDAL/hNyuz/src/libgdal.jl:6635
 [4] #read#79
   @ ~/.julia/packages/ArchGDAL/xZUPv/src/dataset.jl:630 [inlined]
 [5] read(filename::String)
   @ ArchGDAL ~/.julia/packages/ArchGDAL/xZUPv/src/dataset.jl:623
 [6] top-level scope
   @ REPL[34]:1
Some type information was truncated. Use `show(err)` to see complete types.
evetion commented 2 weeks ago

Over at GeoArrays, I've used startswith(fn, "/vsi") || occursin(":", fn) || isfile(fn) || error("File not found."), as GDAL can read non-local filepaths.

rafaqz commented 2 weeks ago

Rasters has something similar, we check all the possible /vsi**

https://github.com/rafaqz/Rasters.jl/blob/main/ext%2FRastersArchGDALExt%2Fgdal_source.jl#L11