@@ -28,17 +28,30 @@ GeoInterface.coordnames(::PointTrait, geom::NamedTuple{Keys,NTuple{N,T}}) where
2828
2929# Default features using NamedTuple and AbstractArray
3030
31- const NamedTupleFeature = NamedTuple{(:geometry , :properties )}
31+ # Any named tuple with a `:geometry` field is a feature
32+ _is_namedtuple_feature (:: Type{<:NamedTuple{K}} ) where K = :geometry in K
33+ _is_namedtuple_feature (nt:: NamedTuple ) = _is_namedtuple_feature (typeof (nt))
34+
35+ GeoInterface. isfeature (T:: Type{<:NamedTuple} ) = _is_namedtuple_feature (T)
36+ GeoInterface. trait (nt:: NamedTuple ) = _is_namedtuple_feature (nt) ? FeatureTrait () : nothing
37+ GeoInterface. geometry (nt:: NamedTuple ) = _is_namedtuple_feature (nt) ? nt. geometry : nothing
38+ GeoInterface. properties (nt:: NamedTuple ) = _is_namedtuple_feature (nt) ? _nt_properties (nt) : nothing
39+
40+ # Use Val to force constant propagation through `reduce`
41+ function _nt_properties (nt:: NamedTuple{K} ) where K
42+ valkeys = reduce (K; init= ()) do acc, k
43+ k == :geometry ? acc : (acc... , k)
44+ end
45+ return nt[valkeys]
46+ end
3247
33- GeoInterface. isfeature (:: Type{<:NamedTupleFeature} ) = true
34- GeoInterface. trait (:: NamedTupleFeature ) = FeatureTrait ()
35- GeoInterface. geometry (f:: NamedTupleFeature ) = f. geometry
36- GeoInterface. properties (f:: NamedTupleFeature ) = f. properties
48+ const MaybeArrayFeatureCollection = AbstractArray{<: NamedTuple }
3749
38- const ArrayFeatureCollection = AbstractArray{<: NamedTupleFeature }
50+ _is_array_featurecollection (:: Type{<:AbstractArray{T}} ) where {T<: NamedTuple } = _is_namedtuple_feature (T)
51+ _is_array_featurecollection (A:: AbstractArray{<:NamedTuple} ) = _is_array_featurecollection (typeof (A))
3952
40- GeoInterface. isfeaturecollection (:: Type{<:ArrayFeatureCollection } ) = true
41- GeoInterface. trait (:: ArrayFeatureCollection ) = FeatureCollectionTrait ()
42- GeoInterface. nfeature (:: FeatureCollectionTrait , fc:: ArrayFeatureCollection ) = Base. length (fc)
43- GeoInterface. getfeature (:: FeatureCollectionTrait , fc:: ArrayFeatureCollection , i:: Integer ) = fc [i]
44- GeoInterface. geometrycolumns (fc:: ArrayFeatureCollection ) = ( :geometry ,)
53+ GeoInterface. isfeaturecollection (T :: Type{<:MaybeArrayFeatureCollection } ) = _is_array_featurecollection (T)
54+ GeoInterface. trait (fc :: MaybeArrayFeatureCollection ) = _is_array_featurecollection (fc) ? FeatureCollectionTrait () : nothing
55+ GeoInterface. nfeature (:: FeatureCollectionTrait , fc:: MaybeArrayFeatureCollection ) = _is_array_featurecollection (fc) ? Base. length (fc) : nothing
56+ GeoInterface. getfeature (:: FeatureCollectionTrait , fc:: MaybeArrayFeatureCollection , i:: Integer ) = _is_array_featurecollection (fc) ? fc [i] : nothing
57+ GeoInterface. geometrycolumns (fc:: MaybeArrayFeatureCollection ) = _is_array_featurecollection (fc) ? ( :geometry ,) : nothing
0 commit comments