Vector tile route: "cannot get/" error with MVT function in PostGIS code

Viewed 32

I am trying to reproduce the example of https://medium.com/nyc-planning-digital/using-the-new-mvt-function-in-postgis-75f8addc1d68

For some reason i do always receive the output "Cannot GET /" when going to localhost:5000.

Unfortunately after several tests i am not able to identify the error.

Please note: i tested the sql query separately and it works, so the error seem to be somewhere else.

Any ideas? Thank you for your help.

This is my code (i inserted three dots in place of the postgresql connection keys):

var express = require('express');
var SphericalMercator = require('@mapbox/sphericalmercator');
var router = express()

var mercator = new SphericalMercator({
    size: 256,
    antimeridian: true
});



var pgp = require('pg-promise')({});
var db = pgp("postgresql:// ...");

/* 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, 'internal-layer-name', 4096, 'geom') FROM (SELECT ST_GeomFromGeoJSON(geojson),ST_AsMVTGeom(ST_GeomFromGeoJSON(geojson),ST_MakeEnvelope(${bbox[0]}, ${bbox[1]}, ${bbox[2]}, ${bbox[3]} 4326),4096,256,false) geom FROM geojson_example_3 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 204)
    if (tile.st_asmvt.length === 0) {
      res.status(204);
    }

    // send the tile!
    res.send(tile.st_asmvt);
  } catch (e) {
    res.status(404).send({
      error: e.toString(),
    });
  }
});

//var server = router.listen(5000)

router.listen(5000, function(){
console.log('Server started...');
});
0 Answers
Related