raster_tile_source
StableAdds raster image tiles for a raster layer to draw.
tiles
- @imagery
- day
- @status
- waiting
- @data_events
- 0
- @failed_tile
- nil
<div id="raster-tile-source-example">
<.example_frame
error={@map_error}
error_id="example-raster-map-error"
hosts={["gibs.earthdata.nasa.gov"]}
>
<%!-- A blank style, in both themes: the raster layer below is all the
map draws. --%>
<.map
id="example-raster-map"
center={[10, 30]}
zoom={1.5}
max_zoom={8}
style={blank_style()}
events={["map:error"]}
class="h-full w-full"
/>
</.example_frame>
<%!-- NASA's imagery is public domain; the attribution credits the service. --%>
<.raster_tile_source
id="example-raster-tiles"
map_id="example-raster-map"
tiles={tiles(@imagery)}
tile_size={256}
max_zoom={8}
attribution="Imagery: NASA EOSDIS GIBS"
events={["source:added", "source:data", "source:error"]}
/>
<.raster_layer
id="example-raster-tiles-layer"
map_id="example-raster-map"
source_id="example-raster-tiles"
/>
<div class="border-t border-line px-4 py-4">
<%!-- A new imagery changes the tiles; the source keeps its place. --%>
<.example_group id="example-raster-imageries" label="tiles" code>
<.example_button
:for={imagery <- imageries()}
id={"example-raster-imagery-#{imagery}"}
pressed={@imagery == imagery}
phx-click="imagery"
phx-value-imagery={imagery}
>
{imagery_name(imagery)}
</.example_button>
</.example_group>
</div>
<%!-- source:added and source:data keep status current; source:error
names the tile that failed, if any. --%>
<.example_readout values={[
imagery: @imagery,
status: @status,
data_events: @data_events,
failed_tile: @failed_tile || "nil"
]} />
</div>
The example's template and LiveView, read from the files that run in the preview.
<div id="raster-tile-source-example">
<.example_frame
error={@map_error}
error_id="example-raster-map-error"
hosts={["gibs.earthdata.nasa.gov"]}
>
<%!-- A blank style, in both themes: the raster layer below is all the
map draws. --%>
<.map
id="example-raster-map"
center={[10, 30]}
zoom={1.5}
max_zoom={8}
style={blank_style()}
events={["map:error"]}
class="h-full w-full"
/>
</.example_frame>
<%!-- NASA's imagery is public domain; the attribution credits the service. --%>
<.raster_tile_source
id="example-raster-tiles"
map_id="example-raster-map"
tiles={tiles(@imagery)}
tile_size={256}
max_zoom={8}
attribution="Imagery: NASA EOSDIS GIBS"
events={["source:added", "source:data", "source:error"]}
/>
<.raster_layer
id="example-raster-tiles-layer"
map_id="example-raster-map"
source_id="example-raster-tiles"
/>
<div class="border-t border-line px-4 py-4">
<%!-- A new imagery changes the tiles; the source keeps its place. --%>
<.example_group id="example-raster-imageries" label="tiles" code>
<.example_button
:for={imagery <- imageries()}
id={"example-raster-imagery-#{imagery}"}
pressed={@imagery == imagery}
phx-click="imagery"
phx-value-imagery={imagery}
>
{imagery_name(imagery)}
</.example_button>
</.example_group>
</div>
<%!-- source:added and source:data keep status current; source:error
names the tile that failed, if any. --%>
<.example_readout values={[
imagery: @imagery,
status: @status,
data_events: @data_events,
failed_tile: @failed_tile || "nil"
]} />
</div>
defmodule MaplibrexDemoWeb.Examples.RasterTileSourceExample do
@moduledoc """
The example of `<.raster_tile_source>`: NASA's public imagery tiles as
the whole map, on a style with no layers of its own. New tile URLs from
the server are applied to the source in place.
"""
use MaplibrexDemoWeb, :live_view
import MaplibreX.Components
import MaplibrexDemoWeb.Examples.ExampleComponents
# Demo plumbing, not needed in an application: applies the page's locale
# and copies each map event to the page's Events tab.
on_mount MaplibrexDemoWeb.Examples.Plumbing
# NASA GIBS layers in Web Mercator, 256-pixel JPEG tiles up to zoom 8.
# GIBS orders the tile path {z}/{y}/{x}.
@imagery %{
"day" => "BlueMarble_ShadedRelief_Bathymetry",
"night" => "VIIRS_CityLights_2012"
}
# A style with no sources or layers: the raster layer is the whole map.
@blank_style %{"version" => 8, "sources" => %{}, "layers" => []}
@impl true
def mount(_params, _session, socket) do
{:ok,
assign(socket,
imagery: "day",
status: "waiting",
data_events: 0,
failed_tile: nil,
map_error: nil
)}
end
# A new imagery is new tile URLs, which MaplibreX hands to the source
# without removing it.
@impl true
def handle_event("imagery", %{"imagery" => imagery}, socket)
when is_map_key(@imagery, imagery) do
{:noreply, assign(socket, :imagery, imagery)}
end
def handle_event("imagery", _params, socket), do: {:noreply, socket}
# Events from the source.
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}
# source:error names the tile that failed to load, when one did, by its
# coordinates (MaplibreX 0.6.2).
def handle_event("source:error", %{"error" => message} = payload, socket) do
{:noreply, assign(socket, map_error: message, failed_tile: tile_name(payload["tile"]))}
end
def handle_event("map:error", %{"error" => message}, socket) do
{:noreply, assign(socket, :map_error, message)}
end
defp tiles(imagery) do
[
"https://gibs.earthdata.nasa.gov/wmts/epsg3857/best/#{@imagery[imagery]}/default/GoogleMapsCompatible_Level8/{z}/{y}/{x}.jpeg"
]
end
defp tile_name(%{"z" => z, "x" => x, "y" => y}), do: "z=#{z} x=#{x} y=#{y}"
defp tile_name(_tile), do: nil
defp imageries, do: ["day", "night"]
defp blank_style, do: @blank_style
defp imagery_name("day"), do: gettext("Blue Marble")
defp imagery_name("night"), do: gettext("City lights, 2012")
end