Skip to content

hillshade_layer

Stable

Shades relief from a raster DEM source.

The example's template and LiveView, read from the files that run in the preview.

lib/maplibrex_demo_web/examples/hillshade_layer_example.html.heex
<div id="hillshade-layer-example">
  <.example_frame
    error={@map_error}
    error_id="example-hillshade-map-error"
    hosts={["tiles.openfreemap.org", "tiles.mapterhorn.com"]}
  >
    <.map
      id="example-hillshade-map"
      center={[7.6586, 45.9763]}
      zoom={11}
      style={basemap(@theme)}
      events={["map:error"]}
      class="h-full w-full"
    />
  </.example_frame>

  <.raster_dem_source
    id="example-hillshade-dem"
    map_id="example-hillshade-map"
    url="https://tiles.mapterhorn.com/tilejson.json"
    events={["source:error"]}
  />

  <%!-- A new look is a new paint: the layer repaints in place. --%>
  <.hillshade_layer
    :if={@visible}
    id="example-hillshade-layer"
    map_id="example-hillshade-map"
    source_id="example-hillshade-dem"
    paint={paint(@look)}
    events={["layer:added", "layer:removed"]}
  />

  <div class="grid gap-x-8 gap-y-4 border-t border-line px-4 py-4 lg:grid-cols-[auto_1fr]">
    <.example_group id="example-hillshade-looks" label={gettext("Set assigns")}>
      <.example_button
        :for={look <- looks()}
        id={"example-hillshade-look-#{look}"}
        code
        pressed={@look == look}
        phx-click="look"
        phx-value-look={look}
      >
        {look}
      </.example_button>
    </.example_group>

    <.example_group id="example-hillshade-toggle" label=":if" code>
      <.form for={@options} id="example-hillshade-options" phx-change="options" class="py-1.5">
        <.example_checkbox field={@options[:visible]} label="visible" />
      </.form>
    </.example_group>
  </div>

  <%!-- layer:added and layer:removed keep on_map current. --%>
  <.example_readout values={[look: @look, visible: @visible, on_map: @on_map]} />
</div>
lib/maplibrex_demo_web/examples/hillshade_layer_example.ex
defmodule MaplibrexDemoWeb.Examples.HillshadeLayerExample do
  @moduledoc """
  The example of `<.hillshade_layer>`: relief shading from a raster DEM
  source, in one of three looks set by its paint, shown and hidden with
  `:if`.
  """
  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/positron"

  # Paint for each look; "plain" keeps MapLibre's defaults.
  @looks %{
    "plain" => %{},
    "strong" => %{
      "hillshade-exaggeration" => 1.0,
      "hillshade-illumination-direction" => 315
    },
    "warm" => %{
      "hillshade-exaggeration" => 0.7,
      "hillshade-shadow-color" => "#473b24",
      "hillshade-highlight-color" => "#fff8e7",
      "hillshade-accent-color" => "#8a7f71"
    }
  }

  @impl true
  def mount(_params, _session, socket) do
    socket =
      socket
      |> assign(look: "plain", visible: true, on_map: false, map_error: nil)
      |> assign(:options, to_form(%{"visible" => true}))

    {:ok, socket}
  end

  @impl true
  def handle_event("look", %{"look" => look}, socket) when is_map_key(@looks, look) do
    {:noreply, assign(socket, :look, look)}
  end

  def handle_event("look", _params, socket), do: {:noreply, socket}

  def handle_event("options", %{"visible" => visible}, socket) do
    {:noreply,
     assign(socket, visible: visible == "true", options: to_form(%{"visible" => visible}))}
  end

  # Events from the layer: it is on the map from layer:added until
  # layer:removed. A new paint sends neither.

  def handle_event("layer:added", _payload, socket) do
    {:noreply, assign(socket, :on_map, true)}
  end

  def handle_event("layer:removed", _payload, socket) do
    {:noreply, assign(socket, :on_map, false)}
  end

  def handle_event(event, %{"error" => message}, socket)
      when event in ["map:error", "source:error"] do
    {:noreply, assign(socket, :map_error, message)}
  end

  defp looks, do: Map.keys(@looks)
  defp paint(look), do: @looks[look]
end