@furkot/directions 2.0.0 → 2.0.2
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.
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
// https://openrouteservice.org/dev/#/api-docs/directions
|
|
2
|
+
|
|
3
|
+
const LatLon = require('geodesy/latlon-spherical');
|
|
1
4
|
const { pathType, travelMode } = require("../../model");
|
|
2
5
|
const status = require('../status');
|
|
3
6
|
const util = require('../util');
|
|
@@ -17,6 +20,7 @@ const profileRestrictions = {
|
|
|
17
20
|
}
|
|
18
21
|
};
|
|
19
22
|
const ferryType = 9;
|
|
23
|
+
const maxRoundabout = 12 * 60 * 60; // 12 hours maximum for route that is 10 times longer than direct distance
|
|
20
24
|
|
|
21
25
|
function nextFerry(result, points = [-1, -1]) {
|
|
22
26
|
let { waytypes } = result;
|
|
@@ -87,9 +91,35 @@ function extractDirections(result, { distance, duration, steps }, i) {
|
|
|
87
91
|
return result;
|
|
88
92
|
}
|
|
89
93
|
|
|
94
|
+
function toLatLon(p) {
|
|
95
|
+
return new LatLon(p[1], p[0]);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function directDistance(locations) {
|
|
99
|
+
return locations.reduce((result, next) => {
|
|
100
|
+
next = toLatLon(next);
|
|
101
|
+
result.dist += result.ll.distanceTo(next);
|
|
102
|
+
result.ll = next;
|
|
103
|
+
return result;
|
|
104
|
+
}, {
|
|
105
|
+
dist: 0,
|
|
106
|
+
ll: toLatLon(locations[0])
|
|
107
|
+
}).dist;
|
|
108
|
+
}
|
|
109
|
+
|
|
90
110
|
function getStatus(st, response) {
|
|
91
111
|
if (!st && response && response.routes && response.routes.length) {
|
|
92
|
-
|
|
112
|
+
const { distance, duration } = response.routes.reduce((result, { summary}) => {
|
|
113
|
+
result.distance += summary.distance;
|
|
114
|
+
result.duration += summary.duration;
|
|
115
|
+
return result;
|
|
116
|
+
}, { distance: 0, duration: 0 });
|
|
117
|
+
const coordinates = response.metadata?.query?.coordinates;
|
|
118
|
+
const direct = coordinates ? directDistance(coordinates) : distance;
|
|
119
|
+
if (distance < 5 * direct || duration < maxRoundabout) {
|
|
120
|
+
return status.success;
|
|
121
|
+
}
|
|
122
|
+
delete response.routes;
|
|
93
123
|
}
|
|
94
124
|
return status.empty;
|
|
95
125
|
}
|
package/lib/service/util.js
CHANGED
|
@@ -106,6 +106,7 @@ function collateResults(results, query) {
|
|
|
106
106
|
|
|
107
107
|
function withTimeout(promise, millis, signal) {
|
|
108
108
|
let id;
|
|
109
|
+
let resolve;
|
|
109
110
|
let reject;
|
|
110
111
|
|
|
111
112
|
signal?.addEventListener('abort', onabort);
|
|
@@ -120,10 +121,11 @@ function withTimeout(promise, millis, signal) {
|
|
|
120
121
|
reject(signal.reason);
|
|
121
122
|
}
|
|
122
123
|
|
|
123
|
-
function timeoutPromise(
|
|
124
|
+
function timeoutPromise(_resolve, _reject) {
|
|
125
|
+
resolve = _resolve;
|
|
124
126
|
reject = _reject;
|
|
125
127
|
id = setTimeout(
|
|
126
|
-
() =>
|
|
128
|
+
() => resolve(),
|
|
127
129
|
millis
|
|
128
130
|
);
|
|
129
131
|
}
|
|
@@ -25,6 +25,7 @@ const costingOptions = {
|
|
|
25
25
|
};
|
|
26
26
|
const minSpeed = 15 * 1000 / 3600; // bump speed when it is absurdly low
|
|
27
27
|
const turnDistance = 100; // don't adjust speed of turns
|
|
28
|
+
const maxRoundabout = 12 * 60 * 60; // 12 hours maximum for route that is 10 times longer than direct distance
|
|
28
29
|
|
|
29
30
|
const maneuver = {
|
|
30
31
|
ferryEnter: 28,
|
|
@@ -109,9 +110,17 @@ function toLatLon(p) {
|
|
|
109
110
|
return new LatLon(p.lat, p.lon);
|
|
110
111
|
}
|
|
111
112
|
|
|
112
|
-
function
|
|
113
|
+
function directDistance(locations, units) {
|
|
113
114
|
units = units === 'miles' ? util.metersInMile : util.metersInKm;
|
|
114
|
-
return
|
|
115
|
+
return locations.reduce((result, next) => {
|
|
116
|
+
next = toLatLon(next);
|
|
117
|
+
result.dist += result.ll.distanceTo(next);
|
|
118
|
+
result.ll = next;
|
|
119
|
+
return result;
|
|
120
|
+
}, {
|
|
121
|
+
dist: 0,
|
|
122
|
+
ll: toLatLon(locations[0])
|
|
123
|
+
}).dist / units;
|
|
115
124
|
}
|
|
116
125
|
|
|
117
126
|
function getStatus(err, response) {
|
|
@@ -130,10 +139,14 @@ function getStatus(err, response) {
|
|
|
130
139
|
}
|
|
131
140
|
st = response.trip && response.trip.status;
|
|
132
141
|
if (st === 0) {
|
|
133
|
-
if (response.trip.legs && response.trip.legs.length
|
|
142
|
+
if (response.trip.legs && response.trip.legs.length) {
|
|
143
|
+
const { length, time } = response.trip.summary;
|
|
144
|
+
const distance = directDistance(response.trip.locations, response.trip.units);
|
|
134
145
|
// make sure points are not too far from roads
|
|
135
|
-
|
|
136
|
-
|
|
146
|
+
if (length > 0.9 * distance &&
|
|
147
|
+
(length < 10 * distance || time < maxRoundabout)) {
|
|
148
|
+
return status.success;
|
|
149
|
+
}
|
|
137
150
|
}
|
|
138
151
|
delete response.trip.legs;
|
|
139
152
|
return status.empty;
|
|
@@ -169,7 +182,7 @@ function init(options) {
|
|
|
169
182
|
if (query.avoidHighways) {
|
|
170
183
|
req.costing_options[req.costing].use_highways = 0;
|
|
171
184
|
} else {
|
|
172
|
-
req.costing_options[req.costing].use_highways = 1;
|
|
185
|
+
req.costing_options[req.costing].use_highways = 1.1;
|
|
173
186
|
}
|
|
174
187
|
vehicleSize(query, req.costing_options[req.costing]);
|
|
175
188
|
|