-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtransport_json_parser.js
248 lines (226 loc) · 11.5 KB
/
transport_json_parser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
var path = require('path'),
fs = require('fs'),
request = require('request'),
direction_string = [],
generalData = [],
direction_JSON = [];
//Please use 'direction' to use it in other js file or use 'test' to print on command prompt
module.exports = function (src, dest, key){
return{
//********To use it in other JS file, returns a string with all the directions
direction: function(cb){
getDirections(src, dest, key, function(err, res){
if(err){
console.log(err);
}
cb(err, res[0].directionJSON);
});
},
//*********To use it for console
test: function(){
getDirections(src, dest, key, function(err, res){
for (var i = 0; i < res[0].directionString.length; i++) {
console.log(res[0].directionString[i]);
};
});
},
//*********To use it in other JS file, returns a JSON object with general details of the journey
getTransInfo: function(cb){
getDirections(src, dest, key, function(err, res){
if(err){
console.log(err);
}
cb(err, res[0].genDetails[0]);
});
}
}
};
function returnDir(direction_string_from_get){
direction_string = direction_string_from_get;
}
function generalTransportInfo(generalData){
var generalData = generalData;
return generalData;
}
//Main parser for JSON file
function parseDirection(src, dest, json){
if (src !== dest) {
var allDet = [];
var legs = json.routes[0].legs[0];
var steps = json.routes[0].legs[0].steps;
if (typeof (legs.departure_time) != 'undefined') {
//Extracting basic travel details
var time_zone = legs.departure_time.time_zone;
var depart_time = legs.departure_time.text;
var arrival_time = legs.arrival_time.text;
var duration = legs.duration.text;
var distance = legs.distance.text;
//Generating general information
generalData.push({
"arrival_time": arrival_time,
"departure_time": depart_time,
"distance": distance,
"duration": duration
});
generalTransportInfo(generalData);
direction_string.push("***********Route from " + src + " to " + dest + " in the time zone " + time_zone + "************");
//Displaying basic travel details
direction_string.push("\n\t#####Your Journey Details#####");
direction_string.push("\tDeparture time from " + src + ": " + depart_time +
"\n\tArrival time at " + dest + ": " + arrival_time +
"\n\tDistance: " + distance +
"\n\tduration: " + duration);
//Fetching Directions
if (typeof (json.routes[0].legs[0].steps) == 'undefined') {
direction_string.push("No Directions for the given source and destination");
//console.log("No Directions for the given source and destination");
} else {
//Printing directions until no steps left
var noStep = steps;
var steps_count = 1;
direction_string.push("\n\t---->Directions<----");
//console.log("\n\t---->Directions<----");
for (var i = 0; i < steps.length; i++) {
//****It Works. Only if you want to parse the steps from JSON****
/* while (typeof noStep !='undefined'){
var step_instructions = noStep[0].html_instructions;
console.log("\nStep"+steps_count+": "+noStep[0].travel_mode+": "+ step_instructions);
var start_end_location_step = {
slat: noStep[0].start_location.lat,
slon: noStep[0].start_location.lng,
elat: noStep[0].end_location.lat,
elon: noStep[0].end_location.lng
}
console.log("\n\tStart Location: "+start_end_location_step.slat, start_end_location_step.slon+
"\n\tEnd Location: "+start_end_location_step.elat, start_end_location_step.slon+
"\n\tDistance: "+noStep[0].distance.text+
"\n\tduration: "+noStep[0].duration.text);
noStep = noStep[0].steps;
steps_count++;
}
*/
//Checking the condition whether it's a transit or walking to calculate no. of stops
var no_of_stops = (typeof (steps[i].transit_details) !== 'undefined') ? steps[i].transit_details.num_stops : 'Not Applicable';
var line_name = (typeof (steps[i].transit_details) !== 'undefined')? steps[i].transit_details.line.short_name : 'Not Applicable';
var direction = (typeof (steps[i].transit_details) !== 'undefined')? steps[i].transit_details.headsign : 'Not Applicable';
//Puhsing in direction JSON
direction_JSON.push({
"step": (i + 1),
"travel_mode": steps[i].travel_mode,
"instructions": steps[i].html_instructions,
"start_location": steps[i].start_location.lat+", "+steps[i].start_location.lng,
"end_location": steps[i].end_location.lat+", "+steps[i].end_location.lng,
"distance": steps[i].distance.text,
"duration": steps[i].duration.text,
"num_stops": no_of_stops,
"line": line_name,
"direction": direction
});
direction_string.push("\n\tStep" + (i + 1) + ":\n\tTravel Mode: " + steps[i].travel_mode +
"\n\tInstruction: " + steps[i].html_instructions +
"\n\tStart Location: " + steps[i].start_location.lat+", "+ steps[i].start_location.lng +
"\n\tEnd Location: " + steps[i].end_location.lat+", "+steps[i].end_location.lng +
"\n\tDistance: " + steps[i].distance.text +
"\n\tduration: " + steps[i].duration.text +
"\n\tNumber of Stops: " + no_of_stops+
"\n\tLine: "+line_name+
"\n\tDirection: "+direction);
}
}
} else {
direction_string.push("***********Route from " + src + " to " + dest + "*************");
var duration = legs.duration.text;
var distance = legs.distance.text;
//Displaying basic travel details
direction_string.push("\n\t#####Your Journey Details#####"+
"\n\t@@ 'No transport available for this journey, so don't be lazy and start walking' @@"+
"\n\tDistance: " + distance +
"\n\tduration: " + duration);
//Fetching Directions
if (typeof (json.routes[0].legs[0].steps) == 'undefined') {
direction_string.push("No Directions for the given source and destination");
} else {
//Printing directions until no steps left
var noStep = steps;
var steps_count = 1;
direction_string.push("\n\t---->Directions<----");
for (var i = 0; i < steps.length; i++) {
//****It Works. Only if you want to parse the steps from JSON****
/* while (typeof noStep !='undefined'){
var step_instructions = noStep[0].html_instructions;
console.log("\nStep"+steps_count+": "+noStep[0].travel_mode+": "+ step_instructions);
var start_end_location_step = {
slat: noStep[0].start_location.lat,
slon: noStep[0].start_location.lng,
elat: noStep[0].end_location.lat,
elon: noStep[0].end_location.lng
}
console.log("\n\tStart Location: "+start_end_location_step.slat, start_end_location_step.slon+
"\n\tEnd Location: "+start_end_location_step.elat, start_end_location_step.slon+
"\n\tDistance: "+noStep[0].distance.text+
"\n\tduration: "+noStep[0].duration.text);
noStep = noStep[0].steps;
steps_count++;
}
*/
//Checking the condition whether it's a transit or walking to calculate no. of stops
var no_of_stops = (typeof (steps[i].transit_details) !== 'undefined') ? steps[i].transit_details.num_stops : 'Not Applicable'
var line_name = (typeof (steps[i].transit_details) !== 'undefined')? steps[i].transit_details.line.short_name : 'Not Applicable';
var direction = (typeof (steps[i].transit_details) !== 'undefined')? steps[i].transit_details.headsign : 'Not Applicable';
//Puhsing in direction JSON
direction_JSON.push({
"step": (i + 1),
"travel_mode": steps[i].travel_mode,
"instructions": steps[i].html_instructions,
"start_location": steps[i].start_location.lat+", "+steps[i].start_location.lng,
"end_location": steps[i].end_location.lat+", "+steps[i].end_location.lng,
"distance": steps[i].distance.text,
"duration": steps[i].duration.text,
"num_stops": no_of_stops,
"line": line_name,
"direction": direction
});
direction_string.push("\n\tStep" + (i + 1) + ":\n\tTravel Mode: " + steps[i].travel_mode +
"\n\tInstruction: " + steps[i].html_instructions +
"\n\tStart Location: " + steps[i].start_location.lat+", "+steps[i].start_location.lng +
"\n\tEnd Location: " + steps[i].end_location.lat+", "+ steps[i].end_location.lng +
"\n\tDistance: " + steps[i].distance.text +
"\n\tduration: " + steps[i].duration.text +
"\n\tNumber of Stops: " + no_of_stops);
}
}
}
returnDir(direction_string);
allDet.push({
"directionString": direction_string,
"genDetails": generalData,
"directionJSON": direction_JSON
});
return allDet;
} else {
direction_string.push("Error!! Please enter different source and destination");
return direction_string
}
}
function getDirections(src, dest, key, callback) {
request.get('https://maps.googleapis.com/maps/api/directions/json?alternatives=true&origin=' + src +
'Berlin&destination=' + dest + ',Berlin&mode=transit&key='+key+'',
function(error, response, body) {
if (!error && response.statusCode == 200) {
var direction_string = [];
direction_string.push("\nFetching the Directions....\n");
var json = JSON.parse(body);
if (json.status == "NOT_FOUND") {
direction_string.push("source or destination not found");
} else {
var temp = parseDirection(src, dest, json);
console.log("Hello");
callback(null,temp);
}
} else {
direction_string.push("Error! " + error);
console.log("Error! " + error);
callback(error);
}
});
}