Skip to content

Commit 7484c4b

Browse files
authored
Merge pull request #6 from JuliaGeo/better_convert
clarify convert for mixed, improve tests and bump version to 0.3
2 parents 184d90a + 3416d4a commit 7484c4b

File tree

3 files changed

+82
-22
lines changed

3 files changed

+82
-22
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "GeoFormatTypes"
22
uuid = "68eda718-8dee-11e9-39e7-89f7f65f511f"
33
authors = ["Rafael Schouten <rafaelschouten@gmail.com>"]
4-
version = "0.2.1"
4+
version = "0.3.0"
55

66
[compat]
77
julia = "1"

src/GeoFormatTypes.jl

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,27 @@ abstract type GeoFormat end
4343
Base.convert(::Type{T1}, source::T2) where {T1<:GeoFormat,T2<:T1} = source
4444
# Convert uses the `mode` trait to distinguish crs form geometry conversion
4545
Base.convert(target::Type{T1}, source::T2) where {T1<:GeoFormat,T2<:GeoFormat} = begin
46-
formatmode = mode(source)
46+
sourcemode = mode(source)
4747
targetmode = mode(target)
48-
convertmode = if formatmode isa Mixed
49-
if targetmode isa Mixed
48+
convertmode = if targetmode isa Geom
49+
if sourcemode isa Union{Mixed,Geom}
5050
Geom() # Geom is the default if both formats are mixed
5151
else
52-
targetmode
52+
throw(ArgumentError("cannot convert $(typeof(source)) to $target"))
53+
end
54+
elseif targetmode isa CRS
55+
if sourcemode isa Union{Mixed,CRS}
56+
CRS()
57+
else
58+
throw(ArgumentError("cannot convert $(typeof(source)) to $target"))
59+
end
60+
else # targetmode isa Mixed
61+
# Mixed to Mixed defaults to Geom
62+
if sourcemode isa Union{Mixed,Geom}
63+
Geom()
64+
else
65+
CRS()
5366
end
54-
elseif targetmode isa typeof(formatmode)
55-
formatmode
56-
else
57-
throw(ArgumentError("cannot convert $(typeof(source)) to $target"))
5867
end
5968
convert(target, convertmode, source)
6069
end
@@ -120,6 +129,7 @@ struct WellKnownText{X,T<:String} <: AbstractWellKnownText{X}
120129
mode::X
121130
val::T
122131
end
132+
WellKnownText(val) = WellKnownText(Mixed(), val)
123133

124134
"""
125135
Well known text v2 following the new OGC standard
@@ -128,6 +138,7 @@ struct WellKnownText2{X,T<:String} <: AbstractWellKnownText{X}
128138
mode::X
129139
val::T
130140
end
141+
WellKnownText2(val) = WellKnownText(Mixed(), val)
131142

132143
"""
133144
Well known text following the ESRI standard
@@ -144,6 +155,7 @@ struct WellKnownBinary{X,T} <: MixedFormat{X}
144155
mode::X
145156
val::T
146157
end
158+
WellKnownBinary(val) = WellKnownBinary(Mixed(), val)
147159

148160
Base.convert(::Type{String}, input::WellKnownBinary) =
149161
error("`convert` to `String` is not defined for `WellKnownBinary`")
@@ -184,6 +196,7 @@ struct GML{X,T<:String} <: MixedFormat{X}
184196
mode::X
185197
val::T
186198
end
199+
GML(val) = GML(Mixed(), val)
187200

