Guest User

Untitled

a guest
Jun 24th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. /* GET /tiles/:z/:x/:y.mvt */
  2. /* Retreive a vector tile by tileid */
  3. router.get('/tiles/:z/:x/:y.mvt', async (req, res) => {
  4. const { z, x, y } = req.params;
  5.  
  6. // calculate the bounding polygon for this tile
  7. const bbox = mercator.bbox(x, y, z, false);
  8.  
  9. // Query the database, using ST_AsMVTGeom() to clip the geometries
  10. // Wrap the whole query with ST_AsMVT(), which will create a protocol buffer
  11. const SQL = `
  12. SELECT ST_AsMVT(q, 'project-centroids', 4096, 'geom')
  13. FROM (
  14. SELECT
  15. somecolumn,
  16. ST_AsMVTGeom(
  17. geom,
  18. ST_MakeEnvelope(${bbox[0]}, ${bbox[1]}, ${bbox[2]}, ${bbox[3]}, 4326),
  19. 4096,
  20. 256,
  21. false
  22. ) geom
  23. FROM sometable c
  24. ) q
  25. `;
  26.  
  27. try {
  28. const tile = await db.one(SQL);
  29.  
  30. // set the response header content type
  31. res.setHeader('Content-Type', 'application/x-protobuf');
  32.  
  33. // trigger catch if the vector tile has no data, (return a 404)
  34. if (tile.st_asmvt.length === 0) return;
  35. res.send(tile.st_asmvt);
  36. } catch (e) {
  37. res.status(404).send({
  38. error: e.toString(),
  39. });
  40. }
  41. });
Add Comment
Please, Sign In to add comment