towerops/lib/towerops_web/live/coverage_live/map.ex
Graham McIntire 434f5782d0 feat(coverage): multi-SM-height compute + cnHeat-style height slider
Vertical 'Height above clutter' slider on the left of the map now
matches cnHeat's experience: pick an SM install height, the heatmap
re-renders to show only the coverage achievable from that height.

Backend
- New JSONB height_tiers column on coverages keyed by string metres
  → relative PNG path.
- CoverageWorker runs compute_pixels once per tier in
  [1.83, 3.05, 4.57, 6.10, 9.14, 12.19] m (~6/10/15/20/30/40 ft)
  by varying receiver_height_m on a copied struct. Each tier
  writes its own raster + PNG via Raster.write/4 with a per-height
  filename suffix; the default 3.05 m tier also populates the
  legacy raster_path / png_path so existing show pages keep
  working.

Frontend
- Vertical range slider (writing-mode: vertical-lr) anchored
  centre-left. Step picks one of HEIGHT_TIERS_FT; label updates
  to '6 ft'..'40 ft'.
- pngForCoverage/2 picks the closest available tier from the
  payload's height_tiers map, falling back to the original png
  for coverages not yet recomputed.
- On slider change the JS swaps each L.imageOverlay's URL via
  setUrl, drops the cached pixel data, and re-decodes so the RSSI
  filter still applies. coverage_hooks chunk: 20 KB → 22 KB.
2026-05-06 16:38:07 -05:00

161 lines
4.7 KiB
Elixir

defmodule ToweropsWeb.CoverageLive.Map do
@moduledoc """
cnHeat-style unified coverage map.
Shows every ready coverage in the organisation as a single composite
heatmap on a Leaflet map, with:
* a per-tower toggle list (top-right) including Show All / Hide All
* an opacity slider (bottom-centre)
* a base-map toggle (Streets / Satellite, top-left)
* an address search bar (Nominatim, top-left)
* a Distance Tool (click the map → side panel with each tower's
distance from the click and predicted RSSI at that location)
All ready coverages share the same colour palette so the composite
read like a single map instead of N independent overlays.
"""
use ToweropsWeb, :live_view
alias Towerops.Coverages
alias Towerops.Coverages.Coverage
@impl true
def mount(_params, _session, socket) do
organization = socket.assigns.current_scope.organization
if connected?(socket), do: Coverages.subscribe_organization(organization.id)
coverages = Coverages.list_ready_for_organization(organization.id)
{:ok,
socket
|> assign(:page_title, t("Coverage Map"))
|> assign(:organization, organization)
|> assign(:coverages, coverages)
|> assign(:enabled_ids, Enum.map(coverages, & &1.id))
|> assign(:probe, nil)
|> assign(:probe_units, "ft")}
end
@impl true
def handle_event("toggle_coverage", %{"id" => id}, socket) do
enabled =
if id in socket.assigns.enabled_ids,
do: List.delete(socket.assigns.enabled_ids, id),
else: [id | socket.assigns.enabled_ids]
{:noreply,
socket
|> assign(:enabled_ids, enabled)
|> push_event("coverage:enabled-changed", %{enabled: enabled})}
end
@impl true
def handle_event("show_all", _params, socket) do
enabled = Enum.map(socket.assigns.coverages, & &1.id)
{:noreply,
socket
|> assign(:enabled_ids, enabled)
|> push_event("coverage:enabled-changed", %{enabled: enabled})}
end
@impl true
def handle_event("hide_all", _params, socket) do
{:noreply,
socket
|> assign(:enabled_ids, [])
|> push_event("coverage:enabled-changed", %{enabled: []})}
end
@impl true
def handle_event("probe_point", %{"lat" => lat, "lon" => lon}, socket) do
lat = to_float(lat)
lon = to_float(lon)
rows =
socket.assigns.organization.id
|> Coverages.query_point(lat, lon)
|> Enum.sort_by(& &1.distance_m)
probe = %{lat: lat, lon: lon, rows: rows}
{:noreply, assign(socket, :probe, probe)}
end
@impl true
def handle_event("close_probe", _params, socket) do
{:noreply, assign(socket, :probe, nil)}
end
@impl true
def handle_event("set_probe_units", %{"units" => units}, socket) when units in ["ft", "m"] do
{:noreply, assign(socket, :probe_units, units)}
end
@impl true
def handle_info({:coverage_status, _status, _arg}, socket) do
coverages = Coverages.list_ready_for_organization(socket.assigns.organization.id)
enabled =
socket.assigns.enabled_ids
|> MapSet.new()
|> MapSet.intersection(MapSet.new(Enum.map(coverages, & &1.id)))
|> MapSet.to_list()
new_ids =
coverages
|> Enum.map(& &1.id)
|> Enum.reject(&(&1 in socket.assigns.enabled_ids))
enabled = enabled ++ new_ids
{:noreply,
socket
|> assign(:coverages, coverages)
|> assign(:enabled_ids, enabled)
|> push_event("coverage:list-changed", %{
coverages: coverages_payload(coverages),
enabled: enabled
})}
end
@doc false
def coverages_payload(coverages) do
Enum.map(coverages, fn c ->
{tx_lat, tx_lon} = Coverage.location(c) || {nil, nil}
%{
id: c.id,
name: c.name,
png: c.png_path,
bbox: [c.bbox_min_lat, c.bbox_min_lon, c.bbox_max_lat, c.bbox_max_lon],
tx: %{lat: tx_lat, lon: tx_lon, azimuth: c.azimuth_deg},
site: c.site && c.site.name,
# Map of "height_m" string → relative PNG path. Empty for
# coverages computed before the multi-height pipeline.
height_tiers: c.height_tiers || %{}
}
end)
end
@doc false
def fmt_distance(m, "ft") when is_number(m), do: "#{:erlang.float_to_binary(m * 3.28084 / 5280.0, decimals: 2)} mi"
def fmt_distance(m, "m") when is_number(m), do: "#{:erlang.float_to_binary(m / 1000.0, decimals: 2)} km"
def fmt_distance(_, _), do: ""
@doc false
def fmt_rssi(:no_coverage), do: "N/A"
def fmt_rssi(nil), do: ""
def fmt_rssi(v) when is_number(v), do: "#{:erlang.float_to_binary(v * 1.0, decimals: 1)} dBm"
defp to_float(v) when is_number(v), do: v * 1.0
defp to_float(v) when is_binary(v) do
case Float.parse(v) do
{n, _} -> n
:error -> 0.0
end
end
end