188201
"""
189202
GeoJSON String or Dict

test/runtests.jl

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,66 @@
11
using GeoFormatTypes, Test
22
using GeoFormatTypes: Geom, CRS, Mixed
33

4-
@test_throws ArgumentError ProjString("+lat_ts=56.5 +ellps=GRS80")
5-
convert(String, ProjString("+proj=merc +lat_ts=56.5 +ellps=GRS80")) == "+proj=merc +lat_ts=56.5 +ellps=GRS80"
4+
@testset "Test construcors" begin
5+
@test_throws ArgumentError ProjString("+lat_ts=56.5 +ellps=GRS80")
6+
@test_throws ArgumentError EPSG("ERROR:4326")
7+
@test EPSG("EPSG:4326") == EPSG(4326)
8+
end
69

7-
@test_throws ArgumentError EPSG("ERROR:4326")
8-
@test convert(String, EPSG("EPSG:4326")) == "EPSG:4326"
9-
@test convert(String, EPSG(4326)) == "EPSG:4326"
10-
@test convert(Int, EPSG("EPSG:4326")) == 4326
10+
@testset "Test conversion to string or int" begin
11+
@test convert(String, ProjString("+proj=test")) == "+proj=test"
12+
@test convert(String, EPSG(4326)) == "EPSG:4326"
13+
@test convert(Int, EPSG(4326)) == 4326
14+
@test convert(String, WellKnownText("test")) == "test"
15+
@test convert(String, WellKnownText2("test")) == "test"
16+
@test convert(String, GML("test")) == "test"
17+
@test convert(String, KML("test")) == "test"
18+
@test convert(String, GeoJSON("test")) == "test"
19+
end
1120

12-
@test convert(String, WellKnownText(Geom(), "test")) == "test"
13-
@test convert(String, WellKnownText2(CRS(), "test")) == "test"
14-
@test convert(String, ESRIWellKnownText(Geom(), "test")) == "test"
15-
@test convert(String, GML(Mixed(), "test")) == "test"
16-
@test convert(String, KML("test")) == "test"
17-
@test convert(String, GeoJSON("test")) == "test"
1821

19-
@test_throws ArgumentError convert(KML, ProjString("+proj=test"))
22+
# `convert` placeholder methods
23+
Base.convert(target::Type{<:GeoFormat}, mode::Union{Geom,Type{Geom}}, source::GeoFormat) =
24+
:geom
25+
Base.convert(target::Type{<:GeoFormat}, mode::Union{CRS,Type{CRS}}, source::GeoFormat) =
26+
:crs
27+
28+
@testset "Test convert mode allocation" begin
29+
@testset "Test identical type is passed through unchanged" begin
30+
@test convert(WellKnownText, WellKnownText(Mixed(), "test")) == WellKnownText(Mixed(), "test")
31+
@test convert(ProjString, ProjString("+proj=test")) == ProjString("+proj=test")
32+
end
33+
@testset "Test conversions are assigned to crs or geom correctly" begin
34+
@test convert(WellKnownText, WellKnownText2(CRS(), "test")) == :crs
35+
@test convert(WellKnownText2, WellKnownText(CRS(), "test")) == :crs
36+
@test convert(WellKnownBinary, WellKnownText(CRS(), "test")) == :crs
37+
@test convert(ProjString, WellKnownText(CRS(), "test")) == :crs
38+
@test convert(EPSG, ProjString("+proj=test")) == :crs
39+
@test convert(CoordSys, ProjString("+proj=test")) == :crs
40+
41+
@test convert(GeoJSON, WellKnownText(Geom(), "test")) == :geom
42+
@test convert(KML, WellKnownText(Geom(), "test")) == :geom
43+
@test convert(GML, WellKnownText(Geom(), "test")) == :geom
44+
@test convert(ESRIWellKnownText, WellKnownText(Geom(), "test")) == :geom
45+
@test convert(WellKnownBinary, WellKnownText(Geom(), "test")) == :geom
46+
@test convert(WellKnownText2, WellKnownText(Geom(), "test")) == :geom
47+
@test convert(WellKnownText2, WellKnownText(Geom(), "test")) == :geom
48+
@test convert(WellKnownText, WellKnownText2(Geom(), "test")) == :geom
49+
50+
@test convert(GeoJSON, WellKnownText(Mixed(), "test")) == :geom
51+
@test convert(KML, WellKnownText(Mixed(), "test")) == :geom
52+
@test convert(GML, WellKnownText(Mixed(), "test")) == :geom
53+
@test convert(ESRIWellKnownText, WellKnownText(Mixed(), "test")) == :geom
54+
@test convert(WellKnownBinary, WellKnownText(Mixed(), "test")) == :geom
55+
@test convert(WellKnownText2, WellKnownText(Mixed(), "test")) == :geom
56+
@test convert(WellKnownText2, WellKnownText(Mixed(), "test")) == :geom
57+
@test convert(WellKnownText, WellKnownText2(Mixed(), "test")) == :geom
58+
end
59+
@testset "Test conversions that are not possible throw an error" begin
60+
@test_throws ArgumentError convert(KML, ProjString("+proj=test"))
61+
@test_throws ArgumentError convert(GeoJSON, ProjString("+proj=test"))
62+
@test_throws ArgumentError convert(ProjString, WellKnownText(Geom(), "test"))
63+
@test_throws ArgumentError convert(CoordSys, WellKnownText(Geom(), "test"))
64+
@test_throws ArgumentError convert(EPSG, WellKnownText(Geom(), "test"))
65+
end
66+
end

0 commit comments

Comments
 (0)