From b7d9d5ebb6564fbfd6410d6be65bfa4f0c7263c3 Mon Sep 17 00:00:00 2001 From: Elias Carvalho Date: Mon, 16 Dec 2024 11:08:28 -0300 Subject: [PATCH 01/18] Add 'InDomain' transform --- src/transforms.jl | 1 + src/transforms/indomain.jl | 41 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 src/transforms/indomain.jl diff --git a/src/transforms.jl b/src/transforms.jl index be49b250a..5613b9b0e 100644 --- a/src/transforms.jl +++ b/src/transforms.jl @@ -94,6 +94,7 @@ include("transforms/affine.jl") include("transforms/stretch.jl") include("transforms/stdcoords.jl") include("transforms/proj.jl") +include("transforms/indomain.jl") include("transforms/morphological.jl") include("transforms/lengthunit.jl") include("transforms/shadow.jl") diff --git a/src/transforms/indomain.jl b/src/transforms/indomain.jl new file mode 100644 index 000000000..60f188511 --- /dev/null +++ b/src/transforms/indomain.jl @@ -0,0 +1,41 @@ +# ------------------------------------------------------------------ +# Licensed under the MIT License. See LICENSE in the project root. +# ------------------------------------------------------------------ + +struct InDomain{CRS} <: CoordinateTransform end + +InDomain(CRS) = InDomain{CRS}() + +InDomain(code::Type{<:EPSG}) = InDomain(CoordRefSystems.get(code)) + +InDomain(code::Type{<:ESRI}) = InDomain(CoordRefSystems.get(code)) + +parameters(::InDomain{CRS}) where {CRS} = (; CRS) + +function preprocess(::InDomain{CRS}, d::Domain) where {CRS} + findall(d) do g + all(pointify(g)) do p + indomain(CRS, coords(p)) + end + end +end + +function preprocess(::InDomain{CRS}, d::Mesh) where {CRS} + findall(d) do g + all(eachvertex(g)) do p + indomain(CRS, coords(p)) + end + end +end + +function preprocess(::InDomain{CRS}, d::GeometrySet{<:Any,<:Any,<:Union{Polytope,MultiPolytope}}) where {CRS} + findall(d) do g + all(eachvertex(g)) do p + indomain(CRS, coords(p)) + end + end +end + +preprocess(::InDomain{CRS}, d::PointSet) where {CRS} = findall(p -> indomain(CRS, coords(p)), d) + +apply(t::InDomain, d::Domain) = view(d, preprocess(t, d)), nothing From 7ab8dd1da09aaf67602078eb0d16b4d9f54a712f Mon Sep 17 00:00:00 2001 From: Elias Carvalho Date: Mon, 16 Dec 2024 14:03:19 -0300 Subject: [PATCH 02/18] Add docstring --- src/transforms/indomain.jl | 46 +++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 25 deletions(-) diff --git a/src/transforms/indomain.jl b/src/transforms/indomain.jl index 60f188511..9f1ba44a3 100644 --- a/src/transforms/indomain.jl +++ b/src/transforms/indomain.jl @@ -2,6 +2,13 @@ # Licensed under the MIT License. See LICENSE in the project root. # ------------------------------------------------------------------ +""" + InDomain(CRS) + InDomain(code) + +Retain the geometries within the domain of the +projection of type `CRS` or with EPSG/ESRI `code`. +""" struct InDomain{CRS} <: CoordinateTransform end InDomain(CRS) = InDomain{CRS}() @@ -12,30 +19,19 @@ InDomain(code::Type{<:ESRI}) = InDomain(CoordRefSystems.get(code)) parameters(::InDomain{CRS}) where {CRS} = (; CRS) -function preprocess(::InDomain{CRS}, d::Domain) where {CRS} - findall(d) do g - all(pointify(g)) do p - indomain(CRS, coords(p)) - end - end -end - -function preprocess(::InDomain{CRS}, d::Mesh) where {CRS} - findall(d) do g - all(eachvertex(g)) do p - indomain(CRS, coords(p)) - end - end -end - -function preprocess(::InDomain{CRS}, d::GeometrySet{<:Any,<:Any,<:Union{Polytope,MultiPolytope}}) where {CRS} - findall(d) do g - all(eachvertex(g)) do p - indomain(CRS, coords(p)) - end - end -end - -preprocess(::InDomain{CRS}, d::PointSet) where {CRS} = findall(p -> indomain(CRS, coords(p)), d) +preprocess(t::InDomain, d::Domain) = findall(g -> all(_indomain(t), pointify(g)), d) + +preprocess(t::InDomain, d::Mesh) = findall(g -> all(_indomain(t), eachvertex(g)), d) + +preprocess(t::InDomain, d::GeometrySet{<:Manifold,<:CRS,<:Union{Polytope,MultiPolytope}}) = + findall(g -> all(_indomain(t), eachvertex(g)), d) + +preprocess(t::InDomain, d::PointSet) = findall(_indomain(t), d) apply(t::InDomain, d::Domain) = view(d, preprocess(t, d)), nothing + +# ----------------- +# HELPER FUNCTIONS +# ----------------- + +_indomain(::InDomain{CRS}) where {CRS} = p -> indomain(CRS, coords(p)) From 75fd05ae643b87d6eb38ec08f3e1a8968eba1b0d Mon Sep 17 00:00:00 2001 From: Elias Carvalho Date: Mon, 16 Dec 2024 15:10:09 -0300 Subject: [PATCH 03/18] Add to documentation && Add to exports --- docs/src/transforms.md | 17 +++++++++++++++++ src/Meshes.jl | 1 + 2 files changed, 18 insertions(+) diff --git a/docs/src/transforms.md b/docs/src/transforms.md index 8bec4a279..fe12e918b 100644 --- a/docs/src/transforms.md +++ b/docs/src/transforms.md @@ -146,6 +146,23 @@ triangle = Triangle((0, 0), (1, 0), (1, 1)) triangle |> Proj(Polar) ``` +## InDomain + +```@docs +InDomain +``` + +```@example transforms +# load coordinate reference system +using CoordRefSystems: LatLon + +# latlon grid +grid = RegularGrid(Point(LatLon(-90, -180)), Point(LatLon(90, 180)), dims=(10, 10)) + +# retains only the elements in the projection domain +grid |> InDomain(Mercator) +``` + ## Morphological ```@docs diff --git a/src/Meshes.jl b/src/Meshes.jl index b6f6d4626..ba87f28e3 100644 --- a/src/Meshes.jl +++ b/src/Meshes.jl @@ -539,6 +539,7 @@ export Stretch, StdCoords, Proj, + InDomain, Morphological, LengthUnit, Shadow, From 556c2b9d0028541d78957c8864473b8de1935bbc Mon Sep 17 00:00:00 2001 From: Elias Carvalho Date: Mon, 16 Dec 2024 16:07:35 -0300 Subject: [PATCH 04/18] Add tests --- src/transforms/indomain.jl | 16 ++++++- test/transforms.jl | 87 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+), 2 deletions(-) diff --git a/src/transforms/indomain.jl b/src/transforms/indomain.jl index 9f1ba44a3..f61295a65 100644 --- a/src/transforms/indomain.jl +++ b/src/transforms/indomain.jl @@ -6,8 +6,8 @@ InDomain(CRS) InDomain(code) -Retain the geometries within the domain of the -projection of type `CRS` or with EPSG/ESRI `code`. +Retain the geometries within the projection domain +of type `CRS` or with EPSG/ESRI `code`. """ struct InDomain{CRS} <: CoordinateTransform end @@ -30,6 +30,18 @@ preprocess(t::InDomain, d::PointSet) = findall(_indomain(t), d) apply(t::InDomain, d::Domain) = view(d, preprocess(t, d)), nothing +# ----------- +# IO METHODS +# ----------- + +Base.show(io::IO, ::InDomain{CRS}) where {CRS} = print(io, "InDomain(CRS: $CRS)") + +function Base.show(io::IO, ::MIME"text/plain", t::InDomain{CRS}) where {CRS} + summary(io, t) + println(io) + print(io, "└─ CRS: $CRS") +end + # ----------------- # HELPER FUNCTIONS # ----------------- diff --git a/test/transforms.jl b/test/transforms.jl index c91c1a024..c0a4a7e6d 100644 --- a/test/transforms.jl +++ b/test/transforms.jl @@ -1459,6 +1459,93 @@ end @test r === d end +@testitem "InDomain" setup = [Setup] begin + @test !isaffine(InDomain(Mercator)) + @test !TB.isrevertible(InDomain(Mercator)) + @test !TB.isinvertible(InDomain(Mercator)) + @test TB.parameters(InDomain(Mercator)) == (; CRS=Mercator) + @test TB.parameters(InDomain(EPSG{3395})) == (; CRS=Mercator{WGS84Latest}) + @test TB.parameters(InDomain(ESRI{54017})) == (; CRS=Behrmann{WGS84Latest}) + f = InDomain(Mercator) + @test sprint(show, f) == "InDomain(CRS: CoordRefSystems.Mercator)" + @test sprint(show, MIME"text/plain"(), f) == """ + InDomain transform + └─ CRS: CoordRefSystems.Mercator""" + + # --------- + # POINTSET + # --------- + + f = InDomain(Mercator) + d = PointSet([latlon(-90, 0), latlon(-45, 0), latlon(0, 0), latlon(45, 0), latlon(90, 0)]) + r, c = TB.apply(f, d) + @test r == PointSet([latlon(-45, 0), latlon(0, 0), latlon(45, 0)]) + + # ------------ + # GEOMETRYSET + # ------------ + + f = InDomain(Mercator) + t1 = Triangle(latlon(-90, 0), latlon(-45, 30), latlon(-45, -30)) + t2 = Triangle(latlon(-45, 0), latlon(0, 30), latlon(0, -30)) + t3 = Triangle(latlon(0, -30), latlon(0, 30), latlon(45, 0)) + t4 = Triangle(latlon(45, -30), latlon(45, 30), latlon(90, 0)) + d = GeometrySet([t1, t2, t3, t4]) + r, c = TB.apply(f, d) + @test r == GeometrySet([t2, t3]) + + f = InDomain(Mercator) + b1 = Box(latlon(-90, -30), latlon(-30, 30)) + b2 = Box(latlon(-30, -30), latlon(30, 30)) + b3 = Box(latlon(30, -30), latlon(90, 30)) + d = GeometrySet([b1, b2, b3]) + r, c = TB.apply(f, d) + @test r == GeometrySet([b2]) + + # -------------- + # CARTESIANGRID + # -------------- + + f = InDomain(Mercator) + d = RegularGrid(latlon(-90, -180), latlon(90, 180), dims=(3, 3)) + r, c = TB.apply(f, d) + linds = LinearIndices(size(d)) + @test r == view(d, [linds[2, 1], linds[2, 2], linds[2, 3]]) + + # ---------------- + # RECTILINEARGRID + # ---------------- + + f = InDomain(Mercator) + g = RegularGrid(latlon(-90, -180), latlon(90, 180), dims=(3, 3)) + d = convert(RectilinearGrid, g) + r, c = TB.apply(f, d) + linds = LinearIndices(size(d)) + @test r == view(d, [linds[2, 1], linds[2, 2], linds[2, 3]]) + + # --------------- + # STRUCTUREDGRID + # --------------- + + f = InDomain(Mercator) + g = RegularGrid(latlon(-90, -180), latlon(90, 180), dims=(3, 3)) + d = convert(StructuredGrid, g) + r, c = TB.apply(f, d) + linds = LinearIndices(size(d)) + @test r == view(d, [linds[2, 1], linds[2, 2], linds[2, 3]]) + + # ----------- + # SIMPLEMESH + # ----------- + + f = InDomain(Mercator) + g = RegularGrid(latlon(-90, -180), latlon(90, 180), dims=(3, 3)) + d = convert(SimpleMesh, g) + r, c = TB.apply(f, d) + linds = LinearIndices(size(d)) + @test r == view(d, [linds[2, 1], linds[2, 2], linds[2, 3]]) +end + @testitem "Morphological" setup = [Setup] begin f = Morphological(c -> c) @test !isaffine(f) From 689bb7efc537811e957822c212c73d3b8f09ba23 Mon Sep 17 00:00:00 2001 From: Elias Carvalho Date: Mon, 16 Dec 2024 16:08:55 -0300 Subject: [PATCH 05/18] Update docstring --- src/transforms/indomain.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/transforms/indomain.jl b/src/transforms/indomain.jl index f61295a65..3e1e9e581 100644 --- a/src/transforms/indomain.jl +++ b/src/transforms/indomain.jl @@ -6,8 +6,8 @@ InDomain(CRS) InDomain(code) -Retain the geometries within the projection domain -of type `CRS` or with EPSG/ESRI `code`. +Retain the geometries within the domain of the +projection of type `CRS` or with EPSG/ESRI `code`. """ struct InDomain{CRS} <: CoordinateTransform end From 344518aebedac1578dc9e14dd5c6d9f6878270a9 Mon Sep 17 00:00:00 2001 From: Elias Carvalho Date: Tue, 17 Dec 2024 15:12:51 -0300 Subject: [PATCH 06/18] Update example --- docs/src/transforms.md | 5 ++++- src/transforms/proj.jl | 2 ++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/docs/src/transforms.md b/docs/src/transforms.md index fe12e918b..8bb5968bf 100644 --- a/docs/src/transforms.md +++ b/docs/src/transforms.md @@ -160,7 +160,10 @@ using CoordRefSystems: LatLon grid = RegularGrid(Point(LatLon(-90, -180)), Point(LatLon(90, 180)), dims=(10, 10)) # retains only the elements in the projection domain -grid |> InDomain(Mercator) +subgrid = grid |> InDomain(Mercator) + +# plot the projected grid +viz(subgrid |> Proj(Mercator), showsegments=true) ``` ## Morphological diff --git a/src/transforms/proj.jl b/src/transforms/proj.jl index 6ca5ae2db..8562486ed 100644 --- a/src/transforms/proj.jl +++ b/src/transforms/proj.jl @@ -71,6 +71,8 @@ applycoord(t::Proj, g::RectilinearGrid) = TransformedGrid(g, t) applycoord(t::Proj, g::StructuredGrid) = TransformedGrid(g, t) +applycoord(t::Proj, d::SubDomain) = TransformedDomain(d, t) + # ----------- # IO METHODS # ----------- From c3661fc3f89728ab5bcc4d3b8616cf0ec64fb8d4 Mon Sep 17 00:00:00 2001 From: Elias Carvalho Date: Tue, 17 Dec 2024 15:23:43 -0300 Subject: [PATCH 07/18] Add more tests --- test/transforms.jl | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/transforms.jl b/test/transforms.jl index c0a4a7e6d..f01a7ed46 100644 --- a/test/transforms.jl +++ b/test/transforms.jl @@ -1414,6 +1414,16 @@ end r, c = TB.apply(f, d) @test r ≈ SimpleMesh(f.(vertices(d)), topology(d)) + # ---------- + # SUBDOMAIN + # ---------- + + f = Proj(Polar) + g = CartesianGrid((10, 10), cart(1, 1), T.((1, 1))) + d = view(g, 1:10) + r, c = TB.apply(f, d) + @test r ≈ view(SimpleMesh(f.(vertices(g)), topology(g)), 1:10) + # -------------- # SPECIAL CASES # -------------- From 7715af97e75e8ee16b138af8da90d69d0443d30d Mon Sep 17 00:00:00 2001 From: Elias Carvalho Date: Tue, 17 Dec 2024 15:49:06 -0300 Subject: [PATCH 08/18] More adjustments --- src/transforms/indomain.jl | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/transforms/indomain.jl b/src/transforms/indomain.jl index 3e1e9e581..e1107ed03 100644 --- a/src/transforms/indomain.jl +++ b/src/transforms/indomain.jl @@ -21,7 +21,16 @@ parameters(::InDomain{CRS}) where {CRS} = (; CRS) preprocess(t::InDomain, d::Domain) = findall(g -> all(_indomain(t), pointify(g)), d) -preprocess(t::InDomain, d::Mesh) = findall(g -> all(_indomain(t), eachvertex(g)), d) +function preprocess(t::InDomain, d::Mesh) + indomain = _indomain(t) + points = vertices(d) + topo = topology(d) + ∂₂₀ = Boundary{2,0}(topo) + findall(1:nelements(d)) do elem + is = ∂₂₀(elem) + all(indomain(points[i]) for i in is) + end +end preprocess(t::InDomain, d::GeometrySet{<:Manifold,<:CRS,<:Union{Polytope,MultiPolytope}}) = findall(g -> all(_indomain(t), eachvertex(g)), d) From 1a809cdb342670cf461e14892bc2a7a70e62d065 Mon Sep 17 00:00:00 2001 From: Elias Carvalho Date: Tue, 17 Dec 2024 15:56:46 -0300 Subject: [PATCH 09/18] Rename InDomain to ValidCoords --- docs/src/transforms.md | 6 ++-- src/Meshes.jl | 2 +- src/transforms.jl | 2 +- src/transforms/indomain.jl | 58 ----------------------------------- src/transforms/validcoords.jl | 58 +++++++++++++++++++++++++++++++++++ test/transforms.jl | 34 ++++++++++---------- 6 files changed, 80 insertions(+), 80 deletions(-) delete mode 100644 src/transforms/indomain.jl create mode 100644 src/transforms/validcoords.jl diff --git a/docs/src/transforms.md b/docs/src/transforms.md index 8bb5968bf..a7dd8c764 100644 --- a/docs/src/transforms.md +++ b/docs/src/transforms.md @@ -146,10 +146,10 @@ triangle = Triangle((0, 0), (1, 0), (1, 1)) triangle |> Proj(Polar) ``` -## InDomain +## ValidCoords ```@docs -InDomain +ValidCoords ``` ```@example transforms @@ -160,7 +160,7 @@ using CoordRefSystems: LatLon grid = RegularGrid(Point(LatLon(-90, -180)), Point(LatLon(90, 180)), dims=(10, 10)) # retains only the elements in the projection domain -subgrid = grid |> InDomain(Mercator) +subgrid = grid |> ValidCoords(Mercator) # plot the projected grid viz(subgrid |> Proj(Mercator), showsegments=true) diff --git a/src/Meshes.jl b/src/Meshes.jl index f1f64bb14..2b56371ca 100644 --- a/src/Meshes.jl +++ b/src/Meshes.jl @@ -540,7 +540,7 @@ export Stretch, StdCoords, Proj, - InDomain, + ValidCoords, Morphological, LengthUnit, Shadow, diff --git a/src/transforms.jl b/src/transforms.jl index 5613b9b0e..2d5788a83 100644 --- a/src/transforms.jl +++ b/src/transforms.jl @@ -94,7 +94,7 @@ include("transforms/affine.jl") include("transforms/stretch.jl") include("transforms/stdcoords.jl") include("transforms/proj.jl") -include("transforms/indomain.jl") +include("transforms/validcoords.jl") include("transforms/morphological.jl") include("transforms/lengthunit.jl") include("transforms/shadow.jl") diff --git a/src/transforms/indomain.jl b/src/transforms/indomain.jl deleted file mode 100644 index e1107ed03..000000000 --- a/src/transforms/indomain.jl +++ /dev/null @@ -1,58 +0,0 @@ -# ------------------------------------------------------------------ -# Licensed under the MIT License. See LICENSE in the project root. -# ------------------------------------------------------------------ - -""" - InDomain(CRS) - InDomain(code) - -Retain the geometries within the domain of the -projection of type `CRS` or with EPSG/ESRI `code`. -""" -struct InDomain{CRS} <: CoordinateTransform end - -InDomain(CRS) = InDomain{CRS}() - -InDomain(code::Type{<:EPSG}) = InDomain(CoordRefSystems.get(code)) - -InDomain(code::Type{<:ESRI}) = InDomain(CoordRefSystems.get(code)) - -parameters(::InDomain{CRS}) where {CRS} = (; CRS) - -preprocess(t::InDomain, d::Domain) = findall(g -> all(_indomain(t), pointify(g)), d) - -function preprocess(t::InDomain, d::Mesh) - indomain = _indomain(t) - points = vertices(d) - topo = topology(d) - ∂₂₀ = Boundary{2,0}(topo) - findall(1:nelements(d)) do elem - is = ∂₂₀(elem) - all(indomain(points[i]) for i in is) - end -end - -preprocess(t::InDomain, d::GeometrySet{<:Manifold,<:CRS,<:Union{Polytope,MultiPolytope}}) = - findall(g -> all(_indomain(t), eachvertex(g)), d) - -preprocess(t::InDomain, d::PointSet) = findall(_indomain(t), d) - -apply(t::InDomain, d::Domain) = view(d, preprocess(t, d)), nothing - -# ----------- -# IO METHODS -# ----------- - -Base.show(io::IO, ::InDomain{CRS}) where {CRS} = print(io, "InDomain(CRS: $CRS)") - -function Base.show(io::IO, ::MIME"text/plain", t::InDomain{CRS}) where {CRS} - summary(io, t) - println(io) - print(io, "└─ CRS: $CRS") -end - -# ----------------- -# HELPER FUNCTIONS -# ----------------- - -_indomain(::InDomain{CRS}) where {CRS} = p -> indomain(CRS, coords(p)) diff --git a/src/transforms/validcoords.jl b/src/transforms/validcoords.jl new file mode 100644 index 000000000..2a5ebd687 --- /dev/null +++ b/src/transforms/validcoords.jl @@ -0,0 +1,58 @@ +# ------------------------------------------------------------------ +# Licensed under the MIT License. See LICENSE in the project root. +# ------------------------------------------------------------------ + +""" + ValidCoords(CRS) + ValidCoords(code) + +Retain the geometries within the domain of the +projection of type `CRS` or with EPSG/ESRI `code`. +""" +struct ValidCoords{CRS} <: CoordinateTransform end + +ValidCoords(CRS) = ValidCoords{CRS}() + +ValidCoords(code::Type{<:EPSG}) = ValidCoords(CoordRefSystems.get(code)) + +ValidCoords(code::Type{<:ESRI}) = ValidCoords(CoordRefSystems.get(code)) + +parameters(::ValidCoords{CRS}) where {CRS} = (; CRS) + +preprocess(t::ValidCoords, d::Domain) = findall(g -> all(_indomain(t), pointify(g)), d) + +function preprocess(t::ValidCoords, d::Mesh) + indomain = _indomain(t) + points = vertices(d) + topo = topology(d) + ∂₂₀ = Boundary{2,0}(topo) + findall(1:nelements(d)) do elem + is = ∂₂₀(elem) + all(indomain(points[i]) for i in is) + end +end + +preprocess(t::ValidCoords, d::GeometrySet{<:Manifold,<:CRS,<:Union{Polytope,MultiPolytope}}) = + findall(g -> all(_indomain(t), eachvertex(g)), d) + +preprocess(t::ValidCoords, d::PointSet) = findall(_indomain(t), d) + +apply(t::ValidCoords, d::Domain) = view(d, preprocess(t, d)), nothing + +# ----------- +# IO METHODS +# ----------- + +Base.show(io::IO, ::ValidCoords{CRS}) where {CRS} = print(io, "ValidCoords(CRS: $CRS)") + +function Base.show(io::IO, ::MIME"text/plain", t::ValidCoords{CRS}) where {CRS} + summary(io, t) + println(io) + print(io, "└─ CRS: $CRS") +end + +# ----------------- +# HELPER FUNCTIONS +# ----------------- + +_indomain(::ValidCoords{CRS}) where {CRS} = p -> indomain(CRS, coords(p)) diff --git a/test/transforms.jl b/test/transforms.jl index f01a7ed46..f52a1eefb 100644 --- a/test/transforms.jl +++ b/test/transforms.jl @@ -1469,24 +1469,24 @@ end @test r === d end -@testitem "InDomain" setup = [Setup] begin - @test !isaffine(InDomain(Mercator)) - @test !TB.isrevertible(InDomain(Mercator)) - @test !TB.isinvertible(InDomain(Mercator)) - @test TB.parameters(InDomain(Mercator)) == (; CRS=Mercator) - @test TB.parameters(InDomain(EPSG{3395})) == (; CRS=Mercator{WGS84Latest}) - @test TB.parameters(InDomain(ESRI{54017})) == (; CRS=Behrmann{WGS84Latest}) - f = InDomain(Mercator) - @test sprint(show, f) == "InDomain(CRS: CoordRefSystems.Mercator)" +@testitem "ValidCoords" setup = [Setup] begin + @test !isaffine(ValidCoords(Mercator)) + @test !TB.isrevertible(ValidCoords(Mercator)) + @test !TB.isinvertible(ValidCoords(Mercator)) + @test TB.parameters(ValidCoords(Mercator)) == (; CRS=Mercator) + @test TB.parameters(ValidCoords(EPSG{3395})) == (; CRS=Mercator{WGS84Latest}) + @test TB.parameters(ValidCoords(ESRI{54017})) == (; CRS=Behrmann{WGS84Latest}) + f = ValidCoords(Mercator) + @test sprint(show, f) == "ValidCoords(CRS: CoordRefSystems.Mercator)" @test sprint(show, MIME"text/plain"(), f) == """ - InDomain transform + ValidCoords transform └─ CRS: CoordRefSystems.Mercator""" # --------- # POINTSET # --------- - f = InDomain(Mercator) + f = ValidCoords(Mercator) d = PointSet([latlon(-90, 0), latlon(-45, 0), latlon(0, 0), latlon(45, 0), latlon(90, 0)]) r, c = TB.apply(f, d) @test r == PointSet([latlon(-45, 0), latlon(0, 0), latlon(45, 0)]) @@ -1495,7 +1495,7 @@ end # GEOMETRYSET # ------------ - f = InDomain(Mercator) + f = ValidCoords(Mercator) t1 = Triangle(latlon(-90, 0), latlon(-45, 30), latlon(-45, -30)) t2 = Triangle(latlon(-45, 0), latlon(0, 30), latlon(0, -30)) t3 = Triangle(latlon(0, -30), latlon(0, 30), latlon(45, 0)) @@ -1504,7 +1504,7 @@ end r, c = TB.apply(f, d) @test r == GeometrySet([t2, t3]) - f = InDomain(Mercator) + f = ValidCoords(Mercator) b1 = Box(latlon(-90, -30), latlon(-30, 30)) b2 = Box(latlon(-30, -30), latlon(30, 30)) b3 = Box(latlon(30, -30), latlon(90, 30)) @@ -1516,7 +1516,7 @@ end # CARTESIANGRID # -------------- - f = InDomain(Mercator) + f = ValidCoords(Mercator) d = RegularGrid(latlon(-90, -180), latlon(90, 180), dims=(3, 3)) r, c = TB.apply(f, d) linds = LinearIndices(size(d)) @@ -1526,7 +1526,7 @@ end # RECTILINEARGRID # ---------------- - f = InDomain(Mercator) + f = ValidCoords(Mercator) g = RegularGrid(latlon(-90, -180), latlon(90, 180), dims=(3, 3)) d = convert(RectilinearGrid, g) r, c = TB.apply(f, d) @@ -1537,7 +1537,7 @@ end # STRUCTUREDGRID # --------------- - f = InDomain(Mercator) + f = ValidCoords(Mercator) g = RegularGrid(latlon(-90, -180), latlon(90, 180), dims=(3, 3)) d = convert(StructuredGrid, g) r, c = TB.apply(f, d) @@ -1548,7 +1548,7 @@ end # SIMPLEMESH # ----------- - f = InDomain(Mercator) + f = ValidCoords(Mercator) g = RegularGrid(latlon(-90, -180), latlon(90, 180), dims=(3, 3)) d = convert(SimpleMesh, g) r, c = TB.apply(f, d) From ab2925b1dd702c9f1ddc0a5839ebcda6ccde755c Mon Sep 17 00:00:00 2001 From: Elias Carvalho Date: Tue, 17 Dec 2024 15:58:44 -0300 Subject: [PATCH 10/18] Rename helper --- src/transforms/validcoords.jl | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/transforms/validcoords.jl b/src/transforms/validcoords.jl index 2a5ebd687..de2a802e6 100644 --- a/src/transforms/validcoords.jl +++ b/src/transforms/validcoords.jl @@ -19,23 +19,23 @@ ValidCoords(code::Type{<:ESRI}) = ValidCoords(CoordRefSystems.get(code)) parameters(::ValidCoords{CRS}) where {CRS} = (; CRS) -preprocess(t::ValidCoords, d::Domain) = findall(g -> all(_indomain(t), pointify(g)), d) +preprocess(t::ValidCoords, d::Domain) = findall(g -> all(_isvalid(t), pointify(g)), d) function preprocess(t::ValidCoords, d::Mesh) - indomain = _indomain(t) + isvalid = _isvalid(t) points = vertices(d) topo = topology(d) ∂₂₀ = Boundary{2,0}(topo) findall(1:nelements(d)) do elem is = ∂₂₀(elem) - all(indomain(points[i]) for i in is) + all(isvalid(points[i]) for i in is) end end preprocess(t::ValidCoords, d::GeometrySet{<:Manifold,<:CRS,<:Union{Polytope,MultiPolytope}}) = - findall(g -> all(_indomain(t), eachvertex(g)), d) + findall(g -> all(_isvalid(t), eachvertex(g)), d) -preprocess(t::ValidCoords, d::PointSet) = findall(_indomain(t), d) +preprocess(t::ValidCoords, d::PointSet) = findall(_isvalid(t), d) apply(t::ValidCoords, d::Domain) = view(d, preprocess(t, d)), nothing @@ -55,4 +55,4 @@ end # HELPER FUNCTIONS # ----------------- -_indomain(::ValidCoords{CRS}) where {CRS} = p -> indomain(CRS, coords(p)) +_isvalid(::ValidCoords{CRS}) where {CRS} = p -> indomain(CRS, coords(p)) From 41f432ed2e54c644b336d2bed88494abb5b830b6 Mon Sep 17 00:00:00 2001 From: Elias Carvalho Date: Wed, 18 Dec 2024 08:20:55 -0300 Subject: [PATCH 11/18] Apply suggestions --- src/transforms/validcoords.jl | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/src/transforms/validcoords.jl b/src/transforms/validcoords.jl index de2a802e6..c5d01ba66 100644 --- a/src/transforms/validcoords.jl +++ b/src/transforms/validcoords.jl @@ -19,24 +19,17 @@ ValidCoords(code::Type{<:ESRI}) = ValidCoords(CoordRefSystems.get(code)) parameters(::ValidCoords{CRS}) where {CRS} = (; CRS) -preprocess(t::ValidCoords, d::Domain) = findall(g -> all(_isvalid(t), pointify(g)), d) +preprocess(t::ValidCoords, d::Domain) = findall(g -> _isvalid(t, g), d) function preprocess(t::ValidCoords, d::Mesh) - isvalid = _isvalid(t) points = vertices(d) topo = topology(d) - ∂₂₀ = Boundary{2,0}(topo) - findall(1:nelements(d)) do elem - is = ∂₂₀(elem) - all(isvalid(points[i]) for i in is) + findall(elements(topo)) do elem + is = indices(elem) + all(_isvalid(t, points[i]) for i in is) end end -preprocess(t::ValidCoords, d::GeometrySet{<:Manifold,<:CRS,<:Union{Polytope,MultiPolytope}}) = - findall(g -> all(_isvalid(t), eachvertex(g)), d) - -preprocess(t::ValidCoords, d::PointSet) = findall(_isvalid(t), d) - apply(t::ValidCoords, d::Domain) = view(d, preprocess(t, d)), nothing # ----------- @@ -55,4 +48,6 @@ end # HELPER FUNCTIONS # ----------------- -_isvalid(::ValidCoords{CRS}) where {CRS} = p -> indomain(CRS, coords(p)) +_isvalid(::ValidCoords{CRS}, p::Point) where {CRS} = indomain(CRS, coords(p)) +_isvalid(t::ValidCoords, g::Union{Polytope,MultiPolytope}) = all(p -> _isvalid(t, p), eachvertex(g)) +_isvalid(t::ValidCoords, g::Geometry) = all(p -> _isvalid(t, p), pointify(g)) From 7cf932994ff6286bb26641a6224823c385c86e9c Mon Sep 17 00:00:00 2001 From: Elias Carvalho Date: Wed, 18 Dec 2024 08:24:54 -0300 Subject: [PATCH 12/18] Apply suggestions --- src/transforms/proj.jl | 2 -- test/transforms.jl | 10 ---------- 2 files changed, 12 deletions(-) diff --git a/src/transforms/proj.jl b/src/transforms/proj.jl index 8562486ed..6ca5ae2db 100644 --- a/src/transforms/proj.jl +++ b/src/transforms/proj.jl @@ -71,8 +71,6 @@ applycoord(t::Proj, g::RectilinearGrid) = TransformedGrid(g, t) applycoord(t::Proj, g::StructuredGrid) = TransformedGrid(g, t) -applycoord(t::Proj, d::SubDomain) = TransformedDomain(d, t) - # ----------- # IO METHODS # ----------- diff --git a/test/transforms.jl b/test/transforms.jl index f52a1eefb..0c708227e 100644 --- a/test/transforms.jl +++ b/test/transforms.jl @@ -1414,16 +1414,6 @@ end r, c = TB.apply(f, d) @test r ≈ SimpleMesh(f.(vertices(d)), topology(d)) - # ---------- - # SUBDOMAIN - # ---------- - - f = Proj(Polar) - g = CartesianGrid((10, 10), cart(1, 1), T.((1, 1))) - d = view(g, 1:10) - r, c = TB.apply(f, d) - @test r ≈ view(SimpleMesh(f.(vertices(g)), topology(g)), 1:10) - # -------------- # SPECIAL CASES # -------------- From ab8057ef413330ccf6fb8219c94cc0242c06c523 Mon Sep 17 00:00:00 2001 From: Elias Carvalho <73039601+eliascarv@users.noreply.github.com> Date: Wed, 18 Dec 2024 08:49:40 -0300 Subject: [PATCH 13/18] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Júlio Hoffimann --- docs/src/transforms.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/src/transforms.md b/docs/src/transforms.md index a7dd8c764..5e9dd0e14 100644 --- a/docs/src/transforms.md +++ b/docs/src/transforms.md @@ -156,10 +156,10 @@ ValidCoords # load coordinate reference system using CoordRefSystems: LatLon -# latlon grid +# Regular grid with LatLon coordinates grid = RegularGrid(Point(LatLon(-90, -180)), Point(LatLon(90, 180)), dims=(10, 10)) -# retains only the elements in the projection domain +# retain elements in the projection domain subgrid = grid |> ValidCoords(Mercator) # plot the projected grid From 244175c681b8037603d648323942b9832800af1d Mon Sep 17 00:00:00 2001 From: Elias Carvalho Date: Wed, 18 Dec 2024 08:52:43 -0300 Subject: [PATCH 14/18] Use the correct parent type --- src/transforms/validcoords.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transforms/validcoords.jl b/src/transforms/validcoords.jl index c5d01ba66..123fd3a09 100644 --- a/src/transforms/validcoords.jl +++ b/src/transforms/validcoords.jl @@ -9,7 +9,7 @@ Retain the geometries within the domain of the projection of type `CRS` or with EPSG/ESRI `code`. """ -struct ValidCoords{CRS} <: CoordinateTransform end +struct ValidCoords{CRS} <: GeometricTransform end ValidCoords(CRS) = ValidCoords{CRS}() From 79e964f1d2b87968f262f1c9873dd0e371870a2c Mon Sep 17 00:00:00 2001 From: Elias Carvalho <73039601+eliascarv@users.noreply.github.com> Date: Wed, 18 Dec 2024 08:54:05 -0300 Subject: [PATCH 15/18] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Júlio Hoffimann --- docs/src/transforms.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/transforms.md b/docs/src/transforms.md index 5e9dd0e14..8f45e9123 100644 --- a/docs/src/transforms.md +++ b/docs/src/transforms.md @@ -156,7 +156,7 @@ ValidCoords # load coordinate reference system using CoordRefSystems: LatLon -# Regular grid with LatLon coordinates +# regular grid with LatLon coordinates grid = RegularGrid(Point(LatLon(-90, -180)), Point(LatLon(90, 180)), dims=(10, 10)) # retain elements in the projection domain From c8e8af5d207da2e7388ef74ec7a1814adc1da78b Mon Sep 17 00:00:00 2001 From: Elias Carvalho Date: Wed, 18 Dec 2024 09:00:35 -0300 Subject: [PATCH 16/18] Apply suggestions --- docs/src/transforms.md | 34 +++---- src/Meshes.jl | 2 +- src/transforms.jl | 2 +- src/transforms/validcoords.jl | 3 +- test/transforms.jl | 174 +++++++++++++++++----------------- 5 files changed, 108 insertions(+), 107 deletions(-) diff --git a/docs/src/transforms.md b/docs/src/transforms.md index 8f45e9123..90d3c002a 100644 --- a/docs/src/transforms.md +++ b/docs/src/transforms.md @@ -129,23 +129,6 @@ viz(fig[1,2], mesh) fig ``` -## Proj - -```@docs -Proj -``` - -```@example transforms -# load coordinate reference system -using CoordRefSystems: Polar - -# triangle with Cartesian coordinates -triangle = Triangle((0, 0), (1, 0), (1, 1)) - -# reproject to polar coordinates -triangle |> Proj(Polar) -``` - ## ValidCoords ```@docs @@ -166,6 +149,23 @@ subgrid = grid |> ValidCoords(Mercator) viz(subgrid |> Proj(Mercator), showsegments=true) ``` +## Proj + +```@docs +Proj +``` + +```@example transforms +# load coordinate reference system +using CoordRefSystems: Polar + +# triangle with Cartesian coordinates +triangle = Triangle((0, 0), (1, 0), (1, 1)) + +# reproject to polar coordinates +triangle |> Proj(Polar) +``` + ## Morphological ```@docs diff --git a/src/Meshes.jl b/src/Meshes.jl index 2b56371ca..981993dbe 100644 --- a/src/Meshes.jl +++ b/src/Meshes.jl @@ -539,8 +539,8 @@ export Affine, Stretch, StdCoords, - Proj, ValidCoords, + Proj, Morphological, LengthUnit, Shadow, diff --git a/src/transforms.jl b/src/transforms.jl index 2d5788a83..7ed360d0e 100644 --- a/src/transforms.jl +++ b/src/transforms.jl @@ -93,8 +93,8 @@ include("transforms/scale.jl") include("transforms/affine.jl") include("transforms/stretch.jl") include("transforms/stdcoords.jl") -include("transforms/proj.jl") include("transforms/validcoords.jl") +include("transforms/proj.jl") include("transforms/morphological.jl") include("transforms/lengthunit.jl") include("transforms/shadow.jl") diff --git a/src/transforms/validcoords.jl b/src/transforms/validcoords.jl index 123fd3a09..3b04cdbaf 100644 --- a/src/transforms/validcoords.jl +++ b/src/transforms/validcoords.jl @@ -49,5 +49,6 @@ end # ----------------- _isvalid(::ValidCoords{CRS}, p::Point) where {CRS} = indomain(CRS, coords(p)) -_isvalid(t::ValidCoords, g::Union{Polytope,MultiPolytope}) = all(p -> _isvalid(t, p), eachvertex(g)) +_isvalid(t::ValidCoords, g::Polytope) = all(p -> _isvalid(t, p), eachvertex(g)) +_isvalid(t::ValidCoords, g::MultiPolytope) = all(p -> _isvalid(t, p), eachvertex(g)) _isvalid(t::ValidCoords, g::Geometry) = all(p -> _isvalid(t, p), pointify(g)) diff --git a/test/transforms.jl b/test/transforms.jl index f52a1eefb..db38e502e 100644 --- a/test/transforms.jl +++ b/test/transforms.jl @@ -1247,6 +1247,93 @@ end @test r == r2 end +@testitem "ValidCoords" setup = [Setup] begin + @test !isaffine(ValidCoords(Mercator)) + @test !TB.isrevertible(ValidCoords(Mercator)) + @test !TB.isinvertible(ValidCoords(Mercator)) + @test TB.parameters(ValidCoords(Mercator)) == (; CRS=Mercator) + @test TB.parameters(ValidCoords(EPSG{3395})) == (; CRS=Mercator{WGS84Latest}) + @test TB.parameters(ValidCoords(ESRI{54017})) == (; CRS=Behrmann{WGS84Latest}) + f = ValidCoords(Mercator) + @test sprint(show, f) == "ValidCoords(CRS: CoordRefSystems.Mercator)" + @test sprint(show, MIME"text/plain"(), f) == """ + ValidCoords transform + └─ CRS: CoordRefSystems.Mercator""" + + # --------- + # POINTSET + # --------- + + f = ValidCoords(Mercator) + d = PointSet([latlon(-90, 0), latlon(-45, 0), latlon(0, 0), latlon(45, 0), latlon(90, 0)]) + r, c = TB.apply(f, d) + @test r == PointSet([latlon(-45, 0), latlon(0, 0), latlon(45, 0)]) + + # ------------ + # GEOMETRYSET + # ------------ + + f = ValidCoords(Mercator) + t1 = Triangle(latlon(-90, 0), latlon(-45, 30), latlon(-45, -30)) + t2 = Triangle(latlon(-45, 0), latlon(0, 30), latlon(0, -30)) + t3 = Triangle(latlon(0, -30), latlon(0, 30), latlon(45, 0)) + t4 = Triangle(latlon(45, -30), latlon(45, 30), latlon(90, 0)) + d = GeometrySet([t1, t2, t3, t4]) + r, c = TB.apply(f, d) + @test r == GeometrySet([t2, t3]) + + f = ValidCoords(Mercator) + b1 = Box(latlon(-90, -30), latlon(-30, 30)) + b2 = Box(latlon(-30, -30), latlon(30, 30)) + b3 = Box(latlon(30, -30), latlon(90, 30)) + d = GeometrySet([b1, b2, b3]) + r, c = TB.apply(f, d) + @test r == GeometrySet([b2]) + + # -------------- + # CARTESIANGRID + # -------------- + + f = ValidCoords(Mercator) + d = RegularGrid(latlon(-90, -180), latlon(90, 180), dims=(3, 3)) + r, c = TB.apply(f, d) + linds = LinearIndices(size(d)) + @test r == view(d, [linds[2, 1], linds[2, 2], linds[2, 3]]) + + # ---------------- + # RECTILINEARGRID + # ---------------- + + f = ValidCoords(Mercator) + g = RegularGrid(latlon(-90, -180), latlon(90, 180), dims=(3, 3)) + d = convert(RectilinearGrid, g) + r, c = TB.apply(f, d) + linds = LinearIndices(size(d)) + @test r == view(d, [linds[2, 1], linds[2, 2], linds[2, 3]]) + + # --------------- + # STRUCTUREDGRID + # --------------- + + f = ValidCoords(Mercator) + g = RegularGrid(latlon(-90, -180), latlon(90, 180), dims=(3, 3)) + d = convert(StructuredGrid, g) + r, c = TB.apply(f, d) + linds = LinearIndices(size(d)) + @test r == view(d, [linds[2, 1], linds[2, 2], linds[2, 3]]) + + # ----------- + # SIMPLEMESH + # ----------- + + f = ValidCoords(Mercator) + g = RegularGrid(latlon(-90, -180), latlon(90, 180), dims=(3, 3)) + d = convert(SimpleMesh, g) + r, c = TB.apply(f, d) + linds = LinearIndices(size(d)) + @test r == view(d, [linds[2, 1], linds[2, 2], linds[2, 3]]) +end + @testitem "Proj" setup = [Setup] begin @test !isaffine(Proj(Polar)) @test !TB.isrevertible(Proj(Polar)) @@ -1469,93 +1556,6 @@ end @test r === d end -@testitem "ValidCoords" setup = [Setup] begin - @test !isaffine(ValidCoords(Mercator)) - @test !TB.isrevertible(ValidCoords(Mercator)) - @test !TB.isinvertible(ValidCoords(Mercator)) - @test TB.parameters(ValidCoords(Mercator)) == (; CRS=Mercator) - @test TB.parameters(ValidCoords(EPSG{3395})) == (; CRS=Mercator{WGS84Latest}) - @test TB.parameters(ValidCoords(ESRI{54017})) == (; CRS=Behrmann{WGS84Latest}) - f = ValidCoords(Mercator) - @test sprint(show, f) == "ValidCoords(CRS: CoordRefSystems.Mercator)" - @test sprint(show, MIME"text/plain"(), f) == """ - ValidCoords transform - └─ CRS: CoordRefSystems.Mercator""" - - # --------- - # POINTSET - # --------- - - f = ValidCoords(Mercator) - d = PointSet([latlon(-90, 0), latlon(-45, 0), latlon(0, 0), latlon(45, 0), latlon(90, 0)]) - r, c = TB.apply(f, d) - @test r == PointSet([latlon(-45, 0), latlon(0, 0), latlon(45, 0)]) - - # ------------ - # GEOMETRYSET - # ------------ - - f = ValidCoords(Mercator) - t1 = Triangle(latlon(-90, 0), latlon(-45, 30), latlon(-45, -30)) - t2 = Triangle(latlon(-45, 0), latlon(0, 30), latlon(0, -30)) - t3 = Triangle(latlon(0, -30), latlon(0, 30), latlon(45, 0)) - t4 = Triangle(latlon(45, -30), latlon(45, 30), latlon(90, 0)) - d = GeometrySet([t1, t2, t3, t4]) - r, c = TB.apply(f, d) - @test r == GeometrySet([t2, t3]) - - f = ValidCoords(Mercator) - b1 = Box(latlon(-90, -30), latlon(-30, 30)) - b2 = Box(latlon(-30, -30), latlon(30, 30)) - b3 = Box(latlon(30, -30), latlon(90, 30)) - d = GeometrySet([b1, b2, b3]) - r, c = TB.apply(f, d) - @test r == GeometrySet([b2]) - - # -------------- - # CARTESIANGRID - # -------------- - - f = ValidCoords(Mercator) - d = RegularGrid(latlon(-90, -180), latlon(90, 180), dims=(3, 3)) - r, c = TB.apply(f, d) - linds = LinearIndices(size(d)) - @test r == view(d, [linds[2, 1], linds[2, 2], linds[2, 3]]) - - # ---------------- - # RECTILINEARGRID - # ---------------- - - f = ValidCoords(Mercator) - g = RegularGrid(latlon(-90, -180), latlon(90, 180), dims=(3, 3)) - d = convert(RectilinearGrid, g) - r, c = TB.apply(f, d) - linds = LinearIndices(size(d)) - @test r == view(d, [linds[2, 1], linds[2, 2], linds[2, 3]]) - - # --------------- - # STRUCTUREDGRID - # --------------- - - f = ValidCoords(Mercator) - g = RegularGrid(latlon(-90, -180), latlon(90, 180), dims=(3, 3)) - d = convert(StructuredGrid, g) - r, c = TB.apply(f, d) - linds = LinearIndices(size(d)) - @test r == view(d, [linds[2, 1], linds[2, 2], linds[2, 3]]) - - # ----------- - # SIMPLEMESH - # ----------- - - f = ValidCoords(Mercator) - g = RegularGrid(latlon(-90, -180), latlon(90, 180), dims=(3, 3)) - d = convert(SimpleMesh, g) - r, c = TB.apply(f, d) - linds = LinearIndices(size(d)) - @test r == view(d, [linds[2, 1], linds[2, 2], linds[2, 3]]) -end - @testitem "Morphological" setup = [Setup] begin f = Morphological(c -> c) @test !isaffine(f) From 243c58a6eddea8c9953029fd64526473ac77e8a6 Mon Sep 17 00:00:00 2001 From: Elias Carvalho Date: Wed, 18 Dec 2024 09:04:03 -0300 Subject: [PATCH 17/18] Add more tests --- test/transforms.jl | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/transforms.jl b/test/transforms.jl index db38e502e..8795867d7 100644 --- a/test/transforms.jl +++ b/test/transforms.jl @@ -1282,6 +1282,17 @@ end r, c = TB.apply(f, d) @test r == GeometrySet([t2, t3]) + f = ValidCoords(Mercator) + t1 = Triangle(latlon(-90, 0), latlon(-45, 30), latlon(-45, -30)) + t2 = Triangle(latlon(-45, 0), latlon(0, 30), latlon(0, -30)) + t3 = Triangle(latlon(0, -30), latlon(0, 30), latlon(45, 0)) + t4 = Triangle(latlon(45, -30), latlon(45, 30), latlon(90, 0)) + m1 = Multi([t1, t4]) + m2 = Multi([t2, t3]) + d = GeometrySet([m1, m2]) + r, c = TB.apply(f, d) + @test r == GeometrySet([m2]) + f = ValidCoords(Mercator) b1 = Box(latlon(-90, -30), latlon(-30, 30)) b2 = Box(latlon(-30, -30), latlon(30, 30)) From b02186e59ebefef76bed32e9cc1348bd21ce11a8 Mon Sep 17 00:00:00 2001 From: Elias Carvalho Date: Wed, 18 Dec 2024 09:14:26 -0300 Subject: [PATCH 18/18] Adjust code order --- docs/src/transforms.md | 40 ++++----- src/Meshes.jl | 2 +- src/transforms.jl | 2 +- test/transforms.jl | 196 ++++++++++++++++++++--------------------- 4 files changed, 120 insertions(+), 120 deletions(-) diff --git a/docs/src/transforms.md b/docs/src/transforms.md index 90d3c002a..d135a58a2 100644 --- a/docs/src/transforms.md +++ b/docs/src/transforms.md @@ -129,26 +129,6 @@ viz(fig[1,2], mesh) fig ``` -## ValidCoords - -```@docs -ValidCoords -``` - -```@example transforms -# load coordinate reference system -using CoordRefSystems: LatLon - -# regular grid with LatLon coordinates -grid = RegularGrid(Point(LatLon(-90, -180)), Point(LatLon(90, 180)), dims=(10, 10)) - -# retain elements in the projection domain -subgrid = grid |> ValidCoords(Mercator) - -# plot the projected grid -viz(subgrid |> Proj(Mercator), showsegments=true) -``` - ## Proj ```@docs @@ -193,6 +173,26 @@ using Unitful: m, cm Point(1m, 2m, 3m) |> LengthUnit(cm) ``` +## ValidCoords + +```@docs +ValidCoords +``` + +```@example transforms +# load coordinate reference system +using CoordRefSystems: LatLon + +# regular grid with LatLon coordinates +grid = RegularGrid(Point(LatLon(-90, -180)), Point(LatLon(90, 180)), dims=(10, 10)) + +# retain elements in the projection domain +subgrid = grid |> ValidCoords(Mercator) + +# plot the projected grid +viz(subgrid |> Proj(Mercator), showsegments=true) +``` + ## Shadow ```@docs diff --git a/src/Meshes.jl b/src/Meshes.jl index 981993dbe..58965ef93 100644 --- a/src/Meshes.jl +++ b/src/Meshes.jl @@ -539,10 +539,10 @@ export Affine, Stretch, StdCoords, - ValidCoords, Proj, Morphological, LengthUnit, + ValidCoords, Shadow, Slice, Repair, diff --git a/src/transforms.jl b/src/transforms.jl index 7ed360d0e..c9e4749ce 100644 --- a/src/transforms.jl +++ b/src/transforms.jl @@ -93,10 +93,10 @@ include("transforms/scale.jl") include("transforms/affine.jl") include("transforms/stretch.jl") include("transforms/stdcoords.jl") -include("transforms/validcoords.jl") include("transforms/proj.jl") include("transforms/morphological.jl") include("transforms/lengthunit.jl") +include("transforms/validcoords.jl") include("transforms/shadow.jl") include("transforms/slice.jl") include("transforms/repair.jl") diff --git a/test/transforms.jl b/test/transforms.jl index 8795867d7..c2dcc54d0 100644 --- a/test/transforms.jl +++ b/test/transforms.jl @@ -1247,104 +1247,6 @@ end @test r == r2 end -@testitem "ValidCoords" setup = [Setup] begin - @test !isaffine(ValidCoords(Mercator)) - @test !TB.isrevertible(ValidCoords(Mercator)) - @test !TB.isinvertible(ValidCoords(Mercator)) - @test TB.parameters(ValidCoords(Mercator)) == (; CRS=Mercator) - @test TB.parameters(ValidCoords(EPSG{3395})) == (; CRS=Mercator{WGS84Latest}) - @test TB.parameters(ValidCoords(ESRI{54017})) == (; CRS=Behrmann{WGS84Latest}) - f = ValidCoords(Mercator) - @test sprint(show, f) == "ValidCoords(CRS: CoordRefSystems.Mercator)" - @test sprint(show, MIME"text/plain"(), f) == """ - ValidCoords transform - └─ CRS: CoordRefSystems.Mercator""" - - # --------- - # POINTSET - # --------- - - f = ValidCoords(Mercator) - d = PointSet([latlon(-90, 0), latlon(-45, 0), latlon(0, 0), latlon(45, 0), latlon(90, 0)]) - r, c = TB.apply(f, d) - @test r == PointSet([latlon(-45, 0), latlon(0, 0), latlon(45, 0)]) - - # ------------ - # GEOMETRYSET - # ------------ - - f = ValidCoords(Mercator) - t1 = Triangle(latlon(-90, 0), latlon(-45, 30), latlon(-45, -30)) - t2 = Triangle(latlon(-45, 0), latlon(0, 30), latlon(0, -30)) - t3 = Triangle(latlon(0, -30), latlon(0, 30), latlon(45, 0)) - t4 = Triangle(latlon(45, -30), latlon(45, 30), latlon(90, 0)) - d = GeometrySet([t1, t2, t3, t4]) - r, c = TB.apply(f, d) - @test r == GeometrySet([t2, t3]) - - f = ValidCoords(Mercator) - t1 = Triangle(latlon(-90, 0), latlon(-45, 30), latlon(-45, -30)) - t2 = Triangle(latlon(-45, 0), latlon(0, 30), latlon(0, -30)) - t3 = Triangle(latlon(0, -30), latlon(0, 30), latlon(45, 0)) - t4 = Triangle(latlon(45, -30), latlon(45, 30), latlon(90, 0)) - m1 = Multi([t1, t4]) - m2 = Multi([t2, t3]) - d = GeometrySet([m1, m2]) - r, c = TB.apply(f, d) - @test r == GeometrySet([m2]) - - f = ValidCoords(Mercator) - b1 = Box(latlon(-90, -30), latlon(-30, 30)) - b2 = Box(latlon(-30, -30), latlon(30, 30)) - b3 = Box(latlon(30, -30), latlon(90, 30)) - d = GeometrySet([b1, b2, b3]) - r, c = TB.apply(f, d) - @test r == GeometrySet([b2]) - - # -------------- - # CARTESIANGRID - # -------------- - - f = ValidCoords(Mercator) - d = RegularGrid(latlon(-90, -180), latlon(90, 180), dims=(3, 3)) - r, c = TB.apply(f, d) - linds = LinearIndices(size(d)) - @test r == view(d, [linds[2, 1], linds[2, 2], linds[2, 3]]) - - # ---------------- - # RECTILINEARGRID - # ---------------- - - f = ValidCoords(Mercator) - g = RegularGrid(latlon(-90, -180), latlon(90, 180), dims=(3, 3)) - d = convert(RectilinearGrid, g) - r, c = TB.apply(f, d) - linds = LinearIndices(size(d)) - @test r == view(d, [linds[2, 1], linds[2, 2], linds[2, 3]]) - - # --------------- - # STRUCTUREDGRID - # --------------- - - f = ValidCoords(Mercator) - g = RegularGrid(latlon(-90, -180), latlon(90, 180), dims=(3, 3)) - d = convert(StructuredGrid, g) - r, c = TB.apply(f, d) - linds = LinearIndices(size(d)) - @test r == view(d, [linds[2, 1], linds[2, 2], linds[2, 3]]) - - # ----------- - # SIMPLEMESH - # ----------- - - f = ValidCoords(Mercator) - g = RegularGrid(latlon(-90, -180), latlon(90, 180), dims=(3, 3)) - d = convert(SimpleMesh, g) - r, c = TB.apply(f, d) - linds = LinearIndices(size(d)) - @test r == view(d, [linds[2, 1], linds[2, 2], linds[2, 3]]) -end - @testitem "Proj" setup = [Setup] begin @test !isaffine(Proj(Polar)) @test !TB.isrevertible(Proj(Polar)) @@ -1880,6 +1782,104 @@ end @test r ≈ SimpleMesh(f.(vertices(d)), topology(d)) end +@testitem "ValidCoords" setup = [Setup] begin + @test !isaffine(ValidCoords(Mercator)) + @test !TB.isrevertible(ValidCoords(Mercator)) + @test !TB.isinvertible(ValidCoords(Mercator)) + @test TB.parameters(ValidCoords(Mercator)) == (; CRS=Mercator) + @test TB.parameters(ValidCoords(EPSG{3395})) == (; CRS=Mercator{WGS84Latest}) + @test TB.parameters(ValidCoords(ESRI{54017})) == (; CRS=Behrmann{WGS84Latest}) + f = ValidCoords(Mercator) + @test sprint(show, f) == "ValidCoords(CRS: CoordRefSystems.Mercator)" + @test sprint(show, MIME"text/plain"(), f) == """ + ValidCoords transform + └─ CRS: CoordRefSystems.Mercator""" + + # --------- + # POINTSET + # --------- + + f = ValidCoords(Mercator) + d = PointSet([latlon(-90, 0), latlon(-45, 0), latlon(0, 0), latlon(45, 0), latlon(90, 0)]) + r, c = TB.apply(f, d) + @test r == PointSet([latlon(-45, 0), latlon(0, 0), latlon(45, 0)]) + + # ------------ + # GEOMETRYSET + # ------------ + + f = ValidCoords(Mercator) + t1 = Triangle(latlon(-90, 0), latlon(-45, 30), latlon(-45, -30)) + t2 = Triangle(latlon(-45, 0), latlon(0, 30), latlon(0, -30)) + t3 = Triangle(latlon(0, -30), latlon(0, 30), latlon(45, 0)) + t4 = Triangle(latlon(45, -30), latlon(45, 30), latlon(90, 0)) + d = GeometrySet([t1, t2, t3, t4]) + r, c = TB.apply(f, d) + @test r == GeometrySet([t2, t3]) + + f = ValidCoords(Mercator) + t1 = Triangle(latlon(-90, 0), latlon(-45, 30), latlon(-45, -30)) + t2 = Triangle(latlon(-45, 0), latlon(0, 30), latlon(0, -30)) + t3 = Triangle(latlon(0, -30), latlon(0, 30), latlon(45, 0)) + t4 = Triangle(latlon(45, -30), latlon(45, 30), latlon(90, 0)) + m1 = Multi([t1, t4]) + m2 = Multi([t2, t3]) + d = GeometrySet([m1, m2]) + r, c = TB.apply(f, d) + @test r == GeometrySet([m2]) + + f = ValidCoords(Mercator) + b1 = Box(latlon(-90, -30), latlon(-30, 30)) + b2 = Box(latlon(-30, -30), latlon(30, 30)) + b3 = Box(latlon(30, -30), latlon(90, 30)) + d = GeometrySet([b1, b2, b3]) + r, c = TB.apply(f, d) + @test r == GeometrySet([b2]) + + # -------------- + # CARTESIANGRID + # -------------- + + f = ValidCoords(Mercator) + d = RegularGrid(latlon(-90, -180), latlon(90, 180), dims=(3, 3)) + r, c = TB.apply(f, d) + linds = LinearIndices(size(d)) + @test r == view(d, [linds[2, 1], linds[2, 2], linds[2, 3]]) + + # ---------------- + # RECTILINEARGRID + # ---------------- + + f = ValidCoords(Mercator) + g = RegularGrid(latlon(-90, -180), latlon(90, 180), dims=(3, 3)) + d = convert(RectilinearGrid, g) + r, c = TB.apply(f, d) + linds = LinearIndices(size(d)) + @test r == view(d, [linds[2, 1], linds[2, 2], linds[2, 3]]) + + # --------------- + # STRUCTUREDGRID + # --------------- + + f = ValidCoords(Mercator) + g = RegularGrid(latlon(-90, -180), latlon(90, 180), dims=(3, 3)) + d = convert(StructuredGrid, g) + r, c = TB.apply(f, d) + linds = LinearIndices(size(d)) + @test r == view(d, [linds[2, 1], linds[2, 2], linds[2, 3]]) + + # ----------- + # SIMPLEMESH + # ----------- + + f = ValidCoords(Mercator) + g = RegularGrid(latlon(-90, -180), latlon(90, 180), dims=(3, 3)) + d = convert(SimpleMesh, g) + r, c = TB.apply(f, d) + linds = LinearIndices(size(d)) + @test r == view(d, [linds[2, 1], linds[2, 2], linds[2, 3]]) +end + @testitem "Shadow" setup = [Setup] begin @test !isaffine(Shadow(:xy)) @test !TB.isrevertible(Shadow("xy"))