From 32ab5aa510ec997c35a7bedb51749454243766a1 Mon Sep 17 00:00:00 2001 From: Anshul Singhvi Date: Wed, 25 Dec 2024 12:41:10 +0530 Subject: [PATCH] Allow axes to be constructed by namedtuple syntax in Map (#118) * Allow axes to be constructed by namedtuple syntax in Map * minor fixes + use namedtuple as default * add 2 basic tests * Update runtests.jl * Update test/runtests.jl * Fix test * Update runtests.jl * try more adjustments --------- Co-authored-by: Rafael Schouten --- src/map.jl | 54 ++++++++++++++++++++++++++++++++++++++++++++++-- test/runtests.jl | 16 ++++++++++++++ 2 files changed, 68 insertions(+), 2 deletions(-) diff --git a/src/map.jl b/src/map.jl index 066881b..eccf2bc 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_kws_nt::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) @@ -123,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, @@ -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) diff --git a/test/runtests.jl b/test/runtests.jl index 1901797..9356575 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -38,6 +38,22 @@ 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) + 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() + 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? # provider = TileProviders.NASAGIBS() # m = Tyler.Map(Rect2f(0, 50, 40, 20), 5; provider=provider, min_tiles=8, max_tiles=32)