Skip to content

Commit f533e32

Browse files
authored
stop using Base.convert (#66)
* stop using Base.convert * move convert no-op to interface.jl And expand the example a bit.
1 parent 9c1916d commit f533e32

File tree

4 files changed

+11
-19
lines changed

4 files changed

+11
-19
lines changed

docs/src/guides/developer.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,14 @@ And lastly, there are many other optional functions for each specific geometry.
4242
### Conversion
4343
It is useful if others can convert any custom geometry into your
4444
geometry type, if their custom geometry supports GeoInterface as well.
45-
This requires the following three methods, and the last one requires more code to generate `T` with `ngeom`, `getgeom` or just `coordinates` calls.
45+
This requires the following methods, where the implementation should be defined in terms
46+
of GeoInterface methods like `ngeom`, `getgeom`, or just `coordinates` calls.
4647

4748
```julia
48-
Base.convert(::Type{T}, geom) where T<:AbstractPackageType = Base.convert(T, geomtrait(geom), geom)
49-
Base.convert(::Type{T}, ::LineStringTrait, geom::T) = geom # fast fallthrough without conversion
50-
Base.convert(::Type{T}, ::LineStringTrait, geom) = ... # slow custom conversion based on ngeom and getgeom
49+
# This method will get called on GeoInterface.convert(::Type{T}, geom)
50+
# if geomtrait(geom) == LineStringTrait()
51+
GeoInterface.convert(::Type{CustomLineString}, ::LineStringTrait, geom) = ...
52+
GeoInterface.convert(::Type{CustomPolygon}, ::PolygonTrait, geom) = ...
5153
```
5254

5355
## Required for Feature(Collection)s

src/fallbacks.jl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,12 @@ getfeature(t::AbstractFeatureCollectionTrait, fc) = (getfeature(t, fc, i) for i
103103
# Backwards compatibility
104104
coordinates(t::AbstractPointTrait, geom) = collect(getcoord(t, geom))
105105
coordinates(t::AbstractGeometryTrait, geom) = collect(coordinates.(getgeom(t, geom)))
106-
function coordinates(t::AbstractFeatureTrait, feature)
106+
function coordinates(t::AbstractFeatureTrait, feature)
107107
geom = geometry(feature)
108108
isnothing(geom) ? [] : coordinates(geom)
109109
end
110110
coordinates(t::AbstractFeatureCollectionTrait, fc) = [coordinates(f) for f in getfeature(fc)]
111111

112-
Base.convert(T::Type, ::AbstractGeometryTrait, geom) = error("Conversion is enabled for type $T, but not implemented. Please report this issue to the package maintainer.")
113-
114112
# Subtraits
115113

116114
"""

src/interface.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -606,10 +606,10 @@ coordinates(obj) = coordinates(trait(obj), obj)
606606
"""
607607
convert(type::CustomGeom, geom)
608608
609-
Convert `geom` into the `CustomGeom` type if both geom as the CustomGeom package
610-
have implemented GeoInterface.
609+
Create a `CustomGeom` from any `geom` that implements the GeoInterface.
611610
"""
612611
convert(T, geom) = convert(T, geomtrait(geom), geom)
612+
convert(::Type{T}, x::T) where {T} = x # no-op
613613

614614
"""
615615
astext(geom) -> WKT

test/test_primitives.jl

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ using Test
3535
GeoInterface.geomtrait(::MyCurve) = LineStringTrait()
3636
GeoInterface.ngeom(::LineStringTrait, geom::MyCurve) = 2
3737
GeoInterface.getgeom(::LineStringTrait, geom::MyCurve, i) = MyPoint()
38-
Base.convert(T::Type{MyCurve}, geom::X) where {X} = Base.convert(T, geomtrait(geom), geom)
39-
Base.convert(::Type{MyCurve}, ::LineStringTrait, geom::MyCurve) = geom
4038

4139
GeoInterface.isgeometry(::MyPolygon) = true
4240
GeoInterface.geomtrait(::MyPolygon) = PolygonTrait()
@@ -235,15 +233,9 @@ end
235233
struct XCurve end
236234
struct XPolygon end
237235

238-
Base.convert(T::Type{XCurve}, geom::X) where {X} = Base.convert(T, geomtrait(geom), geom)
239-
Base.convert(::Type{XCurve}, ::LineStringTrait, geom::XCurve) = geom # fast fallthrough
240-
Base.convert(::Type{XCurve}, ::LineStringTrait, geom) = geom
241-
242236
geom = MyCurve()
243-
@test !isnothing(convert(MyCurve, geom))
244-
245-
Base.convert(T::Type{XPolygon}, geom::X) where {X} = Base.convert(T, geomtrait(geom), geom)
246-
@test_throws Exception convert(MyPolygon, geom)
237+
@test GeoInterface.convert(MyCurve, geom) === geom
238+
@test_throws Exception GeoInterface.convert(MyPolygon, geom)
247239
end
248240

249241
@testset "Operations" begin

0 commit comments

Comments
 (0)