-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
182 lines (149 loc) · 5.2 KB
/
server.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
require('dotenv').config()
const environment = process.env.NODE_ENV;
const port = process.env.PORT || 3030;
const isDev = environment === 'development';
const fs = require('fs');
const path = require('path');
const request = require('request');
const zlib = require('zlib');
const clone = require('clone');
const express = require('express');
const morgan = require('morgan');
const Pbf = require('pbf');
const MBTiles = require('@mapbox/mbtiles');
const VectorTile = require('@mapbox/vector-tile').VectorTile;
const MBILES_FILE = './tiles/us-constituencies.mbtiles';
let tiles; // mbtiles to render
let tileInfo; // mbtiles metadata
const loadTiles = () => new Promise((resolve) => {
const mbtilesFile = path.resolve(process.cwd(), MBILES_FILE);
// check file exists
const mbtilesStats = fs.statSync(mbtilesFile);
if (!mbtilesStats.isFile() || mbtilesStats.size === 0) {
console.log(`ERROR: Not valid MBTiles file: ${mbtilesFile}`);
process.exit(1);
}
// Load MBTiles
tiles = new MBTiles(mbtilesFile, (err) => {
if (err) {
console.error(err);
console.log('ERROR: Unable to open MBTiles.');
console.log(` Make sure ${path.basename(mbtilesFile)} is valid MBTiles.`);
process.exit(1);
}
// load tileset metadata
tiles.getInfo((err, info) => {
if (err || !info) {
console.error(err);
console.log('ERROR: Metadata missing in the MBTiles.');
console.log(` Make sure ${path.basename(mbtilesFile)} is valid MBTiles.`);
process.exit(1);
}
tileInfo = info;
console.log('MBTiles loaded:', tileInfo);
resolve();
});
});
});
const startServer = async () => {
await loadTiles(); // wait for tiles to laod
const app = express();
app.use(morgan('tiny'));
// serve style for map
app.get('/:theme/style.json', (req, res, next) => {
res.header("Content-Type",'application/json');
res.sendFile(path.join(__dirname, `/styles/${req.params.theme}/style.json`));
});
// serve tile info
app.get('/:id.json', (req, res, next) => {
const info = clone(tileInfo);
// set url for fetching tiles
info.tiles = [`http://localhost:3030/{z}/{x}/{y}.${tileInfo.format}`];
return res.json(info);
});
// handle tile requests in /z/x/y format
app.get('/:z(\\d+)/:x(\\d+)/:y(\\d+).:format([\\w.]+)', (req, res, next) => {
let { x, y, z, format } = req.params;
// convert to int and truncate
x = x | 0;
y = y | 0;
z = z | 0;
// check given format is supported by tileset
if (format !== tileInfo.format && format !== 'geojson') {
return res.status(404).send(`Unsupported format: ${format}`);
}
// check valid bounds
if (z < tileInfo.minzoom || x < 0 || y < 0 || z > tileInfo.maxzoom || x >= Math.pow(2, z) || y >= Math.pow(2, z)) {
return res.status(404).send('Out of bounds');
}
// lookup tile in tileset
tiles.getTile(z, x, y, (err, tileData, headers) => {
if (err) {
if (/does not exist/.test(err.message)) {
return res.status(204).send();
} else {
return res.status(500).send(err.message);
}
}
if (tileData == null) {
return res.status(404).send('Not found');
}
// check if tile data is gzipped
let isGzipped;
if (tileInfo.format === 'pbf') {
isGzipped = tileData.slice(0, 2).indexOf(Buffer.from([0x1f, 0x8b])) === 0;
}
// if request is for geojson data, convert the pbf data to GeoJSON
if (format === 'geojson') {
if (isGzipped) {
tileData = zlib.unzipSync(tileData);
isGzipped = false;
}
const tile = new VectorTile(new Pbf(tileData));
const geojson = {
"type": "FeatureCollection",
"features": []
};
for (let layerName in tile.layers) {
const layer = tile.layers[layerName];
for (let i = 0; i < layer.length; i++) {
const feature = layer.feature(i);
const featureGeoJSON = feature.toGeoJSON(x, y, z);
featureGeoJSON.properties.layer = layerName;
geojson.features.push(featureGeoJSON);
}
}
tileData = JSON.stringify(geojson);
}
// if request for pbf, no need to do additional parsing, can send pbf to client
if (format === 'pbf') {
headers['Content-Type'] = 'application/x-protobuf';
}
// if data is not compressed (such as when converting to geojson)
// gzip data before sending response
if (!isGzipped) {
tileData = zlib.gzipSync(tileData);
}
headers['Content-Encoding'] = 'gzip';
res.set(headers);
return res.status(200).send(tileData);
});
});
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});
app.get('/basemap', (req, res) => {
fs.readFile('./basemap.html', (err, file) => {
if (err) {
console.error(err);
return res.status(500);
}
res.header("Content-Type",'text/html');
res.send(file.toString().replace('MAPBOX_ACCESS_TOKEN', process.env.MAPBOX_ACCESS_TOKEN));
});
});
app.listen(port, () => {
console.log(`Server listening at http://localhost:${port}`)
});
}
startServer();