Skip to content

Commit 1f97721

Browse files
committed
Implementations for custom order NamedTuple.
1 parent d554c54 commit 1f97721

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/base.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,10 @@ for i in 2:4
1717
GeoInterface.ncoord(::PointTrait, geom::sig) = i
1818
GeoInterface.getcoord(::PointTrait, geom::sig, i) = getindex(geom, i)
1919
end
20+
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

test/test_primitives.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,15 +312,20 @@ end
312312
@testset "NamedTuple" begin
313313
geom = (; X=1, Y=2)
314314
@test testgeometry(geom)
315+
@test GeoInterface.x(geom) == 1
316+
@test collect(GeoInterface.getcoord(geom)) == [1, 2]
317+
315318
geom = (; X=1, Y=2, Z=3)
316319
@test testgeometry(geom)
317320
geom = (; X=1, Y=2, Z=3, M=4)
318321
@test testgeometry(geom)
322+
geom = (; Z=3, X=1, Y=2, M=4)
323+
@test testgeometry(geom)
319324

320325
@test GeoInterface.x(geom) == 1
321326
@test GeoInterface.m(geom) == 4
322327
@test GeoInterface.ncoord(geom) == 4
323-
@test collect(GeoInterface.getcoord(geom)) == [1, 2, 3, 4]
328+
@test collect(GeoInterface.getcoord(geom)) == [3, 1, 2, 4]
324329

325330
end
326331
end

0 commit comments

Comments
 (0)