incyclist-services 1.9.10 → 1.9.11
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.
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
export const MANEUVER_TEXT = {
|
|
2
|
+
'straight': 'Straight',
|
|
3
|
+
'slight-left': 'Slight left',
|
|
4
|
+
'slight-right': 'Slight right',
|
|
5
|
+
'left': 'Left',
|
|
6
|
+
'right': 'Right',
|
|
7
|
+
'sharp-left': 'Sharp left',
|
|
8
|
+
'sharp-right': 'Sharp right',
|
|
9
|
+
'keep-left': 'Keep left',
|
|
10
|
+
'keep-right': 'Keep right',
|
|
11
|
+
'u-turn': 'Turn around',
|
|
12
|
+
'roundabout': 'Roundabout',
|
|
13
|
+
'unknown': 'Road'
|
|
14
|
+
};
|
|
15
|
+
const COMPASS_WORDS = ['North', 'Northeast', 'East', 'Southeast', 'South', 'Southwest', 'West', 'Northwest'];
|
|
16
|
+
export const FALLBACK_LABEL = {
|
|
17
|
+
roundabout: 'Roundabout',
|
|
18
|
+
slip: 'Slip road',
|
|
19
|
+
unnamed: 'Unnamed road'
|
|
20
|
+
};
|
|
21
|
+
export const normalizeAngle = (angle) => {
|
|
22
|
+
if (!Number.isFinite(angle))
|
|
23
|
+
return angle;
|
|
24
|
+
let a = angle % 360;
|
|
25
|
+
if (a > 180)
|
|
26
|
+
a -= 360;
|
|
27
|
+
if (a <= -180)
|
|
28
|
+
a += 360;
|
|
29
|
+
return a;
|
|
30
|
+
};
|
|
31
|
+
export const getCompassWord = (heading) => {
|
|
32
|
+
if (!Number.isFinite(heading))
|
|
33
|
+
return undefined;
|
|
34
|
+
const normalized = ((heading % 360) + 360) % 360;
|
|
35
|
+
return COMPASS_WORDS[Math.round(normalized / 45) % 8];
|
|
36
|
+
};
|
|
37
|
+
const bucket = (angle) => {
|
|
38
|
+
const abs = Math.abs(angle);
|
|
39
|
+
const right = angle > 0;
|
|
40
|
+
if (abs <= 20)
|
|
41
|
+
return 'straight';
|
|
42
|
+
if (abs <= 60)
|
|
43
|
+
return right ? 'slight-right' : 'slight-left';
|
|
44
|
+
if (abs <= 135)
|
|
45
|
+
return right ? 'right' : 'left';
|
|
46
|
+
if (abs <= 170)
|
|
47
|
+
return right ? 'sharp-right' : 'sharp-left';
|
|
48
|
+
return 'u-turn';
|
|
49
|
+
};
|
|
50
|
+
export const deriveManeuvers = (options) => {
|
|
51
|
+
const result = options.map(o => {
|
|
52
|
+
if (o.roundabout)
|
|
53
|
+
return 'roundabout';
|
|
54
|
+
if (!Number.isFinite(o.direction))
|
|
55
|
+
return 'unknown';
|
|
56
|
+
return bucket(o.direction);
|
|
57
|
+
});
|
|
58
|
+
const groups = new Map();
|
|
59
|
+
result.forEach((m, idx) => {
|
|
60
|
+
if (m === 'roundabout' || m === 'unknown' || m === 'u-turn')
|
|
61
|
+
return;
|
|
62
|
+
groups.set(m, [...(groups.get(m) ?? []), idx]);
|
|
63
|
+
});
|
|
64
|
+
groups.forEach(indexes => {
|
|
65
|
+
if (indexes.length < 2)
|
|
66
|
+
return;
|
|
67
|
+
const sorted = [...indexes].sort((a, b) => options[a].direction - options[b].direction);
|
|
68
|
+
result[sorted[0]] = 'keep-left';
|
|
69
|
+
result[sorted.at(-1)] = 'keep-right';
|
|
70
|
+
});
|
|
71
|
+
return result;
|
|
72
|
+
};
|
|
73
|
+
export const getRoadLabel = (tags) => {
|
|
74
|
+
const roadName = tags?.name || tags?.ref || undefined;
|
|
75
|
+
if (roadName)
|
|
76
|
+
return { roadName, roadLabel: roadName };
|
|
77
|
+
if (tags?.junction === 'roundabout')
|
|
78
|
+
return { roadLabel: FALLBACK_LABEL.roundabout };
|
|
79
|
+
if (tags?.highway?.endsWith('_link'))
|
|
80
|
+
return { roadLabel: FALLBACK_LABEL.slip };
|
|
81
|
+
return { roadLabel: FALLBACK_LABEL.unnamed };
|
|
82
|
+
};
|
|
83
|
+
const getDegradedText = (compass, idx) => compass ? `Heading ${compass.toLowerCase()}` : `Road ${idx + 1}`;
|
|
84
|
+
const FORK_SIDES = {
|
|
85
|
+
2: ['left fork', 'right fork'],
|
|
86
|
+
3: ['left fork', 'middle fork', 'right fork']
|
|
87
|
+
};
|
|
88
|
+
export const buildOptionLabels = (options) => {
|
|
89
|
+
const maneuvers = deriveManeuvers(options);
|
|
90
|
+
const labels = options.map((o, idx) => {
|
|
91
|
+
const maneuver = maneuvers[idx];
|
|
92
|
+
const degraded = maneuver === 'unknown';
|
|
93
|
+
const { roadName, roadLabel } = getRoadLabel(o.tags);
|
|
94
|
+
return {
|
|
95
|
+
maneuver,
|
|
96
|
+
maneuverText: degraded ? getDegradedText(o.compass, idx) : MANEUVER_TEXT[maneuver],
|
|
97
|
+
roadName,
|
|
98
|
+
roadLabel,
|
|
99
|
+
compass: o.compass,
|
|
100
|
+
degraded
|
|
101
|
+
};
|
|
102
|
+
});
|
|
103
|
+
const key = (l) => `${l.maneuverText}|${l.roadLabel}`;
|
|
104
|
+
const collisions = () => {
|
|
105
|
+
const groups = new Map();
|
|
106
|
+
labels.forEach((l, idx) => groups.set(key(l), [...(groups.get(key(l)) ?? []), idx]));
|
|
107
|
+
return [...groups.values()].filter(g => g.length > 1);
|
|
108
|
+
};
|
|
109
|
+
collisions().forEach(group => {
|
|
110
|
+
const compasses = group.map(idx => labels[idx].compass);
|
|
111
|
+
if (compasses.some(c => !c) || new Set(compasses).size !== group.length)
|
|
112
|
+
return;
|
|
113
|
+
group.forEach(idx => { labels[idx].roadLabel = `${labels[idx].roadLabel}, ${labels[idx].compass?.toLowerCase()}`; });
|
|
114
|
+
});
|
|
115
|
+
collisions().forEach(group => {
|
|
116
|
+
const sides = FORK_SIDES[group.length];
|
|
117
|
+
if (!sides || group.some(idx => !Number.isFinite(options[idx].direction)))
|
|
118
|
+
return;
|
|
119
|
+
const sorted = [...group].sort((a, b) => options[a].direction - options[b].direction);
|
|
120
|
+
sorted.forEach((idx, i) => { labels[idx].roadLabel = `${labels[idx].roadLabel}, ${sides[i]}`; });
|
|
121
|
+
});
|
|
122
|
+
return labels;
|
|
123
|
+
};
|
|
124
|
+
export const getLayoutOrder = (options) => {
|
|
125
|
+
const indexes = options.map((_o, idx) => idx);
|
|
126
|
+
if (options.some(o => o.roundabout))
|
|
127
|
+
return indexes;
|
|
128
|
+
const valid = (idx) => Number.isFinite(options[idx].direction);
|
|
129
|
+
const withAngle = indexes.filter(valid).sort((a, b) => options[a].direction - options[b].direction);
|
|
130
|
+
const withoutAngle = indexes.filter(idx => !valid(idx));
|
|
131
|
+
return [...withAngle, ...withoutAngle];
|
|
132
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { FreeRideManeuver } from "../list/types";
|
|
2
|
+
export declare const MANEUVER_TEXT: Record<FreeRideManeuver, string>;
|
|
3
|
+
export declare const FALLBACK_LABEL: {
|
|
4
|
+
roundabout: string;
|
|
5
|
+
slip: string;
|
|
6
|
+
unnamed: string;
|
|
7
|
+
};
|
|
8
|
+
export type OptionLabelInput = {
|
|
9
|
+
id: string;
|
|
10
|
+
direction?: number;
|
|
11
|
+
roundabout?: boolean;
|
|
12
|
+
compass?: string;
|
|
13
|
+
tags?: Record<string, string>;
|
|
14
|
+
};
|
|
15
|
+
export type OptionLabel = {
|
|
16
|
+
maneuver: FreeRideManeuver;
|
|
17
|
+
maneuverText: string;
|
|
18
|
+
roadName?: string;
|
|
19
|
+
roadLabel: string;
|
|
20
|
+
compass?: string;
|
|
21
|
+
degraded: boolean;
|
|
22
|
+
};
|
|
23
|
+
export declare const normalizeAngle: (angle: number) => number;
|
|
24
|
+
export declare const getCompassWord: (heading: number) => string | undefined;
|
|
25
|
+
export declare const deriveManeuvers: (options: Array<Pick<OptionLabelInput, "direction" | "roundabout">>) => FreeRideManeuver[];
|
|
26
|
+
export declare const getRoadLabel: (tags?: Record<string, string>) => {
|
|
27
|
+
roadName?: string;
|
|
28
|
+
roadLabel: string;
|
|
29
|
+
};
|
|
30
|
+
export declare const buildOptionLabels: (options: OptionLabelInput[]) => OptionLabel[];
|
|
31
|
+
export declare const getLayoutOrder: (options: Array<Pick<OptionLabelInput, "direction" | "roundabout">>) => number[];
|