Skip to content

Commit

Permalink
Adapted plotting to changes in CairoMakie
Browse files Browse the repository at this point in the history
  • Loading branch information
Odd committed Dec 27, 2024
1 parent cb4aaf1 commit 2f5f9ee
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 73 deletions.
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Makie = "0.21"
OffsetArrays = "1.13"
Roots = "2.1.2"
julia = "1.9"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

Expand Down
1 change: 1 addition & 0 deletions docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306"
SurfaceWaterIntegratedModeling = "71e2a337-b4d4-4738-9f2b-3c37e1d3d462"

[compat]
CairoMakie = "0.12.18"
ArchGDAL = "0.10.2"
ColorSchemes = "3.24"
GeometryBasics = "0.4.11"
Expand Down
11 changes: 5 additions & 6 deletions examples/flat_areas.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ tex = fill(cmap[:green], size(grid))
sf, fig, sc = plotgrid(grid, texture = tex,
colormap=ColorSchemes.:Paired_12,
colorrange=(1,12))
fig
set_camerapos(fig, sc, view1...)
set_camerapos(sc, view1...)

# ## Identifying flat areas and running spill analysis
#
Expand All @@ -45,7 +44,7 @@ isflat = identify_flat_areas(grid, rel_tol, max_cluster_size)

tex[isflat] .= cmap[:blue]
drape_surface(sf, tex)
set_camerapos(fig, sc, view1...)
set_camerapos(sc, view1...)


# We can see that the ocean part was correctly identified as flat, but so were
Expand All @@ -57,7 +56,7 @@ isflat = identify_flat_areas(grid, rel_tol, max_cluster_size)
tex[isflat] .= cmap[:blue]
tex[.!isflat] .= cmap[:green]
drape_surface(sf, tex)
set_camerapos(fig, sc, view1...)
set_camerapos(sc, view1...)

# With this value for the threshold, only the ocean remains identified as flat.
#
Expand Down Expand Up @@ -97,7 +96,7 @@ tex[tex.==0] .= cmap[:green]-1;
tex[isflat] .= cmap[:blue];

drape_surface(sf, tex)
set_camerapos(fig, sc, view1...)
set_camerapos(sc, view1...)

# ## Adding more sinks
#
Expand All @@ -124,7 +123,7 @@ tex[tex.==0] .= cmap[:green]-1
tex[isflat] .= cmap[:blue]

drape_surface(sf, tex)
set_camerapos(fig, sc, view1...)
set_camerapos(sc, view1...)
#
# [^1]:
# The data used in this example was originally obtained from
Expand Down
63 changes: 31 additions & 32 deletions examples/synthetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,11 @@ view2 = (GLMakie.Vec(90, 494, 8.6), GLMakie.Vec(100, 114, -4.5), 0.68);
sf, fig, sc = plotgrid(grid, texture=fill(cmap[:bright], size(grid)),
colormap=ColorSchemes.:Paired_12,
colorrange=(1, 12), wireframe=true)
fig
set_camerapos(fig, sc, view1...)
set_camerapos(sc, view1...)

# Grid seen from above. Trap 1 and 2 from Fig. 1 can be seen on the upper left
# side, wheras trap 3 is seen downstream on the lower right side.
set_camerapos(fig, sc, view2...)
set_camerapos(sc, view2...)

# Grid seen from the side. Here we see how trap 1 and 2 (right) constitute
# subtraps, or pockets, within a larger trap 4. As they gradually fill with
Expand Down Expand Up @@ -95,7 +94,7 @@ tex[tstruct.footprints[2]] .= cmap[:green]
tex[tstruct.footprints[3]] .= cmap[:blue]

drape_surface(sf, tex);
set_camerapos(fig, sc, view1...)
set_camerapos(sc, view1...)

# Trap 1 and 2 are shown in red and green respectively, trap 3 in blue and trap
# 4 in orange.
Expand Down Expand Up @@ -124,7 +123,7 @@ tex[tstruct.footprints[2]] .= cmap[:green]
tex[tstruct.footprints[3]] .= cmap[:blue]

drape_surface(sf, tex);
set_camerapos(fig, sc, view1...)
set_camerapos(sc, view1...)

