diff --git a/src/predicates/in.jl b/src/predicates/in.jl index 26f31f0ef..e2f6d88d7 100644 --- a/src/predicates/in.jl +++ b/src/predicates/in.jl @@ -34,6 +34,24 @@ Base.in(p::Point, pl::Plane) = isapproxzero(udot(normal(pl), p - pl(0, 0))) Base.in(p::Point, b::Box) = minimum(b) ⪯ p ⪯ maximum(b) +function Base.in(p::Point{🌐}, b::Box{🌐}) + l, r = extrema(b) + + latlonₚ = convert(LatLon, coords(p)) + latlonₗ = convert(LatLon, coords(l)) + latlonᵣ = convert(LatLon, coords(r)) + + latlonₗ.lat ≤ latlonₚ.lat ≤ latlonᵣ.lat && inlonrange(latlonₗ.lon, latlonₚ.lon, latlonᵣ.lon) +end + +function inlonrange(lonₗ, lonₚ, lonᵣ) + if isnegative(lonₗ) && isnonnegative(lonᵣ) + lonₚ ≤ lonₗ || (isnonnegative(lonₚ) && lonₚ ≤ lonᵣ) + else + lonₗ ≤ lonₚ ≤ lonᵣ + end +end + function Base.in(p::Point, b::Ball) c = center(b) r = radius(b) diff --git a/test/predicates.jl b/test/predicates.jl index e3e2a439e..dd186477a 100644 --- a/test/predicates.jl +++ b/test/predicates.jl @@ -115,6 +115,23 @@ end @test cart(1 / 3, 1 / 3, 1 / 3) ∈ t @test cart(0, 0, 0) ∉ t @test cart(1, 1, 1) ∉ t + + # tests for LatLon + box = Box(latlon(48.0110, 11.2930), latlon(48.2511, 11.8650)) + @test latlon(48.1, 11.5) ∈ box + @test latlon(48.034642451455, 10.94121275924084) ∉ box + + box = Box(latlon(48.0110, -178.5), latlon(48.2511, 2.001)) + @test latlon(48.1, 0) ∈ box + @test latlon(48.1, -179.1) ∈ box + @test latlon(48.1, 1.1) ∈ box + @test latlon(48.1, -178.1) ∉ box + @test latlon(48.1, 2.1) ∉ box + + box = Box(latlon(48.0110, -50.5), latlon(48.2511, -45.4)) + @test latlon(48.1, -48) ∈ box + @test latlon(48.1, -50.8) ∉ box + @test latlon(48.1, -45.3) ∉ box end @testitem "issimplex" setup = [Setup] begin