-
-
Notifications
You must be signed in to change notification settings - Fork 628
Expand file tree
/
Copy pathprint-atlas.ts
More file actions
675 lines (639 loc) · 23.6 KB
/
Copy pathprint-atlas.ts
File metadata and controls
675 lines (639 loc) · 23.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
/**
* Atlas (map series) generation for the Print Layout (GH #1291).
*
* Pure, framework-free helpers: token substitution, feature bounds, filtering,
* sorting, and page building. The dialog drives the live map from the pages
* produced here and composes each capture through the regular layout pipeline,
* so everything in this module is unit-testable without a map.
*/
import type { FeatureCollection, Geometry, Position } from "geojson";
/** Geographic bounds as `[west, south, east, north]` in WGS84 degrees. */
export type AtlasBounds = [number, number, number, number];
/** Symmetric fit padding and matching cover-crop rectangle for an atlas page. */
export interface AtlasViewportFrame {
padding: { top: number; bottom: number; left: number; right: number };
crop: { top: number; bottom: number; left: number; right: number };
}
/**
* Fit a target print-frame aspect ratio inside the live map viewport. The
* returned padding constrains `fitBounds` to the exact centered rectangle that
* the print renderer later keeps when it cover-crops the captured map.
*
* @param viewportWidth - Live map width in CSS pixels.
* @param viewportHeight - Live map height in CSS pixels.
* @param frameAspect - Printed map-frame width divided by height.
*/
export function atlasViewportFrame(
viewportWidth: number,
viewportHeight: number,
frameAspect: number,
): AtlasViewportFrame {
const width = Math.max(0, viewportWidth);
const height = Math.max(0, viewportHeight);
let horizontal = 0;
let vertical = 0;
if (width > 0 && height > 0 && Number.isFinite(frameAspect) && frameAspect > 0) {
const viewportAspect = width / height;
if (viewportAspect > frameAspect) {
horizontal = Math.max(0, (width - height * frameAspect) / 2);
} else if (viewportAspect < frameAspect) {
vertical = Math.max(0, (height - width / frameAspect) / 2);
}
}
return {
padding: {
top: vertical,
bottom: vertical,
left: horizontal,
right: horizontal,
},
crop: {
top: vertical,
bottom: height - vertical,
left: horizontal,
right: width - horizontal,
},
};
}
/** One page of an atlas: a coverage feature plus its resolved identity. */
export interface AtlasPage {
/** 0-based position in the final (filtered + sorted) page order. */
index: number;
/** 0-based position of the feature in the source collection: a stable
* identity that survives filtering and sorting. */
sourceIndex: number;
/** Display name resolved from the name field, or `Feature N` (1-based, from
* the feature's position in the source collection) when unset/blank. */
name: string;
/** The feature's attributes, for `{atlas.attr:FIELD}` tokens. */
properties: Record<string, unknown>;
/** Bounding box of the feature geometry (margin not yet applied). */
bounds: AtlasBounds;
}
/** Values substituted into `{atlas.*}` tokens for one page. */
export interface AtlasTokenContext {
name: string;
/** 1-based page number in the final page order. */
pageNumber: number;
/** Total number of pages in the atlas. */
total: number;
properties: Record<string, unknown>;
}
const ATTR_TOKEN = /\{atlas\.attr:([^{}]+)\}/g;
/**
* Replace `{atlas.name}`, `{atlas.pagenumber}`, `{atlas.total}`, and
* `{atlas.attr:FIELD}` tokens in a template string. Unknown attribute fields
* (and null/undefined values) resolve to an empty string; text without tokens
* passes through unchanged.
*/
export function substituteAtlasTokens(text: string, ctx: AtlasTokenContext): string {
if (!text) return text;
return text
.replace(/\{atlas\.name\}/g, ctx.name)
.replace(/\{atlas\.pagenumber\}/g, String(ctx.pageNumber))
.replace(/\{atlas\.total\}/g, String(ctx.total))
.replace(ATTR_TOKEN, (_m, field: string) => {
const v = ctx.properties[field.trim()];
return v === null || v === undefined ? "" : String(v);
});
}
/**
* Remove all `{atlas.*}` tokens from a template (used to derive a token-free
* base filename for the combined PDF, where no single page's values apply).
*/
export function stripAtlasTokens(text: string): string {
return text
.replace(/\{atlas\.(?:name|pagenumber|total)\}/g, "")
.replace(ATTR_TOKEN, "")
.replace(/\s{2,}/g, " ")
.trim();
}
function walkPositions(coords: unknown, visit: (pos: Position) => void): void {
if (!Array.isArray(coords)) return;
if (typeof coords[0] === "number") {
visit(coords as Position);
return;
}
for (const c of coords) walkPositions(c, visit);
}
/**
* Compute the `[west, south, east, north]` bounding box of a GeoJSON geometry
* (GeometryCollections included). Returns `null` for empty or missing
* geometries so callers can skip features that cannot produce a page.
*/
export function geometryBounds(geometry: Geometry | null | undefined): AtlasBounds | null {
if (!geometry) return null;
let w = Infinity;
let s = Infinity;
let e = -Infinity;
let n = -Infinity;
// Longitudes shifted into [0, 360), tracked in parallel to detect features
// that cross the antimeridian (Fiji, the Aleutians, ...): for those, the
// raw min/max box spans the far side of the globe instead of the feature.
let wShifted = Infinity;
let eShifted = -Infinity;
const visit = (pos: Position) => {
const [x, y] = pos;
if (!Number.isFinite(x) || !Number.isFinite(y)) return;
w = Math.min(w, x);
s = Math.min(s, y);
e = Math.max(e, x);
n = Math.max(n, y);
const shifted = x < 0 ? x + 360 : x;
wShifted = Math.min(wShifted, shifted);
eShifted = Math.max(eShifted, shifted);
};
if (geometry.type === "GeometryCollection") {
// Children are merged on their raw boxes; a collection whose *combination*
// crosses the antimeridian is not unwrapped (leaf geometries are).
for (const g of geometry.geometries) {
const b = geometryBounds(g);
if (!b) continue;
w = Math.min(w, b[0]);
s = Math.min(s, b[1]);
e = Math.max(e, b[2]);
n = Math.max(n, b[3]);
}
} else {
walkPositions(geometry.coordinates, visit);
}
if (!Number.isFinite(w) || !Number.isFinite(s)) return null;
// A raw span over 180° that shrinks in the shifted frame means the feature
// crosses ±180°: return the shifted box (east may exceed 180°, which
// MapLibre's fitBounds understands and renders across the antimeridian).
if (e - w > 180 && eShifted - wShifted < e - w) {
return [wShifted, s, eShifted, n];
}
return [w, s, e, n];
}
/**
* Expand a bounding box by a symmetric margin (a percentage of each span) and
* pad degenerate spans (points, vertical/horizontal lines) to a minimum span
* so `fitBounds` never receives a zero-area box.
*
* @param bounds - The box to expand.
* @param marginPct - Margin as a percentage of each side's span (10 = 10%).
* @param minSpanDeg - Minimum span, in degrees, either axis is padded to.
*/
export function expandBounds(
bounds: AtlasBounds,
marginPct: number,
minSpanDeg = 0.005,
): AtlasBounds {
let [w, s, e, n] = bounds;
const padX = Math.max(((e - w) * marginPct) / 100, 0);
const padY = Math.max(((n - s) * marginPct) / 100, 0);
w -= padX;
e += padX;
s -= padY;
n += padY;
if (e - w < minSpanDeg) {
const cx = (w + e) / 2;
w = cx - minSpanDeg / 2;
e = cx + minSpanDeg / 2;
}
// Clamp latitudes into Web Mercator's renderable range *before* enforcing
// the minimum span, and keep the padded centre inside it too: clamping after
// padding could invert the box for a feature at the poles (e.g. a point at
// latitude 90 became [~89.9975, 85]).
s = Math.max(-85, Math.min(85, s));
n = Math.max(-85, Math.min(85, n));
if (n - s < minSpanDeg) {
const cy = Math.max(-85 + minSpanDeg / 2, Math.min(85 - minSpanDeg / 2, (s + n) / 2));
s = cy - minSpanDeg / 2;
n = cy + minSpanDeg / 2;
}
return [w, s, e, n];
}
/**
* Collect the union of attribute names across the features (or a sample of
* them, when `sampleLimit` is set), in first-seen order, for populating the
* name/sort field selectors. Accepts anything carrying `properties`, so both
* GeoJSON Features and precomputed {@link AtlasFeatureInfo}s work.
*/
export function listAtlasFields(
features: ReadonlyArray<{
properties?: Record<string, unknown> | null;
}>,
sampleLimit = Infinity,
): string[] {
const fields: string[] = [];
const seen = new Set<string>();
const limit = Math.min(features.length, sampleLimit);
for (let i = 0; i < limit; i++) {
for (const key of Object.keys(features[i].properties ?? {})) {
if (seen.has(key)) continue;
seen.add(key);
fields.push(key);
}
}
return fields;
}
export type AtlasFilterPredicate = (properties: Record<string, unknown>) => boolean;
/** A single parsed `field op value` comparison. */
interface FilterCondition {
field: string;
op: "=" | "!=" | ">" | ">=" | "<" | "<=" | "contains";
value: string;
numericValue: number | null;
}
const CONDITION_RE = /^(.+?)\s*(==|!=|>=|<=|=|>|<)\s*(.+)$|^(.+?)\s+(contains)\s+(.+)$/i;
function unquote(raw: string): string {
const v = raw.trim();
if ((v.startsWith('"') && v.endsWith('"')) || (v.startsWith("'") && v.endsWith("'"))) {
return v.slice(1, -1);
}
return v;
}
/**
* Split a filter expression on the standalone keyword `and`, ignoring any
* occurrence inside a single- or double-quoted value so expressions like
* `NAME = "Sam and Max"` stay one condition.
*/
function splitConditions(expr: string): string[] {
const parts: string[] = [];
let current = "";
let quote: string | null = null;
let i = 0;
while (i < expr.length) {
const ch = expr[i];
if (quote) {
if (ch === quote) quote = null;
current += ch;
i++;
continue;
}
if (ch === '"' || ch === "'") {
quote = ch;
current += ch;
i++;
continue;
}
if (/\s/.test(ch)) {
const m = /^\s+and\s+/i.exec(expr.slice(i));
if (m) {
parts.push(current);
current = "";
i += m[0].length;
continue;
}
}
current += ch;
i++;
}
parts.push(current);
return parts;
}
/**
* Parse a simple atlas filter expression into a predicate over feature
* attributes, or return `null` when the expression is malformed (so the dialog
* can show an inline error instead of silently matching nothing).
*
* Grammar: one or more `field op value` comparisons joined by `and`. Supported
* operators: `=` (or `==`), `!=`, `>`, `>=`, `<`, `<=`, and `contains`
* (case-insensitive substring). Values may be numbers, quoted strings, or bare
* words. Equality compares numerically when both sides are numbers, otherwise
* as strings; ordering comparisons require numbers on both sides.
*/
export function parseAtlasFilter(expression: string): AtlasFilterPredicate | null {
const expr = expression.trim();
if (!expr) return () => true;
const parts = splitConditions(expr);
const conditions: FilterCondition[] = [];
for (const part of parts) {
const m = CONDITION_RE.exec(part.trim());
if (!m) return null;
const field = unquote((m[1] ?? m[4]).trim());
const opRaw = (m[2] ?? m[5]).toLowerCase();
const value = unquote((m[3] ?? m[6]).trim());
if (!field || !value) return null;
const op = (opRaw === "==" ? "=" : opRaw) as FilterCondition["op"];
const num = Number(value);
conditions.push({
field,
op,
value,
numericValue: value !== "" && Number.isFinite(num) ? num : null,
});
}
return (properties) => conditions.every((c) => matchCondition(c, properties[c.field]));
}
function matchCondition(c: FilterCondition, raw: unknown): boolean {
// A feature without the field at all: it is "not equal" to any value, but
// cannot satisfy equality, ordering, or containment.
if (raw === null || raw === undefined) return c.op === "!=";
const asNum = typeof raw === "number" ? raw : Number(raw);
const bothNumeric =
c.numericValue !== null && Number.isFinite(asNum) && String(raw).trim() !== "";
switch (c.op) {
case "=":
return bothNumeric ? asNum === c.numericValue : String(raw) === c.value;
case "!=":
return bothNumeric ? asNum !== c.numericValue : String(raw) !== c.value;
case ">":
return bothNumeric && asNum > (c.numericValue as number);
case ">=":
return bothNumeric && asNum >= (c.numericValue as number);
case "<":
return bothNumeric && asNum < (c.numericValue as number);
case "<=":
return bothNumeric && asNum <= (c.numericValue as number);
case "contains":
return String(raw).toLowerCase().includes(c.value.toLowerCase());
}
}
function compareValues(a: unknown, b: unknown): number {
// Missing values sort last regardless of direction... handled by caller sign;
// here: undefined/null compare greater than any present value.
const aMissing = a === null || a === undefined || a === "";
const bMissing = b === null || b === undefined || b === "";
if (aMissing && bMissing) return 0;
if (aMissing) return 1;
if (bMissing) return -1;
const an = typeof a === "number" ? a : Number(a);
const bn = typeof b === "number" ? b : Number(b);
if (Number.isFinite(an) && Number.isFinite(bn)) {
return an === bn ? 0 : an < bn ? -1 : 1;
}
return String(a).localeCompare(String(b), undefined, { numeric: true });
}
export interface BuildAtlasPagesOptions {
/** Attribute used for each page's display name; blank = `Feature N`. */
nameField?: string;
/** Attribute to order pages by; blank keeps the source feature order. */
sortField?: string;
/** Reverse the sort order (missing values always sort last). */
sortDescending?: boolean;
/** Predicate from {@link parseAtlasFilter}; features failing it are skipped. */
filter?: AtlasFilterPredicate | null;
}
/**
* A coverage feature reduced to what atlas paging needs: stable identity,
* bounds, and attributes. Precomputing these once per layer (the geometry
* walk is the expensive part) keeps filter/sort edits cheap for large layers.
*/
export interface AtlasFeatureInfo {
/** 0-based position of the feature in the source collection. */
sourceIndex: number;
bounds: AtlasBounds;
properties: Record<string, unknown>;
}
/**
* Reduce a coverage layer's features to {@link AtlasFeatureInfo}s, dropping
* features without a usable geometry. This walks every vertex, so callers
* iterating settings should compute it once per layer and pass the result to
* {@link buildAtlasPages}.
*/
export function collectAtlasFeatures(
collection: Pick<FeatureCollection, "features">,
): AtlasFeatureInfo[] {
const infos: AtlasFeatureInfo[] = [];
collection.features.forEach((feature, i) => {
const bounds = geometryBounds(feature.geometry);
if (!bounds) return;
infos.push({
sourceIndex: i,
bounds,
properties: (feature.properties ?? {}) as Record<string, unknown>,
});
});
return infos;
}
/**
* Build the ordered page list for an atlas from a coverage layer's features
* (or from precomputed {@link AtlasFeatureInfo}s): drop features without a
* usable geometry, apply the filter, sort, and assign final page indices.
*/
export function buildAtlasPages(
source: Pick<FeatureCollection, "features"> | AtlasFeatureInfo[],
options: BuildAtlasPagesOptions = {},
): AtlasPage[] {
const { nameField, sortField, sortDescending, filter } = options;
const infos = Array.isArray(source) ? source : collectAtlasFeatures(source);
const pages: Omit<AtlasPage, "index">[] = [];
for (const info of infos) {
const { sourceIndex, bounds, properties } = info;
if (filter && !filter(properties)) continue;
let name = "";
if (nameField) {
const v = properties[nameField];
if (v !== null && v !== undefined) name = String(v).trim();
}
pages.push({
sourceIndex,
name: name || `Feature ${sourceIndex + 1}`,
properties,
bounds,
});
}
if (sortField) {
const sign = sortDescending ? -1 : 1;
pages.sort((a, b) => {
const cmp = compareValues(a.properties[sortField], b.properties[sortField]);
// Missing values stay last in both directions: compareValues already
// pushed them to the end, so only flip the sign for present-vs-present.
const aMissing =
a.properties[sortField] === null ||
a.properties[sortField] === undefined ||
a.properties[sortField] === "";
const bMissing =
b.properties[sortField] === null ||
b.properties[sortField] === undefined ||
b.properties[sortField] === "";
if (aMissing || bMissing) return cmp;
return cmp * sign;
});
}
return pages.map((p, index) => ({ ...p, index }));
}
const EARTH_RADIUS_M = 6371008.8;
/** Great-circle distance in metres between two `[lng, lat]` positions. */
function haversineMeters(a: Position, b: Position): number {
const toRad = (d: number) => (d * Math.PI) / 180;
const dLat = toRad(b[1] - a[1]);
const dLng = toRad(b[0] - a[0]);
const la1 = toRad(a[1]);
const la2 = toRad(b[1]);
const h = Math.sin(dLat / 2) ** 2 + Math.cos(la1) * Math.cos(la2) * Math.sin(dLng / 2) ** 2;
return 2 * EARTH_RADIUS_M * Math.asin(Math.min(1, Math.sqrt(h)));
}
/** Linear interpolation between two positions — adequate at the sub-segment
* distances the cutter works with (an edge of a digitized line). Longitude is
* unwrapped first so an edge crossing the antimeridian (179° to -179°)
* interpolates across the short dateline path haversine measured, not through
* 0°; the result may exceed ±180°, which MapLibre and {@link geometryBounds}
* both understand. */
function lerpPosition(a: Position, b: Position, t: number): Position {
let dx = b[0] - a[0];
if (dx > 180) dx -= 360;
else if (dx < -180) dx += 360;
return [a[0] + dx * t, a[1] + (b[1] - a[1]) * t];
}
/** Collect the line parts (as coordinate arrays) of a geometry: LineStrings,
* MultiLineStrings, and any of either nested in a GeometryCollection.
* Non-line geometries contribute nothing. */
function lineParts(geometry: Geometry | null | undefined): Position[][] {
if (!geometry) return [];
switch (geometry.type) {
case "LineString":
return [geometry.coordinates];
case "MultiLineString":
return geometry.coordinates;
case "GeometryCollection":
return geometry.geometries.flatMap((g) => lineParts(g));
default:
return [];
}
}
/**
* Whether a geometry contributes any line parts to along-a-line coverage —
* the same traversal {@link buildLineAtlasPages} uses (GeometryCollections
* included), so UI counts can never disagree with the page builder.
*/
export function hasLineGeometry(geometry: Geometry | null | undefined): boolean {
return lineParts(geometry).length > 0;
}
/**
* Hard ceiling on pages one along-a-line series may produce. A tiny segment
* length against a very long line would otherwise build an unbounded page
* list synchronously on the main thread; the dialog surfaces a truncation
* notice when this cap is hit.
*/
export const MAX_LINE_ATLAS_PAGES = 5000;
interface LineSegment {
coords: Position[];
startM: number;
endM: number;
}
/**
* Cut a (possibly multi-part) line into consecutive stretches of
* `segmentM` metres of ground length, interpolating cut points inside edges.
* Chainage runs continuously across parts (gaps between MultiLineString parts
* add no distance), and a trailing remainder shorter than `segmentM` becomes
* the final segment.
*/
function segmentLine(parts: Position[][], segmentM: number): LineSegment[] {
const segments: LineSegment[] = [];
let walked = 0;
let segStart = 0;
let current: Position[] = [];
const flush = () => {
// Skip degenerate leftovers (an exact-multiple cut leaves a zero-length
// tail) so no page ever frames a single point.
if (current.length >= 2 && walked - segStart > 0.5) {
segments.push({ coords: current, startM: segStart, endM: walked });
}
};
for (const part of parts) {
if (part.length < 2) continue;
current.push(part[0]);
for (let i = 1; i < part.length; i++) {
let from = part[i - 1];
const to = part[i];
let d = haversineMeters(from, to);
// Cut every segment boundary that falls inside this edge.
while (d > 0 && walked + d >= segStart + segmentM) {
const t = (segStart + segmentM - walked) / d;
const cut = lerpPosition(from, to, t);
current.push(cut);
walked = segStart + segmentM;
flush();
segStart = walked;
current = [cut];
from = cut;
d = haversineMeters(from, to);
}
walked += d;
current.push(to);
}
}
flush();
return segments;
}
export interface BuildLineAtlasPagesOptions {
/** Ground length of one page's stretch of line, in kilometres. */
segmentKm: number;
/** Attribute naming the source line; blank = `Line N`. */
nameField?: string;
/** Predicate over source-feature attributes; failing lines are skipped. */
filter?: AtlasFilterPredicate | null;
}
/** Round a chainage value to one decimal for labels and attributes. */
function roundKm(v: number): number {
return Math.round(v * 10) / 10;
}
/**
* Build atlas pages that tile the line features of a coverage layer in
* fixed-length stretches (GH #1291 follow-up: uniformly scaled map series
* along a river or trail). Each page covers `segmentKm` kilometres of line;
* pages follow the line's own direction, per feature, in source order.
*
* Page properties carry the source feature's attributes plus `km_start`,
* `km_end`, `segment`, and `segments`, all usable as `{atlas.attr:FIELD}`
* tokens. Non-line features are skipped.
*/
export function buildLineAtlasPages(
collection: Pick<FeatureCollection, "features">,
options: BuildLineAtlasPagesOptions,
): AtlasPage[] {
const { segmentKm, nameField, filter } = options;
if (!(segmentKm > 0)) return [];
const pages: Omit<AtlasPage, "index">[] = [];
for (
let featureIndex = 0;
featureIndex < collection.features.length && pages.length < MAX_LINE_ATLAS_PAGES;
featureIndex++
) {
const feature = collection.features[featureIndex];
const parts = lineParts(feature.geometry);
if (parts.length === 0) continue;
const properties = (feature.properties ?? {}) as Record<string, unknown>;
if (filter && !filter(properties)) continue;
let base = "";
if (nameField) {
const v = properties[nameField];
if (v !== null && v !== undefined) base = String(v).trim();
}
base = base || `Line ${featureIndex + 1}`;
const segments = segmentLine(parts, segmentKm * 1000);
for (let i = 0; i < segments.length; i++) {
if (pages.length >= MAX_LINE_ATLAS_PAGES) break;
const seg = segments[i];
const bounds = geometryBounds({
type: "LineString",
coordinates: seg.coords,
});
if (!bounds) continue;
const startKm = roundKm(seg.startM / 1000);
const endKm = roundKm(seg.endM / 1000);
pages.push({
// Pages keep the *feature's* stable identity (the AtlasPage contract);
// ordering within it lives in `index`/`segment`.
sourceIndex: featureIndex,
name: `${base} km ${startKm}-${endKm}`,
properties: {
...properties,
km_start: startKm,
km_end: endKm,
segment: i + 1,
segments: segments.length,
},
bounds,
});
}
}
return pages.map((p, index) => ({ ...p, index }));
}
/**
* Resolve a zip entry filename for one atlas page: substitute tokens in the
* pattern, sanitize the result for the filesystem, and fall back to the page
* number when everything sanitizes away.
*/
export function atlasEntryName(pattern: string, ctx: AtlasTokenContext): string {
const substituted = substituteAtlasTokens(pattern || "{atlas.pagenumber}-{atlas.name}", ctx);
const cleaned = substituted
.trim()
.replace(/[^\p{L}\p{N} ._-]+/gu, "")
.replace(/\s+/g, "-");
return cleaned || String(ctx.pageNumber);
}