Created
August 6, 2017 16:25
-
-
Save zimmicz/9e78d9888ab73abc7e87553b77999bc8 to your computer and use it in GitHub Desktop.
PostGIS MVT Express routing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require("express") | |
const app = express() | |
const { Pool } = require("pg") | |
const SphericalMercator = require("sphericalmercator") | |
const pool = new Pool({ | |
host: "localhost", | |
port: 15432, | |
user: "postgres", | |
database: "postgres" | |
}) | |
const mercator = new SphericalMercator() | |
app.use(express.static("./")) | |
app.get("/mvt/:x/:y/:z", function(req, res) { | |
let bbox = mercator.bbox(req.params.x, req.params.y, req.params.z) | |
console.log(bbox.join(", ")) | |
const sql = ` | |
SELECT ST_AsMVT('test', 4096, 'geom', q) | |
FROM ( | |
SELECT | |
code, | |
name, | |
ST_AsMVTGeom( | |
geom, | |
TileBBox(${req.params.z}, ${req.params.x}, ${req.params.y}, 3857), | |
4096, | |
0, | |
false | |
) geom | |
FROM cadastre_area | |
WHERE ST_Intersects(geom, (SELECT ST_Transform(ST_MakeEnvelope($1, $2, $3, $4, $5), 3857))) | |
) q` | |
const values = [bbox[0], bbox[1], bbox[2], bbox[3], 4326] | |
pool.query(sql, values , function(err, mvt) { | |
if (err) { | |
console.log(err) | |
} else { | |
res.setHeader('Content-Type', 'application/x-protobuf') | |
res.send(mvt.rows[0].st_asmvt) | |
} | |
}) | |
}) | |
app.listen(3000, () => { | |
console.log("Listening on port 3000") | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment