Skip to content

Commit 71f249c

Browse files
authored
Merge pull request #193 from gaelforget/polygons_202512
Polygons operations
2 parents b21add3 + 2f9f25a commit 71f249c

File tree

8 files changed

+102
-35
lines changed

8 files changed

+102
-35
lines changed

Project.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "MeshArrays"
22
uuid = "cb8c808f-1acf-59a3-9d2b-6e38d009f683"
3-
version = "0.4.0"
3+
version = "0.4.1"
44
authors = ["gaelforget <[email protected]>"]
55

66
[deps]
@@ -21,6 +21,7 @@ Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
2121
[weakdeps]
2222
DataDeps = "124859b0-ceae-595e-8997-d05f6a7a8dfe"
2323
GeoJSON = "61d90e0f-e114-555e-ac52-39dfb47a3ef9"
24+
GeometryOps = "3251bfac-6a57-4b6d-aa61-ac1fef2975ab"
2425
JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
2526
Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a"
2627
Proj = "c94c279d-25a6-4763-9509-64d165bea63e"
@@ -29,6 +30,7 @@ Shapefile = "8e980c4a-a4fe-5da2-b3a7-4b4b0353a2f4"
2930
[extensions]
3031
MeshArraysDataDepsExt = ["DataDeps"]
3132
MeshArraysGeoJSONExt = ["GeoJSON"]
33+
MeshArraysGeometryOpsExt = ["GeometryOps"]
3234
MeshArraysJLD2Ext = ["JLD2"]
3335
MeshArraysMakieExt = ["Makie"]
3436
MeshArraysProjExt = ["Proj"]
@@ -39,6 +41,7 @@ CatViews = "1.0"
3941
DataDeps = "0.7"
4042
GeoInterface = "1.4, 1.5, 1.6"
4143
GeoJSON = "0.6, 0.7, 0.8"
44+
GeometryOps = "0.1"
4245
Glob = "1"
4346
JLD2 = "0.4, 0.5, 0.6"
4447
Makie = "0.19, 0.20, 0.21, 0.22, 0.23, 0.24"

examples/basics.jl

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ end
9494

9595
# ╔═╡ 4a3f3b75-1c75-4ce5-8e75-f4dd263199cd
9696
let
97-
γ=GridSpec("LatLonCap",MeshArrays.GRID_LLC90)
97+
γ=GridSpec(ID=:LLC90())
9898

9999
a=fill(2.0,MeshArray(γ))
100100
aa=write(a)
@@ -158,15 +158,15 @@ md"""## 2. Cube Sphere Grid
158158
159159
Now, let's instead use a grid that has 6 subdomains, 32x32 points each, covering the six faces of a cube. This _cube sphere_ grid has connections between subdomains that are slightly more complicated than in the first example.
160160
161-
Here, grid variables are read from the `MeshArrays.GRID_CS32` folder by the `GridLoad` function. The `GridSpec` function provides the corresponding subdomain sizes. Again we call `comp()` which combines steps 2 and 3, and `viz()` to visualize.
161+
Here, grid variables are read from the `GRID_CS32` folder by the `GridLoad` function. The `GridSpec` function provides the corresponding subdomain sizes. Again we call `comp()` which combines steps 2 and 3, and `viz()` to visualize.
162162
"""
163163

164164
# ╔═╡ d37ae153-8646-44ae-abd9-7e0e7d479275
165165
md"""## 3. LLC90 grid
166166
167167
The [Lat-Lon-Cap grid](http://www.geosci-model-dev.net/8/3071/2015/) (or LLC) is a global ocean model grid which is widely used in the [MITgcm user community](https://mitgcm.readthedocs.io/en/latest/). It has 5 uneven subdomains, variable grid spacing, and continents [(Forget et al 2015)](http://www.geosci-model-dev.net/8/3071/2015/). LLC90's resolution is one degree albeit with modications in the Arctic and along the Equator.
168168
169-
Here, grid variables are read from the `MeshArrays.GRID_LLC90` folder by the `GridLoad` function. The `GridSpec` function provides the corresponding subdomain sizes. Again we call `comp()` which combines steps 2 and 3, and `viz()` to visualize.
169+
Here, grid variables are read from the `GRID_LLC90` folder by the `GridLoad` function. The `GridSpec` function provides the corresponding subdomain sizes. Again we call `comp()` which combines steps 2 and 3, and `viz()` to visualize.
170170
"""
171171

