Skip to content

raster_layer

Stable

Shows raster tiles, an image or a video from a source.

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

lib/maplibrex_demo_web/examples/raster_layer_example.html.heex
<div id="raster-layer-example">
  <.example_frame
    error={@map_error}
    error_id="example-raster-layer-map-error"
    hosts={["tiles.openfreemap.org", "gibs.earthdata.nasa.gov"]}
  >
    <.map
      id="example-raster-layer-map"
      center={[10, 30]}
      zoom={1.6}
      style={basemap(@theme)}
      events={["map:error"]}
      class="h-full w-full"
    />
  </.example_frame>

  <%!-- NASA's imagery is public domain; the attribution credits the service.
        GIBS orders the tile path {z}/{y}/{x}. --%>
  <.raster_tile_source
    id="example-raster-layer-tiles"
    map_id="example-raster-layer-map"
    tiles={[
      "https://gibs.earthdata.nasa.gov/wmts/epsg3857/best/BlueMarble_ShadedRelief_Bathymetry/default/GoogleMapsCompatible_Level8/{z}/{y}/{x}.jpeg"
    ]}
    tile_size={256}
    max_zoom={8}
    attribution="Imagery: NASA EOSDIS GIBS"
    events={["source:error"]}
  />

  <.raster_layer
    id="example-raster-layer-imagery"
    map_id="example-raster-layer-map"
    source_id="example-raster-layer-tiles"
    paint={
      %{
        "raster-opacity" => @opacity,
        "raster-saturation" => @saturation,
        "raster-hue-rotate" => @hue
      }
    }
  />

  <div class="border-t border-line px-4 py-4">
    <.form
      for={@options}
      id="example-raster-layer-options"
      phx-change="options"
      class="grid w-full gap-4 sm:grid-cols-3"
    >
      <.example_range
        field={@options[:opacity]}
        label="raster-opacity"
        min="0"
        max="1"
        step="0.05"
      />
      <.example_range
        field={@options[:saturation]}
        label="raster-saturation"
        min="-1"
        max="1"
        step="0.1"
      />
      <.example_range field={@options[:hue]} label="raster-hue-rotate" min="0" max="360" step="5" />
    </.form>
  </div>

  <.example_readout values={[opacity: @opacity, saturation: @saturation, hue: @hue]} />
</div>
lib/maplibrex_demo_web/examples/raster_layer_example.ex
defmodule MaplibrexDemoWeb.Examples.RasterLayerExample do
  @moduledoc """
  The example of `<.raster_layer>`: NASA's Blue Marble imagery drawn over
  the basemap, with its opacity, saturation and hue set from the assigns.
  """
  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"

  @impl true
  def mount(_params, _session, socket) do
    {:ok, socket |> assign(map_error: nil) |> assign_options(0.7, 0, 0)}
  end

  @impl true
  def handle_event("options", params, socket) do
    %{"opacity" => opacity, "saturation" => saturation, "hue" => hue} = params

    with {opacity, _rest} <- Float.parse(opacity),
         {saturation, _rest} <- Float.parse(saturation),
         {hue, _rest} <- Integer.parse(hue) do
      {:noreply, assign_options(socket, opacity, saturation, hue)}
    else
      :error -> {:noreply, socket}
    end
  end

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

  defp assign_options(socket, opacity, saturation, hue) do
    form = to_form(%{"opacity" => opacity, "saturation" => saturation, "hue" => hue})
    assign(socket, opacity: opacity, saturation: saturation, hue: hue, options: form)
  end
end