terrain
StableRenders 3D terrain from a raster DEM source, with adjustable exaggeration.
Set assigns
- @exaggeration
- 1.5
- @pitch
- 70
- @enabled
- true
- @status
- waiting
- @control
- false
- @control_enabled
- true
<div id="terrain-example">
<.example_frame
error={@map_error}
error_id="example-terrain-map-error"
hosts={["tiles.openfreemap.org", "tiles.mapterhorn.com"]}
>
<%!-- max_pitch lets the camera tilt past MapLibre's default of 60. --%>
<.map
id="example-terrain-map"
center={[11.3909, 47.2757]}
zoom={12}
pitch={@pitch}
bearing={-20}
max_pitch={85}
style={basemap(@theme)}
events={["map:moved", "map:error"]}
class="h-full w-full"
/>
</.example_frame>
<.raster_dem_source
id="example-terrain-dem"
map_id="example-terrain-map"
url="https://tiles.mapterhorn.com/tilejson.json"
/>
<.terrain
:if={@enabled}
map_id="example-terrain-map"
source_id="example-terrain-dem"
exaggeration={@exaggeration}
events={["terrain:enabled", "terrain:disabled", "terrain:error"]}
/>
<%!-- A button that switches the terrain on and off. While it is on the
map it decides the terrain, and <.terrain> stands by; its terrain
comes back when the control goes. --%>
<.terrain_control
:if={@control}
id="example-terrain-control"
map_id="example-terrain-map"
terrain_source_id="example-terrain-dem"
exaggeration={@exaggeration}
enabled={@control_enabled}
events={["terrain_control:toggled", "terrain_control:error"]}
/>
<div class="border-t border-line px-4 py-4">
<%!-- Assigns: each control changes an assign; terrain and map follow. --%>
<.example_group id="example-terrain-assigns" label={gettext("Set assigns")}>
<.form
for={@options}
id="example-terrain-options"
phx-change="options"
class="grid w-full gap-4 sm:grid-cols-[1fr_1fr_auto_auto] sm:items-end"
>
<.example_range
field={@options[:exaggeration]}
label="exaggeration"
min="0.5"
max="3"
step="0.1"
/>
<.example_range field={@options[:pitch]} label="pitch" min="0" max="85" />
<.example_checkbox field={@options[:enabled]} label=":if" />
<.example_checkbox field={@options[:control]} label="<.terrain_control>" />
</.form>
</.example_group>
</div>
<%!-- terrain:enabled and terrain:disabled keep status current, and
terrain_control:toggled keeps control_enabled. --%>
<.example_readout values={[
exaggeration: @exaggeration,
pitch: @pitch,
enabled: @enabled,
status: @status,
control: @control,
control_enabled: @control_enabled
]} />
</div>
The example's template and LiveView, read from the files that run in the preview.
<div id="terrain-example">
<.example_frame
error={@map_error}
error_id="example-terrain-map-error"
hosts={["tiles.openfreemap.org", "tiles.mapterhorn.com"]}
>
<%!-- max_pitch lets the camera tilt past MapLibre's default of 60. --%>
<.map
id="example-terrain-map"
center={[11.3909, 47.2757]}
zoom={12}
pitch={@pitch}
bearing={-20}
max_pitch={85}
style={basemap(@theme)}
events={["map:moved", "map:error"]}
class="h-full w-full"
/>
</.example_frame>
<.raster_dem_source
id="example-terrain-dem"
map_id="example-terrain-map"
url="https://tiles.mapterhorn.com/tilejson.json"
/>
<.terrain
:if={@enabled}
map_id="example-terrain-map"
source_id="example-terrain-dem"
exaggeration={@exaggeration}
events={["terrain:enabled", "terrain:disabled", "terrain:error"]}
/>
<%!-- A button that switches the terrain on and off. While it is on the
map it decides the terrain, and <.terrain> stands by; its terrain
comes back when the control goes. --%>
<.terrain_control
:if={@control}
id="example-terrain-control"
map_id="example-terrain-map"
terrain_source_id="example-terrain-dem"
exaggeration={@exaggeration}
enabled={@control_enabled}
events={["terrain_control:toggled", "terrain_control:error"]}
/>
<div class="border-t border-line px-4 py-4">
<%!-- Assigns: each control changes an assign; terrain and map follow. --%>
<.example_group id="example-terrain-assigns" label={gettext("Set assigns")}>
<.form
for={@options}
id="example-terrain-options"
phx-change="options"
class="grid w-full gap-4 sm:grid-cols-[1fr_1fr_auto_auto] sm:items-end"
>
<.example_range
field={@options[:exaggeration]}
label="exaggeration"
min="0.5"
max="3"
step="0.1"
/>
<.example_range field={@options[:pitch]} label="pitch" min="0" max="85" />
<.example_checkbox field={@options[:enabled]} label=":if" />
<.example_checkbox field={@options[:control]} label="<.terrain_control>" />
</.form>
</.example_group>
</div>
<%!-- terrain:enabled and terrain:disabled keep status current, and
terrain_control:toggled keeps control_enabled. --%>
<.example_readout values={[
exaggeration: @exaggeration,
pitch: @pitch,
enabled: @enabled,
status: @status,
control: @control,
control_enabled: @control_enabled
]} />
</div>
defmodule MaplibrexDemoWeb.Examples.TerrainExample do
@moduledoc """
The example of `<.terrain>`: 3D terrain from a raster DEM source, with its
exaggeration and the map's pitch set from assigns, turned off and on with
`:if`. A `<.terrain_control>` can join it: while that button is on the
map, it decides the terrain.
"""
use MaplibrexDemoWeb, :live_view
import MaplibreX.Components
import MaplibrexDemoWeb.Examples.ExampleComponents
# Demo plumbing, not needed in an application: applies the page's locale,
# copies each map event to the page's Events tab and keeps @theme on the
# page's light or dark theme.
on_mount MaplibrexDemoWeb.Examples.Plumbing
# OpenFreeMap's basemap for the page's theme: the map's style follows @theme.
defp basemap("dark"), do: "https://tiles.openfreemap.org/styles/dark"
defp basemap("light"), do: "https://tiles.openfreemap.org/styles/liberty"
@impl true
def mount(_params, _session, socket) do
socket =
socket
|> assign(exaggeration: 1.5, pitch: 70, enabled: true, status: "waiting", map_error: nil)
|> assign(control: false, control_enabled: true)
|> assign_options()
{:ok, socket}
end
# Driven from the server: the terrain follows exaggeration, the map
# follows pitch, :if turns the terrain off and on, and the last checkbox
# adds or removes the terrain control.
@impl true
def handle_event("options", params, socket) do
%{"exaggeration" => exaggeration, "pitch" => pitch, "enabled" => enabled} = params
{:noreply,
socket
|> assign(
exaggeration: float(exaggeration),
pitch: number(pitch),
enabled: enabled == "true",
control: params["control"] == "true"
)
|> assign_options()}
end
# From the terrain control's button, once the terrain is on or off as the
# click asked. Rendered back as enabled, it keeps the button's state when
# the control is removed and added again.
def handle_event("terrain_control:toggled", %{"enabled" => enabled}, socket) do
{:noreply, assign(socket, :control_enabled, enabled)}
end
def handle_event("terrain_control:error", %{"error" => message}, socket) do
{:noreply, assign(socket, :map_error, message)}
end
# Events from the terrain, and the camera from the map, so the pitch slider
# follows a pitch set by dragging the map.
def handle_event("terrain:enabled", _payload, socket) do
{:noreply, assign(socket, :status, "enabled")}
end
def handle_event("terrain:disabled", _payload, socket) do
{:noreply, assign(socket, :status, "disabled")}
end
def handle_event("terrain:error", %{"error" => message}, socket) do
{:noreply, assign(socket, status: "error", map_error: message)}
end
def handle_event("map:moved", %{"pitch" => pitch}, socket) do
{:noreply, socket |> assign(:pitch, pitch) |> assign_options()}
end
def handle_event("map:error", %{"error" => message}, socket) do
{:noreply, assign(socket, :map_error, message)}
end
defp assign_options(socket) do
%{exaggeration: exaggeration, pitch: pitch, enabled: enabled, control: control} =
socket.assigns
form =
to_form(%{
"exaggeration" => exaggeration,
"pitch" => pitch,
"enabled" => enabled,
"control" => control
})
assign(socket, :options, form)
end
# Slider values arrive as strings. The terrain's exaggeration is a float,
# "2" included; the pitch may be either.
defp float(value) do
case Float.parse(value) do
{float, _rest} -> float
:error -> 1.0
end
end
defp number(value) do
case Integer.parse(value) do
{integer, ""} -> integer
_ -> float(value)
end
end
end