-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
141 lines (124 loc) · 3.19 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
"use strict";
const geojsonVt = require("geojson-vt");
const vtPbf = require("vt-pbf");
const request = require("requestretry");
const zlib = require("zlib");
const NodeCache = require("node-cache" );
const _ = require("lodash");
const url = process.env.GRAPHQL_URL || "https://api.stadtnavi.de/routing/v1/router/index/graphql";
const query = `
query bikeparks {
bikeParks {
bikeParkId
name
lon
lat
covered
spacesAvailable
maxCapacity
}
}`;
const getBikeParks = (url, callback) => {
console.log(`Requesting bike parking lots from ${url}`);
request(
{
url: url,
body: query,
maxAttempts: 120,
retryDelay: 30000,
method: "POST",
headers: {
'Content-Type': 'application/graphql'
}
},
function(err, res, body) {
if (err) {
console.log(`Error when downloading GeoJSON data from ${url}: ${err} ${res} ${body}`);
callback(err);
return;
}
callback(null, convertToGeoJson(JSON.parse(body)));
}
);
};
const convertToGeoJson = (json) => {
const features = json.data.bikeParks.map(
({ lat, lon, bikeParkId, name, covered, spacesAvailable, maxCapacity }) => {
return {
type: "Feature",
geometry: {type: "Point", coordinates: [lon, lat]},
properties: {
id: bikeParkId,
name,
covered,
spacesAvailable,
maxCapacity
}
}
}
);
console.log(`Fetched ${features.length} bike parks from ${url}`);
return {
type: "FeatureCollection",
features
};
}
class BikeParkSource {
constructor(uri, callback) {
this.cacheKey = "tileindex";
const ttl = 60 * 60 * 6; // 6 hours
this.cache = new NodeCache({ stdTTL: ttl, useClones: false });
callback(null, this);
}
fetchGeoJson(callback){
getBikeParks(url, (err, geojson) => {
if (err) {
callback(err);
return;
}
callback(geojson);
});
}
getTile(z, x, y, callback) {
if(this.cache.get(this.cacheKey)) {
const geojson = this.cache.get(this.cacheKey);
this.computeTile(geojson, z, x, y, callback);
} else {
this.fetchGeoJson((geojson) => {
this.cache.set(this.cacheKey, geojson);
this.computeTile(geojson, z, x, y, callback);
});
}
}
computeTile(geoJson, z, x, y, callback) {
const tileIndex = geojsonVt(geoJson, { maxZoom: 20, buffer: 512 });
let tile = tileIndex.getTile(z, x, y);
if (tile === null) {
tile = { features: [] };
}
const data = Buffer.from(vtPbf.fromGeojsonVt({ bikeparks: tile }));
zlib.gzip(data, function(err, buffer) {
if (err) {
callback(err);
return;
}
callback(null, buffer, { "content-encoding": "gzip", "cache-control": "public,max-age=3600" });
});
}
getInfo(callback) {
callback(null, {
format: "pbf",
maxzoom: 20,
vector_layers: [
{
description: "Bike parks retrieved from OTP",
id: "bikeparks"
}
]
});
}
}
module.exports = BikeParkSource;
module.exports.registerProtocols = tilelive => {
tilelive.protocols["bikeparks:"] = BikeParkSource;
};