@wemap/routers 12.11.3 → 12.11.5
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/dist/index.d.ts +0 -2
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1 -230
- package/dist/index.mjs.map +1 -1
- package/helpers/InstructionManager.ts +184 -0
- package/helpers/InstructionManagerV1.ts +95 -0
- package/package.json +10 -5
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import { Coordinates } from '@wemap/geo';
|
|
2
|
+
import { diffAngle, rad2deg, roundFactor } from '@wemap/maths';
|
|
3
|
+
import ItineraryInfoManager from '../src/ItineraryInfoManager.js';
|
|
4
|
+
import { Step } from '../src/model/Step.js';
|
|
5
|
+
|
|
6
|
+
export default class InstructionManager {
|
|
7
|
+
|
|
8
|
+
static useProposals = false;
|
|
9
|
+
|
|
10
|
+
static getTurnInfoFromAngle(_angle: number) {
|
|
11
|
+
|
|
12
|
+
let direction, directionExtra;
|
|
13
|
+
|
|
14
|
+
const directionAngle = rad2deg(diffAngle(_angle, Math.PI));
|
|
15
|
+
const directionAngleAbs = Math.abs(directionAngle);
|
|
16
|
+
if (directionAngleAbs <= 20) {
|
|
17
|
+
direction = 'straight';
|
|
18
|
+
} else {
|
|
19
|
+
direction = directionAngle > 0 ? 'left' : 'right';
|
|
20
|
+
if (directionAngleAbs < 55) {
|
|
21
|
+
directionExtra = 'slight';
|
|
22
|
+
} else if (directionAngleAbs > 120) {
|
|
23
|
+
directionExtra = 'sharp';
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return { direction, directionExtra };
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
static getInfoFromStep(step: Step) {
|
|
31
|
+
|
|
32
|
+
let type, direction, directionExtra, levelChange;
|
|
33
|
+
|
|
34
|
+
if (step.levelChange) {
|
|
35
|
+
type = 'level-change';
|
|
36
|
+
levelChange = step.levelChange;
|
|
37
|
+
} else {
|
|
38
|
+
type = 'turn';
|
|
39
|
+
const turnInfo = InstructionManager.getTurnInfoFromAngle(step.angle);
|
|
40
|
+
direction = turnInfo.direction;
|
|
41
|
+
directionExtra = turnInfo.directionExtra;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return {
|
|
45
|
+
type,
|
|
46
|
+
direction,
|
|
47
|
+
directionExtra,
|
|
48
|
+
levelChange,
|
|
49
|
+
name: step.name,
|
|
50
|
+
indoor: step.coords.level !== null
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// eslint-disable-next-line max-statements, complexity
|
|
55
|
+
static getInstructionFromStep(step: Step) {
|
|
56
|
+
|
|
57
|
+
const { direction, directionExtra } = InstructionManager.getTurnInfoFromAngle(step.angle);
|
|
58
|
+
const isTurn = direction !== 'straight';
|
|
59
|
+
|
|
60
|
+
if (step.lastStep) {
|
|
61
|
+
if (isTurn && direction === 'left') {
|
|
62
|
+
return 'Your destination is on your left';
|
|
63
|
+
} else if (isTurn && direction === 'right') {
|
|
64
|
+
return 'Your destination is on your right';
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
let suffix = '';
|
|
69
|
+
if (step.extras?.isGate) {
|
|
70
|
+
suffix = ` on gate ${step.name}`;
|
|
71
|
+
} else if (step.name) {
|
|
72
|
+
suffix = ` on ${step.name}`;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (step.levelChange) {
|
|
76
|
+
if (step.levelChange.direction === 'up') {
|
|
77
|
+
if (step.levelChange.type === 'stairs') {
|
|
78
|
+
return 'Go up the stairs';
|
|
79
|
+
}
|
|
80
|
+
if (step.levelChange.type === 'escalator') {
|
|
81
|
+
return 'Go up the escalator';
|
|
82
|
+
}
|
|
83
|
+
if (step.levelChange.type === 'elevator') {
|
|
84
|
+
return 'Go up the elevator';
|
|
85
|
+
}
|
|
86
|
+
if (step.levelChange.type === 'moving walkway') {
|
|
87
|
+
return 'Go up the moving walkway';
|
|
88
|
+
}
|
|
89
|
+
if (step.levelChange.type === 'incline plane') {
|
|
90
|
+
return 'Go up the incline plane';
|
|
91
|
+
}
|
|
92
|
+
return 'Go up' + suffix;
|
|
93
|
+
}
|
|
94
|
+
if (step.levelChange.direction === 'down') {
|
|
95
|
+
if (step.levelChange.type === 'stairs') {
|
|
96
|
+
return 'Go down the stairs';
|
|
97
|
+
}
|
|
98
|
+
if (step.levelChange.type === 'escalator') {
|
|
99
|
+
return 'Go down the escalator';
|
|
100
|
+
}
|
|
101
|
+
if (step.levelChange.type === 'elevator') {
|
|
102
|
+
return 'Go down the elevator';
|
|
103
|
+
}
|
|
104
|
+
if (step.levelChange.type === 'moving walkway') {
|
|
105
|
+
return 'Go down the moving walkway';
|
|
106
|
+
}
|
|
107
|
+
if (step.levelChange.type === 'incline plane') {
|
|
108
|
+
return 'Go down the incline plane';
|
|
109
|
+
}
|
|
110
|
+
return 'Go down' + suffix;
|
|
111
|
+
}
|
|
112
|
+
if (step.extras?.subwayEntrance) {
|
|
113
|
+
return `Take exit ${step.extras.subwayEntranceRef}`;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (isTurn) {
|
|
118
|
+
if (direction === 'left') {
|
|
119
|
+
if (directionExtra === 'slight') {
|
|
120
|
+
return 'Turn slightly left' + suffix;
|
|
121
|
+
}
|
|
122
|
+
return 'Turn left' + suffix;
|
|
123
|
+
}
|
|
124
|
+
if (direction === 'right') {
|
|
125
|
+
if (directionExtra === 'slight') {
|
|
126
|
+
return 'Turn slightly right' + suffix;
|
|
127
|
+
}
|
|
128
|
+
return 'Turn right' + suffix;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return 'Continue straight';
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
static getInstructionFromPosition(itineraryInfoManager: ItineraryInfoManager, position: Coordinates) {
|
|
138
|
+
const itineraryInfo = itineraryInfoManager.getInfo(position);
|
|
139
|
+
if (!itineraryInfo) {
|
|
140
|
+
return null;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// If distance between itinerary and position is too far,
|
|
144
|
+
// something went wrong. (mapmatching.maxDistance)
|
|
145
|
+
if (this.useProposals && itineraryInfo.projection.distanceFromNearestElement > 15) {
|
|
146
|
+
return 'It seems that we are a little bit lost, please start again the localization process';
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const { nextStep } = itineraryInfo;
|
|
150
|
+
if (!nextStep) {
|
|
151
|
+
return 'You are arrived';
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const distNextStep = position.distanceTo(nextStep.coords);
|
|
155
|
+
const distRounded = roundFactor(distNextStep, 5);
|
|
156
|
+
|
|
157
|
+
if (this.useProposals && distNextStep > 10) {
|
|
158
|
+
return `Continue straight for ${distRounded}m`;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
let instruction = InstructionManager.getInstructionFromStep(nextStep);
|
|
163
|
+
|
|
164
|
+
const stepWithImportantInfo = itineraryInfoManager._steps.find(step => step.levelChange
|
|
165
|
+
&& step.number > nextStep.number
|
|
166
|
+
&& step.coords.distanceTo(nextStep.coords) < 10
|
|
167
|
+
) || null;
|
|
168
|
+
|
|
169
|
+
if (stepWithImportantInfo && stepWithImportantInfo.levelChange) {
|
|
170
|
+
const nextBearing = nextStep.coords.bearingTo(stepWithImportantInfo.coords);
|
|
171
|
+
const { direction } = InstructionManager.getTurnInfoFromAngle(nextBearing - nextStep.previousBearing);
|
|
172
|
+
instruction = direction === 'straight' ? 'Continue straight' : `Turn ${direction}`;
|
|
173
|
+
const { direction: levelDirection, type: levelType } = stepWithImportantInfo.levelChange;
|
|
174
|
+
instruction += ` and take the ${levelType} going ${levelDirection}`;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
if (distNextStep >= 5) {
|
|
178
|
+
instruction += ` in ${distRounded}m`;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return instruction;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { Coordinates } from '@wemap/geo';
|
|
2
|
+
import ItineraryInfoManager from '../src/ItineraryInfoManager.js';
|
|
3
|
+
import { Step } from '../src/model/Step.js';
|
|
4
|
+
import OsrmRemoteRouter from '../src/remote/osrm/OsrmRemoteRouter.js';
|
|
5
|
+
|
|
6
|
+
export default class InstructionManagerV1 {
|
|
7
|
+
|
|
8
|
+
// eslint-disable-next-line max-statements, complexity
|
|
9
|
+
static getInstructionFromStep(step: Step) {
|
|
10
|
+
|
|
11
|
+
const modifier = OsrmRemoteRouter.getModifierFromAngle(step.angle);
|
|
12
|
+
let direction, directionExtra;
|
|
13
|
+
if (modifier.includes('left')) {
|
|
14
|
+
direction = 'left';
|
|
15
|
+
} else if (modifier.includes('right')) {
|
|
16
|
+
direction = 'right';
|
|
17
|
+
}
|
|
18
|
+
if (modifier.includes('slight')) {
|
|
19
|
+
directionExtra = 'slight';
|
|
20
|
+
}
|
|
21
|
+
const isTurn = modifier !== 'straight';
|
|
22
|
+
|
|
23
|
+
if (step.lastStep) {
|
|
24
|
+
if (isTurn && direction === 'left') {
|
|
25
|
+
return 'Your destination is on your left';
|
|
26
|
+
} else if (isTurn && direction === 'right') {
|
|
27
|
+
return 'Your destination is on your right';
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const suffix = step.name ? ` on ${step.name}` : '';
|
|
32
|
+
|
|
33
|
+
if (step.levelChange) {
|
|
34
|
+
if (step.levelChange.direction === 'up') {
|
|
35
|
+
if (step.levelChange.type === 'escalator') {
|
|
36
|
+
return 'Go up the escalator';
|
|
37
|
+
}
|
|
38
|
+
if (step.levelChange.type === 'stairs') {
|
|
39
|
+
return 'Go up the stairs';
|
|
40
|
+
}
|
|
41
|
+
return 'Go up' + suffix;
|
|
42
|
+
}
|
|
43
|
+
if (step.levelChange.direction === 'down') {
|
|
44
|
+
if (step.levelChange.type === 'escalator') {
|
|
45
|
+
return 'Go down the escalator';
|
|
46
|
+
}
|
|
47
|
+
if (step.levelChange.type === 'stairs') {
|
|
48
|
+
return 'Go down the stairs';
|
|
49
|
+
}
|
|
50
|
+
return 'Go down' + suffix;
|
|
51
|
+
}
|
|
52
|
+
if (step.extras?.subwayEntrance) {
|
|
53
|
+
return `Take exit ${step.extras.subwayEntranceRef}`;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (isTurn) {
|
|
58
|
+
if (direction === 'left') {
|
|
59
|
+
if (directionExtra === 'slight') {
|
|
60
|
+
return 'Turn slightly left' + suffix;
|
|
61
|
+
}
|
|
62
|
+
return 'Turn left' + suffix;
|
|
63
|
+
}
|
|
64
|
+
if (direction === 'right') {
|
|
65
|
+
if (directionExtra === 'slight') {
|
|
66
|
+
return 'Turn slightly right' + suffix;
|
|
67
|
+
}
|
|
68
|
+
return 'Turn right' + suffix;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
return '';
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
static getInstructionFromPosition(itineraryInfoManager: ItineraryInfoManager, position: Coordinates) {
|
|
77
|
+
const itineraryInfo = itineraryInfoManager.getInfo(position);
|
|
78
|
+
if (!itineraryInfo) {
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const { nextStep } = itineraryInfo;
|
|
83
|
+
if (!nextStep) {
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const distNextStep = position.distanceTo(nextStep.coords);
|
|
88
|
+
const nextStep2 = itineraryInfoManager._steps.find(step => step.number > nextStep.number);
|
|
89
|
+
if (distNextStep < 3 && nextStep2) {
|
|
90
|
+
return InstructionManagerV1.getInstructionFromStep(nextStep2);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return InstructionManagerV1.getInstructionFromStep(nextStep);
|
|
94
|
+
}
|
|
95
|
+
}
|
package/package.json
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"directory": "packages/routers"
|
|
13
13
|
},
|
|
14
14
|
"name": "@wemap/routers",
|
|
15
|
-
"version": "12.11.
|
|
15
|
+
"version": "12.11.5",
|
|
16
16
|
"bugs": {
|
|
17
17
|
"url": "https://github.com/wemap/wemap-modules-js/issues"
|
|
18
18
|
},
|
|
@@ -46,14 +46,19 @@
|
|
|
46
46
|
"geojson": "^0.5.0"
|
|
47
47
|
},
|
|
48
48
|
"files": [
|
|
49
|
-
"dist"
|
|
49
|
+
"dist",
|
|
50
|
+
"helpers"
|
|
50
51
|
],
|
|
51
52
|
"exports": {
|
|
52
53
|
".": {
|
|
53
|
-
"types": "./dist/index.d.ts",
|
|
54
54
|
"import": "./dist/index.mjs",
|
|
55
|
-
"require": "./dist/index.js"
|
|
55
|
+
"require": "./dist/index.js",
|
|
56
|
+
"types": "./dist/index.d.ts"
|
|
57
|
+
},
|
|
58
|
+
"./helpers/*": {
|
|
59
|
+
"default": "./helpers/*.js",
|
|
60
|
+
"types": "./dist/helpers/*.d.ts"
|
|
56
61
|
}
|
|
57
62
|
},
|
|
58
|
-
"gitHead": "
|
|
63
|
+
"gitHead": "b844e20806d66574493b38d245dfeb3e954dc300"
|
|
59
64
|
}
|