Skip to content

Commit

Permalink
Handle coordinates that are outside the grid bounds (#15)
Browse files Browse the repository at this point in the history
* Handle coordinates that are outside the grid bounds

* Finalize the implementation

* Add tests

* Add more tests
  • Loading branch information
eliascarv authored Dec 12, 2024
1 parent 2c1ea24 commit a27e7e2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
24 changes: 18 additions & 6 deletions src/grids.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,28 @@ function interpolatelatlon(Datumₛ, Datumₜ, lat, lon)
end

function _interpolatelatlon(interps::AbstractVector, lat, lon)
@inbounds for interp in interps
(lonmin, lonmax), (latmin, latmax) = bounds(interp)
if lonmin < lon < lonmax && latmin < lat < latmax
return interp(lon, lat)
itp = zero(eltype(first(interps)))
for interp in interps
if _inbounds(interp, lat, lon)
itp = @inbounds interp(lon, lat)
break
end
end
throw(ArgumentError("coordinates outside of the transform domain"))
itp
end

_interpolatelatlon(interp, lat, lon) = interp(lon, lat)
function _interpolatelatlon(interp, lat, lon)
if _inbounds(interp, lat, lon)
@inbounds interp(lon, lat)
else
zero(eltype(interp))
end
end

function _inbounds(interp, lat, lon)
(lonmin, lonmax), (latmin, latmax) = bounds(interp)
lonmin < lon < lonmax && latmin < lat < latmax
end

"""
interpolators(Datumₛ, Datumₜ)
Expand Down
26 changes: 24 additions & 2 deletions test/converions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,29 @@
c3 = convert(LatLon{NTF}, c2)
@test allapprox(c3, c1)

# error: coordinates outside of the transform domain
# coordinates outside the grid bounds
# hgridshift
c1 = LatLon{SAD96}(T(10), T(-10))
c2 = convert(LatLon{SIRGAS2000}, c1)
@test c2.lat c1.lat
@test c2.lon c1.lon

c1 = LatLon{RD83}(T(65), T(25))
@test_throws ArgumentError convert(LatLon{ETRFLatest}, c1)
c2 = convert(LatLon{ETRFLatest}, c1)
@test c2.lat c1.lat
@test c2.lon c1.lon

# pointmotion
c1 = LatLonAlt{NAD83CSRS{3}}(T(90), T(10), T(1))
c2 = convert(LatLonAlt{NAD83CSRS{4}}, c1)
@test c2.lat c1.lat
@test c2.lon c1.lon
@test c2.alt c1.alt

# geocgridtranslation
c1 = convert(Cartesian, LatLon{NTF}(T(65), T(25)))
c2 = convert(Cartesian{RGF93v1}, c1)
@test c2.x c1.x
@test c2.y c1.y
@test c2.z c1.z
end

0 comments on commit a27e7e2

Please sign in to comment.