Skip to content

Commit af5eac2

Browse files
committed
fix all doctests and add tests for coord transforms
1 parent 6e8baa1 commit af5eac2

File tree

6 files changed

+101
-2
lines changed

6 files changed

+101
-2
lines changed

src/utils/UnitSpherical/UnitSpherical.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ include("cap.jl")
1515

1616
export UnitSphericalPoint, UnitSphereFromGeographic, GeographicFromUnitSphere,
1717
slerp, SphericalCap
18+
export spherical_distance
1819

1920
end

src/utils/UnitSpherical/coordinate_transforms.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ Accepts any [GeoInterface-compatible](https://github.com/JuliaGeo/GeoInterface.j
1313
## Examples
1414
1515
```jldoctest
16+
julia> import GeoInterface as GI; using GeometryOps.UnitSpherical
17+
1618
julia> UnitSphereFromGeographic()(GI.Point(45, 45))
1719
3-element UnitSphericalPoint{Float64} with indices SOneTo(3):
1820
0.5000000000000001
@@ -21,6 +23,8 @@ julia> UnitSphereFromGeographic()(GI.Point(45, 45))
2123
```
2224
2325
```jldoctest
26+
julia> using GeometryOps.UnitSpherical
27+
2428
julia> UnitSphereFromGeographic()((45, 45))
2529
3-element UnitSphericalPoint{Float64} with indices SOneTo(3):
2630
0.5000000000000001
@@ -62,10 +66,12 @@ Accepts any 3-element vector, but the input is assumed to be on the unit sphere.
6266
## Examples
6367
6468
```jldoctest
69+
julia> using GeometryOps.UnitSpherical
70+
6571
julia> GeographicFromUnitSphere()(UnitSphericalPoint(0.5, 0.5, 1/√(2)))
6672
(45.0, 44.99999999999999)
6773
```
68-
(the inaccuracy is due to the precision of `atan` function)
74+
(the inaccuracy is due to the precision of the `atan` function)
6975
7076
"""
7177
struct GeographicFromUnitSphere <: CoordinateTransformations.Transformation

src/utils/UnitSpherical/point.jl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,13 @@ and back again.
2828
## Examples
2929
3030
```jldoctest
31+
julia> using GeometryOps.UnitSpherical
32+
3133
julia> UnitSphericalPoint(1, 0, 0)
32-
UnitSphericalPoint(1.0, 0.0, 0.0)
34+
3-element UnitSphericalPoint{Int64} with indices SOneTo(3):
35+
1
36+
0
37+
0
3338
```
3439
3540
"""
@@ -111,11 +116,15 @@ Returns a `Number`, usually Float64 but that depends on the input type.
111116
## Doctests
112117
113118
```jldoctest
119+
julia> using GeometryOps.UnitSpherical
120+
114121
julia> spherical_distance(UnitSphericalPoint(1, 0, 0), UnitSphericalPoint(0, 1, 0))
115122
1.5707963267948966
116123
```
117124
118125
```jldoctest
126+
julia> using GeometryOps.UnitSpherical
127+
119128
julia> spherical_distance(UnitSphericalPoint(1, 0, 0), UnitSphericalPoint(1, 0, 0))
120129
0.0
121130
```

src/utils/UnitSpherical/slerp.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ between 0 and 1 along the path from `a` to `b`.
2626
## Examples
2727
2828
```jldoctest
29+
julia> using GeometryOps.UnitSpherical
30+
2931
julia> slerp(UnitSphericalPoint(1, 0, 0), UnitSphericalPoint(0, 1, 0), 0.5)
3032
3-element UnitSphericalPoint{Float64} with indices SOneTo(3):
3133
0.7071067811865475

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ end
1717
@safetestset "Utils" begin include("utils/utils.jl") end
1818
@safetestset "LoopStateMachine" begin include("utils/LoopStateMachine.jl") end
1919
@safetestset "SpatialTreeInterface" begin include("utils/SpatialTreeInterface.jl") end
20+
@safetestset "UnitSpherical" begin include("utils/unitspherical.jl") end
2021

2122
# Methods
2223
@safetestset "Angles" begin include("methods/angles.jl") end

test/utils/unitspherical.jl

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
using Test
2+
3+
using GeometryOps.UnitSpherical
4+
5+
import GeoInterface as GI
6+
7+
@testset "Coordinate transforms" begin
8+
@testset "UnitSphereFromGeographic" begin
9+
# Test with GeoInterface Point
10+
point = GI.Point(45, 45)
11+
result = UnitSphereFromGeographic()(point)
12+
@test result isa UnitSphericalPoint{Float64}
13+
@test length(result) == 3
14+
@test isapprox(result[1], 0.5, atol=1e-10)
15+
@test isapprox(result[2], 0.5, atol=1e-10)
16+
@test isapprox(result[3], 1/√2, atol=1e-10)
17+
18+
# Test with tuple
19+
result = UnitSphereFromGeographic()((45, 45))
20+
@test result isa UnitSphericalPoint{Float64}
21+
@test length(result) == 3
22+
@test isapprox(result[1], 0.5, atol=1e-10)
23+
@test isapprox(result[2], 0.5, atol=1e-10)
24+
@test isapprox(result[3], 1/√2, atol=1e-10)
25+
26+
# Test edge cases
27+
# North pole
28+
result = UnitSphereFromGeographic()((0, 90))
29+
@test isapprox(result[1], 0.0, atol=1e-10)
30+
@test isapprox(result[2], 0.0, atol=1e-10)
31+
@test isapprox(result[3], 1.0, atol=1e-10)
32+
33+
# South pole
34+
result = UnitSphereFromGeographic()((0, -90))
35+
@test isapprox(result[1], 0.0, atol=1e-10)
36+
@test isapprox(result[2], 0.0, atol=1e-10)
37+
@test isapprox(result[3], -1.0, atol=1e-10)
38+
39+
# Equator
40+
result = UnitSphereFromGeographic()((0, 0))
41+
@test isapprox(result[1], 1.0, atol=1e-10)
42+
@test isapprox(result[2], 0.0, atol=1e-10)
43+
@test isapprox(result[3], 0.0, atol=1e-10)
44+
end
45+
46+
@testset "GeographicFromUnitSphere" begin
47+
# Test basic conversion
48+
point = UnitSphericalPoint(0.5, 0.5, 1/√2)
49+
result = GeographicFromUnitSphere()(point)
50+
@test result isa Tuple{Float64,Float64}
51+
@test isapprox(result[1], 45.0, atol=1e-10) # longitude
52+
@test isapprox(result[2], 45.0, atol=1e-10) # latitude
53+
54+
# Test edge cases
55+
# North pole
56+
result = GeographicFromUnitSphere()(UnitSphericalPoint(0.0, 0.0, 1.0))
57+
@test isapprox(result[1], 0.0, atol=1e-10) # longitude (undefined at poles, convention is 0)
58+
@test isapprox(result[2], 90.0, atol=1e-10) # latitude
59+
60+
# South pole
61+
result = GeographicFromUnitSphere()(UnitSphericalPoint(0.0, 0.0, -1.0))
62+
@test isapprox(result[1], 0.0, atol=1e-10) # longitude (undefined at poles, convention is 0)
63+
@test isapprox(result[2], -90.0, atol=1e-10) # latitude
64+
65+
# Equator
66+
result = GeographicFromUnitSphere()(UnitSphericalPoint(1.0, 0.0, 0.0))
67+
@test isapprox(result[1], 0.0, atol=1e-10) # longitude
68+
@test isapprox(result[2], 0.0, atol=1e-10) # latitude
69+
70+
# Test with regular vector
71+
result = GeographicFromUnitSphere()([0.5, 0.5, 1/√2])
72+
@test result isa Tuple{Float64,Float64}
73+
@test isapprox(result[1], 45.0, atol=1e-10)
74+
@test isapprox(result[2], 45.0, atol=1e-10)
75+
76+
# Test error handling for non-3D vectors
77+
@test_throws AssertionError GeographicFromUnitSphere()([1.0, 0.0])
78+
@test_throws AssertionError GeographicFromUnitSphere()([1.0, 0.0, 0.0, 0.0])
79+
end
80+
end

0 commit comments

Comments
 (0)