11# Implementation of GeoInterface for Base Types
22
3+
4+ const PointTuple2 = Tuple{<: Real ,<: Real }
5+ const PointTuple3 = Tuple{<: Real ,<: Real ,<: Real }
6+ const PointTuple4 = Tuple{<: Real ,<: Real ,<: Real ,<: Real }
7+ const PointTuple = Union{PointTuple2,PointTuple3,PointTuple4}
8+
39GeoInterface. isgeometry (:: Type{<:AbstractVector{<:Real}} ) = true
410GeoInterface. geomtrait (:: AbstractVector{<:Real} ) = PointTrait ()
511GeoInterface. ncoord (:: PointTrait , geom:: AbstractVector{<:Real} ) = Base. length (geom)
612GeoInterface. getcoord (:: PointTrait , geom:: AbstractVector{<:Real} , i) = getindex (geom, i)
713
8- GeoInterface. isgeometry (:: Type{<:NTuple{N,<:Real}} ) where {N} = true
9- GeoInterface. geomtrait (:: NTuple{N,<:Real} ) where {N} = PointTrait ()
10- GeoInterface. ncoord (:: PointTrait , geom:: NTuple{N,<:Real} ) where {N} = N
11- GeoInterface. getcoord (:: PointTrait , geom:: NTuple{N,<:Real} , i) where {N} = getindex (geom, i)
12-
13- for i in 2 : 4
14- sig = NamedTuple{default_coord_names[1 : i],NTuple{i,T}} where {T<: Real }
15- GeoInterface. isgeometry (:: Type{<:sig} ) = true
16- GeoInterface. geomtrait (:: sig ) = PointTrait ()
17- GeoInterface. ncoord (:: PointTrait , geom:: sig ) = i
18- GeoInterface. getcoord (:: PointTrait , geom:: sig , i) = getindex (geom, i)
14+ GeoInterface. isgeometry (:: Type{<:PointTuple} ) = true
15+ GeoInterface. geomtrait (:: PointTuple ) = PointTrait ()
16+ GeoInterface. ncoord (:: PointTrait , geom:: PointTuple ) = Base. length (geom)
17+ GeoInterface. getcoord (:: PointTrait , geom:: PointTuple , i) = getindex (geom, i)
18+
19+ for (i, pointtype) in enumerate ((PointTuple2, PointTuple3, PointTuple4))
20+ keys = default_coord_names[1 : i+ 1 ]
21+ sig = NamedTuple{keys,<: pointtype }
22+ @eval GeoInterface. isgeometry (:: Type{<:$sig} ) = true
23+ @eval GeoInterface. geomtrait (:: $sig ) = PointTrait ()
24+ @eval GeoInterface. ncoord (:: PointTrait , geom:: $sig ) = $ i + 1
25+ @eval GeoInterface. getcoord (:: PointTrait , geom:: $sig , i) = getindex (geom, i)
1926end
2027
21- # Custom coordinate order/names NamedTuple
22- GeoInterface. isgeometry (:: Type{<:NamedTuple{Keys,NTuple{N,T}}} ) where {Keys,N,T<: Real } = all (in (default_coord_names), Keys)
23- GeoInterface. geomtrait (:: NamedTuple{Keys,NTuple{N,T}} ) where {Keys,N,T<: Real } = PointTrait ()
24- GeoInterface. ncoord (:: PointTrait , geom:: NamedTuple{Keys,NTuple{N,T}} ) where {Keys,N,T<: Real } = Base. length (geom)
25- GeoInterface. getcoord (:: PointTrait , geom:: NamedTuple{Keys,NTuple{N,T}} , i) where {Keys,N,T<: Real } = getindex (geom, i)
26- GeoInterface. coordnames (:: PointTrait , geom:: NamedTuple{Keys,NTuple{N,T}} ) where {Keys,N,T<: Real } = Keys
28+
29+ # Define all possible NamedTuple points
30+ const NamedTuplePoint = Union{
31+ NamedTuple{(:X , :Y ),<: PointTuple2 },
32+ NamedTuple{(:Y , :X ),<: PointTuple2 },
33+ NamedTuple{(:X , :Y , :Z ),<: PointTuple3 },
34+ NamedTuple{(:X , :Z , :Y ),<: PointTuple3 },
35+ NamedTuple{(:Z , :Y , :X ),<: PointTuple3 },
36+ NamedTuple{(:Z , :X , :Y ),<: PointTuple3 },
37+ NamedTuple{(:Y , :X , :Z ),<: PointTuple3 },
38+ NamedTuple{(:Y , :Z , :X ),<: PointTuple3 },
39+ NamedTuple{(:X , :Y , :M ),<: PointTuple3 },
40+ NamedTuple{(:X , :M , :Y ),<: PointTuple3 },
41+ NamedTuple{(:M , :Y , :X ),<: PointTuple3 },
42+ NamedTuple{(:M , :X , :Y ),<: PointTuple3 },
43+ NamedTuple{(:Y , :X , :Z ),<: PointTuple3 },
44+ NamedTuple{(:Y , :Z , :X ),<: PointTuple3 },
45+ NamedTuple{(:X , :Y , :Z , :M ),<: PointTuple4 },
46+ NamedTuple{(:X , :Y , :M , :Z ),<: PointTuple4 },
47+ NamedTuple{(:X , :Z , :Y , :M ),<: PointTuple4 },
48+ NamedTuple{(:X , :Z , :M , :Y ),<: PointTuple4 },
49+ NamedTuple{(:X , :M , :Z , :Y ),<: PointTuple4 },
50+ NamedTuple{(:X , :M , :Y , :Z ),<: PointTuple4 },
51+ NamedTuple{(:Y , :X , :Z , :M ),<: PointTuple4 },
52+ NamedTuple{(:Y , :X , :M , :Z ),<: PointTuple4 },
53+ NamedTuple{(:Y , :Z , :X , :M ),<: PointTuple4 },
54+ NamedTuple{(:Y , :Z , :M , :X ),<: PointTuple4 },
55+ NamedTuple{(:Y , :M , :Z , :X ),<: PointTuple4 },
56+ NamedTuple{(:Y , :M , :X , :Z ),<: PointTuple4 },
57+ NamedTuple{(:Z , :Y , :X , :M ),<: PointTuple4 },
58+ NamedTuple{(:Z , :Y , :M , :X ),<: PointTuple4 },
59+ NamedTuple{(:Z , :X , :Y , :M ),<: PointTuple4 },
60+ NamedTuple{(:Z , :X , :M , :Y ),<: PointTuple4 },
61+ NamedTuple{(:Z , :M , :X , :Y ),<: PointTuple4 },
62+ NamedTuple{(:Z , :M , :Y , :X ),<: PointTuple4 },
63+ NamedTuple{(:M , :Y , :Z , :X ),<: PointTuple4 },
64+ NamedTuple{(:M , :Y , :X , :Z ),<: PointTuple4 },
65+ NamedTuple{(:M , :Z , :Y , :X ),<: PointTuple4 },
66+ NamedTuple{(:M , :Z , :X , :Y ),<: PointTuple4 },
67+ NamedTuple{(:M , :X , :Z , :Y ),<: PointTuple4 },
68+ NamedTuple{(:M , :X , :Y , :Z ),<: PointTuple4 },
69+ }
70+
71+ _keys (:: Type{<:NamedTuple{K}} ) where K = K
72+ GeoInterface. isgeometry (:: Type{T} ) where {T<: NamedTuplePoint } = all (in (default_coord_names), _keys (T))
73+ GeoInterface. geomtrait (:: NamedTuplePoint ) = PointTrait ()
74+ GeoInterface. ncoord (:: PointTrait , geom:: NamedTuplePoint ) = Base. length (geom)
75+ GeoInterface. getcoord (:: PointTrait , geom:: NamedTuplePoint , i) = getindex (geom, i)
76+ GeoInterface. coordnames (:: PointTrait , geom:: NamedTuplePoint ) = _keys (typeof (geom))
77+ GeoInterface. x (:: PointTrait , geom:: NamedTuplePoint ) = geom. X
78+ GeoInterface. y (:: PointTrait , geom:: NamedTuplePoint ) = geom. Y
79+ GeoInterface. z (:: PointTrait , geom:: NamedTuplePoint ) = geom. Z
80+ GeoInterface. m (:: PointTrait , geom:: NamedTuplePoint ) = geom. M
2781
2882
2983# Default features using NamedTuple and AbstractArray
@@ -33,7 +87,7 @@ _is_namedtuple_feature(::Type{<:NamedTuple{K}}) where K = :geometry in K
3387_is_namedtuple_feature (nt:: NamedTuple ) = _is_namedtuple_feature (typeof (nt))
3488
3589GeoInterface. isfeature (T:: Type{<:NamedTuple} ) = _is_namedtuple_feature (T)
36- GeoInterface. trait (nt:: NamedTuple ) = _is_namedtuple_feature (nt) ? FeatureTrait () : nothing
90+ GeoInterface. trait (nt:: NamedTuple ) = _is_namedtuple_feature (nt) ? FeatureTrait () : geomtrait (nt)
3791GeoInterface. geometry (nt:: NamedTuple ) = _is_namedtuple_feature (nt) ? nt. geometry : nothing
3892GeoInterface. properties (nt:: NamedTuple ) = _is_namedtuple_feature (nt) ? _nt_properties (nt) : nothing
3993
@@ -51,7 +105,7 @@ _is_array_featurecollection(::Type{<:AbstractArray{T}}) where {T<:NamedTuple} =
51105_is_array_featurecollection (A:: AbstractArray{<:NamedTuple} ) = _is_array_featurecollection (typeof (A))
52106
53107GeoInterface. isfeaturecollection (T:: Type{<:MaybeArrayFeatureCollection} ) = _is_array_featurecollection (T)
54- GeoInterface. trait (fc:: MaybeArrayFeatureCollection ) = _is_array_featurecollection (fc) ? FeatureCollectionTrait () : nothing
108+ GeoInterface. trait (fc:: MaybeArrayFeatureCollection ) = _is_array_featurecollection (fc) ? FeatureCollectionTrait () : geomtrait (fc)
55109GeoInterface. nfeature (:: FeatureCollectionTrait , fc:: MaybeArrayFeatureCollection ) = _is_array_featurecollection (fc) ? Base. length (fc) : nothing
56110GeoInterface. getfeature (:: FeatureCollectionTrait , fc:: MaybeArrayFeatureCollection , i:: Integer ) = _is_array_featurecollection (fc) ? fc[i] : nothing
57111GeoInterface. geometrycolumns (fc:: MaybeArrayFeatureCollection ) = _is_array_featurecollection (fc) ? (:geometry ,) : nothing
0 commit comments