Skip to content

fix: stored layer state is what the map draws - #22

Merged
TommySquared merged 2 commits into
mainfrom
fix/layer-state-consistency
Nov 19, 2026
Merged

TommySquared merged 2 commits into
mainfrom
fix/layer-state-consistency

Conversation

@TommySquared

Copy link
Copy Markdown
Collaborator

Summary

What the server stored for a layer often differed from what the viewer drew, so any client that read layer state back (a layer panel, a reload, a second viewer) disagreed with the map. Specifically:

  • Opacity was never stored. set_opacity, add_geotiff_*(alpha=…) and add_tile_layer(opacity=…) only reached live viewers; after a reload every layer was back at 100%.
  • Two opacity scales. Fills were always drawn at half their colour's alpha. So style.opacity and set_opacity gave different results for the same number, and #3388ff40 drew at about 13%, not 25%.
  • Style updates replaced the stored style. update_style and PATCH /assets/{id} overwrote the whole stored style, while the live viewer merged the patch. A partial update looked right until the next reload, then colours and labels fell back to defaults.
  • Stacking drifted after a reload. The session snapshot left out z_index, and the viewer renumbered layers by its own rules, so later inserts landed in a different place than the database said.
  • Tile layers disappeared on reload, and the embedding page wasn't told about reorders or snapshot loads.

Change

Server

  • Style merging. update_style, asset_updated and PATCH merge into the stored style. Sending a key as null removes it.
  • One opacity value. set_opacity, GeoTIFF alpha and tile opacity are all stored as style.opacity, normalised to 0..1 (percentages are accepted too).
  • Default style saved on create. Vector assets are created with the config default style written into them, so the stored style is what's drawn.
  • Reordering. reorder_assets renumbers every asset 0..n-1, with the listed ids (top first) on top and the rest kept below in their existing order.
  • Server-owned stacking. Broadcasts that change stacking carry a layer_z map. The snapshot includes z_index and bbox.
  • Visibility on add. create_asset and the add events accept visible, so a layer can be added hidden without a flash.

Viewer (serve_map)

  • One style path. applyAssetStyle() is now the single place a style becomes paint, used for add, update, opacity and snapshot restore.
  • Fill alpha. The hidden 0.5 fill factor is gone: the colour's alpha byte is the drawn alpha and opacity multiplies it. Hover raises the fill colour's alpha instead.
  • Stacking. The viewer adopts the server's layer_z numbers instead of renumbering on its own.
  • Reload. Snapshot restore brings back GeoTIFF opacity and tile layers, and emits assetschanged.
  • Embed contract. reorder_assets is added to esip-contract.js change events.

Defaults and SDK

  • Default alphas. The default fill and drawn-shape fill alphas are halved (#3388ff20, #7cc24240) so the defaults look the same as before.
  • SDK. Asset gains z_index and bbox, _parse_asset keeps opacity, animate and status, and there is a new reorder_assets(). docs/llm-context.md describes the alpha, opacity, merge and stacking rules.

Behaviour changes for clients

  • Stronger fills. A stored fill colour with an explicit alpha now draws at that alpha, which is twice as strong as before. Halve hard-coded alphas to keep the old look.
  • Merge, not replace. Clients that sent a partial style expecting other keys to be cleared need to send those keys as null.
  • Persisted opacity. set_opacity now persists and is read back from style.opacity.

Tests

  • Test harness fix. server/tests/_harness.py adds a DB-only lifespan. Several layer test files only ran their first test because the MCP session manager can't be started twice. While fixing that, I updated 9 stale == 200 asserts to 201.
  • test_layer_state.py (new, 16 tests): merge semantics, stored opacity on every path, reorder, layer_z broadcasts, snapshot z_index, and adding hidden layers.
  • test_layer_state_browser.py (new, Playwright, skips if unavailable): it compares MapLibre's rendered paint and order with the API, live and after a reload. 11 of its 17 checks fail on main and all pass on this branch.
  • Results. 119 server tests and 4 SDK tests pass across the layer, style, GeoTIFF, metadata, drawing and inspector files.
  • Manual check. Built and checked in an embedding app's layer panel against this branch.

🤖 Generated with Claude Code

TommySquared and others added 2 commits November 18, 2026 18:09
Layer managers (the built-in panel, embedding apps like EOGPT) read
GET /assets, but the live viewer drew from other state, so order, opacity
and style regularly disagreed with what they showed:

- update_style and PATCH replaced the whole stored style while viewers
  merged the patch; a reload then dropped every key not resent. Styles are
  now merged server-side (explicit null clears a key) and the complete
  result is broadcast with `full: true`.
- set_opacity was broadcast-only. It is now stored as style.opacity, like
  GeoTIFF alpha and tile-layer opacity, which were never stored either.
- One opacity model: the alpha byte of a #RRGGBBAA colour is the drawn
  alpha and style.opacity multiplies fills, outlines, markers and rasters.
  The hidden 0.5 fill factor is gone (default fill and drawn-shape alpha
  halved to keep their look); hover now lifts the fill colour's alpha.
- All style -> paint goes through one viewer function (applyAssetStyle)
  for add, restore, update_style, set_opacity and asset_updated.
- Vector defaults are materialized into the stored style at creation, and
  add_* broadcasts carry the stored (status-expanded) style.
- Stacking: z-changing broadcasts carry the full stored z map (layer_z) and
  the viewer adopts it instead of renumbering itself; the session snapshot
  now includes z_index. Rasters added after a reload no longer land on top
  of vectors in the viewer while the DB has them below. reorder_assets
  renumbers every asset (unlisted ones stay below, as the viewer does), and
  move_layer/reorder rebuild the live stack from the same numbers.
- Tile layers are restored on reload; drawn-shape broadcasts carry name,
  type and z.
- The parent page is notified on reorder_assets and on snapshot restore.
- SDK: Asset carries z_index/bbox, style parse keeps opacity/animate/status,
  reorder_assets(); docs describe merge and opacity semantics.

Tests: a DB-only harness so asset/order/style tests run again (the MCP
session manager's single-use lifespan errored every test after the first;
their stale 200-vs-201 asserts are fixed), test_layer_state.py, and a
headless-browser check comparing MapLibre's live stack/paint with
GET /assets, live and after reload (11 of its 17 checks fail on main).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The hover lift added a flat 8% to every fill's alpha, so outline-only
layers (fill alpha 00) gained a tint under the cursor they never had.
Lift by the old 0.5 -> 0.72 ratio only, which matches the previous
hover look exactly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@TommySquared
TommySquared merged commit ef68621 into main Nov 19, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant