@wemap/osm 0.3.7 → 1.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.
package/package.json CHANGED
@@ -7,17 +7,17 @@
7
7
  "main": "index.js",
8
8
  "repository": {
9
9
  "type": "git",
10
- "url": "git+https://github.com/wemap/wemap-utils-js.git",
10
+ "url": "git+https://github.com/wemap/wemap-modules-js.git",
11
11
  "directory": "packages/osm"
12
12
  },
13
13
  "name": "@wemap/osm",
14
- "version": "0.3.7",
14
+ "version": "1.0.1",
15
15
  "bugs": {
16
- "url": "https://github.com/wemap/wemap-utils-js/issues"
16
+ "url": "https://github.com/wemap/wemap-modules-js/issues"
17
17
  },
18
- "homepage": "https://github.com/wemap/wemap-utils-js#readme",
18
+ "homepage": "https://github.com/wemap/wemap-modules-js#readme",
19
19
  "scripts": {
20
- "test": "mocha -r esm \\\"src/**/*.spec.js\\\""
20
+ "test": "mocha -r esm \"src/**/*.spec.js\""
21
21
  },
22
22
  "keywords": [
23
23
  "utils",
@@ -26,11 +26,11 @@
26
26
  ],
27
27
  "license": "ISC",
28
28
  "dependencies": {
29
- "@wemap/geo": "^1.0.3",
30
- "@wemap/graph": "^1.0.4",
31
- "@wemap/logger": "^0.1.7",
29
+ "@wemap/geo": "^1.0.5",
30
+ "@wemap/graph": "^1.0.6",
31
+ "@wemap/logger": "^0.1.8",
32
32
  "lodash.isnumber": "^3.0.3",
33
33
  "sax": "^1.2.4"
34
34
  },
35
- "gitHead": "ad902707055bfb5dbc3eb3e42188447019f599e2"
35
+ "gitHead": "1ccef947faf3f37aba592594efe06575b6542170"
36
36
  }
@@ -3,11 +3,12 @@ import {
3
3
  Level, WGS84
4
4
  } from '@wemap/geo';
5
5
  import {
6
- Edge, Node, Itinerary
6
+ Edge, Node, Itinerary, Step, Network
7
7
  } from '@wemap/graph';
8
8
  import {
9
- rad2deg, Utils as MathUtils
9
+ rad2deg, Utils as MathUtils, diffAngle, deg2rad
10
10
  } from '@wemap/maths';
11
+ import Logger from '@wemap/logger';
11
12
 
12
13
 
