Skip to content

fill_extrusion_layer

Stable

Extrudes polygons from a source into 3D shapes, such as buildings.

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

lib/maplibrex_demo_web/examples/fill_extrusion_layer_example.html.heex
<div id="fill-extrusion-layer-example">
  <.example_frame error={@map_error} error_id="example-extrusion-map-error">
    <.map
      id="example-extrusion-map"
      center={[-73.9845, 40.7565]}
      zoom={15}
      pitch={55}
      bearing={-25}
      style={basemap(@theme)}
      events={["map:error"]}
      class="h-full w-full"
    />
  </.example_frame>

  <%!-- OpenFreeMap's TileJSON: OpenStreetMap in the OpenMapTiles schema. --%>
  <.vector_tile_source
    id="example-extrusion-osm"
    map_id="example-extrusion-map"
    url="https://tiles.openfreemap.org/planet"
    events={["source:error"]}
  />

  <.fill_extrusion_layer
    id="example-extrusion-buildings"
    map_id="example-extrusion-map"
    source_id="example-extrusion-osm"
    source_layer="building"
    paint={paint(@color, @scale)}
    events={["layer:feature_clicked"]}
  />

  <div class="space-y-3 border-t border-line px-4 py-4">
    <.example_group id="example-extrusion-assigns" label={gettext("Set assigns")}>
      <.example_button
        :for={{name, hex} <- colors()}
        id={"example-extrusion-color-#{name}"}
        code
        pressed={@color == hex}
        phx-click="color"
        phx-value-color={hex}
      >
        <.example_swatch color={hex} />{hex}
      </.example_button>

      <.form
        for={@options}
        id="example-extrusion-options"
        phx-change="options"
        class="w-full pt-2 sm:max-w-xs"
      >
        <.example_range field={@options[:scale]} label="scale" min="0" max="3" step="0.1" />
      </.form>
    </.example_group>

    <.example_note id="example-extrusion-data" icon="hero-cursor-arrow-rays-micro">
      {gettext(
        "Buildings in Midtown Manhattan from OpenStreetMap, in OpenFreeMap's tiles, at the height recorded for each, times the scale. Click one."
      )}
    </.example_note>
  </div>

  <%!-- layer:feature_clicked sets selected. --%>
  <.example_readout values={[color: @color, scale: @scale, selected: @selected || "—"]} />
</div>
lib/maplibrex_demo_web/examples/fill_extrusion_layer_example.ex
defmodule MaplibrexDemoWeb.Examples.FillExtrusionLayerExample do
  @moduledoc """
  The example of `<.fill_extrusion_layer>`: OpenStreetMap's buildings in
  Midtown Manhattan, from OpenFreeMap's vector tiles, raised to their
  recorded height. The height is multiplied by an assign, and the colour is
  one too.
  """
  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"

  @colors [
    {"purple", "#7c5cd6"},
    {"teal", "#0f8b8d"},
    {"orange", "#d9480f"},
    {"red", "#c92a2a"}
  ]

  @impl true
  def mount(_params, _session, socket) do
    socket =
      socket
      |> assign(color: "#7c5cd6", selected: nil, map_error: nil)
      |> assign_scale(1.0)

    {:ok, socket}
  end

  @impl true
  def handle_event("color", %{"color" => color}, socket) do
    if Enum.any?(@colors, fn {_name, hex} -> hex == color end) do
      {:noreply, assign(socket, :color, color)}
    else
      {:noreply, socket}
    end
  end

  def handle_event("options", %{"scale" => scale}, socket) do
    case Float.parse(scale) do
      {scale, _rest} -> {:noreply, assign_scale(socket, scale)}
      :error -> {:noreply, socket}
    end
  end

  # A click carries every building under the pointer; the first is the
  # nearest. OpenMapTiles records its height in metres as render_height.
  def handle_event("layer:feature_clicked", %{"features" => [feature | _]}, socket) do
    {:noreply, assign(socket, :selected, "#{feature["properties"]["render_height"]} m")}
  end

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

  defp paint(color, scale) do
    %{
      "fill-extrusion-color" => color,
      "fill-extrusion-height" => ["*", ["get", "render_height"], scale],
      "fill-extrusion-base" => ["*", ["get", "render_min_height"], scale],
      "fill-extrusion-opacity" => 0.85
    }
  end

  defp assign_scale(socket, scale) do
    assign(socket, scale: scale, options: to_form(%{"scale" => scale}))
  end

  defp colors, do: @colors
end