Browse Source

Start Navitia handling

Phyks (Lucas Verney) 2 years ago
parent
commit
523e0020df
2 changed files with 52 additions and 4 deletions
  1. 51
    3
      src/api/index.js
  2. 1
    1
      src/views/Home.vue

+ 51
- 3
src/api/index.js View File

@@ -1,5 +1,6 @@
1 1
 import fetch from 'isomorphic-fetch';
2 2
 import moment from 'moment';
3
+import polyline from '@mapbox/polyline';
3 4
 
4 5
 import { BIKE, TRANSIT } from '@/constants';
5 6
 
@@ -18,10 +19,14 @@ export function getRVRoutes(startLatLng, endLatLng) {
18 19
                 {
19 20
                     geojson: path.points,
20 21
                     duration: moment.duration(path.time),
22
+                    durations: {
23
+                        bike: moment.duration(path.time),
24
+                    },
21 25
                     distances: {
22 26
                         bike: path.distance,
23 27
                     },
24 28
                     bbox: path.bbox,
29
+                    sections: null,  // TODO
25 30
                     elevations: path.elevations,
26 31
                     departure_date_time: moment(),
27 32
                     arrival_date_time: moment().add(path.time, 'ms'),
@@ -35,13 +40,56 @@ export function getRVRoutes(startLatLng, endLatLng) {
35 40
 }
36 41
 
37 42
 
43
+function computeNavitiaGeoJSON(sections) {
44
+    let coordinates = [];
45
+    sections.forEach((section) => {
46
+        if (section.geojson) {
47
+            coordinates = coordinates.concat(
48
+                // Should be lat, lng not lng, lat
49
+                section.geojson.coordinates.map(item => item.reverse),
50
+            );
51
+        } else {
52
+            const coordsFrom = section.from[section.from.embedded_type].coord;
53
+            coordinates.push([coordsFrom.lat, coordsFrom.lng]);
54
+
55
+            const coordsTo = section.to[section.to.embedded_type].coord;
56
+            coordinates.push([coordsTo.lat, coordsTo.lng]);
57
+        }
58
+    });
59
+
60
+    return polyline.encode(coordinates);
61
+}
62
+
63
+
38 64
 /**
39 65
  * Fetch routes from Navitia.
40
- * TODO
66
+ * TODO: Broaden search through forbidden_uris.
41 67
  */
42 68
 export function getNavitiaRoutes(startLatLng, endLatLng) {
43
-    console.log(startLatLng, endLatLng);
44
-    return [];
69
+    return fetch(
70
+        `http://127.0.0.1:8081/navitia/v1/journeys?from=${startLatLng.lat};${startLatLng.lng}&to=${endLatLng.lat},${endLatLng.lng}`,
71
+    )
72
+        .then(response => response.json())
73
+        .then(json => json.journeys.map(journey => ({
74
+            geojson: computeNavitiaGeoJSON(journey.sections),
75
+            duration: moment.duration(journey.duration),
76
+            durations: {
77
+                bike: moment.duration(journey.durations.bike),
78
+                walk: moment.duration(journey.durations.walking),
79
+                transit: (
80
+                    moment.duration(journey.durations.total) -
81
+                    moment.duration(journey.durations.bike) -
82
+                    moment.duration(journey.durations.walking)
83
+                ),
84
+            },
85
+            sections: journey.sections,  // TODO
86
+            distances: journey.distances,
87
+            bbox: null,  // TODO
88
+            elevations: null,
89
+            departure_date_time: moment(journey.departure_date_time),
90
+            arrival_date_time: moment(journey.arrival_date_time),
91
+            co2_emissions: journey.co2_emissions,
92
+        })));
45 93
 }
46 94
 
47 95
 

+ 1
- 1
src/views/Home.vue View File

@@ -96,7 +96,7 @@ export default {
96 96
                 this.transport_modes += mode;
97 97
             }
98 98
 
99
-            if (this.transport_modes > 0) {
99
+            if (this.transport_modes === 0) {
100 100
                 this.valid = false;
101 101
             } else {
102 102
                 this.valid = true;