evetion / GeoDataFrames.jl

Simple geographical vector interaction built on top of ArchGDAL
https://www.evetion.nl/GeoDataFrames.jl/dev/
MIT License
68 stars 6 forks source link

Confusing read() error for absolute Windows path arg #78

Closed arlowhite closed 1 month ago

arlowhite commented 1 month ago

GeoDataFrames version 0.3.10

In Windows, if you call read with an absolute path where file does not exist, you get a confusing error. This could confuse and frustrate Windows users.

GeoDataFrames.read("C:\\foo")

ERROR: GDALError (CE_Failure, code 10): Pointer 'hDS' is NULL in 'GDALDatasetH'.

This is due to the colon in the file path.

https://github.com/evetion/GeoDataFrames.jl/blob/master/src/io.jl#L52

function read(fn::AbstractString; kwargs...)
    startswith(fn, "/vsi") ||
        occursin(":", fn) ||
        isfile(fn) ||
        isdir(fn) ||
        error("File not found.")
    t = AG.read(fn; kwargs...) do ds
        if AG.nlayer(ds) > 1
            @warn "This file has multiple layers, you only get the first layer by default now."
        end
        return read(ds, 0)
    end
    return t
end

: in fn causes error to be skipped at occursin(":", fn) . Then AG.nlayer(ds) results in this confusing error. ds

NULL Dataset

typeof(ds)

ArchGDAL.Dataset

Note: you could check for NULL Dataset with ds.ptr == C_NULL

arlowhite commented 1 month ago

Though it's strange, because this has a clear error: ArchGDAL.read("C:\\foo")

ERROR: GDALError (CE_Failure, code 4): C:\foo: No such file or directory

evetion commented 1 month ago

Thanks for reporting! With adding : I overlooked Windows paths it seems. Would you like to make a PR?