13
14
  class OsrmUtils {
@@ -164,6 +165,9 @@ class OsrmUtils {
164
165
  } = json.routes[0];
165
166
  const { steps } = legs[0];
166
167
 
168
+ /**
169
+ * Create nodes and edges from geometry
170
+ */
167
171
  let previousNode;
168
172
  const { coordinates } = geometry;
169
173
  for (let i = 0; i < coordinates.length; i++) {
@@ -179,48 +183,95 @@ class OsrmUtils {
179
183
  previousNode = node;
180
184
  }
181
185
 
182
- for (let i = 0; i < steps.length; i++) {
183
- const step = steps[i];
186
+ if (steps && steps.length > 0) {
187
+ OsrmUtils.parseSteps(itinerary, steps);
188
+ }
184
189
 
185
- const nodeData = {};
186
- const nextEdgeData = {};
190
+ return itinerary;
191
+ }
187
192
 
188
- if (step.wemap) {
189
- if (step.wemap.nodeTags) {
190
- nodeData.tags = step.wemap.nodeTags;
191
- }
193
+ static parseSteps(itinerary, jsonSteps) {
192
194
 
193
- if (step.wemap.nextEdgeTags) {
194
- nextEdgeData.osmTags = step.wemap.nextEdgeTags;
195
- if (nextEdgeData.osmTags.name) {
196
- nextEdgeData.name = nextEdgeData.osmTags.name;
197
- }
198
- }
199
- }
195
+ itinerary._steps = [];
196
+ itinerary._nextStepsIndexes = [];
197
+
198
+ for (let i = 0; i < jsonSteps.length; i++) {
199
+ const jsonStep = jsonSteps[i];
200
+
201
+ const step = new Step();
202
+ step.number = i + 1;
203
+ step.firstStep = i === 0;
204
+ step.lastStep = i === jsonSteps.length - 1;
205
+
206
+
207
+ /**
208
+ * Add nodes and edges to step
209
+ */
210
+ step.nodes = [];
211
+ step.edges = [];
200
212
 
201
- if (step.name) {
202
- nextEdgeData.name = step.name;
213
+ let previousNode;
214
+ let { coordinates } = jsonStep.geometry;
215
+
216
+ // The OSRM last step is twice the last node. So, remove the second.
217
+ if (step.lastStep) {
218
+ coordinates = [coordinates[0]];
203
219
  }
220
+ coordinates.forEach(coords => {
221
+ const wgs84 = OsrmUtils.jsonToWgs84(coords);
222
+ const node = itinerary.getNodeByCoords(wgs84);
223
+ if (!node) {
224
+ Logger.error('Cannot parse this itinerary coordinates');
225
+ return;
226
+ }
227
+ step.nodes.push(node);
204
228
 
205
- const hasNodeData = Object.keys(nodeData).length !== 0;
206
- const hasNextEdgeData = Object.keys(nextEdgeData).length !== 0;
207
- if (hasNodeData || hasNextEdgeData) {
208
- const stepLocation = step.maneuver.location;
209
- const location = OsrmUtils.jsonToWgs84(stepLocation);
210
- const existingNode = itinerary.getNodeByCoords(location);
229
+ const isLastNode = coordinates[coordinates.length - 1] === coords;
230
+ if (!isLastNode || step.lastStep) {
231
+ itinerary._nextStepsIndexes.push(i);
232
+ }
211
233
 
212
- if (hasNodeData) {
213
- existingNode.data = nodeData;
234
+ if (previousNode) {
235
+ const edge = Network.getEdgeByNodes(previousNode.edges, previousNode, node);
236
+ step.edges.push(edge);
214
237
  }
215
238
 
216
- if (hasNextEdgeData && i !== steps.length - 1) {
217
- const nextEdge = existingNode.edges[i === 0 ? 0 : 1];
218
- nextEdge.data = nextEdgeData;
239
+ previousNode = node;
240
+ });
241
+
242
+ /**
243
+ * Add common fields
244
+ */
245
+ step.previousBearing = step.firstStep
246
+ ? itinerary.start.bearingTo(itinerary.nodes[0].coords)
247
+ : deg2rad(jsonStep.maneuver.bearing_before);
248
+ step.nextBearing = step.lastStep
249
+ ? previousNode.coords.bearingTo(itinerary.end)
250
+ : deg2rad(jsonStep.maneuver.bearing_after);
251
+ step.angle = diffAngle(step.previousBearing, step.nextBearing + Math.PI);
252
+ step.name = jsonStep.name;
253
+
254
+ /**
255
+ * Wemap fields
256
+ */
257
+ if (jsonStep.wemap) {
258
+ const firstNode = itinerary.nodes[0];
259
+ if (jsonStep.wemap.nodeTags && firstNode) {
260
+ firstNode.data = { tags: jsonStep.wemap.nodeTags };
261
+ }
262
+
263
+ const firstEdge = itinerary.edges[0];
264
+ if (jsonStep.wemap.nextEdgeTags && firstEdge) {
265
+ firstEdge.data = {
266
+ tags: jsonStep.wemap.nextEdgeTags,
267
+ name: jsonStep.name
268
+ };
269
+
219
270
  }
220
271
  }
221
- }
222
272
 
223
- return itinerary;
273
+ itinerary._steps.push(step);
274
+ }
224
275
  }
225
276
  }
226
277
 
@@ -3,7 +3,9 @@ import { expect } from 'chai';
3
3
  import fs from 'fs';
4
4
  import path from 'path';
5
5
 
6
- import { Level } from '@wemap/geo';
6
+ import {
7
+ Level, WGS84
8
+ } from '@wemap/geo';
7
9
 
8
10
  import OsmParser from '../model/OsmParser';
9
11
  import OsmRouter from '../network/OsmRouter';
@@ -31,7 +33,67 @@ describe('OsrmUtils', () => {
31
33
 
32
34
  expect(json.routes[0].geometry.coordinates.length).equal(itinerary.nodes.length);
33
35
  expect(itinerary.nodes.length).equal(itinerary.edges.length + 1);
34
-
36
+ expect(itinerary.nodes.length).equal(19);
37
+
38
+ const { steps } = itinerary;
39
+ expect(steps.length).equal(5);
40
+
41
+ let step;
42
+ step = steps[0];
43
+ expect(WGS84.equalsTo(step.location, new WGS84(43.600777, 3.875607))).true;
44
+ expect(OsrmUtils.getModifierFromAngle(step.angle)).equals('left');
45
+ expect(step.nodes.length).equals(6);
46
+ expect(step.edges.length).equals(5);
47
+ expect(step.firstStep).true;
48
+ expect(step.lastStep).false;
49
+ expect(step.name).equals('');
50
+
51
+ step = steps[1];
52
+ expect(WGS84.equalsTo(step.location, new WGS84(43.599881, 3.876396))).true;
53
+ expect(OsrmUtils.getModifierFromAngle(step.angle)).equals('right');
54
+ expect(step.nodes.length).equals(12);
55
+ expect(step.edges.length).equals(11);
56
+ expect(step.firstStep).false;
57
+ expect(step.lastStep).false;
58
+ expect(step.name).equals('Boulevard Vieussens');
59
+
60
+ step = steps[2];
61
+ expect(WGS84.equalsTo(step.location, new WGS84(43.599577, 3.874655))).true;
62
+ expect(OsrmUtils.getModifierFromAngle(step.angle)).equals('left');
63
+ expect(step.nodes.length).equals(2);
64
+ expect(step.edges.length).equals(1);
65
+ expect(step.firstStep).false;
66
+ expect(step.lastStep).false;
67
+ expect(step.name).equals('Impasse Bizeray');
68
+
69
+ step = steps[3];
70
+ expect(WGS84.equalsTo(step.location, new WGS84(43.599063, 3.874623))).true;
71
+ expect(OsrmUtils.getModifierFromAngle(step.angle)).equals('right');
72
+ expect(step.nodes.length).equals(2);
73
+ expect(step.edges.length).equals(1);
74
+ expect(step.firstStep).false;
75
+ expect(step.lastStep).false;
76
+ expect(step.name).equals('Rue du Docteur Louis Perrier');
77
+
78
+ step = steps[4];
79
+ expect(WGS84.equalsTo(step.location, new WGS84(43.59906, 3.873865))).true;
80
+ expect(OsrmUtils.getModifierFromAngle(step.angle)).equals('left');
81
+ expect(step.nodes.length).equals(1);
82
+ expect(step.edges.length).equals(0);
83
+ expect(step.firstStep).false;
84
+ expect(step.lastStep).true;
85
+ expect(step.name).equals('Rue du Docteur Louis Perrier');
86
+
87
+ expect(itinerary._nextStepsIndexes.length).equals(itinerary.nodes.length);
88
+ for (let i = 0; i <= 4; i++) {
89
+ expect(itinerary._nextStepsIndexes[i]).equals(0);
90
+ }
91
+ for (let i = 5; i <= 15; i++) {
92
+ expect(itinerary._nextStepsIndexes[i]).equals(1);
93
+ }
94
+ expect(itinerary._nextStepsIndexes[16]).equals(2);
95
+ expect(itinerary._nextStepsIndexes[17]).equals(3);
96
+ expect(itinerary._nextStepsIndexes[18]).equals(4);
35
97
  });
36
98
 
37
99
  let osrmWemapJson, wemapItinerary;