@mapcairn/map 0.1.0-rc.2 → 0.1.0-rc.3
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/README.md
CHANGED
|
@@ -6,6 +6,8 @@ It never imports a map engine, an adapter or a browser service. Everything it ne
|
|
|
6
6
|
|
|
7
7
|
**Status:** a release candidate, for early adopters. The API is still settling, so a breaking change can land before `0.1.0`; the [changelog](https://github.com/MapCairn/mapcairn/blob/main/CHANGELOG.md) lists each one. Until `0.1.0`, add `@next` to every package name in the install commands below.
|
|
8
8
|
|
|
9
|
+
If you would rather install and import the whole SDK once, [`@mapcairn/sdk`](https://github.com/MapCairn/mapcairn/tree/main/packages/sdk) re-exports this package with the others.
|
|
10
|
+
|
|
9
11
|
## Install
|
|
10
12
|
|
|
11
13
|
```bash
|
|
@@ -31,7 +33,7 @@ The JavaScript imports no CSS, so the components render on the server as plain m
|
|
|
31
33
|
|
|
32
34
|
- `MapChrome` fills its container, so give `html`, `body` and the element you render into a height.
|
|
33
35
|
- The tokens name the IBM Plex Sans Variable font, which your app loads (for example from `@fontsource-variable/ibm-plex-sans`). Without it the type falls back to the system font.
|
|
34
|
-
- The map engine and its styles. The package never imports one; your app supplies a `MapAdapter` over MapLibre GL JS in the `Services` bundle. The
|
|
36
|
+
- The map engine and its styles. The package never imports one; your app supplies a `MapAdapter` over MapLibre GL JS in the `Services` bundle. The MapLibre adapter is [`@mapcairn/adapter-maplibre`](https://github.com/MapCairn/mapcairn/tree/main/packages/adapter-maplibre), which `@mapcairn/sdk` re-exports at `@mapcairn/sdk/adapter-maplibre`.
|
|
35
37
|
|
|
36
38
|
The styles of the primitives (buttons, chips, surfaces and list rows) sit in a cascade layer, so an unlayered rule in your own CSS, such as `button { background: transparent; border: 0; padding: 0 }` or `:focus-visible { outline: none }`, beats them. Put your app's base styles and resets in an `@layer` that is declared before `@mapcairn/map/styles.css` is imported, and they rank below the primitives. This is a known trade-off for 0.1.0; see [Overriding a primitive](https://github.com/MapCairn/mapcairn/blob/main/DESIGN.md#overriding-a-primitive).
|
|
37
39
|
|
|
@@ -39,7 +41,7 @@ The generated names of the CSS custom properties in `tokens.css` are private and
|
|
|
39
41
|
|
|
40
42
|
## Usage
|
|
41
43
|
|
|
42
|
-
Build the services once, wrap the tree, then render the map with its chrome. This is a whole app. It runs on the SDK's in-memory fakes, so it requests nothing and draws no map tiles; a real map swaps in
|
|
44
|
+
Build the services once, wrap the tree, then render the map with its chrome. This is a whole app. It runs on the SDK's in-memory fakes, so it requests nothing and draws no map tiles; a real map swaps in the MapLibre adapter, as the [adapter's README](https://github.com/MapCairn/mapcairn/tree/main/packages/adapter-maplibre) shows.
|
|
43
45
|
|
|
44
46
|
```tsx
|
|
45
47
|
import { createFakeServices } from "@mapcairn/core";
|
package/dist/index.d.ts
CHANGED
|
@@ -1085,6 +1085,9 @@ export declare interface ExportSectionProps {
|
|
|
1085
1085
|
|
|
1086
1086
|
/**
|
|
1087
1087
|
* The box around a feature's geometry, or null when it has no coordinates.
|
|
1088
|
+
* It holds every vertex and every edge taken the short way round, so a polygon
|
|
1089
|
+
* wider than half the globe is fitted on the side it covers. Across the
|
|
1090
|
+
* antimeridian `east` passes 180 (see `boundsOfPaths`).
|
|
1088
1091
|
*
|
|
1089
1092
|
* @internal
|
|
1090
1093
|
*/
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { feedUrls, isQueryLayer, layerZoomLimit, loadsFeatures, nextQueryWindow } from "@mapcairn/core";
|
|
2
2
|
// src/layers/loadRequests.ts
|
|
3
3
|
/** Whether a query layer is too far out for the view to ask for features. */
|
|
4
4
|
function isTooFarOut(definition, view) {
|
|
@@ -28,7 +28,7 @@ function wantedLoads(inputs) {
|
|
|
28
28
|
if (!isQueryLayer(definition)) return [{
|
|
29
29
|
id,
|
|
30
30
|
definition,
|
|
31
|
-
key: `${
|
|
31
|
+
key: `${feedUrls(definition, controls, now).join(" ")}#${retries}`,
|
|
32
32
|
controls
|
|
33
33
|
}];
|
|
34
34
|
if (!view || isTooFarOut(definition, view)) return [];
|
|
@@ -36,7 +36,7 @@ function wantedLoads(inputs) {
|
|
|
36
36
|
return [{
|
|
37
37
|
id,
|
|
38
38
|
definition,
|
|
39
|
-
key: `${
|
|
39
|
+
key: `${feedUrls(definition, controls, now, area).join(" ")}#${retries}`,
|
|
40
40
|
controls,
|
|
41
41
|
area
|
|
42
42
|
}];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"loadRequests.js","names":[],"sources":["../../src/layers/loadRequests.ts"],"sourcesContent":["// SPDX-License-Identifier: AGPL-3.0-only\n// Copyright MapCairn contributors\nimport {\n type Bounds,\n type ControlValues,\n type FeedLayerDefinition,\n
|
|
1
|
+
{"version":3,"file":"loadRequests.js","names":[],"sources":["../../src/layers/loadRequests.ts"],"sourcesContent":["// SPDX-License-Identifier: AGPL-3.0-only\n// Copyright MapCairn contributors\nimport {\n type Bounds,\n type ControlValues,\n type FeedLayerDefinition,\n feedUrls,\n type HeatmapLayerDefinition,\n isQueryLayer,\n type LayerDefinition,\n layerZoomLimit,\n loadsFeatures,\n nextQueryWindow,\n type ZoomLimit,\n} from \"@mapcairn/core\";\n\n/** What the map last showed, which a query layer asks for features around. */\nexport interface QueryView {\n readonly area: Bounds;\n /** The normalised zoom. */\n readonly zoom: number;\n}\n\n/** A layer whose features are loaded, from a feed or a query. */\nexport type LoadableLayer = FeedLayerDefinition | HeatmapLayerDefinition;\n\n/** One load a layer asks for, and the key that says when it has changed. */\nexport interface WantedLoad {\n readonly id: string;\n readonly definition: LoadableLayer;\n /** The request's URL and the layer's retry count: a new key is a new load. */\n readonly key: string;\n /** Every control's value, resolved against the definition. */\n readonly controls: ControlValues;\n /** The window a query layer asks for. Absent for a feed. */\n readonly area?: Bounds;\n}\n\n/** What {@link wantedLoads} reads to work out what the enabled layers ask for. */\nexport interface LoadInputs {\n /** The enabled layers' ids. */\n readonly enabled: readonly string[];\n readonly definitions: ReadonlyMap<string, LayerDefinition>;\n readonly controls: Readonly<Record<string, ControlValues>>;\n readonly retries: Readonly<Record<string, number>>;\n readonly view: QueryView | null;\n /** The window each query layer last asked for, kept while the view stays inside it. */\n readonly windows: ReadonlyMap<string, Bounds>;\n /** The clock, in epoch milliseconds. */\n readonly now: number;\n}\n\n/** What {@link planLoads} decides to do about the loads the layers want. */\nexport interface LoadPlan {\n /** Layers whose request in flight is for something that is no longer wanted. */\n readonly cancel: readonly string[];\n /** Layers whose settled request is for something that is no longer wanted. */\n readonly forget: readonly string[];\n /** Layers with no request in flight or settled for what they want now. */\n readonly start: readonly WantedLoad[];\n}\n\n/** Whether a query layer is too far out for the view to ask for features. */\nexport function isTooFarOut(\n definition: LayerDefinition,\n view: QueryView | null,\n): boolean {\n return isQueryLayer(definition) && viewZoomLimit(definition, view) !== null;\n}\n\n/**\n * Which way to zoom, and to which level, for a layer to show what the view is\n * at, or null when the view is inside its zoom window, it has none or the map\n * has not said where it is.\n */\nexport function viewZoomLimit(\n definition: LayerDefinition,\n view: QueryView | null,\n): ZoomLimit | null {\n return view === null ? null : layerZoomLimit(definition, view.zoom);\n}\n\n/**\n * What each enabled layer that loads features asks for. A query layer asks\n * for a window around the map's view, kept while the view stays inside it,\n * and for nothing until the map has said where it is or while it is too far\n * out. Pure: the window it chose is remembered with {@link rememberWindows}.\n */\nexport function wantedLoads(inputs: LoadInputs): WantedLoad[] {\n const { definitions, view, windows, now } = inputs;\n return inputs.enabled.flatMap((id): WantedLoad[] => {\n const definition = definitions.get(id);\n if (!definition || !loadsFeatures(definition)) {\n return [];\n }\n const controls = inputs.controls[id] ?? {};\n const retries = inputs.retries[id] ?? 0;\n if (!isQueryLayer(definition)) {\n const key = `${feedUrls(definition, controls, now).join(\" \")}#${retries}`;\n return [{ id, definition, key, controls }];\n }\n if (!view || isTooFarOut(definition, view)) {\n return [];\n }\n const area = nextQueryWindow(windows.get(id) ?? null, view.area);\n const key = `${feedUrls(definition, controls, now, area).join(\" \")}#${retries}`;\n return [{ id, definition, key, controls, area }];\n });\n}\n\n/**\n * Keeps the window of each query layer that asks for one, and forgets the\n * window of any layer that no longer does (too far out, or off), so it asks\n * afresh when it next has to.\n */\nexport function rememberWindows(\n windows: Map<string, Bounds>,\n wanted: readonly WantedLoad[],\n): void {\n windows.clear();\n for (const { id, area } of wanted) {\n if (area) {\n windows.set(id, area);\n }\n }\n}\n\n/**\n * Works out what has to change for the layers to have what they want. A\n * request is cancelled, or a settled one forgotten, when its key is no\n * longer wanted (including a layer that is no longer wanted at all). A load\n * starts for each wanted request that is neither in flight nor settled, so\n * a layer that has settled is not loaded again because another was turned\n * on or off.\n */\nexport function planLoads(\n wanted: readonly WantedLoad[],\n inFlight: ReadonlyMap<string, { readonly key: string }>,\n settled: ReadonlyMap<string, string>,\n): LoadPlan {\n const wantedKeys = new Map(wanted.map((load) => [load.id, load.key]));\n const isStale = (id: string, key: string) => wantedKeys.get(id) !== key;\n return {\n cancel: [...inFlight]\n .filter(([id, request]) => isStale(id, request.key))\n .map(([id]) => id),\n forget: [...settled]\n .filter(([id, key]) => isStale(id, key))\n .map(([id]) => id),\n start: wanted.filter(\n (load) =>\n inFlight.get(load.id)?.key !== load.key &&\n settled.get(load.id) !== load.key,\n ),\n };\n}\n"],"mappings":";;;AA+DA,SAAgB,YACd,YACA,MACS;CACT,OAAO,aAAa,UAAU,KAAK,cAAc,YAAY,IAAI,MAAM;AACzE;;;;;;AAOA,SAAgB,cACd,YACA,MACkB;CAClB,OAAO,SAAS,OAAO,OAAO,eAAe,YAAY,KAAK,IAAI;AACpE;;;;;;;AAQA,SAAgB,YAAY,QAAkC;CAC5D,MAAM,EAAE,aAAa,MAAM,SAAS,QAAQ;CAC5C,OAAO,OAAO,QAAQ,SAAS,OAAqB;EAClD,MAAM,aAAa,YAAY,IAAI,EAAE;EACrC,IAAI,CAAC,cAAc,CAAC,cAAc,UAAU,GAC1C,OAAO,CAAC;EAEV,MAAM,WAAW,OAAO,SAAS,OAA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
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { boundsOfPaths, evaluatePointStyle, formatTemplate, heatmapOpacity, heatmapSettings, shapeAppearances, shapeEvaluator, tileUrl, wrapLongitude } from "@mapcairn/core";
|
|
2
2
|
import { tokenValues } from "@mapcairn/theme";
|
|
3
3
|
// src/layers/mapFeatures.ts
|
|
4
4
|
var PALETTE = tokenValues.dataPalette;
|
|
@@ -14,26 +14,21 @@ function paletteColour(slot) {
|
|
|
14
14
|
if (colour === void 0) throw new Error(`The data palette has no colour "${slot}".`);
|
|
15
15
|
return colour;
|
|
16
16
|
}
|
|
17
|
-
|
|
18
|
-
|
|
17
|
+
var toPath = (line) => line.map(([lng, lat]) => ({
|
|
18
|
+
lat,
|
|
19
|
+
lng
|
|
20
|
+
}));
|
|
21
|
+
/** The rings and lines of a geometry, each as its own path (a point is a path of one). */
|
|
22
|
+
function pathsOf(geometry) {
|
|
19
23
|
switch (geometry.type) {
|
|
20
|
-
case "Point": return [{
|
|
24
|
+
case "Point": return [[{
|
|
21
25
|
lng: geometry.coordinates[0],
|
|
22
26
|
lat: geometry.coordinates[1]
|
|
23
|
-
}];
|
|
24
|
-
case "LineString": return geometry.coordinates
|
|
25
|
-
lat,
|
|
26
|
-
lng
|
|
27
|
-
}));
|
|
27
|
+
}]];
|
|
28
|
+
case "LineString": return [toPath(geometry.coordinates)];
|
|
28
29
|
case "MultiLineString":
|
|
29
|
-
case "Polygon": return geometry.coordinates.
|
|
30
|
-
|
|
31
|
-
lng
|
|
32
|
-
})));
|
|
33
|
-
case "MultiPolygon": return geometry.coordinates.flatMap((rings) => rings.flatMap((ring) => ring.map(([lng, lat]) => ({
|
|
34
|
-
lat,
|
|
35
|
-
lng
|
|
36
|
-
}))));
|
|
30
|
+
case "Polygon": return geometry.coordinates.map(toPath);
|
|
31
|
+
case "MultiPolygon": return geometry.coordinates.flatMap((rings) => rings.map(toPath));
|
|
37
32
|
}
|
|
38
33
|
}
|
|
39
34
|
/** The most vertices one ring or line contributes to an outline drawn over the map. */
|
|
@@ -79,12 +74,14 @@ function featureOutlines(feature) {
|
|
|
79
74
|
}
|
|
80
75
|
/**
|
|
81
76
|
* The box around a feature's geometry, or null when it has no coordinates.
|
|
77
|
+
* It holds every vertex and every edge taken the short way round, so a polygon
|
|
78
|
+
* wider than half the globe is fitted on the side it covers. Across the
|
|
79
|
+
* antimeridian `east` passes 180 (see `boundsOfPaths`).
|
|
82
80
|
*
|
|
83
81
|
* @internal
|
|
84
82
|
*/
|
|
85
83
|
function featureBounds(feature) {
|
|
86
|
-
|
|
87
|
-
return boundsOfPoints(positions) ?? null;
|
|
84
|
+
return boundsOfPaths(pathsOf(feature.geometry)) ?? null;
|
|
88
85
|
}
|
|
89
86
|
/**
|
|
90
87
|
* Where a feature is: a point's own position, and for a polygon or line the
|
|
@@ -96,7 +93,7 @@ function featurePosition(feature) {
|
|
|
96
93
|
const bounds = featureBounds(feature);
|
|
97
94
|
return bounds ? {
|
|
98
95
|
lat: (bounds.south + bounds.north) / 2,
|
|
99
|
-
lng: (bounds.west + bounds.east) / 2
|
|
96
|
+
lng: wrapLongitude((bounds.west + bounds.east) / 2)
|
|
100
97
|
} : null;
|
|
101
98
|
}
|
|
102
99
|
function toMapLook(look) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mapFeatures.js","names":[],"sources":["../../src/layers/mapFeatures.ts"],"sourcesContent":["// SPDX-License-Identifier: AGPL-3.0-only\n// Copyright MapCairn contributors\nimport {\n type Bounds,\n boundsOfPoints,\n type ControlValues,\n evaluatePointStyle,\n type FeedLayerDefinition,\n type FeedShapeStyle,\n type FormatterRegistry,\n formatTemplate,\n type HeatmapLayerDefinition,\n type HeatmapSpec,\n heatmapOpacity,\n heatmapSettings,\n type LatLng,\n type LayerDefinition,\n type LayerFeature,\n type LayerGeometry,\n type MapFeature,\n type MapHeatmap,\n type MapHeatmapStyle,\n type MapLookRule,\n type MapShapeLook,\n type MapStyledShape,\n type MapStyledShapes,\n type MapVectorSourceLayer,\n type MapVectorTiles,\n type MessageCatalogue,\n type PaletteSlot,\n type PointStyle,\n type ShapeAppearance,\n shapeAppearances,\n shapeEvaluator,\n tileUrl,\n type VectorTileLayerDefinition,\n} from \"@mapcairn/core\";\nimport { tokenValues } from \"@mapcairn/theme\";\n\nconst PALETTE: Readonly<Record<string, string>> = tokenValues.dataPalette;\n\n/**\n * The hex value of a palette slot. Definitions name slots, `@mapcairn/theme`\n * holds the values, and the definition schema only accepts slots that exist,\n * so a missing value is a wiring bug.\n *\n * @internal\n */\nexport function paletteColour(slot: PaletteSlot): string {\n const colour = PALETTE[slot];\n if (colour === undefined) {\n throw new Error(`The data palette has no colour \"${slot}\".`);\n }\n return colour;\n}\n\n/** Every position of a geometry, in any nesting. */\nfunction positionsOf(geometry: LayerGeometry): LatLng[] {\n switch (geometry.type) {\n case \"Point\":\n return [{ lng: geometry.coordinates[0], lat: geometry.coordinates[1] }];\n case \"LineString\":\n return geometry.coordinates.map(([lng, lat]) => ({ lat, lng }));\n case \"MultiLineString\":\n case \"Polygon\":\n return geometry.coordinates.flatMap((line) =>\n line.map(([lng, lat]) => ({ lat, lng })),\n );\n case \"MultiPolygon\":\n return geometry.coordinates.flatMap((rings) =>\n rings.flatMap((ring) => ring.map(([lng, lat]) => ({ lat, lng }))),\n );\n }\n}\n\n/** The most vertices one ring or line contributes to an outline drawn over the map. */\nexport const MAX_OUTLINE_POINTS = 400;\n\n/** One ring or line of a feature, ready to draw as a path. */\nexport interface FeatureOutline {\n readonly points: readonly LatLng[];\n /** True for a polygon ring, whose path closes. */\n readonly closed: boolean;\n}\n\n/** Keeps at most `limit` evenly spread positions, always the first and the last. */\nfunction thin(positions: readonly LatLng[], limit: number): LatLng[] {\n if (positions.length <= limit) {\n return [...positions];\n }\n const step = (positions.length - 1) / (limit - 1);\n return Array.from(\n { length: limit },\n (_, index) => positions[Math.round(index * step)] as LatLng,\n );\n}\n\n/**\n * The rings and lines of a polygon or line feature, thinned so an outline\n * drawn from them over the map stays cheap to redraw while the map moves. A\n * point has none.\n *\n * @internal\n */\nexport function featureOutlines(feature: LayerFeature): FeatureOutline[] {\n const { geometry } = feature;\n const toPoints = (line: readonly (readonly [number, number])[]) =>\n thin(\n line.map(([lng, lat]) => ({ lat, lng })),\n MAX_OUTLINE_POINTS,\n );\n switch (geometry.type) {\n case \"Point\":\n return [];\n case \"LineString\":\n return [{ points: toPoints(geometry.coordinates), closed: false }];\n case \"MultiLineString\":\n return geometry.coordinates.map((line) => ({\n points: toPoints(line),\n closed: false,\n }));\n case \"Polygon\":\n return geometry.coordinates.map((ring) => ({\n points: toPoints(ring),\n closed: true,\n }));\n case \"MultiPolygon\":\n return geometry.coordinates.flatMap((rings) =>\n rings.map((ring) => ({ points: toPoints(ring), closed: true })),\n );\n }\n}\n\n/**\n * The box around a feature's geometry, or null when it has no coordinates.\n *\n * @internal\n */\nexport function featureBounds(feature: LayerFeature): Bounds | null {\n const positions = positionsOf(feature.geometry);\n return boundsOfPoints(positions) ?? null;\n}\n\n/**\n * Where a feature is: a point's own position, and for a polygon or line the\n * centre of its box. Null when the geometry has no coordinates.\n *\n * @internal\n */\nexport function featurePosition(feature: LayerFeature): LatLng | null {\n const bounds = featureBounds(feature);\n return bounds\n ? {\n lat: (bounds.south + bounds.north) / 2,\n lng: (bounds.west + bounds.east) / 2,\n }\n : null;\n}\n\nfunction toMapLook(look: ShapeAppearance): MapShapeLook {\n return {\n colour: paletteColour(look.colour),\n fillOpacity: look.fillOpacity,\n pattern: look.pattern,\n dash: look.dash,\n width: look.width,\n };\n}\n\n/**\n * Turns a shape style's colour rule into the rule a vector tile engine\n * evaluates for each feature: categories become cases with the other class\n * as the fallback, and steps and equal intervals become ascending minimums.\n *\n * @internal\n */\nexport function toLookRule(style: FeedShapeStyle): MapLookRule {\n const classes = shapeAppearances(style).map(toMapLook);\n const at = (index: number) => classes[index] as MapShapeLook;\n const { colour } = style;\n switch (colour.kind) {\n case \"fixed\":\n return { kind: \"fixed\", look: at(0) };\n case \"category\":\n return {\n kind: \"category\",\n field: colour.field,\n cases: colour.categories.map((category, index) => ({\n value: category.value,\n look: at(index),\n })),\n fallback: at(colour.categories.length),\n };\n case \"steps\":\n return {\n kind: \"steps\",\n field: colour.field,\n steps: colour.steps.map((step, index) => ({\n min: step.min,\n look: at(index),\n })),\n };\n case \"intervals\": {\n const width = (colour.max - colour.min) / colour.ramp.length;\n return {\n kind: \"steps\",\n field: colour.field,\n steps: colour.ramp.map((_, index) => ({\n min: colour.min + index * width,\n look: at(index),\n })),\n };\n }\n }\n}\n\n/** The engine's source for a definition's vector tiles, with the tile URL filled from the controls. */\nfunction toMapSource(\n definition: VectorTileLayerDefinition,\n controls: ControlValues,\n now: number,\n) {\n return {\n tiles: tileUrl(definition, controls, now),\n minzoom: definition.source.minzoom ?? 0,\n maxzoom: definition.source.maxzoom,\n };\n}\n\n/**\n * The vector tile layer for the polygon and line source layers of a\n * definition, with each rule, window and label worked out. Heatmap source\n * layers are drawn separately (see {@link toVectorHeatmaps}).\n *\n * @internal\n */\nexport function toMapVector(\n definition: VectorTileLayerDefinition,\n controls: ControlValues,\n now: number,\n): MapVectorTiles {\n return {\n source: toMapSource(definition, controls, now),\n layers: definition.sourceLayers.flatMap((layer): MapVectorSourceLayer[] => {\n if (\"heatmap\" in layer) {\n return [];\n }\n const { style } = layer;\n return [\n {\n sourceLayer: layer.sourceLayer,\n geometry: style.geometry,\n rule: toLookRule(style),\n ...(layer.minzoom !== undefined ? { minzoom: layer.minzoom } : {}),\n ...(layer.maxzoom !== undefined ? { maxzoom: layer.maxzoom } : {}),\n ...(style.label\n ? {\n label: {\n field: style.label.template.slice(1, -1),\n ...(style.label.minzoom !== undefined\n ? { minzoom: style.label.minzoom }\n : {}),\n },\n }\n : {}),\n },\n ];\n }),\n ...(definition.minzoom !== undefined\n ? { minzoom: definition.minzoom }\n : {}),\n ...(definition.maxzoom !== undefined\n ? { maxzoom: definition.maxzoom }\n : {}),\n };\n}\n\nfunction heatmapStyle(\n definition: LayerDefinition,\n spec: HeatmapSpec,\n controls: ControlValues,\n): MapHeatmapStyle {\n return {\n ...heatmapSettings(spec, controls),\n opacity: heatmapOpacity(definition, controls),\n ramp: spec.ramp.map(paletteColour),\n };\n}\n\n/**\n * A heatmap of a feed or query layer's points, weighted by its weight field.\n *\n * @internal\n */\nexport function toMapHeatmap(\n definition: HeatmapLayerDefinition,\n features: readonly LayerFeature[],\n controls: ControlValues,\n): MapHeatmap {\n const { weightField } = definition.heatmap;\n return {\n source: {\n kind: \"points\",\n points: features.flatMap((feature) => {\n if (feature.geometry.type !== \"Point\") {\n return [];\n }\n const raw = weightField ? feature.fields[weightField] : null;\n const [lng, lat] = feature.geometry.coordinates;\n return [\n {\n position: { lat, lng },\n weight: typeof raw === \"number\" && Number.isFinite(raw) ? raw : 1,\n },\n ];\n }),\n },\n style: heatmapStyle(definition, definition.heatmap, controls),\n };\n}\n\n/**\n * One heatmap for each heatmap source layer of a vector tile definition, in order.\n *\n * @internal\n */\nexport function toVectorHeatmaps(\n definition: VectorTileLayerDefinition,\n controls: ControlValues,\n now: number,\n): MapHeatmap[] {\n return definition.sourceLayers.flatMap((layer): MapHeatmap[] => {\n if (!(\"heatmap\" in layer)) {\n return [];\n }\n return [\n {\n source: {\n kind: \"vector\",\n source: toMapSource(definition, controls, now),\n sourceLayer: layer.sourceLayer,\n ...(layer.heatmap.weightField\n ? { weightField: layer.heatmap.weightField }\n : {}),\n ...(layer.minzoom !== undefined ? { minzoom: layer.minzoom } : {}),\n ...(layer.maxzoom !== undefined ? { maxzoom: layer.maxzoom } : {}),\n },\n style: heatmapStyle(definition, layer.heatmap, controls),\n },\n ];\n });\n}\n\n/**\n * Works out how each polygon or line of a feed layer is drawn, with its label\n * text, so the map adapter gets plain colours, widths and patterns and needs\n * no style rule. Features of the geometry the style does not draw are skipped.\n *\n * @internal\n */\nexport function toStyledShapes(\n definition: FeedLayerDefinition<FeedShapeStyle>,\n features: readonly LayerFeature[],\n formatters: FormatterRegistry,\n now: number,\n catalogue: MessageCatalogue,\n): MapStyledShapes {\n const { style } = definition;\n const evaluate = shapeEvaluator(style, catalogue);\n const drawn =\n style.geometry === \"area\"\n ? [\"Polygon\", \"MultiPolygon\"]\n : [\"LineString\", \"MultiLineString\"];\n const shapes = features.flatMap((feature): MapStyledShape[] => {\n if (!drawn.includes(feature.geometry.type)) {\n return [];\n }\n const look = evaluate(feature.fields);\n const label = style.label\n ? formatTemplate(style.label.template, feature.fields, formatters, {\n now,\n catalogue,\n })\n : null;\n return [\n {\n id: feature.id,\n geometry: feature.geometry,\n look: {\n colour: paletteColour(look.colour),\n fillOpacity: look.fillOpacity,\n pattern: look.pattern,\n dash: look.dash,\n width: look.width,\n },\n ...(label ? { label } : {}),\n },\n ];\n });\n return {\n geometry: style.geometry,\n shapes,\n ...(style.label?.minzoom !== undefined\n ? { labelMinZoom: style.label.minzoom }\n : {}),\n };\n}\n\n/**\n * Works out how each point of a feed layer is drawn, so the map adapter gets\n * plain colours, sizes and shapes and needs no style rule.\n *\n * @internal\n */\nexport function toMapFeatures(\n style: PointStyle,\n features: readonly LayerFeature[],\n): MapFeature[] {\n return features.flatMap((feature) => {\n const position =\n feature.geometry.type === \"Point\" ? featurePosition(feature) : null;\n if (!position) {\n return [];\n }\n const marker = evaluatePointStyle(style, feature.fields);\n return [\n {\n id: feature.id,\n position,\n colour: paletteColour(marker.colour),\n radius: marker.radius,\n shape: marker.shape,\n },\n ];\n });\n}\n"],"mappings":";;;AAuCA,IAAM,UAA4C,YAAY;;;;;;;;AAS9D,SAAgB,cAAc,MAA2B;CACvD,MAAM,SAAS,QAAQ;CACvB,IAAI,WAAW,KAAA,GACb,MAAM,IAAI,MAAM,mCAAmC,KAAK,GAAG;CAE7D,OAAO;AACT;;AAGA,SAAS,YAAY,UAAmC;CACtD,QAAQ,SAAS,MAAjB;EACE,KAAK,SACH,OAAO,CAAC;GAAE,KAAK,SAAS,YAAY;GAAI,KAAK,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,QAAQ,YAAY;GAC/D;GACA;EACF,CAAC,IACD;EACJ,OAAO,CACL;GACE,IAAI,QAAQ;GACZ,UAAU,QAAQ;GAClB,MAAM;IACJ,QAAQ,cAAc,KAAK,MAAM;IACjC,aAAa,KAAK;IAClB,SAAS,KAAK;IACd,MAAM,KAAK;IACX,OAAO,KAAK;GACd;GACA,GAAI,QAAQ,EAAE,MAAM,IAAI,CAAC;EAC3B,CACF;CACF,CAAC;CACD,OAAO;EACL,UAAU,MAAM;EAChB;EACA,GAAI,MAAM,OAAO,YAAY,KAAA,IACzB,EAAE,cAAc,MAAM,MAAM,QAAQ,IACpC,CAAC;CACP;AACF;;;;;;;AAQA,SAAgB,cACd,OACA,UACc;CACd,OAAO,SAAS,SAAS,YAAY;EACnC,MAAM,WACJ,QAAQ,SAAS,SAAS,UAAU,gBAAgB,OAAO,IAAI;EACjE,IAAI,CAAC,UACH,OAAO,CAAC;EAEV,MAAM,SAAS,mBAAmB,OAAO,QAAQ,MAAM;EACvD,OAAO,CACL;GACE,IAAI,QAAQ;GACZ;GACA,QAAQ,cAAc,OAAO,MAAM;GACnC,QAAQ,OAAO;GACf,OAAO,OAAO;EAChB,CACF;CACF,CAAC;AACH"}
|
|
1
|
+
{"version":3,"file":"mapFeatures.js","names":[],"sources":["../../src/layers/mapFeatures.ts"],"sourcesContent":["// SPDX-License-Identifier: AGPL-3.0-only\n// Copyright MapCairn contributors\nimport {\n type Bounds,\n boundsOfPaths,\n type ControlValues,\n evaluatePointStyle,\n type FeedLayerDefinition,\n type FeedShapeStyle,\n type FormatterRegistry,\n formatTemplate,\n type HeatmapLayerDefinition,\n type HeatmapSpec,\n heatmapOpacity,\n heatmapSettings,\n type LatLng,\n type LayerDefinition,\n type LayerFeature,\n type LayerGeometry,\n type MapFeature,\n type MapHeatmap,\n type MapHeatmapStyle,\n type MapLookRule,\n type MapShapeLook,\n type MapStyledShape,\n type MapStyledShapes,\n type MapVectorSourceLayer,\n type MapVectorTiles,\n type MessageCatalogue,\n type PaletteSlot,\n type PointStyle,\n type ShapeAppearance,\n shapeAppearances,\n shapeEvaluator,\n tileUrl,\n type VectorTileLayerDefinition,\n wrapLongitude,\n} from \"@mapcairn/core\";\nimport { tokenValues } from \"@mapcairn/theme\";\n\nconst PALETTE: Readonly<Record<string, string>> = tokenValues.dataPalette;\n\n/**\n * The hex value of a palette slot. Definitions name slots, `@mapcairn/theme`\n * holds the values, and the definition schema only accepts slots that exist,\n * so a missing value is a wiring bug.\n *\n * @internal\n */\nexport function paletteColour(slot: PaletteSlot): string {\n const colour = PALETTE[slot];\n if (colour === undefined) {\n throw new Error(`The data palette has no colour \"${slot}\".`);\n }\n return colour;\n}\n\nconst toPath = (line: readonly (readonly [number, number])[]): LatLng[] =>\n line.map(([lng, lat]) => ({ lat, lng }));\n\n/** The rings and lines of a geometry, each as its own path (a point is a path of one). */\nfunction pathsOf(geometry: LayerGeometry): LatLng[][] {\n switch (geometry.type) {\n case \"Point\":\n return [[{ lng: geometry.coordinates[0], lat: geometry.coordinates[1] }]];\n case \"LineString\":\n return [toPath(geometry.coordinates)];\n case \"MultiLineString\":\n case \"Polygon\":\n return geometry.coordinates.map(toPath);\n case \"MultiPolygon\":\n return geometry.coordinates.flatMap((rings) => rings.map(toPath));\n }\n}\n\n/** The most vertices one ring or line contributes to an outline drawn over the map. */\nexport const MAX_OUTLINE_POINTS = 400;\n\n/** One ring or line of a feature, ready to draw as a path. */\nexport interface FeatureOutline {\n readonly points: readonly LatLng[];\n /** True for a polygon ring, whose path closes. */\n readonly closed: boolean;\n}\n\n/** Keeps at most `limit` evenly spread positions, always the first and the last. */\nfunction thin(positions: readonly LatLng[], limit: number): LatLng[] {\n if (positions.length <= limit) {\n return [...positions];\n }\n const step = (positions.length - 1) / (limit - 1);\n return Array.from(\n { length: limit },\n (_, index) => positions[Math.round(index * step)] as LatLng,\n );\n}\n\n/**\n * The rings and lines of a polygon or line feature, thinned so an outline\n * drawn from them over the map stays cheap to redraw while the map moves. A\n * point has none.\n *\n * @internal\n */\nexport function featureOutlines(feature: LayerFeature): FeatureOutline[] {\n const { geometry } = feature;\n const toPoints = (line: readonly (readonly [number, number])[]) =>\n thin(\n line.map(([lng, lat]) => ({ lat, lng })),\n MAX_OUTLINE_POINTS,\n );\n switch (geometry.type) {\n case \"Point\":\n return [];\n case \"LineString\":\n return [{ points: toPoints(geometry.coordinates), closed: false }];\n case \"MultiLineString\":\n return geometry.coordinates.map((line) => ({\n points: toPoints(line),\n closed: false,\n }));\n case \"Polygon\":\n return geometry.coordinates.map((ring) => ({\n points: toPoints(ring),\n closed: true,\n }));\n case \"MultiPolygon\":\n return geometry.coordinates.flatMap((rings) =>\n rings.map((ring) => ({ points: toPoints(ring), closed: true })),\n );\n }\n}\n\n/**\n * The box around a feature's geometry, or null when it has no coordinates.\n * It holds every vertex and every edge taken the short way round, so a polygon\n * wider than half the globe is fitted on the side it covers. Across the\n * antimeridian `east` passes 180 (see `boundsOfPaths`).\n *\n * @internal\n */\nexport function featureBounds(feature: LayerFeature): Bounds | null {\n return boundsOfPaths(pathsOf(feature.geometry)) ?? null;\n}\n\n/**\n * Where a feature is: a point's own position, and for a polygon or line the\n * centre of its box. Null when the geometry has no coordinates.\n *\n * @internal\n */\nexport function featurePosition(feature: LayerFeature): LatLng | null {\n const bounds = featureBounds(feature);\n return bounds\n ? {\n lat: (bounds.south + bounds.north) / 2,\n lng: wrapLongitude((bounds.west + bounds.east) / 2),\n }\n : null;\n}\n\nfunction toMapLook(look: ShapeAppearance): MapShapeLook {\n return {\n colour: paletteColour(look.colour),\n fillOpacity: look.fillOpacity,\n pattern: look.pattern,\n dash: look.dash,\n width: look.width,\n };\n}\n\n/**\n * Turns a shape style's colour rule into the rule a vector tile engine\n * evaluates for each feature: categories become cases with the other class\n * as the fallback, and steps and equal intervals become ascending minimums.\n *\n * @internal\n */\nexport function toLookRule(style: FeedShapeStyle): MapLookRule {\n const classes = shapeAppearances(style).map(toMapLook);\n const at = (index: number) => classes[index] as MapShapeLook;\n const { colour } = style;\n switch (colour.kind) {\n case \"fixed\":\n return { kind: \"fixed\", look: at(0) };\n case \"category\":\n return {\n kind: \"category\",\n field: colour.field,\n cases: colour.categories.map((category, index) => ({\n value: category.value,\n look: at(index),\n })),\n fallback: at(colour.categories.length),\n };\n case \"steps\":\n return {\n kind: \"steps\",\n field: colour.field,\n steps: colour.steps.map((step, index) => ({\n min: step.min,\n look: at(index),\n })),\n };\n case \"intervals\": {\n const width = (colour.max - colour.min) / colour.ramp.length;\n return {\n kind: \"steps\",\n field: colour.field,\n steps: colour.ramp.map((_, index) => ({\n min: colour.min + index * width,\n look: at(index),\n })),\n };\n }\n }\n}\n\n/** The engine's source for a definition's vector tiles, with the tile URL filled from the controls. */\nfunction toMapSource(\n definition: VectorTileLayerDefinition,\n controls: ControlValues,\n now: number,\n) {\n return {\n tiles: tileUrl(definition, controls, now),\n minzoom: definition.source.minzoom ?? 0,\n maxzoom: definition.source.maxzoom,\n };\n}\n\n/**\n * The vector tile layer for the polygon and line source layers of a\n * definition, with each rule, window and label worked out. Heatmap source\n * layers are drawn separately (see {@link toVectorHeatmaps}).\n *\n * @internal\n */\nexport function toMapVector(\n definition: VectorTileLayerDefinition,\n controls: ControlValues,\n now: number,\n): MapVectorTiles {\n return {\n source: toMapSource(definition, controls, now),\n layers: definition.sourceLayers.flatMap((layer): MapVectorSourceLayer[] => {\n if (\"heatmap\" in layer) {\n return [];\n }\n const { style } = layer;\n return [\n {\n sourceLayer: layer.sourceLayer,\n geometry: style.geometry,\n rule: toLookRule(style),\n ...(layer.minzoom !== undefined ? { minzoom: layer.minzoom } : {}),\n ...(layer.maxzoom !== undefined ? { maxzoom: layer.maxzoom } : {}),\n ...(style.label\n ? {\n label: {\n field: style.label.template.slice(1, -1),\n ...(style.label.minzoom !== undefined\n ? { minzoom: style.label.minzoom }\n : {}),\n },\n }\n : {}),\n },\n ];\n }),\n ...(definition.minzoom !== undefined\n ? { minzoom: definition.minzoom }\n : {}),\n ...(definition.maxzoom !== undefined\n ? { maxzoom: definition.maxzoom }\n : {}),\n };\n}\n\nfunction heatmapStyle(\n definition: LayerDefinition,\n spec: HeatmapSpec,\n controls: ControlValues,\n): MapHeatmapStyle {\n return {\n ...heatmapSettings(spec, controls),\n opacity: heatmapOpacity(definition, controls),\n ramp: spec.ramp.map(paletteColour),\n };\n}\n\n/**\n * A heatmap of a feed or query layer's points, weighted by its weight field.\n *\n * @internal\n */\nexport function toMapHeatmap(\n definition: HeatmapLayerDefinition,\n features: readonly LayerFeature[],\n controls: ControlValues,\n): MapHeatmap {\n const { weightField } = definition.heatmap;\n return {\n source: {\n kind: \"points\",\n points: features.flatMap((feature) => {\n if (feature.geometry.type !== \"Point\") {\n return [];\n }\n const raw = weightField ? feature.fields[weightField] : null;\n const [lng, lat] = feature.geometry.coordinates;\n return [\n {\n position: { lat, lng },\n weight: typeof raw === \"number\" && Number.isFinite(raw) ? raw : 1,\n },\n ];\n }),\n },\n style: heatmapStyle(definition, definition.heatmap, controls),\n };\n}\n\n/**\n * One heatmap for each heatmap source layer of a vector tile definition, in order.\n *\n * @internal\n */\nexport function toVectorHeatmaps(\n definition: VectorTileLayerDefinition,\n controls: ControlValues,\n now: number,\n): MapHeatmap[] {\n return definition.sourceLayers.flatMap((layer): MapHeatmap[] => {\n if (!(\"heatmap\" in layer)) {\n return [];\n }\n return [\n {\n source: {\n kind: \"vector\",\n source: toMapSource(definition, controls, now),\n sourceLayer: layer.sourceLayer,\n ...(layer.heatmap.weightField\n ? { weightField: layer.heatmap.weightField }\n : {}),\n ...(layer.minzoom !== undefined ? { minzoom: layer.minzoom } : {}),\n ...(layer.maxzoom !== undefined ? { maxzoom: layer.maxzoom } : {}),\n },\n style: heatmapStyle(definition, layer.heatmap, controls),\n },\n ];\n });\n}\n\n/**\n * Works out how each polygon or line of a feed layer is drawn, with its label\n * text, so the map adapter gets plain colours, widths and patterns and needs\n * no style rule. Features of the geometry the style does not draw are skipped.\n *\n * @internal\n */\nexport function toStyledShapes(\n definition: FeedLayerDefinition<FeedShapeStyle>,\n features: readonly LayerFeature[],\n formatters: FormatterRegistry,\n now: number,\n catalogue: MessageCatalogue,\n): MapStyledShapes {\n const { style } = definition;\n const evaluate = shapeEvaluator(style, catalogue);\n const drawn =\n style.geometry === \"area\"\n ? [\"Polygon\", \"MultiPolygon\"]\n : [\"LineString\", \"MultiLineString\"];\n const shapes = features.flatMap((feature): MapStyledShape[] => {\n if (!drawn.includes(feature.geometry.type)) {\n return [];\n }\n const look = evaluate(feature.fields);\n const label = style.label\n ? formatTemplate(style.label.template, feature.fields, formatters, {\n now,\n catalogue,\n })\n : null;\n return [\n {\n id: feature.id,\n geometry: feature.geometry,\n look: {\n colour: paletteColour(look.colour),\n fillOpacity: look.fillOpacity,\n pattern: look.pattern,\n dash: look.dash,\n width: look.width,\n },\n ...(label ? { label } : {}),\n },\n ];\n });\n return {\n geometry: style.geometry,\n shapes,\n ...(style.label?.minzoom !== undefined\n ? { labelMinZoom: style.label.minzoom }\n : {}),\n };\n}\n\n/**\n * Works out how each point of a feed layer is drawn, so the map adapter gets\n * plain colours, sizes and shapes and needs no style rule.\n *\n * @internal\n */\nexport function toMapFeatures(\n style: PointStyle,\n features: readonly LayerFeature[],\n): MapFeature[] {\n return features.flatMap((feature) => {\n const position =\n feature.geometry.type === \"Point\" ? featurePosition(feature) : null;\n if (!position) {\n return [];\n }\n const marker = evaluatePointStyle(style, feature.fields);\n return [\n {\n id: feature.id,\n position,\n colour: paletteColour(marker.colour),\n radius: marker.radius,\n shape: marker.shape,\n },\n ];\n });\n}\n"],"mappings":";;;AAwCA,IAAM,UAA4C,YAAY;;;;;;;;AAS9D,SAAgB,cAAc,MAA2B;CACvD,MAAM,SAAS,QAAQ;CACvB,IAAI,WAAW,KAAA,GACb,MAAM,IAAI,MAAM,mCAAmC,KAAK,GAAG;CAE7D,OAAO;AACT;AAEA,IAAM,UAAU,SACd,KAAK,KAAK,CAAC,KAAK,UAAU;CAAE;CAAK;AAAI,EAAE;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/D;GACA;EACF,CAAC,IACD;EACJ,OAAO,CACL;GACE,IAAI,QAAQ;GACZ,UAAU,QAAQ;GAClB,MAAM;IACJ,QAAQ,cAAc,KAAK,MAAM;IACjC,aAAa,KAAK;IAClB,SAAS,KAAK;IACd,MAAM,KAAK;IACX,OAAO,KAAK;GACd;GACA,GAAI,QAAQ,EAAE,MAAM,IAAI,CAAC;EAC3B,CACF;CACF,CAAC;CACD,OAAO;EACL,UAAU,MAAM;EAChB;EACA,GAAI,MAAM,OAAO,YAAY,KAAA,IACzB,EAAE,cAAc,MAAM,MAAM,QAAQ,IACpC,CAAC;CACP;AACF;;;;;;;AAQA,SAAgB,cACd,OACA,UACc;CACd,OAAO,SAAS,SAAS,YAAY;EACnC,MAAM,WACJ,QAAQ,SAAS,SAAS,UAAU,gBAAgB,OAAO,IAAI;EACjE,IAAI,CAAC,UACH,OAAO,CAAC;EAEV,MAAM,SAAS,mBAAmB,OAAO,QAAQ,MAAM;EACvD,OAAO,CACL;GACE,IAAI,QAAQ;GACZ;GACA,QAAQ,cAAc,OAAO,MAAM;GACnC,QAAQ,OAAO;GACf,OAAO,OAAO;EAChB,CACF;CACF,CAAC;AACH"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mapcairn/map",
|
|
3
|
-
"version": "0.1.0-rc.
|
|
3
|
+
"version": "0.1.0-rc.3",
|
|
4
4
|
"description": "Generic map rendering and UI components for MapCairn",
|
|
5
5
|
"license": "AGPL-3.0-only",
|
|
6
6
|
"repository": {
|
|
@@ -52,9 +52,9 @@
|
|
|
52
52
|
"typecheck": "npm --prefix ../.. run build:sdk && tsc --noEmit"
|
|
53
53
|
},
|
|
54
54
|
"dependencies": {
|
|
55
|
-
"@mapcairn/core": "0.1.0-rc.
|
|
56
|
-
"@mapcairn/react": "0.1.0-rc.
|
|
57
|
-
"@mapcairn/theme": "0.1.0-rc.
|
|
55
|
+
"@mapcairn/core": "0.1.0-rc.3",
|
|
56
|
+
"@mapcairn/react": "0.1.0-rc.3",
|
|
57
|
+
"@mapcairn/theme": "0.1.0-rc.3",
|
|
58
58
|
"@radix-ui/react-slider": "^1.6.1",
|
|
59
59
|
"@radix-ui/react-switch": "^1.4.1",
|
|
60
60
|
"@radix-ui/react-toggle-group": "^1.2.1",
|