forked from Jungle-Bus/unroll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
line_data.js
99 lines (88 loc) · 4.16 KB
/
line_data.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
/**
* This module is part of Unroll tool by Jungle Bus.
* It gets structured data about a public transport line by its id
* needs osmtogeojson (from https://github.com/tyrasd/osmtogeojson)
*/
var line_data = (function() {
var data_last_check;
var line_tags;
var trips = [];
return {
get_data_age: function(){
return data_last_check;
},
get_trips_number: function(){
return trips.length;
},
get_tags: function(){
return line_tags;
},
get_trips: function(){
return trips;
},
/**
* fetch and process data
* return a status
*/
init_from_overpass: async function(line_id){
try {
var overpass_url = `https://overpass-api.de/api/interpreter?data=[out:json][timeout:25];relation(${line_id});(._;>>;);out;`
var overpass_response = await fetch(overpass_url);
overpass_data = await overpass_response.json();
data_last_check = overpass_data['osm3s']['timestamp_osm_base'];
// extract tags and re-structure Overpass response
var other_relations = {}
for (i = 0; i < overpass_data['elements'].length; i++) {
if (overpass_data['elements'][i]['id'] == line_id) {
var relation = overpass_data['elements'][i];
line_tags = relation['tags'];
} else if (overpass_data['elements'][i]['type'] == "relation") {
var relation_id = overpass_data['elements'][i]['id'];
other_relations[relation_id] = overpass_data['elements'][i];
}
}
if (line_tags['type'] != 'route_master') {
return "This is not a public transport line"
}
//extract trips info, and convert stops and shapes to geojson
var data_as_geojson = osmtogeojson(overpass_data);
for (i = 0; i < relation['members'].length; i++) {
var route_id = relation['members'][i]['ref'];
var route = other_relations[route_id];
var geojson_elems = {}
for (j = 0; j < data_as_geojson['features'].length; j++) {
if (data_as_geojson['features'][j]['id'] == "relation/"+route_id) {
var geojson_feature = data_as_geojson['features'][j]
} else {
geojson_elems[data_as_geojson['features'][j]['id']] = data_as_geojson['features'][j]
}
}
var platform_list_as_geojson = []
route['members']
.filter(member => member['role'].startsWith("platform"))
.map(member => platform_list_as_geojson.push(geojson_elems[member['type'] + '/' + member['ref']]));
var stop_position_list_as_geojson = []
route['members']
.filter(member => member['role'].startsWith("stop"))
.map(member => stop_position_list_as_geojson.push(geojson_elems[member['type'] + '/' + member['ref']]));
var mode = route['tags']['route'];
if (["subway", "tram", "train", "railway"].includes(mode)){
var stop_list_as_geojson = stop_position_list_as_geojson
} else {
var stop_list_as_geojson = platform_list_as_geojson
}
trips.push({
"id": route_id,
"tags": route["tags"],
"shape": geojson_feature,
"stop_list" : stop_list_as_geojson,
});
}
return "ok"
} catch (error) {
console.error(error)
return "Oops, something went wrong"
}
}
}
}());