172172
# ╔═╡ 0865dd0f-c43e-43a7-aa65-74aba4f4460d
@@ -248,7 +248,7 @@ end
248248

249249
# ╔═╡ dacc2261-1d75-4d68-b82c-174e4fae3631
250250
begin
251-
γ_b=GridSpec("CubeSphere",MeshArrays.GRID_CS32)
251+
γ_b=GridSpec("CubeSphere",MeshArrays.Dataset("GRID_CS32"))
252252
Γ_b=GridLoad(γ_b;option="full")
253253

254254
Rini_b,Rend_b=smoothing_example(Γ_b)
@@ -265,7 +265,7 @@ end
265265

266266
# ╔═╡ 23198b8d-6bf0-4af9-91d0-7886c9574f81
267267
begin
268-
γ_c=GridSpec("LatLonCap",MeshArrays.GRID_LLC90)
268+
γ_c=GridSpec(ID=:LLC90)
269269
Γ_c=GridLoad(γ_c;option="full")
270270
Rini_c,Rend_c=smoothing_example(Γ_c)
271271
PlutoUI.with_terminal() do
@@ -777,18 +777,36 @@ git-tree-sha1 = "7a214fdac5ed5f59a22c2d9a885a16da1c74bbc7"
777777
uuid = "559328eb-81f9-559d-9380-de523a88c83c"
778778
version = "1.0.17+0"
779779
780+
[[deps.GeoFormatTypes]]
781+
git-tree-sha1 = "7528a7956248c723d01a0a9b0447bf254bf4da52"
782+
uuid = "68eda718-8dee-11e9-39e7-89f7f65f511f"
783+
version = "0.4.5"
784+
785+
[[deps.GeoInterface]]
786+
deps = ["DataAPI", "Extents", "GeoFormatTypes"]
787+
git-tree-sha1 = "b7c5cdf45298877bb683bdda3f871ff7070985c4"
788+
uuid = "cf35fbd7-0cd7-5166-be24-54bfbe79505f"
789+
version = "1.6.0"
790+
791+
[deps.GeoInterface.extensions]
792+
GeoInterfaceMakieExt = ["Makie", "GeometryBasics"]
793+
GeoInterfaceRecipesBaseExt = "RecipesBase"
794+
795+
[deps.GeoInterface.weakdeps]
796+
GeometryBasics = "5c1252a2-5f33-56bf-86c9-59e7332b4326"
797+
Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a"
798+
RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
799+
780800
[[deps.GeometryBasics]]
781801
deps = ["EarCut_jll", "Extents", "IterTools", "LinearAlgebra", "PrecompileTools", "Random", "StaticArrays"]
782802
git-tree-sha1 = "1f5a80f4ed9f5a4aada88fc2db456e637676414b"
783803
uuid = "5c1252a2-5f33-56bf-86c9-59e7332b4326"
784804
version = "0.5.10"
805+
weakdeps = ["GeoInterface"]
785806
786807
[deps.GeometryBasics.extensions]
787808
GeometryBasicsGeoInterfaceExt = "GeoInterface"
788809
789-
[deps.GeometryBasics.weakdeps]
790-
GeoInterface = "cf35fbd7-0cd7-5166-be24-54bfbe79505f"
791-
792810
[[deps.GettextRuntime_jll]]
793811
deps = ["Artifacts", "CompilerSupportLibraries_jll", "JLLWrappers", "Libdl", "Libiconv_jll"]
794812
git-tree-sha1 = "45288942190db7c5f760f59c04495064eedf9340"
@@ -1221,10 +1239,10 @@ uuid = "0a4f8689-d25c-4efe-a92b-7142dfc1aa53"
12211239
version = "0.6.7"
12221240
12231241
[[deps.MeshArrays]]
1224-
deps = ["CatViews", "Dates", "Distributed", "Glob", "LazyArtifacts", "NearestNeighbors", "Pkg", "Printf", "SharedArrays", "SparseArrays", "Statistics", "Unitful"]
1225-
git-tree-sha1 = "3ea2dc9aaaa4f4aa8799d568a4fef5abcfd2b7bf"
1242+
deps = ["CatViews", "Dates", "Distributed", "GeoInterface", "Glob", "LazyArtifacts", "NearestNeighbors", "Pkg", "Printf", "SharedArrays", "SparseArrays", "Statistics", "Unitful"]
1243+
git-tree-sha1 = "bc4820b8c8648fc0d60503562a6acae0aeef312d"
12261244
uuid = "cb8c808f-1acf-59a3-9d2b-6e38d009f683"
1227-
version = "0.3.24"
1245+
version = "0.4.0"
12281246
12291247
[deps.MeshArrays.extensions]
12301248
MeshArraysDataDepsExt = ["DataDeps"]

