Skip to content

Commit

Permalink
Added nothing values handling (cf. PR #238):
Browse files Browse the repository at this point in the history
- no difference for geometry columns. Both `nothing` and `missing` values map to an UNSET geometry field (null pointer)
- field set to NULL for `missing` values and not set for `nothing` values
  • Loading branch information
mathieu17g committed Oct 12, 2021
1 parent 541ff9b commit 1b6ab33
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 24 deletions.
17 changes: 10 additions & 7 deletions src/tables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -153,17 +153,20 @@ function _fromtable(
rowgeoms = view(rowvalues, geomindices)
rowfields = view(rowvalues, fieldindices)
addfeature(layer) do feature
# TODO: optimize once PR #238 is merged define in casse of `missing`
# TODO: or `nothing` value, geom or field as to leave unset or set to null
# For geometry fields both `missing` and `nothing` map to not geometry set
# since in GDAL <= v"3.3.2", special fields as geometry field cannot be NULL
# cf. `OGRFeature::IsFieldNull( int iField )` implemetation
for (j, val) in enumerate(rowgeoms)
val !== missing &&
val !== nothing &&
setgeom!(feature, j - 1, val)
end
for (j, val) in enumerate(rowfields)
val !== missing &&
val !== nothing &&
if val === missing
setfieldnull!(feature, j - 1)
elseif val !== nothing
setfield!(feature, j - 1, val)
end
end
end
end
Expand All @@ -178,15 +181,15 @@ function _fromtable(
)::IFeatureLayer where {names}
cols = Tables.columns(rows)
types = (eltype(collect(col)) for col in cols)
return _fromtable(Tables.Schema(names, types), rows; name=name)
return _fromtable(Tables.Schema(names, types), rows; name = name)
end

function _fromtable(::Nothing, rows; name::String = "")::IFeatureLayer
state = iterate(rows)
state === nothing && return IFeatureLayer()
row, _ = state
names = Tables.columnnames(row)
return _fromtable(Tables.Schema(names, nothing), rows; name=name)
return _fromtable(Tables.Schema(names, nothing), rows; name = name)
end

"""
Expand Down Expand Up @@ -232,5 +235,5 @@ function IFeatureLayer(table; name::String = "")::IFeatureLayer
# Extract table data
rows = Tables.rows(table)
schema = Tables.schema(table)
return _fromtable(schema, rows; name=name)
return _fromtable(schema, rows; name = name)
end
54 changes: 37 additions & 17 deletions test/test_tables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -807,33 +807,53 @@ using Tables
return x
end
end
function columntablevalues_toWKT(x)
function ctv_toWKT(x)
return Tuple(toWKT_withmissings.(x[i]) for i in 1:length(x))
end
"""
nt2layer2nt_equals_nt(nt; force_no_schema=true)
Takes a NamedTuple, converts it to an IFeatureLayer and compares the NamedTuple
to the one obtained from the IFeatureLayer conversion to table
_Notes:_
1. _Table columns have geometry column first and then field columns as
enforced by `Tables.columnnames`_
2. _`nothing` values in geometry column are returned as `missing` from
the NamedTuple roundtrip conversion, since geometry fields do not have the
same distinction between NULL and UNSET values the fields have_
"""
function nt2layer2nt_equals_nt(
nt::NamedTuple;
force_no_schema::Bool = false,
)::Bool
if force_no_schema
(ct_in, ct_out) =
Tables.columntable.((
nt,
AG._fromtable(nothing, Tables.rows(nt)),
))
else
(ct_in, ct_out) =
Tables.columntable.((nt, AG.IFeatureLayer(nt)))
end
(ctv_in, ctv_out) =
columntablevalues_toWKT.(values.((ct_in, ct_out)))
force_no_schema ?
layer = AG._fromtable(nothing, Tables.rows(nt)) :
layer = AG.IFeatureLayer(nt)
ngeom = AG.ngeom(layer)
(ct_in, ct_out) = Tables.columntable.((nt, layer))
# we convert IGeometry values to WKT
(ctv_in, ctv_out) = ctv_toWKT.(values.((ct_in, ct_out)))
# we use two index functions to map ctv_in and ctv_out indices to the
# sorted key list indices
(spidx_in, spidx_out) =
sortperm.(([keys(ct_in)...], [keys(ct_out)...]))
return all([
sort([keys(ct_in)...]) == sort([keys(ct_out)...]),
all(
all.([
ctv_in[spidx_in[i]] .=== ctv_out[spidx_out[i]] for
i in 1:length(ct_in)
(
# if we are comparing two geometry columns values, we
# convert `nothing` values to `missing`, see note #2
spidx_out[i] <= ngeom ?
map(
val ->
(val === nothing || val === missing) ?
missing : val,
ctv_in[spidx_in[i]],
) : ctv_in[spidx_in[i]]
) .=== ctv_out[spidx_out[i]] for i in 1:length(nt)
]),
),
])
Expand Down Expand Up @@ -919,8 +939,8 @@ using Tables
]),
],
])
@test_skip nt2layer2nt_equals_nt(nt; force_no_schema = true)
@test_skip nt2layer2nt_equals_nt(nt)
@test nt2layer2nt_equals_nt(nt; force_no_schema = true)
@test nt2layer2nt_equals_nt(nt)

# Test with `missing` values
nt = NamedTuple([
Expand Down

0 comments on commit 1b6ab33

Please sign in to comment.