forked from HSLdevcom/tilelive-otp-stops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
192 lines (173 loc) · 4.29 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
"use strict"
const geojsonVt = require('geojson-vt');
const vtPbf = require('vt-pbf');
const request = require('requestretry');
const zlib = require('zlib');
const _ = require('lodash')
Array.prototype.flatMap = function(lambda) {
return [].concat.apply([],this.map(lambda));
};
Array.prototype.uniq = function() {
return _.uniqWith(this, _.isEqual)
}
const getTileIndex = (url, query, map, callback) => {
request({
url: url,
body: query,
maxAttempts: 120,
retryDelay: 30000,
method: 'POST',
headers: {
'Content-Type': 'application/graphql',
'OTPTimeout': '60000',
'OTPMaxResolves': '100000000'
}
}, function (err, res, body){
if (err){
console.log(err)
callback(err);
return;
}
callback(null, geojsonVt(map(JSON.parse(body)), {
maxZoom: 20,
buffer: 1024,
})); //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
}
}
}
}
`;
const stationQuery = `
query stations{
stations{
gtfsId
name
lat
lon
locationType
stops {
gtfsId
patterns {
route {
mode
shortName
}
}
}
}
}
`;
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,
desc: stop.desc,
parentStation: stop.parentStation == null ? null : stop.parentStation.gtfsId,
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,
})))
}
}))
})
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:"
getTileIndex(uri, stopQuery, stopMapper, (err, stopTileIndex) => {
if (err){
callback(err);
return;
}
this.stopTileIndex = stopTileIndex;
getTileIndex(uri, stationQuery, stationMapper, (err, stationTileIndex) => {
if (err){
callback(err);
return;
}
this.stationTileIndex = stationTileIndex;
console.log("stops loaded from:", uri.host + uri.path)
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, {
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
}