Skip to content

Commit 0758827

Browse files
eliascarvjuliohm
andauthored
Update Proj implementation (#1007)
* Update 'Proj' implementation * Apply suggestions from code review Co-authored-by: Júlio Hoffimann <[email protected]> * Fix ambiguities * Fix tests * Apply suggestions * Update comments --------- Co-authored-by: Júlio Hoffimann <[email protected]>
1 parent 905ff66 commit 0758827

File tree

4 files changed

+48
-3
lines changed

4 files changed

+48
-3
lines changed

src/Meshes.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ using Distances: Haversine, SphericalAngle
2020
using Distances: evaluate, result_type
2121
using Rotations: Rotation, QuatRotation, Angle2d
2222
using Rotations: rotation_between
23+
using CoordRefSystems: Basic, Projected, Geographic
2324
using NearestNeighbors: KDTree, BallTree
2425
using NearestNeighbors: knn, inrange
2526
using DelaunayTriangulation: triangulate, voronoi

src/transforms/proj.jl

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,25 @@ Proj(code::Type{<:ESRI}) = Proj{CoordRefSystems.get(code)}()
2929

3030
parameters(::Proj{CRS}) where {CRS} = (; CRS)
3131

32-
applycoord(::Proj{CRS}, p::Point) where {CRS} = Point(convert(CRS, coords(p)))
32+
# convert the CRS and preserve the manifold
33+
applycoord(::Proj{CRS}, p::Point{<:🌐}) where {CRS<:Basic} = Point{🌐}(convert(CRS, coords(p)))
34+
35+
# convert the CRS and (possibly) change the manifold
36+
applycoord(::Proj{CRS}, p::Point{<:🌐}) where {CRS<:Projected} = _proj(CRS, p)
37+
applycoord(::Proj{CRS}, p::Point{<:🌐}) where {CRS<:Geographic} = _proj(CRS, p)
38+
applycoord(::Proj{CRS}, p::Point{<:𝔼}) where {CRS<:Basic} = _proj(CRS, p)
39+
applycoord(::Proj{CRS}, p::Point{<:𝔼}) where {CRS<:Projected} = _proj(CRS, p)
40+
applycoord(::Proj{CRS}, p::Point{<:𝔼}) where {CRS<:Geographic} = _proj(CRS, p)
3341

3442
applycoord(::Proj, v::Vec) = v
3543

3644
# --------------
3745
# SPECIAL CASES
3846
# --------------
3947

40-
applycoord(t::Proj, g::Primitive) = TransformedGeometry(g, t)
48+
applycoord(t::Proj{<:Projected}, g::Primitive{<:🌐}) = TransformedGeometry(g, t)
49+
50+
applycoord(t::Proj{<:Geographic}, g::Primitive{<:𝔼}) = TransformedGeometry(g, t)
4151

4252
applycoord(t::Proj, g::RectilinearGrid) = applycoord(t, convert(SimpleMesh, g))
4353

@@ -54,3 +64,9 @@ function Base.show(io::IO, ::MIME"text/plain", t::Proj{CRS}) where {CRS}
5464
println(io)
5565
print(io, "└─ CRS: $CRS")
5666
end
67+
68+
# -----------------
69+
# HELPER FUNCTIONS
70+
# -----------------
71+
72+
_proj(CRS, p) = Point(convert(CRS, coords(p)))

test/meshes.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,7 @@
726726
mesh = SimpleMesh(points, connec)
727727
trans = Proj(Cartesian)
728728
tmesh = Meshes.TransformedMesh(mesh, trans)
729-
@test manifold(tmesh) === 𝔼{3}
729+
@test manifold(tmesh) === 🌐
730730
@test crs(tmesh) <: Cartesian
731731
trans = Proj(Polar)
732732
tgrid = Meshes.TransformedMesh(grid, trans)

test/transforms.jl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,6 +1107,20 @@
11071107
r, c = TB.apply(f, g)
11081108
@test r Point(Polar(T(2), T/ 4)))
11091109

1110+
# change the manifold
1111+
f = Proj(Mercator)
1112+
g = latlon(0, 0)
1113+
r, c = TB.apply(f, g)
1114+
@test manifold(r) === 𝔼{2}
1115+
@test r merc(0, 0)
1116+
1117+
# preserve the manifold
1118+
f = Proj(Cartesian)
1119+
g = latlon(0, 0)
1120+
r, c = TB.apply(f, g)
1121+
@test manifold(r) === 🌐
1122+
@test r cart(6378137.0, 0, 0)
1123+
11101124
# --------
11111125
# SEGMENT
11121126
# --------
@@ -1213,6 +1227,20 @@
12131227
d = SimpleMesh(p, c)
12141228
r, c = TB.apply(f, d)
12151229
@test r SimpleMesh(f.(vertices(d)), topology(d))
1230+
1231+
# --------------
1232+
# SPECIAL CASES
1233+
# --------------
1234+
1235+
f = Proj(Mercator)
1236+
g = Box(latlon(0, 180), latlon(45, 90))
1237+
r, c = TB.apply(f, g)
1238+
@test manifold(r) === 𝔼{2}
1239+
1240+
f = Proj(LatLon)
1241+
g = Box(merc(0, 0), merc(1, 1))
1242+
r, c = TB.apply(f, g)
1243+
@test manifold(r) === 🌐
12161244
end
12171245

12181246
@testset "LengthUnit" begin

0 commit comments

Comments
 (0)