Skip to content

Commit

Permalink
Allow axes to be constructed by namedtuple syntax in Map (#118)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
asinghvi17 and rafaqz authored Dec 25, 2024
1 parent e5d5618 commit 32ab5aa
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
54 changes: 52 additions & 2 deletions src/map.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand Down
16 changes: 16 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 32ab5aa

Please sign in to comment.