Skip to content

Commit c748850

Browse files
committed
convert to module types
1 parent a5dd3b4 commit c748850

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

src/fallbacks.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,15 @@ function coordinates(t::AbstractFeatureTrait, feature)
109109
end
110110
coordinates(t::AbstractFeatureCollectionTrait, fc) = [coordinates(f) for f in getfeature(fc)]
111111

112+
# Package level `GeoInterface.convert` method
113+
# Packages must implement their own `traittype` method
114+
# that accepts a GeoInterface.jl trait and returns the
115+
# corresponding geometry type
116+
function convert(package::Module, geom)
117+
t = trait(geom)
118+
convert(package.geointerface_traittype(t), t, geom)
119+
end
120+
112121
# Subtraits
113122

114123
"""

src/interface.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,8 +605,12 @@ coordinates(obj) = coordinates(trait(obj), obj)
605605

606606
"""
607607
convert(type::CustomGeom, geom)
608+
convert(module::Module, geom)
608609
609610
Create a `CustomGeom` from any `geom` that implements the GeoInterface.
611+
612+
Can also convert to a `Module`, which finds the corresponding
613+
geom type for the trait using the modules `geointerface_traittype` method.
610614
"""
611615
convert(T, geom) = convert(T, geomtrait(geom), geom)
612616
convert(::Type{T}, x::T) where {T} = x # no-op

test/test_primitives.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,4 +366,19 @@ end
366366
@test GeoInterface.testfeature(feature)
367367
@test GeoInterface.testfeaturecollection([feature, feature])
368368
end
369+
370+
module ConvertTestModule
371+
using GeoInterface
372+
struct TestPolygon end
373+
geointerface_traittype(::GeoInterface.PolygonTrait) = TestPolygon
374+
375+
GeoInterface.isgeometry(::TestPolygon) = true
376+
GeoInterface.geomtrait(::TestPolygon) = PolygonTrait()
377+
GeoInterface.ngeom(::PolygonTrait, geom::TestPolygon) = 2
378+
GeoInterface.getgeom(::PolygonTrait, geom::TestPolygon, i) = TestCurve()
379+
GeoInterface.convert(::Type{<:TestPolygon}, ::PolygonTrait, geom) = TestPolygon()
380+
end
381+
382+
@test GeoInterface.convert(ConvertTestModule, MyPolygon()) == ConvertTestModule.TestPolygon()
383+
369384
end

0 commit comments

Comments
 (0)