Skip to content

Commit 17fdff6

Browse files
committed
add simple projected coordinates
1 parent 7f5297d commit 17fdff6

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

src/SkyCoords.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export AbstractSkyCoords,
1010
GalCoords,
1111
FK5Coords,
1212
CartesianCoords,
13+
ProjectedCoords,
1314
separation,
1415
position_angle,
1516
offset,
@@ -18,6 +19,7 @@ export AbstractSkyCoords,
1819

1920
include("types.jl")
2021
include("cartesian.jl")
22+
include("projected.jl")
2123

2224
# -----------------------------------------------------------------------------
2325
# Helper functions: Create rotation matrix about a given axis (x, y, z)

src/projected.jl

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
abstract type AbstractProjectedCoords <: AbstractSkyCoords end
2+
3+
""" Projected coordinates with origin and offset.
4+
Approximation for small FoV. """
5+
struct ProjectedCoords{TC,T} <: AbstractProjectedCoords
6+
origin::TC
7+
offset::SVector{2,T}
8+
end
9+
10+
origin(c::ProjectedCoords) = c.origin
11+
lon(c::AbstractProjectedCoords) = lon(origin(c)) + c.offset[1] / cos(lat(origin(c)))
12+
lat(c::AbstractProjectedCoords) = lat(origin(c)) + c.offset[2]
13+
14+
Base.convert(::Type{T}, c::T) where {T<:AbstractProjectedCoords} = c
15+
Base.convert(::Type{TCto}, c::AbstractProjectedCoords) where {TCto<:AbstractSkyCoords} =
16+
convert(TCto,
17+
constructorof(typeof(origin(c)))(
18+
lon(c), lat(c)
19+
)
20+
)
21+
22+
Base.isapprox(a::ProjectedCoords, b::ProjectedCoords; kwargs...) = isapprox(origin(a), origin(b); kwargs...) && isapprox(a.offset, b.offset; kwargs...)
23+
24+
function project(origin::AbstractSkyCoords, c::AbstractSkyCoords)
25+
cc = convert(typeof(origin), c)
26+
Δlon = _center_angle(lon(cc) - lon(origin))
27+
offset = SVector(Δlon * cos(lat(origin)), lat(cc) - lat(origin))
28+
ProjectedCoords(origin, offset)
29+
end
30+

test/runtests.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ using ConstructionBase: setproperties
33
using DelimitedFiles
44
using LinearAlgebra: normalize
55
using SkyCoords
6+
using SkyCoords: project, origin
67
using StableRNGs
78
using Statistics
89
using Test
@@ -15,6 +16,19 @@ rad2arcsec(r) = 3600 * rad2deg(r)
1516
# tests against astropy.coordinates
1617
include("astropy.jl")
1718

19+
@testset "projected coords" begin
20+
c0 = ICRSCoords(0.1, -0.2)
21+
c1 = ICRSCoords(0.1 + 1e-5, -0.2 + 3e-5)
22+
cp = project(c0, c1)::ProjectedCoords
23+
@test origin(cp) == c0
24+
@test cp.offset[1] 0.98 * 1e-5 rtol=1e-4
25+
@test cp.offset[2] 3e-5
26+
@test convert(ICRSCoords, cp) c1
27+
@test convert(GalCoords, cp) convert(GalCoords, c1)
28+
@test cp == cp
29+
@test cp cp
30+
end
31+
1832
# Test separation between coordinates and conversion with mixed floating types.
1933
@testset "Separation" begin
2034
c1 = ICRSCoords(ℯ, pi / 2)

0 commit comments

Comments
 (0)