Skip to content

Commit c449aa6

Browse files
authored
convert to module types (#85)
* convert to module types * module at top level * fix tests * rename to geointerface_geomtype
1 parent a5dd3b4 commit c449aa6

File tree

3 files changed

+110
-80
lines changed

3 files changed

+110
-80
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_geomtype(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: 97 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,88 @@
11
using GeoInterface
22
using Test
33

4-
@testset "Developer" begin
5-
# Implement interface
6-
struct MyPoint end
7-
struct MyEmptyPoint end
8-
struct MyCurve end
9-
struct MyPolygon end
10-
struct MyTriangle end
11-
struct MyMultiPoint end
12-
struct MyMultiCurve end
13-
struct MyMultiPolygon end
14-
struct MyTIN end
15-
struct MyCollection end
16-
struct MyFeature{G,P}
17-
geometry::G
18-
properties::P
19-
end
20-
struct MyFeatureCollection{G}
21-
geoms::G
22-
end
4+
# Implement interface
5+
struct MyPoint end
6+
struct MyEmptyPoint end
7+
struct MyCurve end
8+
struct MyPolygon end
9+
struct MyTriangle end
10+
struct MyMultiPoint end
11+
struct MyMultiCurve end
12+
struct MyMultiPolygon end
13+
struct MyTIN end
14+
struct MyCollection end
15+
struct MyFeature{G,P}
16+
geometry::G
17+
properties::P
18+
end
19+
struct MyFeatureCollection{G}
20+
geoms::G
21+
end
22+
23+
GeoInterface.isgeometry(::MyPoint) = true
24+
GeoInterface.geomtrait(::MyPoint) = PointTrait()
25+
GeoInterface.ncoord(::PointTrait, geom::MyPoint) = 2
26+
GeoInterface.getcoord(::PointTrait, geom::MyPoint, i) = [1, 2][i]
27+
28+
GeoInterface.isgeometry(::MyEmptyPoint) = true
29+
GeoInterface.geomtrait(::MyEmptyPoint) = PointTrait()
30+
GeoInterface.ncoord(::PointTrait, geom::MyEmptyPoint) = 0
31+
GeoInterface.isempty(::PointTrait, geom::MyEmptyPoint) = true
32+
33+
GeoInterface.isgeometry(::MyCurve) = true
34+
GeoInterface.geomtrait(::MyCurve) = LineStringTrait()
35+
GeoInterface.ngeom(::LineStringTrait, geom::MyCurve) = 2
36+
GeoInterface.getgeom(::LineStringTrait, geom::MyCurve, i) = MyPoint()
37+
38+
GeoInterface.isgeometry(::MyPolygon) = true
39+
GeoInterface.geomtrait(::MyPolygon) = PolygonTrait()
40+
GeoInterface.ngeom(::PolygonTrait, geom::MyPolygon) = 2
41+
GeoInterface.getgeom(::PolygonTrait, geom::MyPolygon, i) = MyCurve()
42+
43+
GeoInterface.isgeometry(::MyTriangle) = true
44+
GeoInterface.geomtrait(::MyTriangle) = TriangleTrait()
45+
GeoInterface.ngeom(::TriangleTrait, geom::MyTriangle) = 3
46+
GeoInterface.getgeom(::TriangleTrait, geom::MyTriangle, i) = MyCurve()
47+
48+
GeoInterface.isgeometry(::MyMultiPoint) = true
49+
GeoInterface.geomtrait(::MyMultiPoint) = MultiPointTrait()
50+
GeoInterface.ngeom(::MultiPointTrait, geom::MyMultiPoint) = 2
51+
GeoInterface.getgeom(::MultiPointTrait, geom::MyMultiPoint, i) = MyPoint()
52+
53+
GeoInterface.isgeometry(::MyMultiCurve) = true
54+
GeoInterface.geomtrait(::MyMultiCurve) = MultiCurveTrait()
55+
GeoInterface.ngeom(::MultiCurveTrait, geom::MyMultiCurve) = 2
56+
GeoInterface.getgeom(::MultiCurveTrait, geom::MyMultiCurve, i) = MyCurve()
57+
58+
GeoInterface.isgeometry(::MyMultiPolygon) = true
59+
GeoInterface.geomtrait(::MyMultiPolygon) = MultiPolygonTrait()
60+
GeoInterface.ngeom(::MultiPolygonTrait, geom::MyMultiPolygon) = 2
61+
GeoInterface.getgeom(::MultiPolygonTrait, geom::MyMultiPolygon, i) = MyPolygon()
62+
63+
GeoInterface.isgeometry(::MyTIN) = true
64+
GeoInterface.geomtrait(::MyTIN) = PolyhedralSurfaceTrait()
65+
GeoInterface.ngeom(::PolyhedralSurfaceTrait, geom::MyTIN) = 2
66+
GeoInterface.getgeom(::PolyhedralSurfaceTrait, geom::MyTIN, i) = MyTriangle()
67+
68+
GeoInterface.isgeometry(::MyCollection) = true
69+
GeoInterface.geomtrait(::MyCollection) = GeometryCollectionTrait()
70+
GeoInterface.ngeom(::GeometryCollectionTrait, geom::MyCollection) = 2
71+
GeoInterface.getgeom(::GeometryCollectionTrait, geom::MyCollection, i) = MyCurve()
72+
73+
GeoInterface.isfeature(::Type{<:MyFeature}) = true
74+
GeoInterface.trait(feature::MyFeature) = FeatureTrait()
75+
GeoInterface.geometry(f::MyFeature) = f.geometry
76+
GeoInterface.properties(f::MyFeature) = f.properties
77+
GeoInterface.extent(f::MyFeature) = nothing
78+
79+
GeoInterface.isfeaturecollection(fc::Type{<:MyFeatureCollection}) = true
80+
GeoInterface.trait(fc::MyFeatureCollection) = FeatureCollectionTrait()
81+
GeoInterface.nfeature(::FeatureCollectionTrait, fc::MyFeatureCollection) = length(fc.geoms)
82+
GeoInterface.getfeature(::FeatureCollectionTrait, fc::MyFeatureCollection) = fc.geoms
83+
GeoInterface.getfeature(::FeatureCollectionTrait, fc::MyFeatureCollection, i::Integer) = fc.geoms[i]
2384

24-
GeoInterface.isgeometry(::MyPoint) = true
25-
GeoInterface.geomtrait(::MyPoint) = PointTrait()
26-
GeoInterface.ncoord(::PointTrait, geom::MyPoint) = 2
27-
GeoInterface.getcoord(::PointTrait, geom::MyPoint, i) = [1, 2][i]
28-
29-
GeoInterface.isgeometry(::MyEmptyPoint) = true
30-
GeoInterface.geomtrait(::MyEmptyPoint) = PointTrait()
31-
GeoInterface.ncoord(::PointTrait, geom::MyEmptyPoint) = 0
32-
GeoInterface.isempty(::PointTrait, geom::MyEmptyPoint) = true
33-
34-
GeoInterface.isgeometry(::MyCurve) = true
35-
GeoInterface.geomtrait(::MyCurve) = LineStringTrait()
36-
GeoInterface.ngeom(::LineStringTrait, geom::MyCurve) = 2
37-
GeoInterface.getgeom(::LineStringTrait, geom::MyCurve, i) = MyPoint()
38-
39-
GeoInterface.isgeometry(::MyPolygon) = true
40-
GeoInterface.geomtrait(::MyPolygon) = PolygonTrait()
41-
GeoInterface.ngeom(::PolygonTrait, geom::MyPolygon) = 2
42-
GeoInterface.getgeom(::PolygonTrait, geom::MyPolygon, i) = MyCurve()
43-
44-
GeoInterface.isgeometry(::MyTriangle) = true
45-
GeoInterface.geomtrait(::MyTriangle) = TriangleTrait()
46-
GeoInterface.ngeom(::TriangleTrait, geom::MyTriangle) = 3
47-
GeoInterface.getgeom(::TriangleTrait, geom::MyTriangle, i) = MyCurve()
48-
49-
GeoInterface.isgeometry(::MyMultiPoint) = true
50-
GeoInterface.geomtrait(::MyMultiPoint) = MultiPointTrait()
51-
GeoInterface.ngeom(::MultiPointTrait, geom::MyMultiPoint) = 2
52-
GeoInterface.getgeom(::MultiPointTrait, geom::MyMultiPoint, i) = MyPoint()
53-
54-
GeoInterface.isgeometry(::MyMultiCurve) = true
55-
GeoInterface.geomtrait(::MyMultiCurve) = MultiCurveTrait()
56-
GeoInterface.ngeom(::MultiCurveTrait, geom::MyMultiCurve) = 2
57-
GeoInterface.getgeom(::MultiCurveTrait, geom::MyMultiCurve, i) = MyCurve()
58-
59-
GeoInterface.isgeometry(::MyMultiPolygon) = true
60-
GeoInterface.geomtrait(::MyMultiPolygon) = MultiPolygonTrait()
61-
GeoInterface.ngeom(::MultiPolygonTrait, geom::MyMultiPolygon) = 2
62-
GeoInterface.getgeom(::MultiPolygonTrait, geom::MyMultiPolygon, i) = MyPolygon()
63-
64-
GeoInterface.isgeometry(::MyTIN) = true
65-
GeoInterface.geomtrait(::MyTIN) = PolyhedralSurfaceTrait()
66-
GeoInterface.ngeom(::PolyhedralSurfaceTrait, geom::MyTIN) = 2
67-
GeoInterface.getgeom(::PolyhedralSurfaceTrait, geom::MyTIN, i) = MyTriangle()
68-
69-
GeoInterface.isgeometry(::MyCollection) = true
70-
GeoInterface.geomtrait(::MyCollection) = GeometryCollectionTrait()
71-
GeoInterface.ngeom(::GeometryCollectionTrait, geom::MyCollection) = 2
72-
GeoInterface.getgeom(::GeometryCollectionTrait, geom::MyCollection, i) = MyCurve()
73-
74-
GeoInterface.isfeature(::Type{<:MyFeature}) = true
75-
GeoInterface.trait(feature::MyFeature) = FeatureTrait()
76-
GeoInterface.geometry(f::MyFeature) = f.geometry
77-
GeoInterface.properties(f::MyFeature) = f.properties
78-
GeoInterface.extent(f::MyFeature) = nothing
79-
80-
GeoInterface.isfeaturecollection(fc::Type{<:MyFeatureCollection}) = true
81-
GeoInterface.trait(fc::MyFeatureCollection) = FeatureCollectionTrait()
82-
GeoInterface.nfeature(::FeatureCollectionTrait, fc::MyFeatureCollection) = length(fc.geoms)
83-
GeoInterface.getfeature(::FeatureCollectionTrait, fc::MyFeatureCollection) = fc.geoms
84-
GeoInterface.getfeature(::FeatureCollectionTrait, fc::MyFeatureCollection, i::Integer) = fc.geoms[i]
85+
@testset "Developer" begin
8586

8687
@testset "Point" begin
8788
geom = MyPoint()
@@ -366,4 +367,20 @@ end
366367
@test GeoInterface.testfeature(feature)
367368
@test GeoInterface.testfeaturecollection([feature, feature])
368369
end
370+
371+
372+
end
373+
374+
module ConvertTestModule
375+
using GeoInterface
376+
struct TestPolygon end
377+
geointerface_geomtype(::GeoInterface.PolygonTrait) = TestPolygon
378+
379+
GeoInterface.isgeometry(::TestPolygon) = true
380+
GeoInterface.geomtrait(::TestPolygon) = PolygonTrait()
381+
GeoInterface.ngeom(::PolygonTrait, geom::TestPolygon) = 2
382+
GeoInterface.getgeom(::PolygonTrait, geom::TestPolygon, i) = TestCurve()
383+
GeoInterface.convert(::Type{<:TestPolygon}, ::PolygonTrait, geom) = TestPolygon()
369384
end
385+
386+
@test GeoInterface.convert(ConvertTestModule, MyPolygon()) == ConvertTestModule.TestPolygon()

0 commit comments

Comments
 (0)