ArchGDAL.getfield() error

Hi,

I have the following code which aims to transform the data contained in a layer into a DataFrame, which I picked up online. This works for the most part. Notice the try/catch, which slows the code down a lot, and I would rather not use it as the number of features is around 50,000. I am working to transform files provided to me in a different format. The code val = GG.getfield(feature, k) generates an error if the features is missing. The error states: ERROR: Failed to fetch datetime at index 11 which seems to indicates that either an objects of the wrong type is located in that field, or an object is missing. Looking at the source of ARCHGdal, however, shows the use of issetfield(), which checks whether the field is set. So there might be an object of the wrong type in that field. At the moment I decided to create a union structure in the dictionary d to allow for missing values and use a try/catch construct. So the code runs now, but much slower. Is there an alternative approach I could use to accomplish the same task more efficiently? Thanks.

p.s. I realize this is not a MWE, so will not run as is. I would have to create fake data, and although I would like a solution to my question, I do not have the time to pare down the data.

import ArchGDAL
const GG = ArchGDAL

# prepare Dict with empty vectors of the right type for each field
d = Dict{String, Vector}()
featuredefn = G.layerdefn(layer)
for field_no in 0:nfield-1
    field = G.getfielddefn(featuredefn, field_no)
    name = G.getname(field)
    typ = G._FIELDTYPE[G.gettype(field)]
    d[name] = Union{typ, Missing}[] # Create a 1D array of type typ
end

d["geometry"] = G.IGeometry[]

function captureLayer(layer, nfeatures, d)
    for fid in 1:nfeatures
        GG.getfeature(layer, fid) do feature
            for (k, v) in pairs(d)
                if k == "geometry"
                    val = GG.getgeom(feature, 0)
                else
                    try
                        val = GG.getfield(feature, k)  # What if missing fields
                    catch
                        val = missing
                    end

                end
                push!(v, val)   # v is part of (k,v).
            end
        end
    end
end

captureLayer(layer, nfeat, d)

I didnt really think into your code but you could probably check if the field exists beforehand by using hasfield().

Support for tables in ArchGDAL is currently happening in https://github.com/yeesian/ArchGDAL.jl/pull/118 – can you give that branch/PR a shot and let us know if it works for you (and if it doesn’t)?

1 Like