Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* GET /tiles/:z/:x/:y.mvt */
- /* Retreive a vector tile by tileid */
- router.get('/tiles/:z/:x/:y.mvt', async (req, res) => {
- const { z, x, y } = req.params;
- // calculate the bounding polygon for this tile
- const bbox = mercator.bbox(x, y, z, false);
- // Query the database, using ST_AsMVTGeom() to clip the geometries
- // Wrap the whole query with ST_AsMVT(), which will create a protocol buffer
- const SQL = `
- SELECT ST_AsMVT(q, 'project-centroids', 4096, 'geom')
- FROM (
- SELECT
- somecolumn,
- ST_AsMVTGeom(
- geom,
- ST_MakeEnvelope(${bbox[0]}, ${bbox[1]}, ${bbox[2]}, ${bbox[3]}, 4326),
- 4096,
- 256,
- false
- ) geom
- FROM sometable c
- ) q
- `;
- try {
- const tile = await db.one(SQL);
- // set the response header content type
- res.setHeader('Content-Type', 'application/x-protobuf');
- // trigger catch if the vector tile has no data, (return a 404)
- if (tile.st_asmvt.length === 0) return;
- res.send(tile.st_asmvt);
- } catch (e) {
- res.status(404).send({
- error: e.toString(),
- });
- }
- });
Add Comment
Please, Sign In to add comment