examples/geography.jl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ md"""## Appendices
125125

126126
# ╔═╡ 39924391-38ce-46a1-877f-80a7975340a0
127127
begin
128-
pth=MeshArrays.GRID_LLC90
128+
pth=MeshArrays.Dataset("GRID_LLC90")
129129
γ=GridSpec("LatLonCap",pth)
130130
Γ=GridLoad(γ;option="light")
131131
#LC=LatitudeCircles(-89.0:89.0,Γ)
@@ -248,9 +248,9 @@ end
248248
# ╔═╡ 229d395f-f3b4-40df-a482-264d540be875
249249
begin
250250
if source==:shp_example
251-
fil=demo.download_polygons("ne_110m_admin_0_countries.shp")
251+
fil=MeshArrays.Dataset("countries_shp1",do_read=false)
252252
elseif source==:json_example
253-
fil=demo.download_polygons("countries.geojson")
253+
fil=MeshArrays.Dataset("countries_geojson1",do_read=false)
254254
end
255255
pol=MeshArrays.read_polygons(fil)
256256
"Done With Reading Country Polygons"
@@ -260,7 +260,7 @@ end
260260
begin
261261
fig_polygons=Figure()
262262
ax1=Axis(fig_polygons[1,1])
263-
[lines!(ax1,l1,color = :black, linewidth = 0.5) for l1 in pol]
263+
[lines!(ax1,l1.geometry,color = :black, linewidth = 0.5) for l1 in pol]
264264
fig_polygons
265265
end
266266

@@ -506,9 +506,9 @@ version = "1.0.0"
506506
507507
[[deps.Climatology]]
508508
deps = ["CSV", "DataDeps", "DataFrames", "DataStructures", "Dataverse", "Dates", "Distributed", "Glob", "JLD2", "MeshArrays", "Pkg", "Printf", "RollingFunctions", "STAC", "Scratch", "SharedArrays", "Statistics", "TOML", "URIs"]
509-
git-tree-sha1 = "12e7b88c1154042f725d914886604e92d59ba29a"
509+
git-tree-sha1 = "6919ec5589a0424cb1c81b40966f6ae9f0e89ac5"
510510
uuid = "9e9a4d37-2d2e-41e3-8b85-f7978328d9c7"
511-
version = "0.5.16"
511+
version = "0.5.17"
512512
513513
[deps.Climatology.extensions]
514514
ClimatologyMITgcmExt = ["MITgcm"]
@@ -928,9 +928,9 @@ version = "5.2.3+0"
928928
929929
[[deps.Glib_jll]]
930930
deps = ["Artifacts", "GettextRuntime_jll", "JLLWrappers", "Libdl", "Libffi_jll", "Libiconv_jll", "Libmount_jll", "PCRE2_jll", "Zlib_jll"]
931-
git-tree-sha1 = "50c11ffab2a3d50192a228c313f05b5b5dc5acb2"
931+
git-tree-sha1 = "6b4d2dc81736fe3980ff0e8879a9fc7c33c44ddf"
932932
uuid = "7746bdde-850d-59dc-9ae8-88ece973131d"
933-
version = "2.86.0+0"
933+
version = "2.86.2+0"
934934
935935
[[deps.Glob]]
936936
git-tree-sha1 = "97285bbd5230dd766e9ef6749b80fc617126d496"
@@ -1425,10 +1425,10 @@ uuid = "c8ffd9c3-330d-5841-b78e-0817d7145fa1"
14251425
version = "2.28.1010+0"
14261426
14271427
[[deps.MeshArrays]]
1428-
deps = ["CatViews", "Dates", "Distributed", "Glob", "LazyArtifacts", "NearestNeighbors", "Pkg", "Printf", "SharedArrays", "SparseArrays", "Statistics", "Unitful"]
1429-
git-tree-sha1 = "3ea2dc9aaaa4f4aa8799d568a4fef5abcfd2b7bf"
1428+
deps = ["CatViews", "Dates", "Distributed", "GeoInterface", "Glob", "LazyArtifacts", "NearestNeighbors", "Pkg", "Printf", "SharedArrays", "SparseArrays", "Statistics", "Unitful"]
1429+
git-tree-sha1 = "bc4820b8c8648fc0d60503562a6acae0aeef312d"
14301430
uuid = "cb8c808f-1acf-59a3-9d2b-6e38d009f683"
1431-
version = "0.3.24"
1431+
version = "0.4.0"
14321432
weakdeps = ["DataDeps", "GeoJSON", "JLD2", "Makie", "Proj", "Shapefile"]
14331433
14341434
[deps.MeshArrays.extensions]

examples/vectors.jl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ Here we load `MeshArrays.jl` and packages that provide a climatological estimate
8484

8585
# ╔═╡ 3c2de9d4-b091-464b-9210-aa84f3d4c5f1
8686
begin
87-
pth=MeshArrays.GRID_LLC90
87+
pth=MeshArrays.Dataset("GRID_LLC90")
8888
γ=GridSpec("LatLonCap",pth)
8989
Γ=GridLoad(γ;option="full")
9090
LC=LatitudeCircles(-89.0:89.0,Γ)
@@ -521,9 +521,9 @@ version = "0.3.11"
521521
522522
[[deps.Climatology]]
523523
deps = ["CSV", "DataDeps", "DataFrames", "DataStructures", "Dataverse", "Dates", "Distributed", "Glob", "JLD2", "MeshArrays", "Pkg", "Printf", "RollingFunctions", "STAC", "Scratch", "SharedArrays", "Statistics", "TOML", "URIs"]
524-
git-tree-sha1 = "12e7b88c1154042f725d914886604e92d59ba29a"
524+
git-tree-sha1 = "6919ec5589a0424cb1c81b40966f6ae9f0e89ac5"
525525
uuid = "9e9a4d37-2d2e-41e3-8b85-f7978328d9c7"
526-
version = "0.5.16"
526+
version = "0.5.17"
527527
528528
[deps.Climatology.extensions]
529529
ClimatologyMITgcmExt = ["MITgcm"]
@@ -960,9 +960,9 @@ version = "2.52.0+0"
960960
961961
[[deps.Glib_jll]]
962962
deps = ["Artifacts", "GettextRuntime_jll", "JLLWrappers", "Libdl", "Libffi_jll", "Libiconv_jll", "Libmount_jll", "PCRE2_jll", "Zlib_jll"]
963-
git-tree-sha1 = "50c11ffab2a3d50192a228c313f05b5b5dc5acb2"
963+
git-tree-sha1 = "6b4d2dc81736fe3980ff0e8879a9fc7c33c44ddf"
964964
uuid = "7746bdde-850d-59dc-9ae8-88ece973131d"
965-
version = "2.86.0+0"
965+
version = "2.86.2+0"
966966
967967
[[deps.Glob]]
968968
git-tree-sha1 = "97285bbd5230dd766e9ef6749b80fc617126d496"
@@ -1425,9 +1425,9 @@ version = "1.1.0"
14251425
14261426
[[deps.MITgcm]]
14271427
deps = ["ClimateModels", "DataDeps", "Dataverse", "Dates", "Distributed", "FortranFiles", "Glob", "MeshArrays", "Printf", "Scratch", "SharedArrays", "SparseArrays", "Statistics", "StyledStrings", "UUIDs"]
1428-
git-tree-sha1 = "38eaef144504c69593362406b4425d3d2d1a0c9a"
1428+
git-tree-sha1 = "abce854796b41317d2a33eea8714b27cb658cc14"
14291429
uuid = "dce5fa8e-68ce-4431-a242-9469c69627a0"
1430-
version = "0.5.11"
1430+
version = "0.5.12"
14311431
weakdeps = ["NetCDF"]
14321432
14331433
[deps.MITgcm.extensions]
@@ -1503,10 +1503,10 @@ uuid = "c8ffd9c3-330d-5841-b78e-0817d7145fa1"
15031503
version = "2.28.1010+0"
15041504
15051505
[[deps.MeshArrays]]
1506-
deps = ["CatViews", "Dates", "Distributed", "Glob", "LazyArtifacts", "NearestNeighbors", "Pkg", "Printf", "SharedArrays", "SparseArrays", "Statistics", "Unitful"]
1507-
git-tree-sha1 = "3ea2dc9aaaa4f4aa8799d568a4fef5abcfd2b7bf"
1506+
deps = ["CatViews", "Dates", "Distributed", "GeoInterface", "Glob", "LazyArtifacts", "NearestNeighbors", "Pkg", "Printf", "SharedArrays", "SparseArrays", "Statistics", "Unitful"]
1507+
git-tree-sha1 = "bc4820b8c8648fc0d60503562a6acae0aeef312d"
15081508
uuid = "cb8c808f-1acf-59a3-9d2b-6e38d009f683"
1509-
version = "0.3.24"
1509+
version = "0.4.0"
15101510
15111511
[deps.MeshArrays.extensions]
15121512
MeshArraysDataDepsExt = ["DataDeps"]

ext/MeshArraysGeometryOpsExt.jl

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
############################################################
3+
# #
4+
# Geometric Operations on Polygons #
5+
# #
6+
############################################################
7+
8+
module MeshArraysGeometryOpsExt
9+
10+
import GeometryOps as GO
11+
import MeshArrays: within_pol, GI
12+
13+
"""
14+
within_pol(pol; ID=1)
15+
16+
Generate a `name,rule` pair to test if location
17+
`lon,lat` is within polygon `pol[ID].geometry`.
18+
19+
```
20+
using MeshArrays, DataDeps, GeoJSON, GeometryOps
21+
pol=MeshArrays.Dataset("oceans_geojson1")
22+
name,rule=MeshArrays.within_pol(pol; ID=11)
23+
name,rule(-30,40)
24+
```
25+
"""
26+
function within_pol(pol; ID=0)
27+
name,geom=if ID==0
28+
"unknown",pol
29+
else
30+
pol[ID].name,pol[ID].geometry
31+
end
32+
rule = (x,y) -> GO.within(GI.Point(x,y), geom)
33+
(name,rule)
34+
end
35+
36+
end #module MeshArraysGeometryOpsExt

src/MeshArrays.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ function read_jld2 end
1414
function write_jld2 end
1515
function ProjAxis end
1616
function grid_lines! end
17+
function within_pol end
1718

1819
include("Types.jl")
1920
include("Grids.jl")

test/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0"
33
DataDeps = "124859b0-ceae-595e-8997-d05f6a7a8dfe"
44
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
55
GeoJSON = "61d90e0f-e114-555e-ac52-39dfb47a3ef9"
6+
GeometryOps = "3251bfac-6a57-4b6d-aa61-ac1fef2975ab"
67
JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
78
Proj = "c94c279d-25a6-4763-9509-64d165bea63e"
89
Shapefile = "8e980c4a-a4fe-5da2-b3a7-4b4b0353a2f4"

test/runtests.jl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
using Test, Documenter, Suppressor, MeshArrays, CairoMakie
2-
import DataDeps, JLD2, Shapefile, GeoJSON, Proj
2+
import DataDeps, JLD2, Shapefile, GeoJSON, Proj, GeometryOps
33

44
MeshArrays.Dataset("GRID_LL360")
55
MeshArrays.Dataset("GRID_LLC90")
66
MeshArrays.Dataset("GRID_LLC270")
77
MeshArrays.Dataset("GRID_CS32")
88

99
pol_shp=MeshArrays.Dataset("countries_shp1")
10-
pol_json=MeshArrays.Dataset("countries_geojson1")
10+
pol_json=MeshArrays.Dataset("oceans_geojson1")
1111

1212
p=dirname(pathof(MeshArrays))
1313
include(joinpath(p,"../examples/Demos.jl"))
@@ -349,6 +349,14 @@ end
349349
plot_examples(:baseproj,proj,lon0,pol=pol_shp)
350350
end
351351

352+
@testset "polygon operattions" begin
353+
name,rule=MeshArrays.within_pol(pol_json; ID=11)
354+
rule_vec = (x,y) -> rule.(x,y)
355+
356+
np=10000; lo=-180 .+360*rand(np); la=-90 .+180*rand(np);
357+
sum(rule_vec(lo,la))
358+
end
359+
352360
@testset "doctests" begin
353361
doctest(MeshArrays; manual = false)
354362
end

0 commit comments

Comments
 (0)