-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
128 lines (110 loc) · 3.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
const config = require('config')
const fs = require('fs')
const express = require('express')
const MBTiles = require('@mapbox/mbtiles')
const vtpbf = require('vt-pbf')
const zlib = require('zlib')
const vtcomposite = require('@mapbox/vtcomposite')
const farms = require(config.get('farmsPath'))
const port = config.get('port')
const htdocsPath = config.get('htdocsPath')
const stylePath = config.get('stylePath')
const servicePath = config.get('servicePath')
const resource = fs.readFileSync(config.get('resourcePath'))
const emptyTile = zlib.gzipSync(vtpbf({ features: [] }))
const etag = 'a'
const compositeOp = { compress: true }
let mbtilesPool = {}
const buildStyle = (stylePath) => {
let style = require(stylePath)
style.sources.v.tiles[0] = config.get('templateUrl')
style.sources.v.attribution = config.get('attribution')
style.sprite = config.get('spriteUrl')
return JSON.stringify(style, null, 2)
}
const style = buildStyle(stylePath)
const root = buildStyle(stylePath)
// root.sources.v = { type: 'vector', url: '../../' }
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)))
}
const app = express()
app.use(express.static(htdocsPath))
const openMbtiles = (mbtilesPath) => {
return new Promise((resolve, reject) => {
new MBTiles(`${mbtilesPath}?mode=ro`, (err, mbtiles) => {
if (err) {
reject(err)
}
resolve(mbtiles)
})
})
}
const getMbtiles = (mbtilesPath) => {
let mbtiles = mbtilesPool[mbtilesPath]
if (!mbtiles) {
try {
mbtiles = mbtilesPool[mbtilesPath] = openMbtiles(mbtilesPath)
} catch (e) {
console.error(e)
}
}
return mbtiles
}
const getEachTile = (mbtiles, z, x, y) => {
return new Promise((resolve, reject) => {
mbtiles.getTile(z, x, y, (err, tile, headers) => {
if (err) reject(err)
resolve(tile)
})
})
}
const getConcatenatedTile = (z, x, y) => {
return new Promise(async (resolve, reject) => {
let tiles = []
for (let key in farms) {
const mbtilesPath = farms[key].getMbtilesPath(z, x, y)
let mbtiles = getMbtiles(mbtilesPath)
try {
let tile = await getEachTile(await mbtiles, z, x, y)
if (tile) tiles.push({ buffer: tile, z: z, x: x, y: y })
} catch (e) {
continue
}
}
if (tiles.length === 0) resolve(emptyTile)
vtcomposite(tiles, { z: z, x: x, y: y }, compositeOp, (err, result) => {
if (err) {
reject(err)
}
resolve(result)
})
})
}
app.get(`/zxy/:z/:x/:y.pbf`, async (req, res) => {
const z = parseInt(req.params.z)
const x = parseInt(req.params.x)
const y = parseInt(req.params.y)
getConcatenatedTile(z, x, y).then(tile => {
res.set('content-type', 'application/vnd.mapbox-vector-tile')
res.set('content-encoding', 'gzip')
res.send(tile)
}).catch(e => {
res.status(404).send(`tile not found /zxy/${z}/${z}/${y}.pbf: ${e}`)
})
})
app.get('/style.json', async (req, res) => {
res.send(style)
})
app.get(servicePath, async (req, res) => {
res.set('content-type', 'application/json')
res.send(resource)
})
app.get(`${servicePath}/resources/styles/root.json`, async (req, res) => {
res.send(style)
})
app.listen(port)