From 84efe55c1034d0bc61bcb591a494b0c2011a09ba Mon Sep 17 00:00:00 2001 From: Elias Carvalho <73039601+eliascarv@users.noreply.github.com> Date: Thu, 17 Oct 2024 10:57:44 -0300 Subject: [PATCH] Remove 'boundary' option in favor of using 'TransformedGeometry' explicitly (#1119) --- src/transforms/morphological.jl | 41 ++++++++------------------- src/transforms/proj.jl | 32 ++++++++++----------- test/transforms.jl | 50 ++++----------------------------- 3 files changed, 34 insertions(+), 89 deletions(-) diff --git a/src/transforms/morphological.jl b/src/transforms/morphological.jl index 578e2a161..5d10b944a 100644 --- a/src/transforms/morphological.jl +++ b/src/transforms/morphological.jl @@ -3,34 +3,33 @@ # ------------------------------------------------------------------ """ - Morphological(fun; boundary=false) + Morphological(fun) Morphological transform given by a function `fun` that maps the coordinates of a geometry or a domain to new coordinates (`coords -> newcoords`). -Optionally, the transform samples the `boundary` of -polytopes, if this option is `true`, to handle distortions -that occur in manifold conversions. - -# Examples +## Examples ```julia ball = Ball((0, 0), 1) ball |> Morphological(c -> Cartesian(c.x + c.y, c.y, c.x - c.y)) -triangle = Triangle(latlon(0, 0), latlon(0, 45), latlon(45, 0)) -triangle |> Morphological(c -> LatLonAlt(c.lat, c.lon, 0.0m), boundary=true) +triangle = Triangle(Point(LatLon(0, 0)), Point(LatLon(0, 45)), Point(LatLon(45, 0))) +triangle |> Morphological(c -> LatLonAlt(c.lat, c.lon, 0.0m)) ``` + +### Notes + +* By default, only the vertices of the polytopes are transformed, + disregarding distortions that occur in manifold conversions. + To handle this case, use [`TransformedGeometry`](@ref). """ -struct Morphological{Boundary,F<:Function} <: CoordinateTransform +struct Morphological{F<:Function} <: CoordinateTransform fun::F - Morphological{Boundary}(fun::F) where {Boundary,F<:Function} = new{Boundary,F}(fun) end -Morphological(fun; boundary=false) = Morphological{boundary}(fun) - -parameters(t::Morphological{Boundary}) where {Boundary} = (fun=t.fun, boundary=Boundary) +parameters(t::Morphological) = (; fun=t.fun) applycoord(t::Morphological, p::Point) = Point(t.fun(coords(p))) @@ -42,24 +41,8 @@ applycoord(::Morphological, v::Vec) = v applycoord(t::Morphological, g::Primitive) = TransformedGeometry(g, t) -applycoord(t::Morphological{true}, g::Polytope) = TransformedGeometry(g, t) - applycoord(t::Morphological, g::RegularGrid) = TransformedGrid(g, t) applycoord(t::Morphological, g::RectilinearGrid) = TransformedGrid(g, t) applycoord(t::Morphological, g::StructuredGrid) = TransformedGrid(g, t) - -# ----------- -# IO METHODS -# ----------- - -Base.show(io::IO, t::Morphological{Boundary}) where {Boundary} = - print(io, "Morphological(fun: $(t.fun), boundary: $Boundary)") - -function Base.show(io::IO, ::MIME"text/plain", t::Morphological{Boundary}) where {Boundary} - summary(io, t) - println(io) - println(io, "├─ fun: $(t.fun)") - print(io, "└─ boundary: $Boundary") -end diff --git a/src/transforms/proj.jl b/src/transforms/proj.jl index 160ef0ae2..6ca5ae2db 100644 --- a/src/transforms/proj.jl +++ b/src/transforms/proj.jl @@ -3,8 +3,8 @@ # ------------------------------------------------------------------ """ - Proj(CRS; boundary=false) - Proj(code; boundary=false) + Proj(CRS) + Proj(code) Convert the coordinates of geometry or domain to a given coordinate reference system `CRS` or EPSG/ESRI `code`. @@ -21,18 +21,23 @@ Proj(WebMercator) Proj(Mercator{WGS84Latest}) Proj(EPSG{3395}) Proj(ESRI{54017}) -Proj(Robinson, boundary=true) ``` + +### Notes + +* By default, only the vertices of the polytopes are transformed, + disregarding distortions that occur in manifold conversions. + To handle this case, use [`TransformedGeometry`](@ref). """ -struct Proj{CRS,Boundary} <: CoordinateTransform end +struct Proj{CRS} <: CoordinateTransform end -Proj(CRS; boundary=false) = Proj{CRS,boundary}() +Proj(CRS) = Proj{CRS}() -Proj(code::Type{<:EPSG}; kwargs...) = Proj(CoordRefSystems.get(code); kwargs...) +Proj(code::Type{<:EPSG}) = Proj(CoordRefSystems.get(code)) -Proj(code::Type{<:ESRI}; kwargs...) = Proj(CoordRefSystems.get(code); kwargs...) +Proj(code::Type{<:ESRI}) = Proj(CoordRefSystems.get(code)) -parameters(::Proj{CRS,Boundary}) where {CRS,Boundary} = (CRS=CRS, boundary=Boundary) +parameters(::Proj{CRS}) where {CRS} = (; CRS) # avoid constructing a new geometry or domain when the CRS is the same function apply(t::Proj{CRS}, g::GeometryOrDomain) where {CRS} @@ -60,10 +65,6 @@ applycoord(t::Proj{<:Projected}, g::Primitive{<:🌐}) = TransformedGeometry(g, applycoord(t::Proj{<:Geographic}, g::Primitive{<:𝔼}) = TransformedGeometry(g, t) -applycoord(t::Proj{<:Projected,true}, g::Polytope{K,<:🌐}) where {K} = TransformedGeometry(g, t) - -applycoord(t::Proj{<:Geographic,true}, g::Polytope{K,<:𝔼}) where {K} = TransformedGeometry(g, t) - applycoord(t::Proj, g::RegularGrid) = TransformedGrid(g, t) applycoord(t::Proj, g::RectilinearGrid) = TransformedGrid(g, t) @@ -74,13 +75,12 @@ applycoord(t::Proj, g::StructuredGrid) = TransformedGrid(g, t) # IO METHODS # ----------- -Base.show(io::IO, ::Proj{CRS,Boundary}) where {CRS,Boundary} = print(io, "Proj(CRS: $CRS, boundary: $Boundary)") +Base.show(io::IO, ::Proj{CRS}) where {CRS} = print(io, "Proj(CRS: $CRS)") -function Base.show(io::IO, ::MIME"text/plain", t::Proj{CRS,Boundary}) where {CRS,Boundary} +function Base.show(io::IO, ::MIME"text/plain", t::Proj{CRS}) where {CRS} summary(io, t) println(io) - println(io, "├─ CRS: $CRS") - print(io, "└─ boundary: $Boundary") + print(io, "└─ CRS: $CRS") end # ----------------- diff --git a/test/transforms.jl b/test/transforms.jl index ee2c1c289..c91c1a024 100644 --- a/test/transforms.jl +++ b/test/transforms.jl @@ -1251,15 +1251,14 @@ end @test !isaffine(Proj(Polar)) @test !TB.isrevertible(Proj(Polar)) @test !TB.isinvertible(Proj(Polar)) - @test TB.parameters(Proj(Polar)) == (CRS=Polar, boundary=false) - @test TB.parameters(Proj(EPSG{3395})) == (CRS=Mercator{WGS84Latest}, boundary=false) - @test TB.parameters(Proj(ESRI{54017})) == (CRS=Behrmann{WGS84Latest}, boundary=false) + @test TB.parameters(Proj(Polar)) == (; CRS=Polar) + @test TB.parameters(Proj(EPSG{3395})) == (; CRS=Mercator{WGS84Latest}) + @test TB.parameters(Proj(ESRI{54017})) == (; CRS=Behrmann{WGS84Latest}) f = Proj(Mercator) - @test sprint(show, f) == "Proj(CRS: CoordRefSystems.Mercator, boundary: false)" + @test sprint(show, f) == "Proj(CRS: CoordRefSystems.Mercator)" @test sprint(show, MIME"text/plain"(), f) == """ Proj transform - ├─ CRS: CoordRefSystems.Mercator - └─ boundary: false""" + └─ CRS: CoordRefSystems.Mercator""" # ---- # VEC @@ -1458,26 +1457,6 @@ end f = Proj(crs(merc(0, 0))) r, c = TB.apply(f, d) @test r === d - - # ---------------- - # BOUNDARY OPTION - # ---------------- - - f = Proj(Mercator, boundary=false) - g = Triangle(latlon(0, 0), latlon(0, 45), latlon(45, 0)) - r, c = TB.apply(f, g) - @test r isa Triangle - f = Proj(Mercator, boundary=true) - r, c = TB.apply(f, g) - @test r isa TransformedGeometry - - f = Proj(LatLon, boundary=false) - g = Triangle(merc(0, 0), merc(1, 0), merc(1, 1)) - r, c = TB.apply(f, g) - @test r isa Triangle - f = Proj(LatLon, boundary=true) - r, c = TB.apply(f, g) - @test r isa TransformedGeometry end @testitem "Morphological" setup = [Setup] begin @@ -1485,12 +1464,7 @@ end @test !isaffine(f) @test !TB.isrevertible(f) @test !TB.isinvertible(f) - @test TB.parameters(f) == (fun=f.fun, boundary=false) - @test sprint(show, f) == "Morphological(fun: $(f.fun), boundary: false)" - @test sprint(show, MIME"text/plain"(), f) == """ - Morphological transform - ├─ fun: $(f.fun) - └─ boundary: false""" + @test TB.parameters(f) == (; fun=f.fun) # ---- # VEC @@ -1616,18 +1590,6 @@ end d = SimpleMesh(p, c) r, c = TB.apply(f, d) @test r ≈ SimpleMesh(f.(vertices(d)), topology(d)) - - # ---------------- - # BOUNDARY OPTION - # ---------------- - - f = Morphological(c -> Cartesian(c.x, c.y, zero(c.x)), boundary=false) - g = Triangle(cart(0, 0), cart(1, 0), cart(1, 1)) - r, c = TB.apply(f, g) - @test r isa Triangle - f = Morphological(c -> Cartesian(c.x, c.y, zero(c.x)), boundary=true) - r, c = TB.apply(f, g) - @test r isa TransformedGeometry end @testitem "LengthUnit" setup = [Setup] begin