@ruckc/truss-ol 1.28.0 → 1.30.0
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 +75 -0
- package/dist/Map.svelte +487 -54
- package/dist/Map.svelte.d.ts +83 -4
- package/dist/index.d.ts +3 -1
- package/dist/points.d.ts +101 -0
- package/dist/points.js +222 -0
- package/dist/style.d.ts +204 -0
- package/dist/style.js +245 -0
- package/dist/types.d.ts +53 -1
- package/dist/util.d.ts +2 -0
- package/dist/util.js +2 -0
- package/package.json +3 -2
package/dist/Map.svelte.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { type Snippet } from 'svelte';
|
|
2
2
|
import 'ol/ol.css';
|
|
3
|
-
import type { Basemap, MapMarker, MapSelection } from './types.js';
|
|
3
|
+
import type { Basemap, FitOptions, FitTarget, MapApi, MapFeatureInfo, MapMarker, MapSelection, MapViewState } from './types.js';
|
|
4
|
+
import { type ClusterStyle, type FeatureStyle, type HeatmapOptions, type MapStyle, type PointsInput, type PointsMode, type Props } from './util.js';
|
|
4
5
|
type $$ComponentProps = {
|
|
5
6
|
/** [lon, lat] */
|
|
6
7
|
center?: [number, number];
|
|
@@ -9,17 +10,95 @@ type $$ComponentProps = {
|
|
|
9
10
|
markers?: MapMarker[];
|
|
10
11
|
/** GeoJSON FeatureCollection/Feature. Optional `properties.kind` picks the intent color. */
|
|
11
12
|
geojson?: object;
|
|
12
|
-
/**
|
|
13
|
+
/**
|
|
14
|
+
* A (large) point set: an array of `{ lon, lat, ...properties }` or a GeoJSON FeatureCollection of `Point`/`MultiPoint` features.
|
|
15
|
+
* Points with an invalid coordinate are skipped. Held in typed arrays, so 10^6 points are fine; pass a plain array
|
|
16
|
+
* (or `$state.raw`), not a deeply reactive `$state` proxy.
|
|
17
|
+
*/
|
|
18
|
+
points?: PointsInput;
|
|
19
|
+
/**
|
|
20
|
+
* How `points` are drawn: `points` (every point, up to 100,000), `cluster` (circles with counts that split as you zoom in),
|
|
21
|
+
* `heatmap` (density), or `auto` (`points` up to 2,000, `cluster` beyond). Clusters of up to 20,000 points use OpenLayers'
|
|
22
|
+
* `Cluster` source; larger sets use an equivalent grid pass over typed arrays that stays fast at 10^6+ points.
|
|
23
|
+
*/
|
|
24
|
+
pointsMode?: PointsMode;
|
|
25
|
+
/** Cluster cell size in px: points closer than this merge. Default 40. */
|
|
26
|
+
clusterDistance?: number;
|
|
27
|
+
/** Heatmap options: `radius`, `blur`, `weightBy` (a numeric property, summed per area) and `gradient`. */
|
|
28
|
+
heatmap?: HeatmapOptions;
|
|
29
|
+
/**
|
|
30
|
+
* Data-driven styling for `points` and `geojson` features. `size` maps a numeric property to a radius
|
|
31
|
+
* (`{ by, range, domain, scale }`) and `color` maps a property to the chart palette (`{ by, scale: 'sequential' | 'diverging' | 'categorical', domain, midpoint, categories, stops }`);
|
|
32
|
+
* either may be a constant or a function of the feature's properties. Ramps come from `--trc-seq-*`, `--trc-div-*` and `--trc-s*` (import the charts stylesheet for the exact palettes).
|
|
33
|
+
*/
|
|
34
|
+
style?: MapStyle;
|
|
35
|
+
/**
|
|
36
|
+
* Style the clusters of `pointsMode: 'cluster'` from aggregates of their members: `size` and `color` each take `{ by, range, domain, scale, stops, title, format }`
|
|
37
|
+
* where `by` is `'count'` or `{ agg: 'count' | 'sum' | 'mean' | 'min' | 'max', prop }` (a numeric property). Domains default to the whole data set, so a cluster keeps its look while you pan.
|
|
38
|
+
* Without it clusters grow with the log of their count in the primary colour.
|
|
39
|
+
*/
|
|
40
|
+
clusterStyle?: ClusterStyle;
|
|
41
|
+
/**
|
|
42
|
+
* Stable ids for points and GeoJSON features: a property name, or a function of the feature's properties and its index returning a string or number.
|
|
43
|
+
* Wins over `properties.id`. Ids must be unique. Without it ids are `id`/`properties.id`, else `pt:<n>` / `geo:<n>`.
|
|
44
|
+
*/
|
|
45
|
+
idBy?: string | ((properties: Props, index: number) => string | number | undefined);
|
|
46
|
+
/**
|
|
47
|
+
* Draw labels (`featureStyle`'s `label`, `title`) with OpenLayers' decluttering: where labels would overlap only the first is drawn, while the points themselves always show.
|
|
48
|
+
* Default false.
|
|
49
|
+
*/
|
|
50
|
+
declutter?: boolean;
|
|
51
|
+
/** Draw labels only from this zoom level up. */
|
|
52
|
+
labelMinZoom?: number;
|
|
53
|
+
/**
|
|
54
|
+
* A hover tooltip for points and GeoJSON features: `true` shows the feature's name, a function returns the text (`undefined` for none). Use `featureTooltip` for custom content.
|
|
55
|
+
* The tooltip is a `role="tooltip"` element the map points `aria-describedby` at while it shows; Escape hides it, and keyboard selection (Enter) opens the popup instead.
|
|
56
|
+
*/
|
|
57
|
+
tooltip?: boolean | ((feature: MapFeatureInfo) => string | undefined);
|
|
58
|
+
/** Custom tooltip content (implies `tooltip`). */
|
|
59
|
+
featureTooltip?: Snippet<[feature: MapFeatureInfo]>;
|
|
60
|
+
/** Show a legend for the `size` and `color` scales of `style` and `clusterStyle`, and for a heatmap. Titles and value formats come from each spec's `title` and `format`. */
|
|
61
|
+
legend?: boolean;
|
|
62
|
+
/**
|
|
63
|
+
* Per-feature GeoJSON styling by function: receives the feature's properties and returns any of `fill`, `opacity`, `stroke`,
|
|
64
|
+
* `strokeWidth`, `radius`, `label` (all optional). It wins over what the feature carries itself (a `style` property object or the
|
|
65
|
+
* simplestyle keys `fill`, `fill-opacity`, `stroke`, `stroke-width`, `marker-size`, `title`), which wins over `style` and `kind`.
|
|
66
|
+
*/
|
|
67
|
+
featureStyle?: (properties: Props, feature: {
|
|
68
|
+
id: string | number | undefined;
|
|
69
|
+
type: string;
|
|
70
|
+
}) => FeatureStyle | undefined;
|
|
71
|
+
/** Id of the selected marker, GeoJSON feature (its `id`, `properties.id`, or `geo:<n>`) or point. Bindable; set it to select, `undefined` to clear. */
|
|
13
72
|
selected?: string;
|
|
73
|
+
/** Map height: px, or any CSS length. Default 420. */
|
|
14
74
|
height?: number | string;
|
|
15
|
-
/** Fit the view to markers/features on first load. */
|
|
75
|
+
/** Fit the view to markers/features/points on first load (once the container has a size). */
|
|
16
76
|
fitOnLoad?: boolean;
|
|
77
|
+
/** Fit the view again whenever `markers`, `geojson` or `points` change. Default false. */
|
|
78
|
+
fitOnDataChange?: boolean;
|
|
79
|
+
/** Re-measure the map when its container resizes (always), and also refit to the data. Default false. */
|
|
80
|
+
refitOnResize?: boolean;
|
|
81
|
+
/** Lowest zoom level the view allows. Default 1. */
|
|
82
|
+
minZoom?: number;
|
|
83
|
+
/** Highest zoom level the view allows (zoom buttons, wheel, clusters and `fit` all respect it). Default 19. */
|
|
84
|
+
maxZoom?: number;
|
|
17
85
|
/** Custom popup body for the selected marker. */
|
|
18
86
|
popup?: Snippet<[marker: MapMarker]>;
|
|
87
|
+
/** Custom popup body for a selected GeoJSON feature or point; without it the popup lists the feature's properties. */
|
|
88
|
+
featurePopup?: Snippet<[feature: MapFeatureInfo]>;
|
|
19
89
|
/** Extra controls appended to the toolbar. */
|
|
20
90
|
toolbar?: Snippet;
|
|
21
91
|
onselect?: (s: MapSelection | null) => void;
|
|
92
|
+
/** Called once the map exists, with the imperative API (`map`, `fit`, `setView`, `getView`, `updateSize`). */
|
|
93
|
+
onready?: (api: MapApi) => void;
|
|
22
94
|
};
|
|
23
|
-
declare const Map: import("svelte").Component<$$ComponentProps, {
|
|
95
|
+
declare const Map: import("svelte").Component<$$ComponentProps, {
|
|
96
|
+
fit: (target?: FitTarget, options?: FitOptions) => void;
|
|
97
|
+
setView: (v: Partial<Pick<MapViewState, "center" | "zoom">> & {
|
|
98
|
+
duration?: number;
|
|
99
|
+
}) => void;
|
|
100
|
+
getView: () => MapViewState | undefined;
|
|
101
|
+
updateSize: () => void;
|
|
102
|
+
}, "selected" | "center" | "zoom">;
|
|
24
103
|
type Map = ReturnType<typeof Map>;
|
|
25
104
|
export default Map;
|
package/dist/index.d.ts
CHANGED
|
@@ -3,5 +3,7 @@ export { default as ZoomControl } from './ZoomControl.svelte';
|
|
|
3
3
|
export { default as Compass } from './Compass.svelte';
|
|
4
4
|
export { default as ScaleBar } from './ScaleBar.svelte';
|
|
5
5
|
export { default as Attribution } from './Attribution.svelte';
|
|
6
|
-
export type { MapMarker, MarkerKind, Basemap, MapSelection } from './types.js';
|
|
6
|
+
export type { MapMarker, MarkerKind, Basemap, MapSelection, MapFeatureInfo, MapViewState, FitOptions, FitTarget, MapApi } from './types.js';
|
|
7
|
+
export type { MapStyle, SizeSpec, ColorSpec, FeatureStyle, LegendInfo, ClusterStyle, ClusterSizeSpec, ClusterColorSpec, ClusterBy, ClusterAgg, ClusterInfo } from './style.js';
|
|
8
|
+
export type { PointsInput, PointInput, PointsMode, HeatmapOptions } from './points.js';
|
|
7
9
|
export type { ControlPosition, ScaleUnit, HeadingStatus } from './util.js';
|
package/dist/points.d.ts
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure helpers for large point sets: input normalisation, validation, projection, and the grid aggregation behind
|
|
3
|
+
* clusters and the heatmap. Points live in typed arrays (no per-point OpenLayers objects), so a million points cost
|
|
4
|
+
* a few tens of MB and one pass over the arrays per view change.
|
|
5
|
+
*/
|
|
6
|
+
import type { ClusterStat, Props } from './style.js';
|
|
7
|
+
export type PointInput = {
|
|
8
|
+
lon: number;
|
|
9
|
+
lat: number;
|
|
10
|
+
[property: string]: unknown;
|
|
11
|
+
};
|
|
12
|
+
export type PointsGeoJSON = {
|
|
13
|
+
type: 'FeatureCollection';
|
|
14
|
+
features: {
|
|
15
|
+
type?: 'Feature';
|
|
16
|
+
properties?: Props | null;
|
|
17
|
+
geometry?: {
|
|
18
|
+
type: string;
|
|
19
|
+
coordinates?: unknown;
|
|
20
|
+
} | null;
|
|
21
|
+
}[];
|
|
22
|
+
};
|
|
23
|
+
/** A point set: an array of `{ lon, lat, ...properties }` or a GeoJSON FeatureCollection (`Point` and `MultiPoint` features are used). */
|
|
24
|
+
export type PointsInput = PointInput[] | PointsGeoJSON;
|
|
25
|
+
/** How a point set is drawn: individual points, clusters with counts, or a density heatmap. `auto` picks `points` up to {@link AUTO_CLUSTER_ABOVE} points and `cluster` beyond. */
|
|
26
|
+
export type PointsMode = 'auto' | 'points' | 'cluster' | 'heatmap';
|
|
27
|
+
export type HeatmapOptions = {
|
|
28
|
+
/** Blob radius in px. Default 8. */
|
|
29
|
+
radius?: number;
|
|
30
|
+
/** Blur in px. Default 12. */
|
|
31
|
+
blur?: number;
|
|
32
|
+
/** Property whose (non-negative) numeric value weights each point; every point weighs 1 when unset. */
|
|
33
|
+
weightBy?: string;
|
|
34
|
+
/** Colours low to high. Default: the sequential chart ramp. */
|
|
35
|
+
gradient?: string[];
|
|
36
|
+
/** Legend heading. Default: `weightBy`, else `Density`. */
|
|
37
|
+
title?: string;
|
|
38
|
+
};
|
|
39
|
+
/** Extent in EPSG:3857 metres, `[minX, minY, maxX, maxY]`. */
|
|
40
|
+
export type Extent = [number, number, number, number];
|
|
41
|
+
/** Point sets up to this size are drawn as individual points in `auto` mode. */
|
|
42
|
+
export declare const AUTO_CLUSTER_ABOVE = 2000;
|
|
43
|
+
/** `pointsMode="points"` draws at most this many points individually; larger sets cluster instead. */
|
|
44
|
+
export declare const POINTS_MAX = 100000;
|
|
45
|
+
/** Point sets up to this size are clustered by OpenLayers' own `Cluster` source; larger ones by the typed-array grid. */
|
|
46
|
+
export declare const NATIVE_CLUSTER_MAX = 20000;
|
|
47
|
+
/** Ceiling on the number of cells in a grid (memory bound for the aggregation). */
|
|
48
|
+
export declare const MAX_CELLS = 4000000;
|
|
49
|
+
export declare function validLonLat(lon: unknown, lat: unknown): boolean;
|
|
50
|
+
/** Web Mercator (EPSG:3857) metres for a lon/lat; latitude is clamped to the projection's limit. */
|
|
51
|
+
export declare function project(lon: number, lat: number): [number, number];
|
|
52
|
+
export declare function unproject(x: number, y: number): [number, number];
|
|
53
|
+
export type PointStore = {
|
|
54
|
+
n: number;
|
|
55
|
+
/** Projected coordinates. */
|
|
56
|
+
x: Float64Array;
|
|
57
|
+
y: Float64Array;
|
|
58
|
+
/** Property object per point (for plain arrays, the input objects themselves). */
|
|
59
|
+
props: Props[];
|
|
60
|
+
/** Input points dropped for having a coordinate outside lon -180..180 / lat -90..90 or not a number. */
|
|
61
|
+
skipped: number;
|
|
62
|
+
};
|
|
63
|
+
export declare const EMPTY_STORE: PointStore;
|
|
64
|
+
/** Reads a point set into typed arrays, dropping invalid coordinates. */
|
|
65
|
+
export declare function toStore(input: PointsInput | undefined | null): PointStore;
|
|
66
|
+
/** Extent of all points (EPSG:3857), or `undefined` for an empty set. */
|
|
67
|
+
export declare function storeExtent(store: PointStore): Extent | undefined;
|
|
68
|
+
/** The mode to draw `n` points in: `auto` resolves by size, and `points` falls back to `cluster` above {@link POINTS_MAX}. */
|
|
69
|
+
export declare function autoMode(n: number, mode?: PointsMode): Exclude<PointsMode, 'auto'>;
|
|
70
|
+
/** Non-negative numeric weight per point from a property (non-numbers weigh 0). Without `by`, `undefined`: every point weighs 1. */
|
|
71
|
+
export declare function weightsOf(store: PointStore, by?: string): Float64Array | undefined;
|
|
72
|
+
export type Cell = {
|
|
73
|
+
/** Mean position of the members (EPSG:3857). */
|
|
74
|
+
x: number;
|
|
75
|
+
y: number;
|
|
76
|
+
count: number;
|
|
77
|
+
/** Sum of the members' weights. */
|
|
78
|
+
weight: number;
|
|
79
|
+
/** Index of the first member (the only one when `count` is 1). */
|
|
80
|
+
index: number;
|
|
81
|
+
/** Extent of the members. */
|
|
82
|
+
bbox: Extent;
|
|
83
|
+
/** Stats of the requested numeric properties over the members (see `aggregate`'s `values`). */
|
|
84
|
+
stats?: Record<string, ClusterStat>;
|
|
85
|
+
};
|
|
86
|
+
/** A property's numeric value per point (`NaN` where it isn't a number), for cluster aggregates. */
|
|
87
|
+
export declare function numbersOf(store: PointStore, by: string): Float64Array;
|
|
88
|
+
export declare const newStat: () => ClusterStat;
|
|
89
|
+
/** Folds a value into running stats; non-finite values are ignored. */
|
|
90
|
+
export declare function addStat(s: ClusterStat, v: number): void;
|
|
91
|
+
/**
|
|
92
|
+
* Bins the points inside `extent` into square cells of `cell` metres, aligned to a global grid so cells stay put while
|
|
93
|
+
* panning. Returns one {@link Cell} per occupied bin. If the extent would need more than {@link MAX_CELLS} cells the cell size doubles until it fits.
|
|
94
|
+
*/
|
|
95
|
+
export declare function aggregate(store: PointStore, extent: Extent, cell: number, weights?: Float64Array, values?: Record<string, Float64Array>): Cell[];
|
|
96
|
+
/** Radius (px) of a cluster circle for `count` members: grows with the logarithm of the count, capped. */
|
|
97
|
+
export declare function clusterRadius(count: number): number;
|
|
98
|
+
/** `count` as a short label: `42`, `1.2k`, `35k`, `1.2M`. */
|
|
99
|
+
export declare function compactCount(count: number): string;
|
|
100
|
+
/** Lon/lat extent `[west, south, east, north]` of what `fit` accepts: an extent, `{ lon, lat }` points, or GeoJSON. `undefined` when it holds no valid position. */
|
|
101
|
+
export declare function lonLatExtent(target: unknown): [number, number, number, number] | undefined;
|
package/dist/points.js
ADDED
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
/** Point sets up to this size are drawn as individual points in `auto` mode. */
|
|
2
|
+
export const AUTO_CLUSTER_ABOVE = 2000;
|
|
3
|
+
/** `pointsMode="points"` draws at most this many points individually; larger sets cluster instead. */
|
|
4
|
+
export const POINTS_MAX = 100_000;
|
|
5
|
+
/** Point sets up to this size are clustered by OpenLayers' own `Cluster` source; larger ones by the typed-array grid. */
|
|
6
|
+
export const NATIVE_CLUSTER_MAX = 20000;
|
|
7
|
+
/** Ceiling on the number of cells in a grid (memory bound for the aggregation). */
|
|
8
|
+
export const MAX_CELLS = 4_000_000;
|
|
9
|
+
const R = 6378137;
|
|
10
|
+
const MAX_LAT = 85.0511287798;
|
|
11
|
+
export function validLonLat(lon, lat) {
|
|
12
|
+
return typeof lon === 'number' && typeof lat === 'number' && lon >= -180 && lon <= 180 && lat >= -90 && lat <= 90;
|
|
13
|
+
}
|
|
14
|
+
/** Web Mercator (EPSG:3857) metres for a lon/lat; latitude is clamped to the projection's limit. */
|
|
15
|
+
export function project(lon, lat) {
|
|
16
|
+
const la = Math.min(Math.max(lat, -MAX_LAT), MAX_LAT) * (Math.PI / 180);
|
|
17
|
+
return [(lon * Math.PI * R) / 180, R * Math.log(Math.tan(Math.PI / 4 + la / 2))];
|
|
18
|
+
}
|
|
19
|
+
export function unproject(x, y) {
|
|
20
|
+
return [(x / R) * (180 / Math.PI), (2 * Math.atan(Math.exp(y / R)) - Math.PI / 2) * (180 / Math.PI)];
|
|
21
|
+
}
|
|
22
|
+
export const EMPTY_STORE = { n: 0, x: new Float64Array(0), y: new Float64Array(0), props: [], skipped: 0 };
|
|
23
|
+
/** Reads a point set into typed arrays, dropping invalid coordinates. */
|
|
24
|
+
export function toStore(input) {
|
|
25
|
+
if (!input)
|
|
26
|
+
return EMPTY_STORE;
|
|
27
|
+
const x = [], y = [], props = [];
|
|
28
|
+
let skipped = 0;
|
|
29
|
+
const add = (lon, lat, p) => {
|
|
30
|
+
if (!validLonLat(lon, lat)) {
|
|
31
|
+
skipped++;
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
const [px, py] = project(lon, lat);
|
|
35
|
+
x.push(px);
|
|
36
|
+
y.push(py);
|
|
37
|
+
props.push(p);
|
|
38
|
+
};
|
|
39
|
+
if (Array.isArray(input))
|
|
40
|
+
for (const p of input)
|
|
41
|
+
add(p.lon, p.lat, p);
|
|
42
|
+
else
|
|
43
|
+
for (const f of input.features ?? []) {
|
|
44
|
+
const g = f.geometry, p = f.properties ?? {};
|
|
45
|
+
if (g?.type === 'Point')
|
|
46
|
+
add(g.coordinates[0], g.coordinates[1], p);
|
|
47
|
+
else if (g?.type === 'MultiPoint')
|
|
48
|
+
for (const c of g.coordinates)
|
|
49
|
+
add(c[0], c[1], p);
|
|
50
|
+
}
|
|
51
|
+
return { n: x.length, x: Float64Array.from(x), y: Float64Array.from(y), props, skipped };
|
|
52
|
+
}
|
|
53
|
+
/** Extent of all points (EPSG:3857), or `undefined` for an empty set. */
|
|
54
|
+
export function storeExtent(store) {
|
|
55
|
+
if (!store.n)
|
|
56
|
+
return undefined;
|
|
57
|
+
const e = [Infinity, Infinity, -Infinity, -Infinity];
|
|
58
|
+
for (let i = 0; i < store.n; i++) {
|
|
59
|
+
const x = store.x[i], y = store.y[i];
|
|
60
|
+
if (x < e[0])
|
|
61
|
+
e[0] = x;
|
|
62
|
+
if (x > e[2])
|
|
63
|
+
e[2] = x;
|
|
64
|
+
if (y < e[1])
|
|
65
|
+
e[1] = y;
|
|
66
|
+
if (y > e[3])
|
|
67
|
+
e[3] = y;
|
|
68
|
+
}
|
|
69
|
+
return e;
|
|
70
|
+
}
|
|
71
|
+
/** The mode to draw `n` points in: `auto` resolves by size, and `points` falls back to `cluster` above {@link POINTS_MAX}. */
|
|
72
|
+
export function autoMode(n, mode = 'auto') {
|
|
73
|
+
if (mode === 'auto')
|
|
74
|
+
return n > AUTO_CLUSTER_ABOVE ? 'cluster' : 'points';
|
|
75
|
+
return mode === 'points' && n > POINTS_MAX ? 'cluster' : mode;
|
|
76
|
+
}
|
|
77
|
+
/** Non-negative numeric weight per point from a property (non-numbers weigh 0). Without `by`, `undefined`: every point weighs 1. */
|
|
78
|
+
export function weightsOf(store, by) {
|
|
79
|
+
if (!by)
|
|
80
|
+
return undefined;
|
|
81
|
+
const w = new Float64Array(store.n);
|
|
82
|
+
for (let i = 0; i < store.n; i++) {
|
|
83
|
+
const v = Number(store.props[i][by]);
|
|
84
|
+
w[i] = v > 0 && isFinite(v) ? v : 0;
|
|
85
|
+
}
|
|
86
|
+
return w;
|
|
87
|
+
}
|
|
88
|
+
/** A property's numeric value per point (`NaN` where it isn't a number), for cluster aggregates. */
|
|
89
|
+
export function numbersOf(store, by) {
|
|
90
|
+
const w = new Float64Array(store.n);
|
|
91
|
+
for (let i = 0; i < store.n; i++) {
|
|
92
|
+
const v = store.props[i][by];
|
|
93
|
+
w[i] = typeof v === 'number' || (typeof v === 'string' && v.trim() !== '') ? Number(v) : NaN;
|
|
94
|
+
}
|
|
95
|
+
return w;
|
|
96
|
+
}
|
|
97
|
+
export const newStat = () => ({ sum: 0, min: Infinity, max: -Infinity, n: 0 });
|
|
98
|
+
/** Folds a value into running stats; non-finite values are ignored. */
|
|
99
|
+
export function addStat(s, v) {
|
|
100
|
+
if (!Number.isFinite(v))
|
|
101
|
+
return;
|
|
102
|
+
s.sum += v;
|
|
103
|
+
s.n++;
|
|
104
|
+
if (v < s.min)
|
|
105
|
+
s.min = v;
|
|
106
|
+
if (v > s.max)
|
|
107
|
+
s.max = v;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Bins the points inside `extent` into square cells of `cell` metres, aligned to a global grid so cells stay put while
|
|
111
|
+
* panning. Returns one {@link Cell} per occupied bin. If the extent would need more than {@link MAX_CELLS} cells the cell size doubles until it fits.
|
|
112
|
+
*/
|
|
113
|
+
export function aggregate(store, extent, cell, weights, values) {
|
|
114
|
+
let size = cell;
|
|
115
|
+
const dims = () => {
|
|
116
|
+
const x0 = Math.floor(extent[0] / size), y0 = Math.floor(extent[1] / size);
|
|
117
|
+
return { x0, y0, w: Math.floor(extent[2] / size) - x0 + 1, h: Math.floor(extent[3] / size) - y0 + 1 };
|
|
118
|
+
};
|
|
119
|
+
let d = dims();
|
|
120
|
+
while (d.w * d.h > MAX_CELLS) {
|
|
121
|
+
size *= 2;
|
|
122
|
+
d = dims();
|
|
123
|
+
}
|
|
124
|
+
const grid = new Int32Array(d.w * d.h);
|
|
125
|
+
const cells = [];
|
|
126
|
+
const keys = values ? Object.keys(values) : [];
|
|
127
|
+
const { x, y } = store;
|
|
128
|
+
for (let i = 0; i < store.n; i++) {
|
|
129
|
+
const px = x[i], py = y[i];
|
|
130
|
+
if (px < extent[0] || px > extent[2] || py < extent[1] || py > extent[3])
|
|
131
|
+
continue;
|
|
132
|
+
const g = (Math.floor(py / size) - d.y0) * d.w + Math.floor(px / size) - d.x0;
|
|
133
|
+
const k = grid[g], wt = weights ? weights[i] : 1;
|
|
134
|
+
if (k === 0) {
|
|
135
|
+
const cell = { x: px, y: py, count: 1, weight: wt, index: i, bbox: [px, py, px, py] };
|
|
136
|
+
if (keys.length) {
|
|
137
|
+
cell.stats = {};
|
|
138
|
+
for (const k of keys)
|
|
139
|
+
addStat(cell.stats[k] = newStat(), values[k][i]);
|
|
140
|
+
}
|
|
141
|
+
cells.push(cell);
|
|
142
|
+
grid[g] = cells.length;
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
const c = cells[k - 1], b = c.bbox;
|
|
146
|
+
c.x += px;
|
|
147
|
+
c.y += py;
|
|
148
|
+
c.count++;
|
|
149
|
+
c.weight += wt;
|
|
150
|
+
if (c.stats)
|
|
151
|
+
for (const k of keys)
|
|
152
|
+
addStat(c.stats[k], values[k][i]);
|
|
153
|
+
if (px < b[0])
|
|
154
|
+
b[0] = px;
|
|
155
|
+
else if (px > b[2])
|
|
156
|
+
b[2] = px;
|
|
157
|
+
if (py < b[1])
|
|
158
|
+
b[1] = py;
|
|
159
|
+
else if (py > b[3])
|
|
160
|
+
b[3] = py;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
for (const c of cells)
|
|
164
|
+
if (c.count > 1) {
|
|
165
|
+
c.x /= c.count;
|
|
166
|
+
c.y /= c.count;
|
|
167
|
+
}
|
|
168
|
+
return cells;
|
|
169
|
+
}
|
|
170
|
+
/** Radius (px) of a cluster circle for `count` members: grows with the logarithm of the count, capped. */
|
|
171
|
+
export function clusterRadius(count) {
|
|
172
|
+
return Math.min(10 + 7 * Math.log10(Math.max(count, 1)), 32);
|
|
173
|
+
}
|
|
174
|
+
/** `count` as a short label: `42`, `1.2k`, `35k`, `1.2M`. */
|
|
175
|
+
export function compactCount(count) {
|
|
176
|
+
if (count < 1000)
|
|
177
|
+
return String(count);
|
|
178
|
+
const [v, u] = count < 1e6 ? [count / 1e3, 'k'] : [count / 1e6, 'M'];
|
|
179
|
+
return `${v < 10 ? Math.round(v * 10) / 10 : Math.round(v)}${u}`;
|
|
180
|
+
}
|
|
181
|
+
/** Lon/lat extent `[west, south, east, north]` of what `fit` accepts: an extent, `{ lon, lat }` points, or GeoJSON. `undefined` when it holds no valid position. */
|
|
182
|
+
export function lonLatExtent(target) {
|
|
183
|
+
const out = [Infinity, Infinity, -Infinity, -Infinity];
|
|
184
|
+
const add = (lon, lat) => {
|
|
185
|
+
if (!validLonLat(lon, lat))
|
|
186
|
+
return;
|
|
187
|
+
out[0] = Math.min(out[0], lon);
|
|
188
|
+
out[1] = Math.min(out[1], lat);
|
|
189
|
+
out[2] = Math.max(out[2], lon);
|
|
190
|
+
out[3] = Math.max(out[3], lat);
|
|
191
|
+
};
|
|
192
|
+
const walk = (c) => { if (Array.isArray(c) && typeof c[0] === 'number')
|
|
193
|
+
add(c[0], c[1]);
|
|
194
|
+
else if (Array.isArray(c))
|
|
195
|
+
c.forEach(walk); };
|
|
196
|
+
const geo = (g) => {
|
|
197
|
+
if (!g)
|
|
198
|
+
return;
|
|
199
|
+
if (g.type === 'FeatureCollection')
|
|
200
|
+
g.features?.forEach(geo);
|
|
201
|
+
else if (g.type === 'Feature')
|
|
202
|
+
geo(g.geometry);
|
|
203
|
+
else if (g.type === 'GeometryCollection')
|
|
204
|
+
g.geometries?.forEach(geo);
|
|
205
|
+
else
|
|
206
|
+
walk(g.coordinates);
|
|
207
|
+
};
|
|
208
|
+
if (Array.isArray(target) && target.length === 4 && target.every((v) => typeof v === 'number')) {
|
|
209
|
+
add(target[0], target[1]);
|
|
210
|
+
add(target[2], target[3]);
|
|
211
|
+
}
|
|
212
|
+
else if (Array.isArray(target))
|
|
213
|
+
for (const p of target) {
|
|
214
|
+
if (p && 'lon' in p)
|
|
215
|
+
add(p.lon, p.lat);
|
|
216
|
+
else
|
|
217
|
+
geo(p);
|
|
218
|
+
}
|
|
219
|
+
else
|
|
220
|
+
geo(target);
|
|
221
|
+
return out[0] <= out[2] ? out : undefined;
|
|
222
|
+
}
|
package/dist/style.d.ts
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
/** Pure helpers for data-driven map styling: colour maths, scales, domains and the compiled style a layer calls per feature. */
|
|
2
|
+
export type Props = Record<string, unknown>;
|
|
3
|
+
export type Rgb = [number, number, number];
|
|
4
|
+
/** Palettes for colour scales, resolved from the truss chart tokens (`--trc-seq-*`, `--trc-div-*`, `--trc-s*`). */
|
|
5
|
+
export type Palettes = {
|
|
6
|
+
sequential: string[];
|
|
7
|
+
diverging: string[];
|
|
8
|
+
categorical: string[];
|
|
9
|
+
other: string;
|
|
10
|
+
};
|
|
11
|
+
/** Marker or point radius (px) from a numeric property. */
|
|
12
|
+
export type SizeSpec = {
|
|
13
|
+
/** Property name. */
|
|
14
|
+
by: string;
|
|
15
|
+
/** Output radii `[min, max]` in px. Default `[3, 14]`. */
|
|
16
|
+
range?: [number, number];
|
|
17
|
+
/** Input values mapped to the ends of `range`. Default: the data's min and max. Values outside are clamped. */
|
|
18
|
+
domain?: [number, number];
|
|
19
|
+
/** `sqrt` makes the marker's area, not its radius, proportional to the value. Default `linear`. */
|
|
20
|
+
scale?: 'linear' | 'sqrt' | 'log';
|
|
21
|
+
/** Legend heading. Default: the property name. */
|
|
22
|
+
title?: string;
|
|
23
|
+
/** Legend value formatter, e.g. `(v) => v.toFixed(1)`. Default: a short plain number. */
|
|
24
|
+
format?: (value: number) => string;
|
|
25
|
+
};
|
|
26
|
+
/** Colour from a property: a numeric ramp from the chart palette, or categories in the chart's series colours. */
|
|
27
|
+
export type ColorSpec = {
|
|
28
|
+
/** Property name. */
|
|
29
|
+
by: string;
|
|
30
|
+
/** `sequential` (low to high), `diverging` (two poles around `midpoint`), or `categorical` (distinct values, series palette). Default: `sequential` for a numeric property, `categorical` otherwise. */
|
|
31
|
+
scale?: 'sequential' | 'diverging' | 'categorical';
|
|
32
|
+
/** Numeric scales: values mapped to the ends of the ramp. Default: the data's min and max. */
|
|
33
|
+
domain?: [number, number];
|
|
34
|
+
/** Diverging only: the value at the neutral centre. Default: the middle of `domain`. */
|
|
35
|
+
midpoint?: number;
|
|
36
|
+
/** Categorical only: the categories in palette order. Default: the distinct values in order of appearance. The ninth and later categories are neutral grey. */
|
|
37
|
+
categories?: string[];
|
|
38
|
+
/** Your own colours, low to high (numeric scales) or one per category, replacing the chart palette. Any CSS hex colour. */
|
|
39
|
+
stops?: string[];
|
|
40
|
+
/** Legend heading. Default: the property name. */
|
|
41
|
+
title?: string;
|
|
42
|
+
/** Legend value formatter for numeric scales (the ends of the ramp). Default: a short plain number. */
|
|
43
|
+
format?: (value: number) => string;
|
|
44
|
+
};
|
|
45
|
+
/** An aggregate over a cluster's members: their `count`, or the `sum`, `mean`, `min` or `max` of a numeric property. */
|
|
46
|
+
export type ClusterAgg = 'count' | 'sum' | 'mean' | 'min' | 'max';
|
|
47
|
+
/** What a cluster is sized or coloured by: `'count'` (or `{ agg: 'count' }`), or `{ agg, prop }` over a numeric property. */
|
|
48
|
+
export type ClusterBy = 'count' | {
|
|
49
|
+
agg: ClusterAgg;
|
|
50
|
+
prop?: string;
|
|
51
|
+
};
|
|
52
|
+
export type ClusterSizeSpec = {
|
|
53
|
+
by: ClusterBy;
|
|
54
|
+
/** Output radii `[min, max]` in px. Default `[10, 32]`. */
|
|
55
|
+
range?: [number, number];
|
|
56
|
+
/** Aggregate values mapped to the ends of `range`. Default: derived from the whole data set, so sizes stay put while panning. */
|
|
57
|
+
domain?: [number, number];
|
|
58
|
+
/** Default `log` for `count` and `sum` (they span orders of magnitude), `linear` otherwise. */
|
|
59
|
+
scale?: 'linear' | 'sqrt' | 'log';
|
|
60
|
+
title?: string;
|
|
61
|
+
format?: (value: number) => string;
|
|
62
|
+
};
|
|
63
|
+
export type ClusterColorSpec = {
|
|
64
|
+
by: ClusterBy;
|
|
65
|
+
/** Colours low to high. Default: the sequential chart ramp. */
|
|
66
|
+
stops?: string[];
|
|
67
|
+
domain?: [number, number];
|
|
68
|
+
/** Default `log` for `count` and `sum`, `linear` otherwise. */
|
|
69
|
+
scale?: 'linear' | 'sqrt' | 'log';
|
|
70
|
+
title?: string;
|
|
71
|
+
format?: (value: number) => string;
|
|
72
|
+
};
|
|
73
|
+
/** Styling of clusters (`pointsMode: 'cluster'`) from aggregates of their members. Without it clusters grow with the log of their count in the primary colour. */
|
|
74
|
+
export type ClusterStyle = {
|
|
75
|
+
size?: ClusterSizeSpec | number;
|
|
76
|
+
color?: ClusterColorSpec | string;
|
|
77
|
+
opacity?: number;
|
|
78
|
+
};
|
|
79
|
+
/** Sum, min, max and number of numeric values of one property over a cluster's members. */
|
|
80
|
+
export type ClusterStat = {
|
|
81
|
+
sum: number;
|
|
82
|
+
min: number;
|
|
83
|
+
max: number;
|
|
84
|
+
n: number;
|
|
85
|
+
};
|
|
86
|
+
/** What a cluster knows about its members: how many, and the stats of the properties the cluster style needs. */
|
|
87
|
+
export type ClusterInfo = {
|
|
88
|
+
count: number;
|
|
89
|
+
stats?: Record<string, ClusterStat>;
|
|
90
|
+
};
|
|
91
|
+
/** Data-driven styling for points and GeoJSON features. Each of `size` and `color` takes a spec, a constant, or a function of the feature's properties. */
|
|
92
|
+
export type MapStyle = {
|
|
93
|
+
size?: SizeSpec | number | ((properties: Props) => number);
|
|
94
|
+
color?: ColorSpec | string | ((properties: Props) => string);
|
|
95
|
+
/** Fill opacity 0 to 1. Default 0.85 for points, 0.14 for shapes. */
|
|
96
|
+
opacity?: number;
|
|
97
|
+
};
|
|
98
|
+
export type LegendSize = {
|
|
99
|
+
by: string;
|
|
100
|
+
title: string;
|
|
101
|
+
domain: [number, number];
|
|
102
|
+
range: [number, number];
|
|
103
|
+
steps: {
|
|
104
|
+
value: number;
|
|
105
|
+
radius: number;
|
|
106
|
+
label: string;
|
|
107
|
+
}[];
|
|
108
|
+
};
|
|
109
|
+
export type LegendColor = {
|
|
110
|
+
by: string;
|
|
111
|
+
title: string;
|
|
112
|
+
scale: 'sequential' | 'diverging' | 'categorical';
|
|
113
|
+
domain?: [number, number];
|
|
114
|
+
labels?: [string, string];
|
|
115
|
+
gradient?: string;
|
|
116
|
+
categories?: {
|
|
117
|
+
label: string;
|
|
118
|
+
color: string;
|
|
119
|
+
}[];
|
|
120
|
+
};
|
|
121
|
+
export type LegendInfo = {
|
|
122
|
+
size?: LegendSize;
|
|
123
|
+
color?: LegendColor;
|
|
124
|
+
/** Legend for cluster size / colour (aggregates). */
|
|
125
|
+
clusterSize?: LegendSize;
|
|
126
|
+
clusterColor?: LegendColor;
|
|
127
|
+
/** Heatmap ramp, low to high density. */
|
|
128
|
+
heatmap?: LegendColor;
|
|
129
|
+
};
|
|
130
|
+
/** The ramps used when the chart tokens are not defined; they match `charts.css` in light mode. */
|
|
131
|
+
export declare const DEFAULT_PALETTES: Palettes;
|
|
132
|
+
/** `#rgb`, `#rrggbb` or `rgb[a](r, g, b…)` to [r, g, b]; `undefined` for anything else (named colours, `var()`). */
|
|
133
|
+
export declare function parseColor(s: string): Rgb | undefined;
|
|
134
|
+
/** `color` with an alpha channel. A colour that can't be parsed is returned unchanged. */
|
|
135
|
+
export declare function withAlpha(color: string, a: number): string;
|
|
136
|
+
/** The colour at `t` (0 to 1, clamped) along evenly spaced `stops`, linearly interpolated in sRGB, as `#rrggbb`. */
|
|
137
|
+
export declare function rampColor(stops: string[], t: number): string;
|
|
138
|
+
/** A number for a legend: whole numbers and values from 100 up as is, others to 3 significant figures. */
|
|
139
|
+
export declare function formatNumber(v: number): string;
|
|
140
|
+
/** A CSS `linear-gradient` over `stops`, left to right. */
|
|
141
|
+
export declare function cssGradient(stops: string[]): string;
|
|
142
|
+
/** `[min, max]` of the finite numbers among `values` (numeric strings count), or `undefined` when there are none. */
|
|
143
|
+
export declare function extentOf(values: Iterable<unknown>): [number, number] | undefined;
|
|
144
|
+
/** A function from a value to `range`: linear, or `sqrt` for area-proportional symbols. Clamped to the domain; a flat domain maps to the middle of the range; `NaN` maps to the low end. */
|
|
145
|
+
export declare function scaleOf(domain: [number, number], range: [number, number], kind?: 'linear' | 'sqrt' | 'log'): (v: number) => number;
|
|
146
|
+
/** Distinct values of `by` in order of appearance. */
|
|
147
|
+
export declare function categoriesOf(rows: Iterable<Props>, by: string): string[];
|
|
148
|
+
export type Compiled = {
|
|
149
|
+
/** Radius in px for a feature's properties, or `undefined` when no size is configured. */
|
|
150
|
+
size?: (p: Props) => number;
|
|
151
|
+
/** Colour for a feature's properties, or `undefined` when no colour is configured. */
|
|
152
|
+
color?: (p: Props) => string;
|
|
153
|
+
opacity?: number;
|
|
154
|
+
legend: LegendInfo;
|
|
155
|
+
};
|
|
156
|
+
/**
|
|
157
|
+
* Turns a style into per-feature functions. `rows` are the features' property objects, read once to find each
|
|
158
|
+
* spec's domain or categories (not when given, nor for function and constant forms).
|
|
159
|
+
*/
|
|
160
|
+
export declare function compileStyle(style: MapStyle | undefined, rows: Iterable<Props>, palettes?: Palettes): Compiled;
|
|
161
|
+
/** Per-feature drawing overrides. Any field left out keeps the feature's default (or data-driven) look. */
|
|
162
|
+
export type FeatureStyle = {
|
|
163
|
+
/** Fill colour of polygons and points. */
|
|
164
|
+
fill?: string;
|
|
165
|
+
/** Fill opacity 0 to 1. */
|
|
166
|
+
opacity?: number;
|
|
167
|
+
/** Outline / line colour. */
|
|
168
|
+
stroke?: string;
|
|
169
|
+
/** Outline / line width in px. */
|
|
170
|
+
strokeWidth?: number;
|
|
171
|
+
/** Point radius in px. */
|
|
172
|
+
radius?: number;
|
|
173
|
+
/** Text drawn next to the feature. */
|
|
174
|
+
label?: string;
|
|
175
|
+
};
|
|
176
|
+
/**
|
|
177
|
+
* The style a GeoJSON feature carries in its own properties: a `style` object (`{ fill, opacity, stroke, strokeWidth, radius, label }`)
|
|
178
|
+
* and/or the simplestyle keys `fill`, `fill-opacity`, `stroke`, `stroke-width`, `marker-size` (a radius in px here) and `title`. A `style` object wins over simplestyle keys.
|
|
179
|
+
*/
|
|
180
|
+
export declare function propertyStyle(p: Props): FeatureStyle;
|
|
181
|
+
/** A feature's heading for a popup: its `name`, `title`, `label`, or `id` property, else `Feature`. */
|
|
182
|
+
export declare function featureTitle(p: Props): string;
|
|
183
|
+
/** The properties a default popup lists: at most `max` entries, without styling keys, empty values and internals; objects as JSON. */
|
|
184
|
+
export declare function popupEntries(p: Props, max?: number): [string, string][];
|
|
185
|
+
/** The value of a cluster aggregate: its member count, or the sum/mean/min/max of `prop` over its numeric members (`NaN` when it has none). */
|
|
186
|
+
export declare function clusterValue(by: ClusterBy, c: ClusterInfo): number;
|
|
187
|
+
/** Properties whose stats a cluster style needs (so clusters collect only those). */
|
|
188
|
+
export declare function clusterProps(cs: ClusterStyle | undefined): string[];
|
|
189
|
+
export type CompiledCluster = {
|
|
190
|
+
/** Radius in px for a cluster, or `undefined` for the default (grows with the log of the count). */
|
|
191
|
+
size?: (c: ClusterInfo) => number;
|
|
192
|
+
/** Fill colour for a cluster, or `undefined` for the primary colour. */
|
|
193
|
+
color?: (c: ClusterInfo) => string;
|
|
194
|
+
opacity?: number;
|
|
195
|
+
legend: Pick<LegendInfo, 'clusterSize' | 'clusterColor'>;
|
|
196
|
+
};
|
|
197
|
+
/**
|
|
198
|
+
* Compiles a cluster style. `rows` are the point set's property objects, read once to find the default domains
|
|
199
|
+
* (the data's own extent of the aggregated property, `[2, n]` for count, `[min, total]` for sum), so a cluster keeps its
|
|
200
|
+
* size and colour while you pan and zoom.
|
|
201
|
+
*/
|
|
202
|
+
export declare function compileCluster(cs: ClusterStyle | undefined, rows: Props[], palettes?: Palettes): CompiledCluster;
|
|
203
|
+
/** Whether `color` is light enough that dark text reads on it. Colours that can't be parsed count as dark. */
|
|
204
|
+
export declare function isLight(color: string): boolean;
|