# The spill region of trap 4 is the union of the blue and green regions on this
# plot. The beige-colored part of the surface does not spill to any trap; water
Expand All @@ -140,7 +139,7 @@ set_camerapos(fig, sc, view1...)
# We can visualize the spillfield by draping it directly on the surface:

drape_surface(sf, tstruct.spillfield .*2)
set_camerapos(fig, sc, view1...)
set_camerapos(sc, view1...)

# The eight different colors on this plot indicate the eight different local
# flow directions. The bright blue color, which makes up most of the grid's
Expand All @@ -167,7 +166,7 @@ for sp in tstruct.spillpoints
tex[sp.downstream_region_cell] = cmap[:lilac]
end
drape_surface(sf, tex);
set_camerapos(fig, sc,
set_camerapos(sc,
GLMakie.Vec(49.3, 156.7, 90.7), # set observer position
GLMakie.Vec(86.8, 100.7, -3.3), # set observer target point
0.95) # zoom level
Expand All @@ -183,7 +182,7 @@ tex = show_region_selection(tstruct, selection=[3, 4],
region_color=cmap[:green]-1, trap_color=cmap[:green],
river_color=cmap[:red])
drape_surface(sf, tex);
set_camerapos(fig, sc, view1...)
set_camerapos(sc, view1...)

# Here, we can see how trap 4 spills into trap 3, and trap 3 spills out of the
# domain.
Expand Down Expand Up @@ -282,17 +281,17 @@ tex = runoff_at(seq, 5)
filled_trapcells = vcat(tstruct.footprints[:]...) ## footprint of all cells
tex[filled_trapcells] .= extrema(tex)[2] # maximum value
sf_flow, fig_flow, sc_flow = plotgrid(grid, texture=tex, colormap=:Blues);
fig_flow
set_camerapos(fig_flow, sc_flow, view1...)

set_camerapos(sc_flow, view1...)

# The flow is strongly concentrated along the streams exiting from each trap,
# drowning out any other detail. To make more details visible, we can use
# a logarithmic plot:
tex = log10.(runoff_at(seq, 5))
tex[filled_trapcells] .= extrema(tex)[2] # maximum value
sf_flow_log, fig_flow_log, sc_flow_log = plotgrid(grid, texture=tex, colormap=:Blues)
fig_flow_log
set_camerapos(fig_flow_log, sc_flow_log, view1...)

set_camerapos(sc_flow_log, view1...)

# Although the `SpillEvent`s in `seq` describe the points in time where one or
# more trap statuses change, we may also be interested in the amount of water
Expand Down Expand Up @@ -331,16 +330,16 @@ tex, = interpolate_timeseries(tstruct, seq, tpoints,
river_color=cmap[:red])

drape_surface(sf, tex[1])
fig
set_camerapos(fig, sc, view1...)

set_camerapos(sc, view1...)
# At time 0.1

drape_surface(sf, tex[2])
set_camerapos(fig, sc, view1...)
set_camerapos(sc, view1...)
# At time 0.3

drape_surface(sf, tex[3])
set_camerapos(fig, sc, view1...)
set_camerapos(sc, view1...)
# At time 0.42

# ## Infiltration
Expand All @@ -360,8 +359,8 @@ infil[1:110, 1:end] .= 2.0;
sf_infil, fig_infil, sc_infil = plotgrid(grid, texture=infil,
colormap=ColorSchemes.:rainbow,
colorrange=(0, 2))
fig_infil
set_camerapos(fig_infil, sc_infil, view1...)

set_camerapos(sc_infil, view1...)

#
# In this figure, the purple part of the surface is impermeable, and the red
Expand All @@ -388,20 +387,20 @@ tex, = interpolate_timeseries(tstruct, seq2, tpoints2,
trap_color=cmap[:orange],
river_color=cmap[:red])
drape_surface(sf, tex[1])
fig
set_camerapos(fig, sc, view1...)

set_camerapos(sc, view1...)
# At time 0.1

drape_surface(sf, tex[2])
set_camerapos(fig, sc, view1...)
set_camerapos(sc, view1...)
# At time 0.3

drape_surface(sf, tex[3])
set_camerapos(fig, sc, view1...)
set_camerapos(sc, view1...)
# At time 0.42

drape_surface(sf, tex[4])
set_camerapos(fig, sc, view1...)
set_camerapos(sc, view1...)
# At time 0.60
#
# Here, we can see that trap 3 does not start to fill up before
Expand All @@ -420,8 +419,8 @@ dry_terrain = Float64.(runoff .< 0.0)
dry_terrain[filled_trapcells] .= false ## submerged terrain should be considered wet
## regardless of inflow and infiltration rates
drape_surface(sf_infil, dry_terrain)
fig_infil
set_camerapos(fig_infil, sc_infil, view1...)

set_camerapos(sc_infil, view1...)

# From this figure, where dry terrain is shown in green, the following
# observations can be noted:
Expand All @@ -435,8 +434,8 @@ tex_flow = log10.(max.(runoff, eps()))
tex_flow[runoff .< 0.0] .= minimum(tex_flow[:])
tex_flow[filled_trapcells] .= maximum(tex_flow[:])
drape_surface(sf_flow_log, tex_flow)
fig_flow_log
set_camerapos(fig_flow_log, sc_flow_log, view1...)

set_camerapos(sc_flow_log, view1...)

# One interesting thing to note from this plot is how overland flow builds up
# across the impermeable part of the surface, and then gradually attenuates as
Expand All @@ -459,20 +458,20 @@ tex, = interpolate_timeseries(tstruct, seq3, tpoints3,
filled_color=cmap[:blue],
trap_color=cmap[:orange],
river_color=cmap[:red])
fig

drape_surface(sf, tex[1])
set_camerapos(fig, sc, view1...)
set_camerapos(sc, view1...)
# At time 0.3. This is identical to the previous case above.

drape_surface(sf, tex[2])
set_camerapos(fig, sc, view1...)
set_camerapos(sc, view1...)
# At time 0.6. This is also identical to the previous case above.
drape_surface(sf, tex[3])
set_camerapos(fig, sc, view1...)
set_camerapos(sc, view1...)
# At time 0.8. This is when rain stops.

drape_surface(sf, tex[4])
set_camerapos(fig, sc, view1...)
set_camerapos(sc, view1...)
# At time 2.1. We note that the water level in trap 4 on the impermeable part of
# the terrain remains unchanged, whereas the water level in trap 3, on the
# permeable part of the domain, has dropped significantly.
Expand Down
39 changes: 14 additions & 25 deletions examples/urban.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,10 @@ view1 = (GLMakie.Vec(1223, 587, 1056), GLMakie.Vec(391, 327, 13.8), 0.66);
view2 = (GLMakie.Vec(1223, 587, 1056), GLMakie.Vec(391, 327, 13.8), 0.30);

# Here is a snapshot for the terrain model:
figmap
set_camerapos(figmap, scmap, view1...)
set_camerapos(scmap, view1...)

# And a snapshot of the surface model:
figpho
set_camerapos(figpho, scpho, view1...)
set_camerapos(scpho, view1...)

# Here is a visualization of the different masks loaded:
fig = GLMakie.Figure()
Expand Down Expand Up @@ -171,11 +169,10 @@ overlay_sinks[river_mask .> Images.Gray{Images.N0f8}(0.0)] .= blue_color;
# seen in the close-up view below.
sf_nosink, fig_nosinks, sc_nosinks = plotgrid(grid_dtm, texture=overlay_nosinks)

fig_nosinks
set_camerapos(fig_nosinks, sc_nosinks, view1...)
set_camerapos(sc_nosinks, view1...)

# Close-up view:
set_camerapos(fig_nosinks, sc_nosinks, view2...)
set_camerapos(sc_nosinks, view2...)

# #### With sinks:
#
Expand All @@ -185,11 +182,10 @@ set_camerapos(fig_nosinks, sc_nosinks, view2...)
# water.
sf_sinks, fig_sinks, sc_sinks = plotgrid(grid_dtm, texture=overlay_sinks);

fig_sinks
set_camerapos(fig_sinks, sc_sinks, view1...)
set_camerapos(sc_sinks, view1...)

# Close-up view:
set_camerapos(fig_sinks, sc_sinks, view2...)
set_camerapos(sc_sinks, view2...)

# ## Visualize flow intensity
#
Expand Down Expand Up @@ -222,12 +218,11 @@ runoff, = watercourses(tstruct_sinks, filled_traps,
sf_flow, fig_flow, sc_flow =
plotgrid(grid_dtm, texture=runoff,
colormap=:Blues)
fig_flow
set_camerapos(fig_flow, sc_flow, view1...)
#

set_camerapos(sc_flow, view1...)

## Close-up view:
set_camerapos(fig_flow, sc_flow, view2...)
set_camerapos(sc_flow, view2...)

# Only a few spots are colored non-white in the above plots. This is because
# the flow across the terrain is highly concentrated in a few locations with
Expand All @@ -237,13 +232,10 @@ set_camerapos(fig_flow, sc_flow, view2...)
sf_flow_log, fig_flow_log, sc_flow_log =
plotgrid(grid_dtm, texture=log10.(runoff), colormap=:Blues)

fig_flow_log
set_camerapos(fig_flow_log, sc_flow_log, view1...)

#
set_camerapos(sc_flow_log, view1...)

# Close-up view:
set_camerapos(fig_flow_log, sc_flow_log, view2...)
set_camerapos(sc_flow_log, view2...)

# On these logarithmic plots, differences between strong and weak flows
# are attenuated, and it is easier to see how the water flows.
Expand Down Expand Up @@ -285,8 +277,7 @@ upstream_texture[pt_ix_upscaled] .= red_color
## Plot the grid
sf_upstr_log, fig_upstr_log, sc_upstr_log = plotgrid(grid_dtm, texture=upstream_texture)

fig_upstr_log
set_camerapos(fig_upstr_log, sc_upstr_log,
set_camerapos(sc_upstr_log,
GLMakie.Vec(-17, 202, 178), # observer position
GLMakie.Vec(472, 113, -255), # observer target
0.8)
Expand Down Expand Up @@ -407,13 +398,11 @@ end
# Although the animation above can not be shown directly in the online
# documentation, we can show the end states. To better see the differences, we
# use closeup views:
f1
set_camerapos(f1, sc1, view2...)
set_camerapos(sc1, view2...)

# Terrain state at end of animated period, assuming no infiltration.
#
f2
set_camerapos(f2, sc2, view2...)
set_camerapos(sc2, view2...)

# Terrain state at end of animated period, including the effect of infiltration.

Expand Down
20 changes: 10 additions & 10 deletions src/IOandplot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,11 @@ end

# ----------------------------------------------------------------------------
function _get_tex_transform(tex)
if _using_cairo() && eltype(tex) <: Real
x -> x
else
x -> reverse(transpose(x), dims=1)
end
# if _using_cairo() && eltype(tex) <: Real
# x -> x
# else
x -> reverse(transpose(x), dims=1)
#end
#_using_glmakie() ? x -> reverse(transpose(x), dims=1) : x -> x
end

Expand Down Expand Up @@ -206,15 +206,15 @@ end

# ----------------------------------------------------------------------------
"""
set_camerapos(figure, scene, cpos, ctarget, czoom)
set_camerapos(scene, cpos, ctarget, czoom)
Set the camera position, target and zoom level for a given scene.
This function is provided as a workaround to smooth over different
idiosyncracies in camera handling for the Makie backends GLMakie and
CairoMakie.
"""
function set_camerapos(fig, scene, cpos, ctarget, czoom)
function set_camerapos(scene, cpos, ctarget, czoom)
cam = Makie.cameracontrols(scene)
upvec = Makie.Vec3f0(0.0, 0.0, 1.0)
# if _using_glmakie()
Expand All @@ -227,7 +227,7 @@ function set_camerapos(fig, scene, cpos, ctarget, czoom)
Makie.update_cam!(scene, cpos, ctarget, upvec)

#scene.center[] = false
if _using_cairo()
return fig
end
#if _using_cairo()
return scene
#end
end

0 comments on commit 2f5f9ee

Please sign in to comment.