@@ -807,33 +807,53 @@ using Tables
807
807
return x
808
808
end
809
809
end
810
- function columntablevalues_toWKT (x)
810
+ function ctv_toWKT (x)
811
811
return Tuple (toWKT_withmissings .(x[i]) for i in 1 : length (x))
812
812
end
813
+ """
814
+ nt2layer2nt_equals_nt(nt; force_no_schema=true)
815
+
816
+ Takes a NamedTuple, converts it to an IFeatureLayer and compares the NamedTuple
817
+ to the one obtained from the IFeatureLayer conversion to table
818
+
819
+ _Notes:_
820
+ 1. _Table columns have geometry column first and then field columns as
821
+ enforced by `Tables.columnnames`_
822
+ 2. _`nothing` values in geometry column are returned as `missing` from
823
+ the NamedTuple roundtrip conversion, since geometry fields do not have the
824
+ same distinction between NULL and UNSET values the fields have_
825
+
826
+ """
813
827
function nt2layer2nt_equals_nt (
814
828
nt:: NamedTuple ;
815
829
force_no_schema:: Bool = false ,
816
830
):: Bool
817
- if force_no_schema
818
- (ct_in, ct_out) =
819
- Tables. columntable .((
820
- nt,
821
- AG. _fromtable (nothing , Tables. rows (nt)),
822
- ))
823
- else
824
- (ct_in, ct_out) =
825
- Tables. columntable .((nt, AG. IFeatureLayer (nt)))
826
- end
827
- (ctv_in, ctv_out) =
828
- columntablevalues_toWKT .(values .((ct_in, ct_out)))
831
+ force_no_schema ?
832
+ layer = AG. _fromtable (nothing , Tables. rows (nt)) :
833
+ layer = AG. IFeatureLayer (nt)
834
+ ngeom = AG. ngeom (layer)
835
+ (ct_in, ct_out) = Tables. columntable .((nt, layer))
836
+ # we convert IGeometry values to WKT
837
+ (ctv_in, ctv_out) = ctv_toWKT .(values .((ct_in, ct_out)))
838
+ # we use two index functions to map ctv_in and ctv_out indices to the
839
+ # sorted key list indices
829
840
(spidx_in, spidx_out) =
830
841
sortperm .(([keys (ct_in)... ], [keys (ct_out)... ]))
831
842
return all ([
832
843
sort ([keys (ct_in)... ]) == sort ([keys (ct_out)... ]),
833
844
all (
834
845
all .([
835
- ctv_in[spidx_in[i]] .=== ctv_out[spidx_out[i]] for
836
- i in 1 : length (ct_in)
846
+ (
847
+ # if we are comparing two geometry columns values, we
848
+ # convert `nothing` values to `missing`, see note #2
849
+ spidx_out[i] <= ngeom ?
850
+ map (
851
+ val ->
852
+ (val === nothing || val === missing ) ?
853
+ missing : val,
854
+ ctv_in[spidx_in[i]],
855
+ ) : ctv_in[spidx_in[i]]
856
+ ) .=== ctv_out[spidx_out[i]] for i in 1 : length (nt)
837
857
]),
838
858
),
839
859
])
@@ -919,8 +939,8 @@ using Tables
919
939
]),
920
940
],
921
941
])
922
- @test_skip nt2layer2nt_equals_nt (nt; force_no_schema = true )
923
- @test_skip nt2layer2nt_equals_nt (nt)
942
+ @test nt2layer2nt_equals_nt (nt; force_no_schema = true )
943
+ @test nt2layer2nt_equals_nt (nt)
924
944
925
945
# Test with `missing` values
926
946
nt = NamedTuple ([
0 commit comments