-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
210 lines (190 loc) · 5.05 KB
/
index.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
"use strict"
const geojsonVt = require('geojson-vt');
const vtPbf = require('vt-pbf');
const request = require('requestretry');
const zlib = require('zlib');
const _ = require('lodash')
// cache indices
const stationIndex = {};
const stopIndex = {};
Array.prototype.flatMap = function(lambda) {
return [].concat.apply([],this.map(lambda));
};
Array.prototype.uniq = function() {
return _.uniqWith(this, _.isEqual)
}
const getTileIndex = (url, cachedIndex, query, map, callback) => {
if (cachedIndex) {
callback(null, cachedIndex);
} else {
request({
url: url,
body: query,
maxAttempts: 120,
retryDelay: 30000,
method: 'POST',
headers: {
'Content-Type': 'application/graphql',
'OTPTimeout': '120000',
'OTPMaxResolves': '100000000'
}
}, function (err, res, body){
if (err){
console.log(err)
callback(err);
return;
}
callback(null, geojsonVt(map(JSON.parse(body)), {
maxZoom: 20,
buffer: 64,
})); //TODO: this should be configurable)
})
}
}
const stopQuery = `
query stops {
stops{
gtfsId
name
code
platformCode
lat
lon
locationType
desc
parentStation {
gtfsId
}
patterns {
headsign
route {
mode
shortName
gtfsType: type
}
}
}
}
`;
const stationQuery = `
query stations{
stations{
gtfsId
name
lat
lon
locationType
stops {
gtfsId
patterns {
route {
mode
shortName
gtfsType: type
}
}
}
}
}
`;
const stopMapper = data => ({
type: "FeatureCollection",
features: data.data.stops.map(stop => ({
type: "Feature",
geometry: {type: "Point", coordinates: [stop.lon, stop.lat]},
properties: {
gtfsId: stop.gtfsId,
name: stop.name,
code: stop.code,
platform: stop.platformCode == null ? 'null' : stop.platformCode, // TODO: 'null' -string should be changed to null after the map style of HSL app has been updated.
desc: stop.desc,
parentStation: stop.parentStation == null ? 'null' : stop.parentStation.gtfsId, // TODO: 'null' -string should be changed to null after the map style of HSL app has been updated.
type: stop.patterns == null ? null : stop.patterns.map(pattern => pattern.route.mode).uniq().join(","),
patterns: stop.patterns == null ? null : JSON.stringify(stop.patterns.map(pattern => ({
headsign: pattern.headsign,
type: pattern.route.mode,
shortName: pattern.route.shortName,
gtfsType: pattern.route.gtfsType,
})))
}
}))
})
const stationMapper = data => ({
type: "FeatureCollection",
features: data.data.stations.map(station => ({
type: "Feature",
geometry: {type: "Point", coordinates: [station.lon, station.lat]},
properties: {
gtfsId: station.gtfsId,
name: station.name,
type: Array.from(new Set(station.stops.flatMap(stop => stop.patterns.flatMap(pattern => pattern.route.mode)))).join(','),
stops: JSON.stringify(station.stops.map(stop => stop.gtfsId)),
routes: JSON.stringify(station.stops.flatMap(stop => stop.patterns.flatMap(pattern => pattern.route)).uniq()),
}
}))
})
class GeoJSONSource {
constructor(uri, callback){
uri.protocol = "http:"
const key = uri.host + uri.path;
getTileIndex(uri, stopIndex[key], stopQuery, stopMapper, (err, stopTileIndex) => {
if (err){
callback(err);
return;
}
this.stopTileIndex = stopTileIndex;
stopIndex[key] = stopTileIndex;
getTileIndex(uri, stationIndex[key], stationQuery, stationMapper, (err, stationTileIndex) => {
if (err){
callback(err);
return;
}
this.stationTileIndex = stationTileIndex;
if (!stationIndex[key]) {
console.log("stops and stations loaded from:", uri.host + uri.path)
} else {
stationIndex[key] = stationTileIndex;
}
callback(null, this);
})
})
};
getTile(z, x, y, callback){
let stopTile = this.stopTileIndex.getTile(z, x, y)
let stationTile = this.stationTileIndex.getTile(z, x, y)
if (stopTile === null){
stopTile = {features: []}
}
if (stationTile === null){
stationTile = {features: []}
}
const data = Buffer.from(vtPbf.fromGeojsonVt({stops: stopTile, stations: stationTile}));
zlib.gzip(data, function (err, buffer) {
if (err){
callback(err);
return;
}
callback(null, buffer, {"content-encoding": "gzip"})
})
}
getInfo(callback){
callback(null, {
name: "Stops",
format: "pbf",
maxzoom: 20,
minzoom: 0,
vector_layers: [{
description: "",
id: "stops"
},
{
description: "",
id: "stations"
}]
})
}
}
module.exports = GeoJSONSource
module.exports.registerProtocols = (tilelive) => {
tilelive.protocols['otpstops:'] = GeoJSONSource
}