Commit 9ea9f68e authored by Rodrigo Tapia-McClung's avatar Rodrigo Tapia-McClung

Added geojsonmvt

parent aa360524
//var fs = require('fs');
var geojson2mvt = require('geojson2mvt');
const sqlTableName = require('./utils/sqltablename.js');
var turf = require("@turf/turf");
const tableSQL = (params, query) => {
return `
SELECT
${query.columns}
FROM
${sqlTableName(params.table)}
-- Optional Filter
${query.filter ? `WHERE ${query.filter}` : '' }
-- Optional Group
${query.group ? `GROUP BY ${query.group}` : '' }
-- Optional sort
${query.sort ? `ORDER BY ${query.sort}` : '' }
-- Optional limit
${query.limit ? `LIMIT ${query.limit}` : '' }
`
}
const geojsonSQL = (params, query) => {
let bounds = query.bounds ? query.bounds.split(',').map(Number) : null;
bounds && bounds.length === 3 ? bounds = merc.bbox(bounds[1], bounds[2], bounds[0]) : null;
return `
SELECT
Row_to_json(fc) as geojson
FROM (
SELECT
'FeatureCollection' AS type,
COALESCE(Array_to_json(Array_agg(f)), '[]'::json) AS features
FROM (
SELECT
'Feature' AS type,
St_asgeojson(ST_Transform(lg.${query.geom_column}, 4326))::json AS geometry,
${query.columns ? `
Row_to_json(
(
SELECT
l
FROM
(SELECT ${query.columns}) AS l
)
) AS properties
` : `'{}'::json AS properties`}
FROM
${sqlTableName(params.table)} AS lg
${bounds ? `, (SELECT ST_SRID(${query.geom_column}) as srid FROM ${sqlTableName(params.table)} LIMIT 1) sq` : ''}
-- Optional Filter
${query.filter || bounds ? 'WHERE' : ''}
${query.filter ? `${query.filter}` : '' }
${query.filter && bounds ? 'AND' : ''}
${bounds ? `
${query.geom_column} &&
ST_Transform(
ST_MakeEnvelope(${bounds.join()}, 4326),
srid
)
` : ''}
) AS f
) AS fc;
`
}
module.exports = function(app, pool) {
app.get('/data/geojsonmvt/:table', async (req, res) => {
//console.log(req)
if (!req.query.geom_column) {
req.query.geom_column = 'geom';
}
const geojson = geojsonSQL(req.params, req.query);
//const bbox = tableSQL(req.params, req.query);
try {
const result = await pool.query(geojson);
let geoJson = result.rows[0].geojson;
const geojsonBBOX = turf.bbox(geoJson);
// minX, minY, maxX, maxY order
// minY, minX, maxY, maxX
// 1, 0, 3, 2
// south,west,north,east]
//res.json(geoJson);
//const box = await pool.query(bbox);
//res.json(box.row[0].geojson)
//const box = await pool.query(sqlStringBox);
//res.json(box.rows[0])
//const filePath = './bus_routes.geojson';
//let geoJson = result.rows[0].box;
//let geoJson = res.json(result.rows[0].geojson);
var options = {
rootDir: 'public/tiles',
//bbox : [40.426042,-74.599228,40.884448,-73.409958], //[south,west,north,east]
bbox: [geojsonBBOX[1], geojsonBBOX[0], geojsonBBOX[3], geojsonBBOX[2]],
zoom : {
min : 8,
max : 18,
},
layerName: 'denue',
};
//var geoJson = JSON.parse(fs.readFileSync(filePath, "utf8"));
// build the static tile pyramid
geojson2mvt(geoJson, options);
}
catch (err) {
console.log(err);
let status = 500;
switch (err.code) {
case '42P01':
// table does not exist
status = 422;
break;
case '42703':
// column does not exist
status = 422;
break;
default:
}
res.status(status).json({error:err.message})
}
})
}
\ No newline at end of file
......@@ -27,23 +27,7 @@ let header = {
"Content-Encoding":"gzip"
};
let options = {
extensions: ['pbf'],
setHeaders: function (res, path, req) {
let ext = path.split('.').pop()
//console.log(res)
if (ext === 'pbf') {
/*res.header('Content-Type', 'application/x-protobuf');
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET');
res.header('Access-Control-Allow-Headers', 'Content-Type');
//res.header('Content-Encoding', 'gzip');*/
res.header(header);
}
}
}
app.use('/', express.static(__dirname + '/public'));
//app.use('/', express.static(__dirname + '/public', options));
// Route which handles requests like the following: /<mbtiles-name>/0/1/2.pbf
app.get('/denue/:z/:x/:y.pbf', function(req, res) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment