-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
113 lines (94 loc) · 2.88 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
// import mapnik from 'mapnik';
import express from 'express';
import path from 'path';
import fs from 'fs';
import compression from 'compression';
import { json } from 'body-parser';
// import csv from 'fast-csv';
import { csvtogeojson } from 'csvtogeojson';
// import csv from 'express-csv';
import tilelive from 'tilelive';
import tileliveFile from 'tilelive-file';
tileliveFile.registerProtocols(tilelive);
const app = express();
app.use(json());
app.use(compression());
const isDevelopment = process.env.NODE_ENV !== 'production';
const BUILDINGS_FILE = path.join(__dirname, 'resources/');
const WINTER_FILE = path.join(__dirname, 'data/winter/');
const SUMMER_FILE = path.join(__dirname, 'data/summer/');
const ENTRY_FILE = path.join(__dirname, 'index.html');
const PORT = isDevelopment ? 3000 : process.env.PORT;
app.use(express.static(__dirname));
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
tilelive.load(`file://${WINTER_FILE}`, (err, source) => {
if (err) throw err;
app.get('/winter/:z/:x/:y.png', (req, res) => {
const { z, x, y } = req.params;
source.getTile(z, x, y, (err, tile, headers) => {
if (err) {
res.status(404);
// res.send(err.message);
} else {
res.status(200);
res.set(headers);
res.send(tile);
}
});
});
});
tilelive.load(`file://${SUMMER_FILE}`, (err, source) => {
if (err) throw err;
// console.log(source);
app.get('/summer/:z/:x/:y.png', (req, res) => {
const { z, x, y } = req.params;
console.log(`Fetching %d %d %d ${z} ${x} ${y}`);
source.getTile(z, x, y, (err, tile, headers) => {
if (err) {
res.status(404);
// res.send(err.message);
} else {
res.status(200);
res.set(headers);
res.send(tile);
}
});
});
});
// const sthlmBuildings = fs.readFileSync(path.join(BUILDINGS_FILE, 'sthlm.csv'), 'utf8');
// console.log(sthlmBuildings);
//
// app.get('/buildings', (req, res) => {
// csvtogeojson(sthlmBuildings, (err, data) => {
// if (err) throw err;
//
// console.log(data);
//
// })
// });
// const sthlmBuildings = csvStream.fromPath(path.join(BUILDINGS_FILE, 'sthlm.csv'))
// .on('data', (data) => {})
// .on('end', () => {
// console.log(BUILDINGS_FILE, ' DONE');
// app.get('/buildings', (req, res) => {
// res.csv(sthlmBuildings);
// });
// });
// app.get('/:z/:x/:y', (req, res) => {
//
// const { z, x, y } = req.params;
//
// const map = new mapnik.Map(256, 256);
//
// const bbox = mercator.xyz_to_envelope(x, y, z, false);
//
// map.extent = bbox;
//
// })
// Port setup
app.set('port', PORT);
app.listen(app.get('port'));