Skip to content

Commit

Permalink
Refactor the 'isapprox' and 'show' methods of the 'Coordinates' (#736)
Browse files Browse the repository at this point in the history
  • Loading branch information
eliascarv authored Feb 7, 2024
1 parent 28bd035 commit 8ee19ea
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 39 deletions.
22 changes: 16 additions & 6 deletions src/coordinates.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,34 @@ Parent type of all coordinate types.
"""
abstract type Coordinates{N} end

Base.isapprox(c₁::C, c₂::C; kwargs...) where {C<:Coordinates} =
all(isapprox(getfield(c₁, n), getfield(c₂, n); kwargs...) for n in fieldnames(C))
_fields(coords::Coordinates) = coords
_fnames(coords::Coordinates) = fieldnames(typeof(coords))

function Base.isapprox(coords₁::C, coords₂::C; kwargs...) where {N,C<:Coordinates{N}}
f₁ = _fields(coords₁)
f₂ = _fields(coords₂)
all(isapprox(getfield(f₁, i), getfield(f₂, i); kwargs...) for i in 1:N)
end

# -----------
# IO METHODS
# -----------

function Base.show(io::IO, coords::Coordinates)
name = nameof(typeof(coords))
name = prettyname(coords)
print(io, "$name(")
printfields(io, coords, compact=true)
fields = _fields(coords)
fnames = _fnames(coords)
printfields(io, fields, fnames, compact=true)
print(io, ")")
end

function Base.show(io::IO, ::MIME"text/plain", coords::Coordinates)
name = nameof(typeof(coords))
name = prettyname(coords)
print(io, "$name coordinates")
printfields(io, coords)
fields = _fields(coords)
fnames = _fnames(coords)
printfields(io, fields, fnames)
end

# ----------------
Expand Down
19 changes: 2 additions & 17 deletions src/coordinates/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,8 @@ Cartesian(coords::Vararg{L,N}) where {N,L<:Len} = Cartesian{N,L}(coords)
Cartesian(coords::Len...) = Cartesian(promote(coords...)...)
Cartesian(coords::Number...) = Cartesian(addunit.(coords, u"m")...)

Base.isapprox(c₁::Cartesian{N}, c₂::Cartesian{N}; kwargs...) where {N} =
all(isapprox(x₁, x₂; kwargs...) for (x₁, x₂) in zip(c₁.coords, c₂.coords))

function Base.show(io::IO, (; coords)::Cartesian{N}) where {N}
print(io, "Cartesian(")
fnames = _cartfields(N)
printfields(io, coords, fnames, compact=true)
print(io, ")")
end

function Base.show(io::IO, ::MIME"text/plain", (; coords)::Cartesian{N}) where {N}
print(io, "Cartesian coordinates")
fnames = _cartfields(N)
printfields(io, coords, fnames)
end

function _cartfields(N)
_fields(coords::Cartesian) = coords.coords
function _fnames(::Cartesian{N}) where {N}
if N == 1
("x",)
elseif N == 2
Expand Down
19 changes: 3 additions & 16 deletions src/coordinates/gis.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,16 @@ end

EPSG{Code,N,Coords}(args...) where {Code,N,Coords} = EPSG{Code,N,Coords}(Coords(args))

_fields(coords::EPSG) = coords.coords
_fnames(coords::EPSG) = keys(coords.coords)

"""
typealias(::Type{EPSG{code}})
Returns a coordinate type that has the EPSG `code`.
"""
function typealias end

Base.isapprox(c₁::T, c₂::T; kwargs...) where {T<:EPSG} =
all(isapprox(x₁, x₂; kwargs...) for (x₁, x₂) in zip(c₁.coords, c₂.coords))

function Base.show(io::IO, coords::EPSG)
name = prettyname(coords)
print(io, "$name(")
printfields(io, coords.coords, compact=true)
print(io, ")")
end

function Base.show(io::IO, ::MIME"text/plain", coords::EPSG)
name = prettyname(coords)
print(io, "$name coordinates")
printfields(io, coords.coords)
end

"""
LatLon(lat, lon)
Expand Down

0 comments on commit 8ee19ea

Please sign in to comment.