Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow axes to be constructed by namedtuple syntax in Map #118

Merged
merged 8 commits into from
Dec 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
asinghvi17 marked this conversation as resolved.
Show resolved Hide resolved
_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
Loading