@furkot/directions 2.0.0 → 2.0.1

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
- return status.success;
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
  }
@@ -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 minDistance(locations, units) {
113
+ function directDistance(locations, units) {
113
114
  units = units === 'miles' ? util.metersInMile : util.metersInKm;
114
- return 0.9 * toLatLon(locations[0]).distanceTo(toLatLon(util.last(locations))) / units;
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
- response.trip.summary.length > minDistance(response.trip.locations, response.trip.units)) {
136
- return status.success;
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
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@furkot/directions",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "description": "Directions service for Furkot",
5
5
  "author": {
6
6
  "name": "Damian Krzeminski",