-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
139 lines (127 loc) · 3.22 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
const config = require('config')
const express = require('express')
const mbgl = require('@mapbox/mapbox-gl-native')
const vtpbf = require('vt-pbf')
const sharp = require('sharp')
const fs = require('fs')
const Queue = require('better-queue')
const zlib = require('zlib')
const genericPool = require('generic-pool')
const fetch = require('node-fetch')
const htdocsPath = config.get('htdocsPath')
const port = config.get('port')
const wideRenderZoom = config.get('wideRenderZoom')
const emptyTile = vtpbf({ features: [] })
let style
let maps
fetch('https://hfu.github.io/macrostyle/style.json')
.then(res => res.json())
.then(json => {
style = json
maps = prepareMaps()
})
const tile2long = (x, z) => {
return x / 2 ** z * 360 - 180
}
const tile2lat = (y, z) => {
const n = Math.PI - 2 * Math.PI * y / 2 ** z
return 180 / Math.PI * Math.atan(0.5 * (Math.exp(n) - Math.exp(-n)))
}
mbgl.on('message', msg => {
console.log(msg)
})
const dimension = z => {
return z > wideRenderZoom ? 512 : 256
}
const mbglRequestQueue = new Queue((req, cb) => {
let r = {}
fetch(req.url)
.then(res => {
if (res.headers.get('last-modified')) {
r.modified = new Date(res.headers.get('last-modified'))
}
if (res.headers.get('expires')) {
r.expires = new Date(res.headers.get('expires'))
}
if (res.headers.get('etag')) {
r.etag = res.headers.get('etag')
}
console.log(`${res.status} ${req.url} ${JSON.stringify(r)}`)
return res.status === 200 ? res.buffer() : emptyTile
})
.then(buffer => {
r.data = buffer
cb(null, r)
})
.catch(e => {
console.log(`${req.url} was caught.`)
cb(e)
})
}, { concurrent: 20 })
const prepareMaps = () => { return genericPool.createPool({
create: () => {
const map = new mbgl.Map({
request: (req, cb) => {
mbglRequestQueue.push(req, (err, data) => {
if (err) cb(err)
cb(null, data)
})
},
mode: 'tile'
})
map.load(style)
return map
},
destroy: (map) => {
map.release()
}
}, { max: 4, min: 2 }) }
const tileQueue = new Queue((r, cb) => {
const [z, x, y] = [r.z, r.x, r.y]
const center = [
tile2long(x + 0.5, z),
tile2lat(y + 0.5, z)
]
const d = dimension(z)
maps.acquire().then(map => {
map.render({
zoom: z - 1, center: center,
width: d, height: d
}, (err, buffer) => {
maps.release(map)
if (err) return cb(err)
let image = sharp(buffer, {
raw: {
width: d, height: d, channels: 4
}
})
if (z > wideRenderZoom) {
image = image.extract({
left: 128, top: 128, width: 256, height: 256
})
}
cb(null, image)
})
})
}, { concurrent: 2 })
const app = express()
app.use(express.static(htdocsPath))
app.get('/:z/:x/:y.png', (req, res) => {
tileQueue.push({
z: parseInt(req.params.z),
x: parseInt(req.params.x),
y: parseInt(req.params.y)
}, (err, image) => {
if (err) {
res.set('content-type', 'text/plain')
res.send(err)
} else {
res.set('content-type', 'image/png')
image.png().toBuffer()
.then((result) => {
res.send(result)
})
}
})
})
app.listen(port)