@diagrammo/dgmo 0.21.0 → 0.22.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 +16 -6
- package/dist/advanced.cjs +2521 -623
- package/dist/advanced.d.cts +917 -534
- package/dist/advanced.d.ts +917 -534
- package/dist/advanced.js +2516 -623
- package/dist/auto.cjs +2333 -608
- package/dist/auto.js +119 -119
- package/dist/auto.mjs +2335 -609
- package/dist/cli.cjs +168 -168
- package/dist/editor.cjs +13 -15
- package/dist/editor.js +13 -15
- package/dist/highlight.cjs +15 -12
- package/dist/highlight.js +15 -12
- package/dist/index.cjs +2317 -595
- package/dist/index.d.cts +4 -1
- package/dist/index.d.ts +4 -1
- package/dist/index.js +2319 -596
- package/dist/internal.cjs +2521 -623
- package/dist/internal.d.cts +917 -534
- package/dist/internal.d.ts +917 -534
- package/dist/internal.js +2516 -623
- package/dist/map-data/PROVENANCE.json +1 -1
- package/dist/map-data/mountain-ranges.json +1 -0
- package/dist/map-data/water-bodies.json +1 -0
- package/docs/language-reference.md +44 -31
- package/gallery/fixtures/map-categorical-world.dgmo +16 -0
- package/gallery/fixtures/map-categorical.dgmo +0 -1
- package/gallery/fixtures/map-choropleth.dgmo +0 -1
- package/gallery/fixtures/map-coastline.dgmo +7 -0
- package/gallery/fixtures/map-colorize.dgmo +11 -0
- package/gallery/fixtures/map-direct-color.dgmo +9 -0
- package/gallery/fixtures/map-reference-world.dgmo +11 -0
- package/gallery/fixtures/map-region-scope.dgmo +0 -3
- package/gallery/fixtures/map-route.dgmo +0 -1
- package/package.json +1 -1
- package/src/advanced.ts +26 -1
- package/src/boxes-and-lines/renderer.ts +39 -12
- package/src/cli.ts +1 -1
- package/src/completion.ts +32 -24
- package/src/cycle/renderer.ts +14 -1
- package/src/d3.ts +23 -11
- package/src/editor/highlight-api.ts +4 -0
- package/src/editor/keywords.ts +13 -15
- package/src/infra/renderer.ts +35 -7
- package/src/map/colorize.ts +54 -0
- package/src/map/context-labels.ts +429 -0
- package/src/map/data/PROVENANCE.json +1 -1
- package/src/map/data/mountain-ranges.json +1 -0
- package/src/map/data/types.ts +34 -0
- package/src/map/data/water-bodies.json +1 -0
- package/src/map/dimensions.ts +117 -0
- package/src/map/geo-query.ts +295 -0
- package/src/map/geo.ts +305 -2
- package/src/map/invert.ts +111 -0
- package/src/map/layout.ts +1504 -335
- package/src/map/load-data.ts +16 -2
- package/src/map/parser.ts +57 -111
- package/src/map/renderer.ts +556 -13
- package/src/map/resolved-types.ts +24 -2
- package/src/map/resolver.ts +237 -67
- package/src/map/types.ts +39 -23
- package/src/mindmap/renderer.ts +10 -1
- package/src/palettes/atlas.ts +77 -0
- package/src/palettes/blueprint.ts +73 -0
- package/src/palettes/color-utils.ts +58 -1
- package/src/palettes/index.ts +12 -3
- package/src/palettes/slate.ts +73 -0
- package/src/palettes/tidewater.ts +73 -0
- package/src/render.ts +8 -1
- package/src/tech-radar/renderer.ts +3 -0
- package/src/tech-radar/types.ts +3 -0
- package/src/utils/d3-types.ts +5 -0
- package/src/utils/legend-layout.ts +21 -4
- package/src/utils/legend-types.ts +7 -0
- package/src/utils/reserved-key-registry.ts +3 -0
- package/src/palettes/bold.ts +0 -67
package/src/map/geo.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
// Geometry helpers for the resolver: topology indexing + antimeridian-correct
|
|
2
2
|
// feature bounds (via d3-geo geoBounds — NOT naive min/max, which breaks on the
|
|
3
3
|
// antimeridian and on multi-part features like US Alaska/Hawaii; R5/R6).
|
|
4
|
-
import { feature } from 'topojson-client';
|
|
5
|
-
import { geoBounds } from 'd3-geo';
|
|
4
|
+
import { feature, neighbors } from 'topojson-client';
|
|
5
|
+
import { geoBounds, geoArea } from 'd3-geo';
|
|
6
6
|
import type { BoundaryTopology } from './data/types';
|
|
7
7
|
import type { GeoExtent } from './resolved-types';
|
|
8
8
|
|
|
@@ -47,6 +47,205 @@ export function idSet(topo: BoundaryTopology): Set<string> {
|
|
|
47
47
|
return new Set(geomObject(topo).geometries.map((g) => g.id));
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
+
// Memoize adjacency on the RAW asset object (never the per-render-mutated
|
|
51
|
+
// `worldLayer`). Keyed by topology identity — the assets are stable singletons
|
|
52
|
+
// from load-data.ts, so one build per topology lasts the process (G13).
|
|
53
|
+
const adjacencyCache = new WeakMap<BoundaryTopology, Map<string, string[]>>();
|
|
54
|
+
|
|
55
|
+
/** Per-topology arc-adjacency: ISO → neighbour ISOs, from shared TopoJSON arcs
|
|
56
|
+
* (`topojson-client.neighbors()` on the RAW topology geometries — arcs live on
|
|
57
|
+
* the topology, independent of any `feature()` decode — F2/ADR-4). Computed
|
|
58
|
+
* per-topology (a country never neighbours a state) and memoized.
|
|
59
|
+
*
|
|
60
|
+
* DATA HYGIENE (G1): the raw geometry array can carry (a) `type: null`
|
|
61
|
+
* sovereignty stubs with no arcs (e.g. "Ashmore & Cartier Is." tagged `AU`) and
|
|
62
|
+
* (b) genuine duplicate ISO ids. Null stubs are skipped (no arcs → no
|
|
63
|
+
* adjacency), and every geometry sharing one ISO is UNIONED into a single ISO
|
|
64
|
+
* node — matching the merged layer `decodeLayer` actually draws. Without this
|
|
65
|
+
* the ISO-keyed graph corrupts and the AC9 no-collision guarantee degrades. */
|
|
66
|
+
export function buildAdjacency(topo: BoundaryTopology): Map<string, string[]> {
|
|
67
|
+
const cached = adjacencyCache.get(topo);
|
|
68
|
+
if (cached) return cached;
|
|
69
|
+
const geometries = geomObject(topo).geometries as Array<{
|
|
70
|
+
id: string;
|
|
71
|
+
type?: string;
|
|
72
|
+
}>;
|
|
73
|
+
// neighbors() returns, per geometry BY ARRAY POSITION, the indices of
|
|
74
|
+
// arc-sharing geometries. Null-geometry stubs share no arcs → empty lists.
|
|
75
|
+
const nb = neighbors(geometries as never);
|
|
76
|
+
const sets = new Map<string, Set<string>>();
|
|
77
|
+
geometries.forEach((g, i) => {
|
|
78
|
+
if (!g.type || g.type === 'null') return; // skip arc-less sovereignty stubs
|
|
79
|
+
let set = sets.get(g.id);
|
|
80
|
+
if (!set) {
|
|
81
|
+
set = new Set<string>();
|
|
82
|
+
sets.set(g.id, set);
|
|
83
|
+
}
|
|
84
|
+
for (const j of nb[i] ?? []) {
|
|
85
|
+
const nid = geometries[j]?.id;
|
|
86
|
+
if (nid && nid !== g.id) set.add(nid); // union; never self
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
const out = new Map<string, string[]>();
|
|
90
|
+
// Deterministic neighbour order (sorted) so downstream coloring is stable.
|
|
91
|
+
for (const [iso, set] of sets) out.set(iso, [...set].sort());
|
|
92
|
+
adjacencyCache.set(topo, out);
|
|
93
|
+
return out;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** A decoded boundary feature: the GeoJSON geometry plus its ISO id and display
|
|
97
|
+
* name (carried straight from the topology's `properties.name`). Used for
|
|
98
|
+
* point-in-polygon reverse-geocoding by the geo-query. */
|
|
99
|
+
export interface DecodedFeature {
|
|
100
|
+
readonly type: 'Feature';
|
|
101
|
+
readonly id: string;
|
|
102
|
+
readonly properties: { readonly name: string };
|
|
103
|
+
readonly geometry: unknown;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** Decode every feature of a topology into GeoJSON, keyed nowhere — returned as a
|
|
107
|
+
* flat array so the geo-query can decode ONCE at construction and reuse the
|
|
108
|
+
* result across every `regionAt` call (no per-click re-decode). Uses this
|
|
109
|
+
* module's own `geomObject`/`feature` (never layout.ts's private decoder). */
|
|
110
|
+
export function decodeFeatures(topo: BoundaryTopology): DecodedFeature[] {
|
|
111
|
+
return geomObject(topo).geometries.map((g) => {
|
|
112
|
+
const f = feature(topo as never, g as never) as unknown as {
|
|
113
|
+
geometry: unknown;
|
|
114
|
+
};
|
|
115
|
+
return {
|
|
116
|
+
type: 'Feature',
|
|
117
|
+
id: g.id,
|
|
118
|
+
properties: g.properties,
|
|
119
|
+
geometry: f.geometry,
|
|
120
|
+
};
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** Even-odd ray-cast: is `[lon, lat]` inside a single lon/lat ring? Planar (NOT
|
|
125
|
+
* spherical): d3-geo `geoContains` is winding-sensitive and the Natural-Earth
|
|
126
|
+
* rings invert under it (a small country reads as covering the globe). A planar
|
|
127
|
+
* ray-cast on the raw lon/lat coordinates is the correct, robust test at this
|
|
128
|
+
* reverse-geocode resolution (antimeridian crossers ship as seam-split parts, so
|
|
129
|
+
* no ring actually wraps ±180). */
|
|
130
|
+
function pointInRing(
|
|
131
|
+
lon: number,
|
|
132
|
+
lat: number,
|
|
133
|
+
ring: ReadonlyArray<readonly number[]>
|
|
134
|
+
): boolean {
|
|
135
|
+
let inside = false;
|
|
136
|
+
for (let i = 0, j = ring.length - 1; i < ring.length; j = i++) {
|
|
137
|
+
const xi = ring[i]![0]!;
|
|
138
|
+
const yi = ring[i]![1]!;
|
|
139
|
+
const xj = ring[j]![0]!;
|
|
140
|
+
const yj = ring[j]![1]!;
|
|
141
|
+
const intersect =
|
|
142
|
+
yi > lat !== yj > lat && lon < ((xj - xi) * (lat - yi)) / (yj - yi) + xi;
|
|
143
|
+
if (intersect) inside = !inside;
|
|
144
|
+
}
|
|
145
|
+
return inside;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// On-boundary tolerance (degrees). A click landing exactly on a shared border or
|
|
149
|
+
// a ring vertex is otherwise float-dependent (ray-cast vertex double-count → the
|
|
150
|
+
// point reads as inside neither neighbour, returning ocean over land). Treating
|
|
151
|
+
// an on-edge point as INSIDE makes `regionAt` deterministic: the first iterated
|
|
152
|
+
// country whose boundary the point sits on wins, rather than a rounding coin-flip.
|
|
153
|
+
const EDGE_EPS = 1e-9;
|
|
154
|
+
|
|
155
|
+
/** Is `[lon, lat]` on any edge of `ring` (within EDGE_EPS)? */
|
|
156
|
+
function pointOnRingEdge(
|
|
157
|
+
lon: number,
|
|
158
|
+
lat: number,
|
|
159
|
+
ring: ReadonlyArray<readonly number[]>
|
|
160
|
+
): boolean {
|
|
161
|
+
for (let i = 0, j = ring.length - 1; i < ring.length; j = i++) {
|
|
162
|
+
const xi = ring[i]![0]!;
|
|
163
|
+
const yi = ring[i]![1]!;
|
|
164
|
+
const xj = ring[j]![0]!;
|
|
165
|
+
const yj = ring[j]![1]!;
|
|
166
|
+
// Bounding-box reject first (cheap).
|
|
167
|
+
if (lon < Math.min(xi, xj) - EDGE_EPS || lon > Math.max(xi, xj) + EDGE_EPS)
|
|
168
|
+
continue;
|
|
169
|
+
if (lat < Math.min(yi, yj) - EDGE_EPS || lat > Math.max(yi, yj) + EDGE_EPS)
|
|
170
|
+
continue;
|
|
171
|
+
// Collinear with the segment ⇒ on it (bbox already bounded the extent).
|
|
172
|
+
const cross = (xj - xi) * (lat - yi) - (yj - yi) * (lon - xi);
|
|
173
|
+
if (Math.abs(cross) <= EDGE_EPS) return true;
|
|
174
|
+
}
|
|
175
|
+
return false;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** Point-in-polygon for a Polygon/MultiPolygon geometry (outer ring minus holes).
|
|
179
|
+
* A point on the outer boundary counts as inside (deterministic border handling). */
|
|
180
|
+
function pointInGeometry(geometry: unknown, lon: number, lat: number): boolean {
|
|
181
|
+
const g = geometry as {
|
|
182
|
+
type: string;
|
|
183
|
+
coordinates: number[][][] | number[][][][];
|
|
184
|
+
} | null;
|
|
185
|
+
if (!g) return false;
|
|
186
|
+
const polys: number[][][][] =
|
|
187
|
+
g.type === 'Polygon'
|
|
188
|
+
? [g.coordinates as number[][][]]
|
|
189
|
+
: g.type === 'MultiPolygon'
|
|
190
|
+
? (g.coordinates as number[][][][])
|
|
191
|
+
: [];
|
|
192
|
+
for (const rings of polys) {
|
|
193
|
+
if (!rings.length) continue;
|
|
194
|
+
// On the outer boundary ⇒ inside (deterministic; beats the ray-cast coin-flip).
|
|
195
|
+
if (pointOnRingEdge(lon, lat, rings[0]!)) return true;
|
|
196
|
+
if (!pointInRing(lon, lat, rings[0]!)) continue;
|
|
197
|
+
// Inside the outer ring — exclude if it falls strictly within a hole (a point
|
|
198
|
+
// on the hole boundary is still land, so it stays inside).
|
|
199
|
+
let inHole = false;
|
|
200
|
+
for (let h = 1; h < rings.length; h++) {
|
|
201
|
+
if (
|
|
202
|
+
pointInRing(lon, lat, rings[h]!) &&
|
|
203
|
+
!pointOnRingEdge(lon, lat, rings[h]!)
|
|
204
|
+
) {
|
|
205
|
+
inHole = true;
|
|
206
|
+
break;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
if (!inHole) return true;
|
|
210
|
+
}
|
|
211
|
+
return false;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** Reverse-geocode a `[lon, lat]` to its containing country and (US-only) state
|
|
215
|
+
* via planar point-in-polygon. Honest about misses: returns `country: null`
|
|
216
|
+
* when no polygon contains the point (open ocean / outside any country) rather
|
|
217
|
+
* than guessing (F4). State is tested only when the country is the US.
|
|
218
|
+
* `countries` should be world-detail (50m) features; `states` the us-states
|
|
219
|
+
* (10m) features. Both are pre-decoded by the caller. */
|
|
220
|
+
export function regionAt(
|
|
221
|
+
lonLat: readonly [number, number],
|
|
222
|
+
countries: readonly DecodedFeature[],
|
|
223
|
+
states: readonly DecodedFeature[] | null
|
|
224
|
+
): {
|
|
225
|
+
country: { iso: string; name: string } | null;
|
|
226
|
+
state: { iso: string; name: string } | null;
|
|
227
|
+
} {
|
|
228
|
+
const lon = lonLat[0];
|
|
229
|
+
const lat = lonLat[1];
|
|
230
|
+
let country: { iso: string; name: string } | null = null;
|
|
231
|
+
for (const f of countries) {
|
|
232
|
+
if (pointInGeometry(f.geometry, lon, lat)) {
|
|
233
|
+
country = { iso: f.id, name: f.properties.name };
|
|
234
|
+
break;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
let state: { iso: string; name: string } | null = null;
|
|
238
|
+
if (country?.iso === 'US' && states) {
|
|
239
|
+
for (const f of states) {
|
|
240
|
+
if (pointInGeometry(f.geometry, lon, lat)) {
|
|
241
|
+
state = { iso: f.id, name: f.properties.name };
|
|
242
|
+
break;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
return { country, state };
|
|
247
|
+
}
|
|
248
|
+
|
|
50
249
|
/** Antimeridian-correct geographic bbox of one feature (by id), or null. */
|
|
51
250
|
export function featureBbox(
|
|
52
251
|
topo: BoundaryTopology,
|
|
@@ -65,6 +264,110 @@ export function featureBbox(
|
|
|
65
264
|
];
|
|
66
265
|
}
|
|
67
266
|
|
|
267
|
+
// Framing-extent thresholds for `featureBboxPrimary` (R5). A detached polygon is
|
|
268
|
+
// kept in the framing bbox only if it is either near the dominant cluster
|
|
269
|
+
// (within GAP degrees in both axes) or large enough to matter (≥ AREA_FRAC of
|
|
270
|
+
// the largest polygon). This drops far-flung minor territories — French Guiana,
|
|
271
|
+
// Hawaii, the Canaries are already absent from coarse Spain — so a "Europe"
|
|
272
|
+
// choropleth that names France frames on metropolitan France, not the Atlantic,
|
|
273
|
+
// while keeping near islands and large detached parts (Alaska) in frame.
|
|
274
|
+
const DETACH_GAP_DEG = 10;
|
|
275
|
+
const DETACH_AREA_FRAC = 0.25;
|
|
276
|
+
|
|
277
|
+
/** Decompose a Polygon/MultiPolygon GeoJSON geometry into per-polygon features. */
|
|
278
|
+
function explodePolygons(gj: {
|
|
279
|
+
type: string;
|
|
280
|
+
geometry?: { type: string; coordinates: unknown };
|
|
281
|
+
}): Array<{
|
|
282
|
+
type: 'Feature';
|
|
283
|
+
geometry: { type: 'Polygon'; coordinates: unknown };
|
|
284
|
+
}> {
|
|
285
|
+
const g = gj.geometry ?? (gj as never);
|
|
286
|
+
const t = (g as { type: string }).type;
|
|
287
|
+
const coords = (g as { coordinates: unknown[] }).coordinates;
|
|
288
|
+
if (t === 'Polygon') {
|
|
289
|
+
return [
|
|
290
|
+
{ type: 'Feature', geometry: { type: 'Polygon', coordinates: coords } },
|
|
291
|
+
];
|
|
292
|
+
}
|
|
293
|
+
if (t === 'MultiPolygon') {
|
|
294
|
+
return (coords as unknown[]).map((rings) => ({
|
|
295
|
+
type: 'Feature' as const,
|
|
296
|
+
geometry: { type: 'Polygon' as const, coordinates: rings },
|
|
297
|
+
}));
|
|
298
|
+
}
|
|
299
|
+
return [];
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/** Gap (degrees) between two bboxes — 0 if they overlap/touch on an axis. */
|
|
303
|
+
function bboxGap(a: GeoExtent, b: GeoExtent): number {
|
|
304
|
+
const lonGap = Math.max(0, a[0][0] - b[1][0], b[0][0] - a[1][0]);
|
|
305
|
+
const latGap = Math.max(0, a[0][1] - b[1][1], b[0][1] - a[1][1]);
|
|
306
|
+
return Math.max(lonGap, latGap);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/** Like `featureBbox`, but for FRAMING: ignores far-detached minor territories
|
|
310
|
+
* (overseas DOM-TOM, distant small islands) so a multi-part country frames on
|
|
311
|
+
* its dominant landmass cluster. Falls back to the full bbox for single-part
|
|
312
|
+
* features or when decomposition fails. Antimeridian-spanning parts (geoBounds
|
|
313
|
+
* west > east) are treated as full-bbox to avoid mis-clustering across the seam. */
|
|
314
|
+
export function featureBboxPrimary(
|
|
315
|
+
topo: BoundaryTopology,
|
|
316
|
+
geomId: string
|
|
317
|
+
): GeoExtent | null {
|
|
318
|
+
const geom = geomObject(topo).geometries.find((g) => g.id === geomId);
|
|
319
|
+
if (!geom) return null;
|
|
320
|
+
const gj = feature(topo as never, geom as never) as never;
|
|
321
|
+
const parts = explodePolygons(gj);
|
|
322
|
+
if (parts.length <= 1) return featureBbox(topo, geomId);
|
|
323
|
+
|
|
324
|
+
const polys = parts
|
|
325
|
+
.map((p) => {
|
|
326
|
+
const b = geoBounds(p as never);
|
|
327
|
+
if (!b || !Number.isFinite(b[0][0])) return null;
|
|
328
|
+
// Skip antimeridian-wrapping parts for clustering math (handled by full bbox).
|
|
329
|
+
const wraps = b[1][0] < b[0][0];
|
|
330
|
+
const bbox: GeoExtent = [
|
|
331
|
+
[b[0][0], b[0][1]],
|
|
332
|
+
[b[1][0], b[1][1]],
|
|
333
|
+
];
|
|
334
|
+
return { bbox, area: geoArea(p as never), wraps };
|
|
335
|
+
})
|
|
336
|
+
.filter(
|
|
337
|
+
(p): p is { bbox: GeoExtent; area: number; wraps: boolean } => p !== null
|
|
338
|
+
);
|
|
339
|
+
|
|
340
|
+
if (polys.length <= 1 || polys.some((p) => p.wraps))
|
|
341
|
+
return featureBbox(topo, geomId);
|
|
342
|
+
|
|
343
|
+
const maxArea = Math.max(...polys.map((p) => p.area));
|
|
344
|
+
const anchor = polys.find((p) => p.area === maxArea)!;
|
|
345
|
+
// Grow the cluster: keep a part if it is near the current cluster OR large.
|
|
346
|
+
const cluster: GeoExtent = [
|
|
347
|
+
[anchor.bbox[0][0], anchor.bbox[0][1]],
|
|
348
|
+
[anchor.bbox[1][0], anchor.bbox[1][1]],
|
|
349
|
+
];
|
|
350
|
+
const remaining = polys.filter((p) => p !== anchor);
|
|
351
|
+
let added = true;
|
|
352
|
+
while (added) {
|
|
353
|
+
added = false;
|
|
354
|
+
for (let i = remaining.length - 1; i >= 0; i--) {
|
|
355
|
+
const p = remaining[i]!;
|
|
356
|
+
const near = bboxGap(p.bbox, cluster) <= DETACH_GAP_DEG;
|
|
357
|
+
const large = p.area >= DETACH_AREA_FRAC * maxArea;
|
|
358
|
+
if (near || large) {
|
|
359
|
+
cluster[0][0] = Math.min(cluster[0][0], p.bbox[0][0]);
|
|
360
|
+
cluster[0][1] = Math.min(cluster[0][1], p.bbox[0][1]);
|
|
361
|
+
cluster[1][0] = Math.max(cluster[1][0], p.bbox[1][0]);
|
|
362
|
+
cluster[1][1] = Math.max(cluster[1][1], p.bbox[1][1]);
|
|
363
|
+
remaining.splice(i, 1);
|
|
364
|
+
added = true;
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
return cluster;
|
|
369
|
+
}
|
|
370
|
+
|
|
68
371
|
/** Union of bboxes + POI points into one extent; null if empty. Longitude union
|
|
69
372
|
* uses the smaller-arc rule so an antimeridian-crossing union doesn't span the
|
|
70
373
|
* globe.
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
// Composite- + stretch-aware pixel↔lonLat for the geo-query (step-5 inspector).
|
|
2
|
+
// This is the FIRST map code to call `projection.invert()`. It binds to the
|
|
3
|
+
// REAL fitted projection(s) captured on the MapLayout (layout.ts Task 1) — the
|
|
4
|
+
// caller never reconstructs a projection from metadata, so an inverted pixel
|
|
5
|
+
// lands exactly where the rendered SVG drew the corresponding point.
|
|
6
|
+
//
|
|
7
|
+
// Three cases, in priority order:
|
|
8
|
+
// 1. albers-usa insets — a pixel inside an AK/HI frame rect inverts against
|
|
9
|
+
// that inset's FITTED projection (the un-fitted factory would be garbage).
|
|
10
|
+
// 2. global stretch fit — undo the non-uniform stretch BEFORE the main invert
|
|
11
|
+
// (and apply it AFTER the main project).
|
|
12
|
+
// 3. plain regional/world fit — invert/project the main projection directly
|
|
13
|
+
// (clipExtent set on a regional projection is ignored by `.invert`).
|
|
14
|
+
import type { MapLayout, MapLayoutInset } from './layout';
|
|
15
|
+
|
|
16
|
+
/** True if `(px,py)` is inside an inset frame's bounding box. */
|
|
17
|
+
function inInsetFrame(inset: MapLayoutInset, px: number, py: number): boolean {
|
|
18
|
+
return (
|
|
19
|
+
px >= inset.x &&
|
|
20
|
+
px <= inset.x + inset.w &&
|
|
21
|
+
py >= inset.y &&
|
|
22
|
+
py <= inset.y + inset.h
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Undo the global stretch: screen px → base-projection px. */
|
|
27
|
+
function unstretch(
|
|
28
|
+
layout: MapLayout,
|
|
29
|
+
px: number,
|
|
30
|
+
py: number
|
|
31
|
+
): [number, number] {
|
|
32
|
+
const s = layout.stretch!;
|
|
33
|
+
return [
|
|
34
|
+
s.bx0 + (s.sx !== 0 ? (px - s.ox) / s.sx : 0),
|
|
35
|
+
s.by0 + (s.sy !== 0 ? (py - s.oy) / s.sy : 0),
|
|
36
|
+
];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Apply the global stretch: base-projection px → screen px. */
|
|
40
|
+
function applyStretch(
|
|
41
|
+
layout: MapLayout,
|
|
42
|
+
x: number,
|
|
43
|
+
y: number
|
|
44
|
+
): [number, number] {
|
|
45
|
+
const s = layout.stretch!;
|
|
46
|
+
return [s.ox + (x - s.bx0) * s.sx, s.oy + (y - s.by0) * s.sy];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Screen pixel → `[lon, lat]`, or null for an out-of-domain pixel (e.g. deep
|
|
50
|
+
* ocean beyond the projection's clip disc). */
|
|
51
|
+
export function pixelToLonLat(
|
|
52
|
+
layout: MapLayout,
|
|
53
|
+
px: number,
|
|
54
|
+
py: number
|
|
55
|
+
): [number, number] | null {
|
|
56
|
+
// (1) Inset hit-test first — AK/HI frames sit over the lower-left water of the
|
|
57
|
+
// conus, so their pixels would otherwise invert against the conus conic.
|
|
58
|
+
for (const inset of layout.insets) {
|
|
59
|
+
if (inInsetFrame(inset, px, py)) {
|
|
60
|
+
const ll = inset.projection.invert?.([px, py]);
|
|
61
|
+
return ll && Number.isFinite(ll[0]) && Number.isFinite(ll[1])
|
|
62
|
+
? [ll[0], ll[1]]
|
|
63
|
+
: null;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
// (2)/(3) main projection (undo the stretch first for a global fit).
|
|
67
|
+
const [x, y] = layout.stretch ? unstretch(layout, px, py) : [px, py];
|
|
68
|
+
const ll = layout.projection.invert?.([x, y]);
|
|
69
|
+
return ll && Number.isFinite(ll[0]) && Number.isFinite(ll[1])
|
|
70
|
+
? [ll[0], ll[1]]
|
|
71
|
+
: null;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** `[lon, lat]` → screen pixel, or null if it projects nowhere. AK/HI points
|
|
75
|
+
* land in their inset frames; everything else projects via the main projection
|
|
76
|
+
* (with the forward stretch for a global fit). */
|
|
77
|
+
export function lonLatToPixel(
|
|
78
|
+
layout: MapLayout,
|
|
79
|
+
lonLat: readonly [number, number]
|
|
80
|
+
): [number, number] | null {
|
|
81
|
+
const pt: [number, number] = [lonLat[0], lonLat[1]];
|
|
82
|
+
// Main projection first.
|
|
83
|
+
const main = layout.projection(pt);
|
|
84
|
+
const mainPx: [number, number] | null =
|
|
85
|
+
main && Number.isFinite(main[0]) && Number.isFinite(main[1])
|
|
86
|
+
? layout.stretch
|
|
87
|
+
? applyStretch(layout, main[0], main[1])
|
|
88
|
+
: [main[0], main[1]]
|
|
89
|
+
: null;
|
|
90
|
+
// If the main pixel is on-canvas, it's the lower-48/world position — use it.
|
|
91
|
+
const onCanvas =
|
|
92
|
+
!!mainPx &&
|
|
93
|
+
mainPx[0] >= 0 &&
|
|
94
|
+
mainPx[0] <= layout.width &&
|
|
95
|
+
mainPx[1] >= 0 &&
|
|
96
|
+
mainPx[1] <= layout.height;
|
|
97
|
+
if (onCanvas) return mainPx;
|
|
98
|
+
// Off-canvas under the main projection (AK/HI under the conus conic): see if an
|
|
99
|
+
// inset claims it — project via the inset and keep it if it lands in the frame.
|
|
100
|
+
for (const inset of layout.insets) {
|
|
101
|
+
const p = inset.projection(pt);
|
|
102
|
+
if (
|
|
103
|
+
p &&
|
|
104
|
+
Number.isFinite(p[0]) &&
|
|
105
|
+
Number.isFinite(p[1]) &&
|
|
106
|
+
inInsetFrame(inset, p[0], p[1])
|
|
107
|
+
)
|
|
108
|
+
return [p[0], p[1]];
|
|
109
|
+
}
|
|
110
|
+
return mainPx;
|
|
111
|
+
}
|