Skip to content

Commit

Permalink
Allow all Integer types in EPSG constructor.
Browse files Browse the repository at this point in the history
  • Loading branch information
evetion committed Oct 27, 2023
1 parent 5e21c93 commit bbf4c83
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
7 changes: 5 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
name = "GeoFormatTypes"
uuid = "68eda718-8dee-11e9-39e7-89f7f65f511f"
authors = ["Rafael Schouten <[email protected]>", "Julia Computing and contributors"]
version = "0.4.2"
authors = [
"Rafael Schouten <[email protected]>",
"Julia Computing and contributors",
]
version = "0.4.3"

[compat]
julia = "1"
Expand Down
13 changes: 7 additions & 6 deletions src/GeoFormatTypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ struct ProjString <: CoordinateReferenceSystemFormat
val::String
ProjString(input::String) = begin
startswith(input, PROJ_PREFIX) ||
throw(ArgumentError("Not a Proj string: $input does not start with $PROJ_PREFIX"))
throw(ArgumentError("Not a Proj string: $input does not start with $PROJ_PREFIX"))
new(input)
end
end
Expand All @@ -181,12 +181,12 @@ struct ProjJSON <: CoordinateReferenceSystemFormat
val::Union{String,Dict{String,<:Any}}
ProjJSON(input::Dict{String,<:Any}) = begin
haskey(input, "type") ||
throw(ArgumentError("Not a ProjJSON: $input does not have the required key 'type'"))
throw(ArgumentError("Not a ProjJSON: $input does not have the required key 'type'"))
new(input)
end
ProjJSON(input::String) = begin
occursin("type", input) ||
throw(ArgumentError("Not a ProjJSON: $input does not have the required key 'type'"))
throw(ArgumentError("Not a ProjJSON: $input does not have the required key 'type'"))
new(input)
end
end
Expand Down Expand Up @@ -315,17 +315,18 @@ using `convert`, or another `CoordinateReferenceSystemFormat` when ArchGDAL.jl i
struct EPSG{N} <: CoordinateReferenceSystemFormat
val::NTuple{N,Int}
end
EPSG(input::Vararg{Int}) = EPSG(input)
EPSG(input::Vararg{Integer}) = EPSG(input)
EPSG(input::NTuple{N,Integer}) where {N} = EPSG(convert(NTuple{N,Int}, input))
function EPSG(input::AbstractString)
startswith(input, EPSG_PREFIX) || throw(ArgumentError("String $input does no start with $EPSG_PREFIX"))
code = Tuple(parse.(Int, split(input[findlast(EPSG_PREFIX, input).stop+1:end], "+")))
EPSG(code)
end

val(input::EPSG{1}) = input.val[1] # backwards compatible
Base.convert(::Type{Int}, input::EPSG{1}) = val(input)
Base.convert(::Type{T}, input::EPSG{1}) where {T<:Integer} = convert(T, val(input))
Base.convert(::Type{String}, input::EPSG) = string(EPSG_PREFIX, join(input.val, "+"))
Base.convert(::Type{EPSG}, input::Int) = EPSG((input,))
Base.convert(::Type{EPSG}, input::Integer) = EPSG((input,))


"""
Expand Down
4 changes: 3 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ end
@test ProjJSON(Dict("type" => "GeographicCRS")) isa ProjJSON
@test ProjJSON("type: GeographicCRS") isa ProjJSON
@test EPSG(4326) isa EPSG
@test EPSG(Int16(4326)) isa EPSG
@test EPSG((4326, 3855)) isa EPSG
@test WellKnownText("test") isa WellKnownText{Unknown}
@test WellKnownBinary([1, 2, 3, 4]) isa WellKnownBinary{Unknown}
Expand All @@ -34,7 +35,8 @@ end
@testset "Test conversion to string or int" begin
@test convert(String, ProjString("+proj=test")) == "+proj=test"
@test convert(String, EPSG(4326)) == "EPSG:4326"
@test convert(Int, EPSG(4326)) == 4326
@test convert(Int64, EPSG(4326)) == 4326
@test convert(Int32, EPSG(4326)) == Int32(4326)
@test_throws MethodError convert(Int, EPSG(4326, 3855))
@test convert(String, EPSG(4326, 3855)) == "EPSG:4326+3855"
@test convert(String, WellKnownText("test")) == "test"
Expand Down

1 comment on commit bbf4c83

@evetion
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rafaqz I inadvertently pushed to master here. This fixes the

MethodError: no method matching GeoFormatTypes.EPSG(::Int32) over at ArchGDAL.

Please sign in to comment.