From 7005dca63084094a95cd7cdfc1c7103032b1f62b Mon Sep 17 00:00:00 2001 From: Anshul Singhvi Date: Mon, 23 Dec 2024 09:29:18 +0530 Subject: [PATCH 1/8] Allow axes to be constructed by namedtuple syntax in Map --- src/map.jl | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/src/map.jl b/src/map.jl index d3b141c..b47ce9e 100644 --- a/src/map.jl +++ b/src/map.jl @@ -58,6 +58,56 @@ struct Map{Ax<:Makie.AbstractAxis} <: AbstractMap scale::Float64 end +function setup_figure_and_axis!(figure::Makie.Figure, axis, ext_target, crs) + setup_axis!(axis, ext_target, crs) + return figure, axis +end + +function setup_figure_and_axis!(figure::Makie.Figure, axis_kws_nt::NamedTuple, ext_target, crs) + axis_kws = Dict(pairs(axis_kws_nt)) + AxisType = pop!(axis_kws, :type, Axis) + + axis = AxisType(figure[1, 1]; axis_kws...) + + setup_axis!(axis, ext_target, crs) + + return figure, axis +end + +_get_parent_layout(gp::Makie.GridPosition) = _get_parent_layout(gp.layout) +_get_parent_layout(gp::Makie.GridSubposition) = _get_parent_layout(gp.layout) +_get_parent_layout(gl::Makie.GridLayout) = gl + + +_get_parent_figure(fig::Makie.Figure) = fig +_get_parent_figure(gl::Makie.GridLayout) = _get_parent_figure(gl.parent) +_get_parent_figure(gp::Makie.GridPosition) = _get_parent_figure(_get_parent_layout(gp.layout)) +_get_parent_figure(gp::Makie.GridSubposition) = _get_parent_figure(_get_parent_layout(gp.layout)) + +function setup_figure_and_axis!(figure::GridPosition, axis, ext_target, crs) + error(""" + You have tried to construct a `Map` at a given grid position, + but with a materialized axis of type $(typeof(axis)). + + You can only do this if you let Tyler construct the axis, + by passing its parameters as a NamedTuple + (like `axis = (; type = Axis, ...)`). + """) +end + +function setup_figure_and_axis!(gridposition::GridPosition, axis::NamedTuple, ext_target, crs) + figure = _get_parent_figure(gridposition) + + axis_kws = Dict(pairs(axis_kws_nt)) + AxisType = pop!(axis_kws, :type, Axis) + + axis = AxisType(gridposition; axis_kws...) + + setup_axis!(axis, ext_target, crs) + + return figure, axis +end + setup_axis!(::Makie.AbstractAxis, ext_target, crs) = nothing function setup_axis!(axis::Axis, ext_target, crs) @@ -140,7 +190,7 @@ function Map(extent, extent_crs=wgs84; if !isnothing(extent) && !isnothing(extent_crs) extent isa Extent || (extent = Extents.extent(extent)) ext_target = MapTiles.project_extent(extent, extent_crs, crs) - setup_axis!(axis, ext_target, crs) + figure, axis = setup_figure_and_axis!(figure, axis, ext_target, crs) end tiles = TileCache(provider; cache_size_gb=cache_size_gb, max_parallel_downloads=max_parallel_downloads) From 57ad34ee02b5672098c8f459d9370e0d2086b1af Mon Sep 17 00:00:00 2001 From: Anshul Singhvi Date: Mon, 23 Dec 2024 09:41:16 +0530 Subject: [PATCH 2/8] minor fixes + use namedtuple as default --- src/map.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/map.jl b/src/map.jl index b47ce9e..e14076d 100644 --- a/src/map.jl +++ b/src/map.jl @@ -95,7 +95,7 @@ function setup_figure_and_axis!(figure::GridPosition, axis, ext_target, crs) """) end -function setup_figure_and_axis!(gridposition::GridPosition, axis::NamedTuple, ext_target, crs) +function setup_figure_and_axis!(gridposition::GridPosition, axis_kws_nt::NamedTuple, ext_target, crs) figure = _get_parent_figure(gridposition) axis_kws = Dict(pairs(axis_kws_nt)) @@ -173,7 +173,7 @@ end function Map(extent, extent_crs=wgs84; size=(1000, 1000), figure=Makie.Figure(; size=size), - axis=Makie.Axis(figure[1, 1]; aspect=Makie.DataAspect()), + axis=(; type = Axis, aspect = DataAspect()), plot_config=PlotConfig(), provider=TileProviders.OpenStreetMap(:Mapnik), crs=MapTiles.web_mercator, From 182bf55a1e48d09291a0861f9df6c02034917813 Mon Sep 17 00:00:00 2001 From: Anshul Singhvi Date: Mon, 23 Dec 2024 09:44:02 +0530 Subject: [PATCH 3/8] add 2 basic tests --- test/runtests.jl | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/runtests.jl b/test/runtests.jl index 58549b5..71c33cd 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -38,6 +38,19 @@ display(m) @test GeoInterface.crs(m) == Tyler.MapTiles.WebMercator() end +@testset "NamedTuple axis syntax" begin + b = Rect2f(-20.0, -20.0, 40.0, 40.0) + m = @test_nowarn Tyler.Map(b, provider=OpenStreetMap(), axis = (; type = Axis, aspect = AxisAspect(1))) + @test only(contents(m.figure.layout[1, 1])) isa Axis + @test only(contents(m.figure.layout[1, 1])).aspect == AxisAspect(1) +end + +@testset "Pass GridPosition to figure kwarg" begin + f = Figure() + m = @test_nowarn Tyler.Map(b, provider = OpenStreetMap(), figure = f[1, 2]) + @test only(contents(m.figure.layout[1, 2])) isa Axis +end + # Reference tests? # provider = TileProviders.NASAGIBS() # m = Tyler.Map(Rect2f(0, 50, 40, 20), 5; provider=provider, min_tiles=8, max_tiles=32) From de11682424d778c564135fdd8483a03c0fdeedeb Mon Sep 17 00:00:00 2001 From: Anshul Singhvi Date: Mon, 23 Dec 2024 18:43:58 +0530 Subject: [PATCH 4/8] Update runtests.jl --- test/runtests.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/runtests.jl b/test/runtests.jl index 71c33cd..c0e99e5 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -47,7 +47,7 @@ end @testset "Pass GridPosition to figure kwarg" begin f = Figure() - m = @test_nowarn Tyler.Map(b, provider = OpenStreetMap(), figure = f[1, 2]) + m = @test_nowarn Tyler.Map(b, figure = f[1, 2]) @test only(contents(m.figure.layout[1, 2])) isa Axis end From 6088b13bd4489884b000d8134dd4c9565dfc958c Mon Sep 17 00:00:00 2001 From: Rafael Schouten Date: Mon, 23 Dec 2024 14:33:01 +0100 Subject: [PATCH 5/8] Update test/runtests.jl --- test/runtests.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/runtests.jl b/test/runtests.jl index c0e99e5..b8b02b1 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -40,7 +40,7 @@ end @testset "NamedTuple axis syntax" begin b = Rect2f(-20.0, -20.0, 40.0, 40.0) - m = @test_nowarn Tyler.Map(b, provider=OpenStreetMap(), axis = (; type = Axis, aspect = AxisAspect(1))) + m = @test_nowarn Tyler.Map(b, provider=Tyler.TileProviders.OpenStreetMap(), axis = (; type = Axis, aspect = AxisAspect(1))) @test only(contents(m.figure.layout[1, 1])) isa Axis @test only(contents(m.figure.layout[1, 1])).aspect == AxisAspect(1) end From 8369ac4a05d5185ec2437226fd206e2027a3e753 Mon Sep 17 00:00:00 2001 From: Anshul Singhvi Date: Tue, 24 Dec 2024 09:27:22 +0530 Subject: [PATCH 6/8] Fix test --- test/runtests.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/runtests.jl b/test/runtests.jl index b8b02b1..a8c44c3 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -42,7 +42,7 @@ end b = Rect2f(-20.0, -20.0, 40.0, 40.0) m = @test_nowarn Tyler.Map(b, provider=Tyler.TileProviders.OpenStreetMap(), axis = (; type = Axis, aspect = AxisAspect(1))) @test only(contents(m.figure.layout[1, 1])) isa Axis - @test only(contents(m.figure.layout[1, 1])).aspect == AxisAspect(1) + @test only(contents(m.figure.layout[1, 1])).aspect[] == AxisAspect(1) end @testset "Pass GridPosition to figure kwarg" begin From ca4299b3df4de02639c35fb36d36805fb54446cf Mon Sep 17 00:00:00 2001 From: Anshul Singhvi Date: Tue, 24 Dec 2024 10:44:38 +0530 Subject: [PATCH 7/8] Update runtests.jl --- test/runtests.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/test/runtests.jl b/test/runtests.jl index a8c44c3..cec5191 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -46,6 +46,7 @@ end end @testset "Pass GridPosition to figure kwarg" begin + b = Rect2f(-20.0, -20.0, 40.0, 40.0) f = Figure() m = @test_nowarn Tyler.Map(b, figure = f[1, 2]) @test only(contents(m.figure.layout[1, 2])) isa Axis From e11ab3695656763eb7237767ad49fc7c1224c4a3 Mon Sep 17 00:00:00 2001 From: Anshul Singhvi Date: Tue, 24 Dec 2024 21:53:49 +0530 Subject: [PATCH 8/8] try more adjustments --- test/runtests.jl | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/test/runtests.jl b/test/runtests.jl index cec5191..a639dd7 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -40,16 +40,18 @@ end @testset "NamedTuple axis syntax" begin b = Rect2f(-20.0, -20.0, 40.0, 40.0) - m = @test_nowarn Tyler.Map(b, provider=Tyler.TileProviders.OpenStreetMap(), axis = (; type = Axis, aspect = AxisAspect(1))) - @test only(contents(m.figure.layout[1, 1])) isa Axis - @test only(contents(m.figure.layout[1, 1])).aspect[] == AxisAspect(1) + m1 = @test_nowarn Tyler.Map(b, provider=Tyler.TileProviders.OpenStreetMap(), axis = (; type = Axis, aspect = AxisAspect(1))) + @test only(contents(m1.figure.layout[1, 1])) isa Axis + @test only(contents(m1.figure.layout[1, 1])).aspect[] == AxisAspect(1) + close(m1) end @testset "Pass GridPosition to figure kwarg" begin b = Rect2f(-20.0, -20.0, 40.0, 40.0) f = Figure() - m = @test_nowarn Tyler.Map(b, figure = f[1, 2]) - @test only(contents(m.figure.layout[1, 2])) isa Axis + m1 = @test_nowarn Tyler.Map(b, figure = f[1, 2]) + @test only(contents(m1.figure.layout[1, 2])) isa Axis + close(m1) end # Reference tests?