@sjvair/map-sdk 0.0.0-stage → 0.1.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +487 -2
- package/dist/core/basemap.svelte.d.ts +38 -0
- package/dist/core/basemap.svelte.js +163 -0
- package/dist/core/context.d.ts +5 -0
- package/dist/core/context.js +13 -0
- package/dist/core/create-map.svelte.d.ts +207 -0
- package/dist/core/create-map.svelte.js +376 -0
- package/dist/core/gl.d.ts +92 -0
- package/dist/core/gl.js +1 -0
- package/dist/core/interactions.svelte.d.ts +42 -0
- package/dist/core/interactions.svelte.js +213 -0
- package/dist/core/plugin-host.svelte.d.ts +46 -0
- package/dist/core/plugin-host.svelte.js +366 -0
- package/dist/core/plugin.d.ts +148 -0
- package/dist/core/plugin.js +1 -0
- package/dist/core/scoped-context.d.ts +7 -0
- package/dist/core/scoped-context.js +34 -0
- package/dist/data/format.d.ts +10 -0
- package/dist/data/format.js +24 -0
- package/dist/data/index.d.ts +9 -0
- package/dist/data/index.js +9 -0
- package/dist/data/messages/en.json +51 -0
- package/dist/data/messages.d.ts +10 -0
- package/dist/data/messages.js +17 -0
- package/dist/data/metadata.d.ts +22 -0
- package/dist/data/metadata.js +110 -0
- package/dist/data/refs.d.ts +18 -0
- package/dist/data/refs.js +61 -0
- package/dist/data/scale-types.d.ts +7 -0
- package/dist/data/scale-types.js +1 -0
- package/dist/data/scale.d.ts +13 -0
- package/dist/data/scale.js +27 -0
- package/dist/data/selection.d.ts +7 -0
- package/dist/data/selection.js +31 -0
- package/dist/data/static-source.d.ts +4 -0
- package/dist/data/static-source.js +3 -0
- package/dist/data/types.d.ts +69 -0
- package/dist/data/types.js +1 -0
- package/dist/index.d.ts +56 -0
- package/dist/index.js +41 -0
- package/dist/plugins/icons.d.ts +21 -0
- package/dist/plugins/icons.js +73 -0
- package/dist/plugins/points-clusters.d.ts +60 -0
- package/dist/plugins/points-clusters.js +93 -0
- package/dist/plugins/points.svelte.d.ts +93 -0
- package/dist/plugins/points.svelte.js +473 -0
- package/dist/plugins/scale-resolution.svelte.d.ts +27 -0
- package/dist/plugins/scale-resolution.svelte.js +111 -0
- package/dist/plugins/shapes.svelte.d.ts +29 -0
- package/dist/plugins/shapes.svelte.js +307 -0
- package/dist/plugins/shared.d.ts +6 -0
- package/dist/plugins/shared.js +18 -0
- package/dist/plugins/style.d.ts +57 -0
- package/dist/plugins/style.js +193 -0
- package/dist/presets/CollocationTooltip.svelte +132 -0
- package/dist/presets/CollocationTooltip.svelte.d.ts +16 -0
- package/dist/presets/MonitorClusterTooltip.svelte +149 -0
- package/dist/presets/MonitorClusterTooltip.svelte.d.ts +16 -0
- package/dist/presets/MonitorReadingBox.svelte +58 -0
- package/dist/presets/MonitorReadingBox.svelte.d.ts +12 -0
- package/dist/presets/MonitorTooltip.svelte +88 -0
- package/dist/presets/MonitorTooltip.svelte.d.ts +20 -0
- package/dist/presets/calheatscore.d.ts +24 -0
- package/dist/presets/calheatscore.js +22 -0
- package/dist/presets/collocation-sites.d.ts +18 -0
- package/dist/presets/collocation-sites.js +67 -0
- package/dist/presets/fire-groups.d.ts +26 -0
- package/dist/presets/fire-groups.js +51 -0
- package/dist/presets/forecast-zones.d.ts +25 -0
- package/dist/presets/forecast-zones.js +23 -0
- package/dist/presets/hms-fire.d.ts +26 -0
- package/dist/presets/hms-fire.js +87 -0
- package/dist/presets/hms-smoke.d.ts +19 -0
- package/dist/presets/hms-smoke.js +20 -0
- package/dist/presets/monitors.d.ts +48 -0
- package/dist/presets/monitors.js +243 -0
- package/dist/presets/reactive-props.svelte.d.ts +4 -0
- package/dist/presets/reactive-props.svelte.js +7 -0
- package/dist/presets/regions.d.ts +24 -0
- package/dist/presets/regions.js +22 -0
- package/dist/stores/abort.d.ts +16 -0
- package/dist/stores/abort.js +52 -0
- package/dist/stores/collocation-store.svelte.d.ts +25 -0
- package/dist/stores/collocation-store.svelte.js +72 -0
- package/dist/stores/hms-stores.svelte.d.ts +35 -0
- package/dist/stores/hms-stores.svelte.js +97 -0
- package/dist/stores/index.d.ts +4 -0
- package/dist/stores/index.js +4 -0
- package/dist/stores/load-once.svelte.d.ts +25 -0
- package/dist/stores/load-once.svelte.js +93 -0
- package/dist/stores/monitors-store.svelte.d.ts +85 -0
- package/dist/stores/monitors-store.svelte.js +340 -0
- package/dist/theme.css +34 -0
- package/dist/ui/FeatureList.svelte +501 -0
- package/dist/ui/FeatureList.svelte.d.ts +14 -0
- package/dist/ui/Legend.svelte +172 -0
- package/dist/ui/Legend.svelte.d.ts +13 -0
- package/dist/ui/LegendBar.svelte +161 -0
- package/dist/ui/LegendBar.svelte.d.ts +11 -0
- package/dist/ui/MapControls.svelte +135 -0
- package/dist/ui/MapControls.svelte.d.ts +11 -0
- package/dist/ui/MapTooltip.svelte +158 -0
- package/dist/ui/MapTooltip.svelte.d.ts +28 -0
- package/dist/ui/SelectionBar.svelte +123 -0
- package/dist/ui/SelectionBar.svelte.d.ts +10 -0
- package/dist/ui/TooltipItem.svelte +66 -0
- package/dist/ui/TooltipItem.svelte.d.ts +11 -0
- package/dist/ui/ViewSummary.svelte +97 -0
- package/dist/ui/ViewSummary.svelte.d.ts +7 -0
- package/dist/ui/controls.css +45 -0
- package/dist/ui/data-credits.svelte.d.ts +9 -0
- package/dist/ui/data-credits.svelte.js +52 -0
- package/dist/ui/feature-list.d.ts +31 -0
- package/dist/ui/feature-list.js +80 -0
- package/dist/ui/legend-data.d.ts +41 -0
- package/dist/ui/legend-data.js +40 -0
- package/dist/ui/level-chip.d.ts +11 -0
- package/dist/ui/level-chip.js +15 -0
- package/dist/ui/value-format.d.ts +7 -0
- package/dist/ui/value-format.js +13 -0
- package/dist/ui/view-summary.d.ts +11 -0
- package/dist/ui/view-summary.js +33 -0
- package/dist/view/MapView.svelte +373 -0
- package/dist/view/MapView.svelte.d.ts +43 -0
- package/dist/view/create-gl-map.d.ts +34 -0
- package/dist/view/create-gl-map.js +182 -0
- package/dist/view/list-placement.d.ts +21 -0
- package/dist/view/list-placement.js +13 -0
- package/package.json +103 -5
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Central California Asthma Collaborative (SJVAir)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,488 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @sjvair/map-sdk
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Svelte 5 map components for SJVAir air-quality data, built on the MapTiler SDK. A map is a
|
|
4
|
+
`createMap()` context, shown with `<MapView>`, with data drawn by plugins (monitors, region fills,
|
|
5
|
+
CalHeatScore, forecast zones, HMS smoke and fire, or your own GeoJSON). It ships a legend, a
|
|
6
|
+
feature list, a selection bar, tooltips, and zoom controls, all themed from the host's tokens.
|
|
7
|
+
|
|
8
|
+
Supported browsers: Chrome/Edge 111+, Safari 16.4+, Firefox 128+. MIT licensed.
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
Once published:
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npm install @sjvair/map-sdk
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
Peer dependencies: `svelte` (^5.57) and `@sjvair/sdk`, which is published to
|
|
19
|
+
JSR and installed through npm's JSR registry as an alias:
|
|
20
|
+
|
|
21
|
+
```json
|
|
22
|
+
{
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@sjvair/sdk": "npm:@jsr/sjvair__sdk@^5.0.0-beta.2"
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
and in the host's `.npmrc` (npm 12 refuses the JSR registry's tarball URLs unless
|
|
30
|
+
`allow-remote` lets the root project fetch them):
|
|
31
|
+
|
|
32
|
+
```ini
|
|
33
|
+
@jsr:registry=https://npm.jsr.io
|
|
34
|
+
allow-remote=root
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Tailwind CSS v4 is required in the host. Its stylesheet must scan this package for classes and
|
|
38
|
+
import the theme (the path in `@source` is relative to the stylesheet; this one is in `src/`):
|
|
39
|
+
|
|
40
|
+
```css
|
|
41
|
+
@import "tailwindcss";
|
|
42
|
+
@source "../node_modules/@sjvair/map-sdk/dist";
|
|
43
|
+
@import "@sjvair/map-sdk/theme.css";
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Without the `@source` line the components are unstyled: their type sizes and spacing are Tailwind
|
|
47
|
+
classes in the markup.
|
|
48
|
+
|
|
49
|
+
MapTiler needs a key: pass it as `config.maptilerKey`. The key is added to each map's own requests,
|
|
50
|
+
so two maps can use different keys. The MapTiler SDK also logs an "API key is not set" warning to
|
|
51
|
+
the console when the host has not set its global key (`config.apiKey` from `@maptiler/sdk`). That
|
|
52
|
+
is cosmetic and can be ignored, or silenced by setting the global key too.
|
|
53
|
+
|
|
54
|
+
## Theme tokens
|
|
55
|
+
|
|
56
|
+
Components use only the host's tokens and never assume a `.dark` class; dark mode follows
|
|
57
|
+
`prefers-color-scheme` and `[data-theme]`/`.dark` changes on `<html>` (the map re-reads its colors
|
|
58
|
+
when they change). A token the host does not define falls back as below. Define the shadcn names
|
|
59
|
+
at least; the family tokens are optional.
|
|
60
|
+
|
|
61
|
+
Standard (no fallback, must exist): `--background`, `--foreground`, `--card`, `--card-foreground`,
|
|
62
|
+
`--primary`, `--primary-foreground`, `--muted`, `--muted-foreground`, `--border`, `--ring`,
|
|
63
|
+
`--radius`. Read with a fallback: `--popover` (`--background`), `--popover-foreground`
|
|
64
|
+
(`--foreground`), `--accent` (`--muted`).
|
|
65
|
+
|
|
66
|
+
| Family token | Fallback | Used for |
|
|
67
|
+
| ------------------------ | ---------------------------- | ------------------------------------------------------------ |
|
|
68
|
+
| `--highlight` | `--primary` | Selection ring, active fills (never text) |
|
|
69
|
+
| `--highlight-subtle` | `--muted` | Selected-item fill |
|
|
70
|
+
| `--highlight-foreground` | `--primary-foreground` | Text on the selected fill |
|
|
71
|
+
| `--card-border` | `--border` | Panel and control borders |
|
|
72
|
+
| `--shadow-card` | `none` | Panels and controls |
|
|
73
|
+
| `--shadow-lifted` | `--shadow-card`, then `none` | Raised panels |
|
|
74
|
+
| `--radius-control` | `--radius` | Buttons and inputs |
|
|
75
|
+
| `--overlay` | `--background` | Credits and map overlays |
|
|
76
|
+
| `--link` | `--primary` | Links (credits) |
|
|
77
|
+
| `--ring-edge` | `--background` | Edge of the focus ring (2px `--ring` outline plus this edge) |
|
|
78
|
+
| `--swatch-outline-width` | `1px` | Legend swatch outline |
|
|
79
|
+
| `--swatch-outline-color` | a mix of `--foreground` | Legend swatch outline |
|
|
80
|
+
| `--motion-duration-fast` | `120ms` | Control transitions (0 under reduced motion) |
|
|
81
|
+
| `--motion-ease-standard` | `ease` | Control transitions |
|
|
82
|
+
|
|
83
|
+
`theme.css` exposes the family tokens as Tailwind utilities (`bg-highlight`, `border-card-border`,
|
|
84
|
+
`text-link`, `ring-ring-edge`, `shadow-card`, `rounded-control`, `shadow-lifted`).
|
|
85
|
+
|
|
86
|
+
## A map
|
|
87
|
+
|
|
88
|
+
```svelte
|
|
89
|
+
<script lang="ts">
|
|
90
|
+
import { createMap, Legend, MapView, monitorsPlugin } from "@sjvair/map-sdk";
|
|
91
|
+
import { createMonitorsStore } from "@sjvair/map-sdk/stores";
|
|
92
|
+
import { onDestroy, onMount } from "svelte";
|
|
93
|
+
|
|
94
|
+
const store = createMonitorsStore();
|
|
95
|
+
const map = createMap({ config: { maptilerKey: import.meta.env.VITE_MAPTILER_KEY } });
|
|
96
|
+
map.addPlugin(
|
|
97
|
+
monitorsPlugin({ source: store.dataSource, layerLabel: "Monitors", selectable: true }),
|
|
98
|
+
{ selectionMode: "multiple" }
|
|
99
|
+
);
|
|
100
|
+
|
|
101
|
+
onMount(() => void store.init("pm25"));
|
|
102
|
+
onDestroy(() => {
|
|
103
|
+
store.dispose();
|
|
104
|
+
map.destroy();
|
|
105
|
+
});
|
|
106
|
+
</script>
|
|
107
|
+
|
|
108
|
+
<MapView {map} class="h-[600px]">
|
|
109
|
+
{#snippet overlays()}
|
|
110
|
+
<Legend {map} />
|
|
111
|
+
{/snippet}
|
|
112
|
+
</MapView>
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
`createMap(options)` returns the map's state and services (`MapContext`) with no GL map yet;
|
|
116
|
+
`MapView` creates the GL map when it mounts. `MapView` draws the tooltip, selection bar, view
|
|
117
|
+
summary, zoom buttons, and the List button with its feature list, and the engine draws the
|
|
118
|
+
credits. `featureList={false}` leaves out the List button and its panel, and `selectionBar={false}`
|
|
119
|
+
the selection bar. `featureList` also takes options (type `FeatureListOptions`): `position` puts the
|
|
120
|
+
button in any corner (`"top-left"`, the default, `"top-right"`, `"bottom-left"`, `"bottom-right"`),
|
|
121
|
+
stacked below the controls in that corner (above them in a bottom corner), and the open panel
|
|
122
|
+
stops short of the controls and credits at the far end of its side. `reveal: "focus"` shows the
|
|
123
|
+
button only while it or its open panel has keyboard focus, and the whole time the panel is open;
|
|
124
|
+
otherwise it's visually hidden but stays in the Tab order and the accessibility tree, like a skip
|
|
125
|
+
link. monitor-map uses `featureList={{ position: "top-right", reveal: "focus" }}` with
|
|
126
|
+
`controls="engine"`. The List button and its open panel are `z-index: 2` inside the map, so a
|
|
127
|
+
host's own menus over the map can sit above them. `controls` picks the zoom controls: `"mapsdk"` (default) draws map-sdk's zoom
|
|
128
|
+
buttons, `"engine"` the MapTiler SDK's default zoom/compass and locate-me controls (top right, as
|
|
129
|
+
monitor-map uses today), and `"none"` none; the credits show in every case. Choose `controls` up
|
|
130
|
+
front: changing it after mount recreates the map. `tooltip` is `"pinned"` (default: anchored to the
|
|
131
|
+
feature) or `"pointer"` (shown at the pointer, as sjvair.com's map does, and hidden on zoom until
|
|
132
|
+
the pointer moves again); a feature focused from the list is always pinned. Anything else goes in
|
|
133
|
+
the `overlays` snippet, where `getMapContext()` returns the map. `config.basemaps` replaces the
|
|
134
|
+
default MapTiler Streets v4 variants (build on `defaultBasemaps()`, which `createMap` uses: today's
|
|
135
|
+
sjvair.com setup is `{ ...defaultBasemaps(), excludedLayers: [] }`); `config.initialView` and
|
|
136
|
+
`config.cooperativeGestures` set the camera and gesture handling. `config.projection` (`"mercator"`
|
|
137
|
+
by default, or `"globe"`) and `config.space` (`false` by default, or `{ preset: "milkyway" }` for a
|
|
138
|
+
space background behind the globe) are passed to MapTiler only when set.
|
|
139
|
+
|
|
140
|
+
Without `createMonitorsStore`, build a `DataSource` yourself (below).
|
|
141
|
+
|
|
142
|
+
### Hiding the map (the WebGL release)
|
|
143
|
+
|
|
144
|
+
Browsers cap WebGL contexts. `<MapView {map} active={false} />` releases the map's context, and
|
|
145
|
+
`active` back to `true` rebuilds it with the same camera, basemap, selection, and the latest data.
|
|
146
|
+
Use it for a map on a hidden tab.
|
|
147
|
+
|
|
148
|
+
### Plugins and draw order
|
|
149
|
+
|
|
150
|
+
Plugins draw in the order they were added, later on top. `addPlugin(plugin, { before: "id" })` draws
|
|
151
|
+
just below another plugin, and `map.movePlugin(id, { before? })` moves an added plugin and its
|
|
152
|
+
layers to the top, or just below the plugin `before` names. A plugin object belongs to one map:
|
|
153
|
+
create one per map (adding an object that is set up on another map throws). The presets are
|
|
154
|
+
`monitorsPlugin`, `regionsPlugin`, `calHeatScorePlugin`, `forecastZonesPlugin`, `hmsSmokePlugin`,
|
|
155
|
+
`hmsFirePlugin`, and `collocationSitesPlugin`; `pointsPlugin` and `shapesPlugin` are the general
|
|
156
|
+
ones they are built on. Presets don't fetch: the host supplies a `DataSource`. `monitorsPlugin` is
|
|
157
|
+
selectable by default, like the shape presets.
|
|
158
|
+
|
|
159
|
+
Selected points show a highlight ring and check mark by default. `selectedStyle: "enlarge"` on
|
|
160
|
+
`pointsPlugin` or `monitorsPlugin` instead draws the selected icon at 1.3x with no ring (icon
|
|
161
|
+
points only), as sjvair.com's map does today.
|
|
162
|
+
|
|
163
|
+
`outline` on `shapesPlugin` is `"theme"` (default, a 1px `--foreground` edge), `"level"` (the edge
|
|
164
|
+
in each level's color at the fill's opacity; choropleth only), or `"none"` (no outline layer).
|
|
165
|
+
`hmsSmokePlugin` uses `"level"`.
|
|
166
|
+
|
|
167
|
+
`zoomRange: { min?, max? }` on `pointsPlugin` limits every layer it draws (points, clusters,
|
|
168
|
+
selection) to zooms from `min` (inclusive) up to `max` (exclusive), switching live while zooming.
|
|
169
|
+
|
|
170
|
+
`hmsFirePlugin({ id?, source, hotspots?, layerLabel })` draws fires as flame icons sized and
|
|
171
|
+
colored by FRP. With `hotspots`, it switches by itself while zooming, as sjvair.com's map does:
|
|
172
|
+
`source` (the fire store's `groups`) below zoom 10 and `hotspots` from zoom 10. It stays one
|
|
173
|
+
layer: one legend section, one feature list (the groups, so keyboard users reach every fire;
|
|
174
|
+
hotspots aren't listed), and tooltips for both.
|
|
175
|
+
|
|
176
|
+
`legendDisplay: "gradient" | "blocks"` on `pointsPlugin` sets how the legend draws a range scale
|
|
177
|
+
(default a gradient; `hmsFirePlugin` uses `"blocks"`, labeled with the levels' own labels). A blocks
|
|
178
|
+
legend (also every category scale, such as smoke) is one outlined bar of contiguous segments with
|
|
179
|
+
each level's label under its segment; long labels wrap. `legendTitle: "layer" | "entry-type"` sets
|
|
180
|
+
the legend section's title: `layerLabel` (default) or the entry type's label from the metadata
|
|
181
|
+
provider (`monitorsPlugin` uses the latter, so its section reads "PM2.5"; `layerLabel` still names
|
|
182
|
+
the layer in the list, selection bar and summary). `Legend` takes `order` (`"bottom-first"`,
|
|
183
|
+
default, or `"top-first"`).
|
|
184
|
+
|
|
185
|
+
`collocationSitesPlugin({ id?, source, layerLabel })` draws collocation sites as sjvair.com does:
|
|
186
|
+
each at its reference monitor, in that monitor's shape and level color (inactive gray, inside
|
|
187
|
+
outlined), wrapped in a crosshair of `COLLOCATION_CROSSHAIR_COLOR` (`#4A5FC6`, a fixed color on
|
|
188
|
+
every theme). Its tooltip shows the reference and collocated monitors' readings side by side, each
|
|
189
|
+
with its time and level. It isn't selectable and adds no legend section (the monitors' scale is
|
|
190
|
+
shown by `monitorsPlugin`). Add it before `monitorsPlugin` so it draws under the monitors; its
|
|
191
|
+
`hoverPriority` is `Number.MAX_SAFE_INTEGER`, so its tooltip still wins over a monitor drawn above
|
|
192
|
+
it (clicks go to the topmost feature).
|
|
193
|
+
|
|
194
|
+
The monitor marker shapes are exported for your own plugins: `circle`, `square`, and
|
|
195
|
+
`triangle(color, borderWidth, borderColor?)` return a 24px SVG, and `withCrosshair(svg, color)`
|
|
196
|
+
wraps one in the crosshair (`CROSSHAIR_ICON_SIZE`, 56px, square). Pass the size to
|
|
197
|
+
`pointsPlugin`'s `icon.size`.
|
|
198
|
+
|
|
199
|
+
## Data sources
|
|
200
|
+
|
|
201
|
+
A `DataSource` is `{ status, features, scale }`: a load status (`loading`, `ready`, `error` with
|
|
202
|
+
`retry()`, or `unavailable`), a GeoJSON `FeatureCollection` whose features carry
|
|
203
|
+
`{ refKey, name, value }` properties, and a scale. The scale is either a `ScaleRef`
|
|
204
|
+
(`{ dataset, entryType }`, resolved by the metadata provider and used for the legend, tooltips,
|
|
205
|
+
and level labels) or a full `Scale`. A value the scale doesn't cover (null, negative) draws neutral
|
|
206
|
+
and reads "No level"; a level with an empty color draws neutral with its own label. While a
|
|
207
|
+
source loads, fails, or is unavailable, the legend says so (a failure offers "Try again", which
|
|
208
|
+
calls `status.retry()`). Values are shown with the metadata's display precision, except in the
|
|
209
|
+
monitor, cluster, and collocation tooltips, which match monitor-map: the reading and a cluster's
|
|
210
|
+
average are whole numbers, and a cluster's max and min are as reported.
|
|
211
|
+
|
|
212
|
+
`createMonitorsStore()` (from `@sjvair/map-sdk/stores`) loads the monitors list and latest readings
|
|
213
|
+
for an entry type, polls every two minutes, and exposes `dataSource`. `init()`, `update()`,
|
|
214
|
+
`entryTypeFor()`, `pauseAutoUpdate()`, `resumeAutoUpdate()`, and `dispose()` are its controls. A
|
|
215
|
+
dashboard that has its own date-range data builds a `DataSource` itself instead.
|
|
216
|
+
`meta` is the read-only monitors meta (the SDK's `MonitorsMeta`), null until `init()` resolves.
|
|
217
|
+
|
|
218
|
+
`createHmsSmokeStore()` and `createHmsFireStore()` (also from `@sjvair/map-sdk/stores`) load today's
|
|
219
|
+
NOAA HMS smoke and fire once, with no polling. Each has `status`, `init({ signal })`, and
|
|
220
|
+
`dispose()`; overlapping `init()` calls share one request, a failed load reports `error` with
|
|
221
|
+
`retry()` (on `status` and on every source), and a caller's abort rejects only that caller. A day
|
|
222
|
+
with no data is `ready` with an empty collection, so the legend still shows. The smoke store has one
|
|
223
|
+
`dataSource` for `hmsSmokePlugin` (scale `hms-smoke` / `smoke-density`, value the density). The fire
|
|
224
|
+
store has `groups` (nearby detections merged by `groupFires`, value the mean FRP) and `hotspots`
|
|
225
|
+
(each detection after `cleanFires`, value its FRP), for `hmsFirePlugin`'s `source` and `hotspots`
|
|
226
|
+
(scale `hms-fire` / `fire-frp`). Pass `{ messages }` to name the features with your own provider.
|
|
227
|
+
|
|
228
|
+
```ts
|
|
229
|
+
const smoke = createHmsSmokeStore();
|
|
230
|
+
const fires = createHmsFireStore();
|
|
231
|
+
await Promise.all([smoke.init(), fires.init()]);
|
|
232
|
+
map.addPlugin(hmsSmokePlugin({ source: smoke.dataSource, layerLabel: "Smoke" }));
|
|
233
|
+
map.addPlugin(
|
|
234
|
+
hmsFirePlugin({ source: fires.groups, hotspots: fires.hotspots, layerLabel: "Fires" })
|
|
235
|
+
);
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
`createCollocationSitesStore({ monitors })` (also from `@sjvair/map-sdk/stores`) loads the
|
|
239
|
+
collocation sites once and joins each to a monitors store: `dataSource` follows that store's
|
|
240
|
+
readings as it polls and uses its scale. A site whose reference or collocated monitor isn't in the
|
|
241
|
+
monitors store (inactive, filtered out, or not loaded yet) still draws at its own position, in the
|
|
242
|
+
neutral color with "No reading". It has `status`, `init({ signal })`, and `dispose()`, like the
|
|
243
|
+
HMS stores; `init()` doesn't load the monitors store.
|
|
244
|
+
|
|
245
|
+
```ts
|
|
246
|
+
const monitors = createMonitorsStore();
|
|
247
|
+
const collocations = createCollocationSitesStore({ monitors });
|
|
248
|
+
await Promise.all([monitors.init("pm25"), collocations.init()]);
|
|
249
|
+
map.addPlugin(
|
|
250
|
+
collocationSitesPlugin({ source: collocations.dataSource, layerLabel: "Collocation sites" })
|
|
251
|
+
);
|
|
252
|
+
map.addPlugin(monitorsPlugin({ source: monitors.dataSource, layerLabel: "Monitors" }));
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
### Plain GeoJSON: `staticSource`
|
|
256
|
+
|
|
257
|
+
```ts
|
|
258
|
+
import { createMap, shapesPlugin } from "@sjvair/map-sdk";
|
|
259
|
+
import { staticSource } from "@sjvair/map-sdk/data";
|
|
260
|
+
import type { FeatureCollection, Polygon } from "geojson";
|
|
261
|
+
|
|
262
|
+
declare const zones: FeatureCollection<Polygon, { refKey: string; name: string; value: number }>;
|
|
263
|
+
|
|
264
|
+
const map = createMap({ config: { maptilerKey: "…" } });
|
|
265
|
+
map.addPlugin(
|
|
266
|
+
shapesPlugin({
|
|
267
|
+
id: "zones",
|
|
268
|
+
source: staticSource(zones, { dataset: "air-quality", entryType: "pm25" }),
|
|
269
|
+
layerLabel: "Zones",
|
|
270
|
+
mode: "choropleth" // "plain" is neutral, with no legend
|
|
271
|
+
})
|
|
272
|
+
);
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
Pass `null` as the scale for features with no values. A feature's `refKey` is `monitor:<id>`,
|
|
276
|
+
`region:<type>:<code>` (the region's `external_id`, never its sqid), or `feature:<id>` (shown but
|
|
277
|
+
not selectable). `encodeRef`/`decodeRef` convert between refs and keys.
|
|
278
|
+
|
|
279
|
+
### Writing a plugin
|
|
280
|
+
|
|
281
|
+
A plugin is an object with an `id` and a `setup(ctx)` that adds sources, layers, and images
|
|
282
|
+
through its `PluginContext` and returns a teardown. The host runs `setup` once the map's style is
|
|
283
|
+
ready and again after a basemap or theme change; everything added through `ctx` is removed on
|
|
284
|
+
teardown, and ids are namespaced per map. `features()` feeds the feature list, tooltip, and
|
|
285
|
+
summary; `legend()` the legend. A plugin's `tooltip` content sits under the feature's name; set
|
|
286
|
+
`tooltipShowsName` when the content shows the name itself (the monitor and collocation tooltips
|
|
287
|
+
do), and the name header is left out.
|
|
288
|
+
|
|
289
|
+
```ts
|
|
290
|
+
import type { FeatureListItem, MapPlugin, PluginContext } from "@sjvair/map-sdk";
|
|
291
|
+
import { decodeRef, NO_LEVEL } from "@sjvair/map-sdk/data";
|
|
292
|
+
import type { FeatureCollection, Point } from "geojson";
|
|
293
|
+
|
|
294
|
+
type Sites = FeatureCollection<Point, { refKey: string; name: string }>;
|
|
295
|
+
|
|
296
|
+
function sitesPlugin(sites: Sites): MapPlugin {
|
|
297
|
+
return {
|
|
298
|
+
id: "sites",
|
|
299
|
+
label: "Sites",
|
|
300
|
+
setup(ctx: PluginContext) {
|
|
301
|
+
const source = ctx.addSource("sites", { type: "geojson", data: sites });
|
|
302
|
+
const layer = ctx.addLayer("dots", { type: "circle", source, paint: { "circle-radius": 5 } });
|
|
303
|
+
ctx.interactive([layer], (props) => {
|
|
304
|
+
const ref = decodeRef(String(props.refKey));
|
|
305
|
+
const name = String(props.name);
|
|
306
|
+
return ref && { pluginId: "sites", ref, refKey: String(props.refKey), name };
|
|
307
|
+
});
|
|
308
|
+
return () => {};
|
|
309
|
+
},
|
|
310
|
+
features: (): FeatureListItem[] =>
|
|
311
|
+
sites.features.flatMap(({ properties: { refKey, name }, geometry }) => {
|
|
312
|
+
const ref = decodeRef(refKey);
|
|
313
|
+
const [lng, lat] = geometry.coordinates;
|
|
314
|
+
if (!ref) return [];
|
|
315
|
+
const lngLat: [number, number] = [lng, lat];
|
|
316
|
+
return [
|
|
317
|
+
{ refKey, ref, name, value: null, level: NO_LEVEL, units: "", lngLat, selectable: false }
|
|
318
|
+
];
|
|
319
|
+
})
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
```
|
|
323
|
+
|
|
324
|
+
A plugin can also add a MapLibre custom layer, such as a wind layer from a library you add
|
|
325
|
+
yourself, with `ctx.addCustomLayer(local, create)`. The host calls `create` with the namespaced id
|
|
326
|
+
(the layer must use it) and adds the object it returns as is, in the plugin's draw-order slot. It is
|
|
327
|
+
removed on teardown, `create` runs again after a basemap or theme change (return a fresh layer each
|
|
328
|
+
time), and `movePlugin` moves it with the plugin's other layers.
|
|
329
|
+
|
|
330
|
+
The cursor is a pointer over any interactive feature or cluster bubble. Where several plugins'
|
|
331
|
+
features are under the pointer, the hover tooltip shows the one whose plugin has the highest
|
|
332
|
+
`hoverPriority` (default 0), the topmost on a tie; a cluster bubble on top still hides what is
|
|
333
|
+
beneath it, and clicks always go to the topmost.
|
|
334
|
+
|
|
335
|
+
```ts
|
|
336
|
+
import type { CustomLayerInterface, MapPlugin, PluginContext } from "@sjvair/map-sdk";
|
|
337
|
+
|
|
338
|
+
class WindLayer implements CustomLayerInterface {
|
|
339
|
+
readonly type = "custom";
|
|
340
|
+
constructor(readonly id: string) {}
|
|
341
|
+
onAdd() {} // set up the library's renderer
|
|
342
|
+
render() {} // draw one frame
|
|
343
|
+
onRemove() {} // release it
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
const windPlugin: MapPlugin = {
|
|
347
|
+
id: "wind",
|
|
348
|
+
label: "Wind",
|
|
349
|
+
setup(ctx: PluginContext) {
|
|
350
|
+
ctx.addCustomLayer("particles", (id) => new WindLayer(id));
|
|
351
|
+
return () => {};
|
|
352
|
+
}
|
|
353
|
+
};
|
|
354
|
+
```
|
|
355
|
+
|
|
356
|
+
The root entry exports the types a plugin needs (`MapPlugin`, `PluginContext`, `FeatureHit`,
|
|
357
|
+
`FeatureListItem`, `LegendData`, `Providers`, `GlMapLike`, `CustomLayerInterface`,
|
|
358
|
+
`LayerSpecWithoutId`, `ThemeColors`, `ClusterLayers`, `ClusterLayerArgs`, `BlockingClick`,
|
|
359
|
+
`Hovered`) and the scale expressions the built-in plugins use (`colorExpression`,
|
|
360
|
+
`opacityExpression`, `levelKeyExpression`).
|
|
361
|
+
|
|
362
|
+
## Selection
|
|
363
|
+
|
|
364
|
+
Each plugin has a mode: `none`, `single`, or `multiple` (set it with `addPlugin`'s
|
|
365
|
+
`selectionMode` or `map.setSelectionMode(id, mode)`). Click or tap picks; Shift/Cmd/Ctrl-click adds
|
|
366
|
+
or removes in `multiple`, and a tap does the same on touch. The selection is a `SpatialSelection`,
|
|
367
|
+
`{ kind: "monitors" | "regions", ids: string[] }` (region ids are `<type>:<code>`), and the host
|
|
368
|
+
owns it:
|
|
369
|
+
|
|
370
|
+
```ts
|
|
371
|
+
map.setSelection("monitors", { kind: "monitors", ids: ["abc123"] }); // host -> map
|
|
372
|
+
map.addPlugin(plugin, {
|
|
373
|
+
selectionMode: "multiple",
|
|
374
|
+
selection: { kind: "monitors", ids: ["abc123"] },
|
|
375
|
+
onselectionchange: (selection) => save(selection) // map -> host (user picks only)
|
|
376
|
+
});
|
|
377
|
+
```
|
|
378
|
+
|
|
379
|
+
`setSelection` does not call `onselectionchange`. Ids that match no feature are kept and counted
|
|
380
|
+
in the selection bar, and a feature that appears later shows as selected. `map.getSelection(id)`
|
|
381
|
+
and `getSelectionMode(id)` are reactive.
|
|
382
|
+
|
|
383
|
+
`map.on("featureclick", ({ pluginId, ref, lngLat }) => …)` fires for every click on a feature,
|
|
384
|
+
whatever the mode, and also for a pick in the feature list, so keyboard users get the same event;
|
|
385
|
+
`lngLat` is the click point (the item's position for a list pick). It returns an unsubscribe
|
|
386
|
+
function. For your own context menu, `map.featuresAt({ x, y })` returns the hits under
|
|
387
|
+
a canvas point, topmost first, and `map.featureForElement(element)` returns the hit for a row of
|
|
388
|
+
the feature list (an element with `data-mapsdk-ref`). Both return nothing while the map is
|
|
389
|
+
detached.
|
|
390
|
+
|
|
391
|
+
## Camera
|
|
392
|
+
|
|
393
|
+
`map.easeTo({ center, zoom?, minZoom? })` and `map.flyTo(...)` move the camera; `minZoom` means
|
|
394
|
+
"zoom in to at least this" (`max(current zoom, minZoom)`), and `zoom` wins when both are given.
|
|
395
|
+
`map.fitBounds({ west, south, east, north }, { padding?, maxZoom? })` fits a box. All three do
|
|
396
|
+
nothing while the map is detached or destroyed, and use no animation under `prefers-reduced-motion`.
|
|
397
|
+
|
|
398
|
+
```ts
|
|
399
|
+
map.flyTo({ center: [-119.7, 36.7], zoom: 16 });
|
|
400
|
+
map.easeTo({ center: monitor.lngLat, minZoom: 12 });
|
|
401
|
+
```
|
|
402
|
+
|
|
403
|
+
## Ready, and reaching the GL map
|
|
404
|
+
|
|
405
|
+
`map.ready` is reactive: true once the attached map's style has loaded and the map has gone idle
|
|
406
|
+
for the first time, false again on detach and while a basemap switch reloads the style. Show a load
|
|
407
|
+
screen until it is true. `map.gl` is the attached GL map (or `null`), reactive.
|
|
408
|
+
|
|
409
|
+
`maptilerMap(map)` returns it as a MapTiler `Map` (or `null` while detached). It is an escape hatch
|
|
410
|
+
for markers and popups only: never add sources or layers through it, because plugins own those.
|
|
411
|
+
|
|
412
|
+
```ts
|
|
413
|
+
import { maptilerMap } from "@sjvair/map-sdk";
|
|
414
|
+
|
|
415
|
+
const gl = maptilerMap(map);
|
|
416
|
+
if (gl) new Marker().setLngLat(lngLat).addTo(gl);
|
|
417
|
+
```
|
|
418
|
+
|
|
419
|
+
## Providers
|
|
420
|
+
|
|
421
|
+
`createMap` takes three optional providers, so a host controls where its labels and metadata come
|
|
422
|
+
from:
|
|
423
|
+
|
|
424
|
+
- `metadata`: scales, entry-type labels and units, monitor type labels, and dataset attribution.
|
|
425
|
+
The default reads the SJVAir API through `@sjvair/sdk`. `monitorTypeInterval(type)` is an
|
|
426
|
+
optional method returning how often a monitor type reports (e.g. `"2 min"`); the monitor
|
|
427
|
+
tooltip shows "(2 min avg)" under the entry-type label when it exists, and omits the note when a
|
|
428
|
+
host's provider lacks it.
|
|
429
|
+
- `messages`: `(key, params) => string`. The default is the English catalog,
|
|
430
|
+
`@sjvair/map-sdk/messages/en.json`, with `{name}`-style parameters and `_one`/`_other` plurals
|
|
431
|
+
selected by `count`.
|
|
432
|
+
- `format`: numbers, date-times in America/Los_Angeles, a "PT" zone label when the device is
|
|
433
|
+
elsewhere, and `relativeTime(date, now?)` ("5 minutes ago").
|
|
434
|
+
|
|
435
|
+
Every UI string is a `mapsdk_*` key. To translate them in a Paraglide project, add
|
|
436
|
+
`node_modules/@sjvair/map-sdk/dist/data/messages/en.json` (exported as
|
|
437
|
+
`@sjvair/map-sdk/messages/en.json`) to the project's message sources, translate the keys, and
|
|
438
|
+
adapt the generated functions:
|
|
439
|
+
|
|
440
|
+
```ts
|
|
441
|
+
import * as m from "$lib/paraglide/messages.js";
|
|
442
|
+
import type { MessageKey, MessageParams } from "@sjvair/map-sdk/data";
|
|
443
|
+
|
|
444
|
+
const messages = (key: MessageKey, params?: MessageParams) =>
|
|
445
|
+
(m as unknown as Record<string, (p?: MessageParams) => string>)[key]?.(params) ?? key;
|
|
446
|
+
|
|
447
|
+
const map = createMap({ config: { maptilerKey: "…" }, messages });
|
|
448
|
+
```
|
|
449
|
+
|
|
450
|
+
A key that is a plural base (for example `mapsdk_selected_count`) arrives with `params.count`;
|
|
451
|
+
the catalog stores it as `<key>_one` and `<key>_other`, so a provider over a different catalog
|
|
452
|
+
maps it to its own plural form. Layer names (`layerLabel`) are passed already translated.
|
|
453
|
+
|
|
454
|
+
## Examples are checked
|
|
455
|
+
|
|
456
|
+
`tests/unit/readme-examples.test.ts` repeats the TypeScript examples above (the map setup, plain
|
|
457
|
+
GeoJSON, writing a plugin, selection, and a message provider) against the real exports: `npm run
|
|
458
|
+
check` type-checks them and `npm test` runs them. The Svelte markup isn't mounted there. Update
|
|
459
|
+
it with the README.
|
|
460
|
+
|
|
461
|
+
## Demo and the local stack
|
|
462
|
+
|
|
463
|
+
`npm run dev` serves the demo page (`demo/`): two maps on one page, one fed from date-range
|
|
464
|
+
sources like the dashboard and one from the live monitors store, with the presets, selection
|
|
465
|
+
modes, and theme toggles. It calls the API on its own origin, and Vite proxies `/api/` to the
|
|
466
|
+
local sjvair.com stack at `http://localhost:8000`.
|
|
467
|
+
|
|
468
|
+
1. Copy `.env.example` to `.env` and set `VITE_MAPTILER_KEY`. Never commit `.env`.
|
|
469
|
+
2. Start the sjvair.com stack on port 8000.
|
|
470
|
+
3. A fresh database has no calibrations; load them once:
|
|
471
|
+
|
|
472
|
+
```bash
|
|
473
|
+
podman exec sjvaircom_web_1 python manage.py loaddata default-calibrations.yaml
|
|
474
|
+
```
|
|
475
|
+
|
|
476
|
+
## Development
|
|
477
|
+
|
|
478
|
+
```bash
|
|
479
|
+
npm run lint # Prettier check + ESLint
|
|
480
|
+
npm run check # svelte-check
|
|
481
|
+
npm test # unit, client, and browser (Vitest) projects
|
|
482
|
+
npm run test:bundle # build, then check the light entries stay off the map engine
|
|
483
|
+
npm run test:licenses # build, then check bundled dependencies' licenses
|
|
484
|
+
npm run test:e2e # Playwright on the demo (Chromium, Firefox, WebKit)
|
|
485
|
+
npm run build # svelte-package into dist/
|
|
486
|
+
```
|
|
487
|
+
|
|
488
|
+
See `CLAUDE.md` for the architecture.
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { GlMapLike } from "./gl.js";
|
|
2
|
+
export interface BasemapConfig {
|
|
3
|
+
styles: Array<{
|
|
4
|
+
id: string;
|
|
5
|
+
style: unknown;
|
|
6
|
+
}>;
|
|
7
|
+
defaultId: string;
|
|
8
|
+
excludedLayers?: string[];
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* The map's current basemap, and switching it on a GL map.
|
|
12
|
+
*
|
|
13
|
+
* The GL map is created with the style of `current` (the map's creator does that), so `attach`
|
|
14
|
+
* calls `setStyle` for nothing. Later `select` calls load the new style. A newer `select`
|
|
15
|
+
* supersedes one still loading. If the style document fails, the map goes back to the last style
|
|
16
|
+
* that loaded and `current` reverts; if that fails too, the map stays unstyled until the next
|
|
17
|
+
* `select`. Right before each `setStyle`, `onStyleChange` is called (it tears the plugins down,
|
|
18
|
+
* so nothing touches the old style while the new one loads). After each successful load,
|
|
19
|
+
* `excludedLayers` are removed and `onStyleReady` is called (it sets the plugins up again). The
|
|
20
|
+
* map's first style is not announced: the plugin host already waits for it.
|
|
21
|
+
*/
|
|
22
|
+
export declare class BasemapState {
|
|
23
|
+
#private;
|
|
24
|
+
constructor(config: BasemapConfig | undefined);
|
|
25
|
+
/** The selected basemap id. Reactive. */
|
|
26
|
+
get current(): string;
|
|
27
|
+
/** Unknown ids are ignored. While no map is attached, only `current` changes. */
|
|
28
|
+
select(id: string): void;
|
|
29
|
+
/**
|
|
30
|
+
* For MapView only (through the map context's `attach`): uses `gl` from now on, which must
|
|
31
|
+
* already show (or be loading) the style of `current`. `onStyleChange` runs right before each
|
|
32
|
+
* `setStyle`, `onStyleReady` once that style has loaded. Reads `current` untracked, so calling
|
|
33
|
+
* this in an effect doesn't make it depend on it.
|
|
34
|
+
*/
|
|
35
|
+
attach(gl: GlMapLike, onStyleReady: () => void, onStyleChange?: () => void): void;
|
|
36
|
+
/** For MapView only (through the map context's `detach`): stops using the GL map. */
|
|
37
|
+
detach(): void;
|
|
38
|
+
}
|