Skip to content

raster_dem_source

Stable

Adds elevation tiles for hillshading and 3D terrain.

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

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

  <%!-- Elevation from Mapterhorn's public TileJSON. A DEM source draws
        nothing itself: the hillshade layer below makes it visible. --%>
  <.raster_dem_source
    id="example-dem"
    map_id="example-dem-map"
    url="https://tiles.mapterhorn.com/tilejson.json"
    events={["source:added", "source:data", "source:error"]}
  />

  <.hillshade_layer id="example-dem-hillshade" map_id="example-dem-map" source_id="example-dem" />

  <div class="border-t border-line px-4 py-4">
    <%!-- A new centre moves the map; the source loads the tiles in view. --%>
    <.example_group id="example-dem-places" label={gettext("Set assigns")}>
      <.example_button
        :for={{id, name} <- places()}
        id={"example-dem-place-#{id}"}
        pressed={@place == id}
        phx-click="go"
        phx-value-place={id}
      >
        {name}
      </.example_button>
    </.example_group>
  </div>

  <%!-- source:added and source:data keep status and data_events current. --%>
  <.example_readout values={[place: @place, status: @status, data_events: @data_events]} />
</div>
lib/maplibrex_demo_web/examples/raster_dem_source_example.ex
defmodule MaplibrexDemoWeb.Examples.RasterDemSourceExample do
  @moduledoc """
  The example of `<.raster_dem_source>`: elevation tiles from a public DEM
  service, made visible by a hillshade layer, with the source's events
  reporting its progress.
  """
  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"

  @places [
    %{id: "alps", center: [11.3909, 47.2757]},
    %{id: "andes", center: [-70.0109, -32.6532]},
    %{id: "himalaya", center: [86.925, 27.9881]}
  ]

  @impl true
  def mount(_params, _session, socket) do
    {:ok,
     assign(socket,
       place: "alps",
       center: hd(@places).center,
       status: "waiting",
       data_events: 0,
       map_error: nil
     )}
  end

  @impl true
  def handle_event("go", %{"place" => id}, socket) do
    case Enum.find(@places, &(&1.id == id)) do
      nil -> {:noreply, socket}
      place -> {:noreply, assign(socket, place: place.id, center: place.center)}
    end
  end

  # Events from the source. source:data arrives for every tile requested and
  # loaded; isSourceLoaded says whether every tile in view has arrived.

  def handle_event("source:added", _payload, socket) do
    {:noreply, assign(socket, :status, "added")}
  end

  def handle_event("source:data", %{"tile" => %{"isSourceLoaded" => loaded?}}, socket) do
    {:noreply,
     socket
     |> assign(:status, if(loaded?, do: "loaded", else: "loading"))
     |> update(:data_events, &(&1 + 1))}
  end

  # source:data without a tile: the source's metadata (its TileJSON) arriving.
  def handle_event("source:data", _payload, socket), do: {:noreply, socket}

  def handle_event("source:error", %{"error" => message}, socket) do
    {:noreply, assign(socket, status: "error", map_error: message)}
  end

  def handle_event("map:error", %{"error" => message}, socket) do
    {:noreply, assign(socket, :map_error, message)}
  end

  defp places do
    names = %{
      "alps" => gettext("Alps"),
      "andes" => gettext("Andes"),
      "himalaya" => gettext("Himalaya")
    }

    Enum.map(@places, &{&1.id, names[&1.id]})
  end
end