Repository navigation
Expand file tree
/
Copy pathconfig.ts
More file actions
78 lines (72 loc) · 4.26 KB
/
Copy pathconfig.ts
File metadata and controls
78 lines (72 loc) · 4.26 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
import type { Archive } from './layers/data-layer';
export type Bounds = [number, number, number, number];
/** The place at a map position, or null outside `bounds`: the map shows a margin with no data around the region. The
* edge is inside. */
export function regionPlace(bounds: Bounds, { lng, lat }: { lng: number; lat: number }): [number, number] | null {
const [west, south, east, north] = bounds;
return lng >= west && lng <= east && lat >= south && lat <= north ? [lng, lat] : null;
}
/** The planner config: the `active` entry of the planner catalogue in specs/planner-release.md, or `VITE_PLANNER_CONFIG`. */
export interface PlannerConfig {
/** The release ID; a `VITE_PLANNER_CONFIG` has none. */
id?: string;
/** The URL of the release's `release.json`, beside its public pointers; a `VITE_PLANNER_CONFIG` has none. */
manifest?: string;
name: string;
bounds: Bounds;
/** TileJSON URLs. */
basemap: string;
places: string;
overlays: string;
/** A Terrarium WebP tile template. */
terrain: string;
/** The OSM credit, from data/sources.toml. */
attribution: string;
/** The credit of the basemap's land cover, from data/sources.toml. Older releases have none. */
landcover_attribution?: string;
terrain_attribution: string;
glyphs: string;
sprites: string;
routing: string;
search: string;
/** The route catalog of specs/route-catalog.md: a cell template with `{cell}`, or the region file. */
routes?: string;
/** The far file template with `{cell}` of specs/route-catalog.md#trail-files; a release with no trail files has none. */
trails?: string;
/** The trail record file template with `{id}`; present with `trails`. */
trail?: string;
/** The facts file template with `{cell}` of specs/route-catalog.md#facts-files; a release with no facts files has none. */
facts?: string;
/** The data layer archives of the region, by name. */
layers: Partial<Record<Archive, string>>;
/** TrailView's ruleset `rules.json` (specs/trailview-tiles.md#rules); older releases have none. */
trailview_rules?: string;
}
const URLS = ['basemap', 'places', 'overlays', 'terrain', 'glyphs', 'sprites', 'routing', 'search'] as const;
const OPTIONAL_URLS = ['routes', 'trailview_rules', 'trails', 'trail', 'facts'] as const;
/** `value` when it is an absolute URL, such as a URL template; else undefined. */
export function configUrl(value: unknown): string | undefined {
return typeof value === 'string' && /^[a-z]+:\/\//.test(value) ? value : undefined;
}
/** `value` as a config. A config that the planner cannot use throws. */
export function plannerConfig(value: unknown): PlannerConfig {
if (!value || typeof value !== 'object') throw new Error('The planner config is not an object.');
const config: any = value;
const bounds = config.bounds;
const urls = Object.fromEntries(URLS.map((key) => [key, configUrl(config[key])]));
const optional = Object.fromEntries(OPTIONAL_URLS.map((key) => [key, config[key] === undefined ? undefined : configUrl(config[key])]));
const layers = Object.fromEntries(Object.entries(config.layers ?? {}).map(([name, value]) => [name, configUrl(value)]));
const invalid = [
...URLS.filter((key) => !urls[key]),
...OPTIONAL_URLS.filter((key) => config[key] !== undefined && !optional[key]),
...(typeof config.name === 'string' && config.name ? [] : ['name']),
...(typeof config.attribution === 'string' && config.attribution ? [] : ['attribution']),
...(config.landcover_attribution === undefined || typeof config.landcover_attribution === 'string' ? [] : ['landcover_attribution']),
...(typeof config.terrain_attribution === 'string' ? [] : ['terrain_attribution']),
...(config.layers && typeof config.layers === 'object' && Object.values(layers).every(Boolean) ? [] : ['layers']),
...(Array.isArray(bounds) && bounds.length === 4 && bounds.every(Number.isFinite) && bounds[0] >= -180 && bounds[2] <= 180
&& bounds[1] >= -85 && bounds[3] <= 85 && bounds[0] < bounds[2] && bounds[1] < bounds[3] ? [] : ['bounds']),
];
if (invalid.length) throw new Error(`The planner config has invalid fields: ${invalid.join(', ')}.`);
return { ...config, ...urls, ...optional, layers };
}