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