2018-10-23 23:45:42 +08:00
|
|
|
'use strict';
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function TablesExtentBackend (pgQueryRunner) {
|
2015-04-27 18:55:20 +08:00
|
|
|
this.pgQueryRunner = pgQueryRunner;
|
|
|
|
}
|
|
|
|
|
2018-04-10 15:40:09 +08:00
|
|
|
module.exports = TablesExtentBackend;
|
2015-04-27 18:55:20 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a username and a list of tables it will return the estimated extent in SRID 4326 for all the tables based on
|
|
|
|
* the_geom_webmercator (SRID 3857) column.
|
|
|
|
*
|
|
|
|
* @param {String} username
|
|
|
|
* @param {Array} tableNames The named can be schema qualified, so this accepts both `schema_name.table_name` and
|
|
|
|
* `table_name` format as valid input
|
|
|
|
* @param {Function} callback function(err, result) {Object} result with `west`, `south`, `east`, `north`
|
|
|
|
*/
|
2018-04-10 15:40:09 +08:00
|
|
|
TablesExtentBackend.prototype.getBounds = function (username, tables, callback) {
|
2019-10-22 01:07:24 +08:00
|
|
|
var estimatedExtentSQLs = tables.map(function (table) {
|
2016-02-10 02:06:34 +08:00
|
|
|
return "ST_EstimatedExtent('" + table.schema_name + "', '" + table.table_name + "', 'the_geom_webmercator')";
|
2015-04-27 18:55:20 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
var query = [
|
2019-10-22 01:07:24 +08:00
|
|
|
'WITH ext as (' +
|
|
|
|
'SELECT ST_Transform(ST_SetSRID(ST_Extent(ST_Union(ARRAY[',
|
|
|
|
estimatedExtentSQLs.join(','),
|
|
|
|
'])), 3857), 4326) geom)',
|
|
|
|
'SELECT',
|
|
|
|
'ST_XMin(geom) west,',
|
|
|
|
'ST_YMin(geom) south,',
|
|
|
|
'ST_XMax(geom) east,',
|
|
|
|
'ST_YMax(geom) north',
|
|
|
|
'FROM ext'
|
2015-04-27 18:55:20 +08:00
|
|
|
].join(' ');
|
|
|
|
|
2015-12-31 00:44:49 +08:00
|
|
|
this.pgQueryRunner.run(username, query, function handleBoundsResult (err, rows) {
|
|
|
|
if (err) {
|
|
|
|
var msg = err.message ? err.message : err;
|
|
|
|
return callback(new Error('could not fetch source tables: ' + msg));
|
|
|
|
}
|
|
|
|
var result = null;
|
|
|
|
if (rows.length > 0) {
|
|
|
|
result = {
|
2019-06-17 16:43:30 +08:00
|
|
|
bbox: rows[0]
|
2015-12-31 00:44:49 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
callback(null, result);
|
|
|
|
});
|
2015-04-27 18:55:20 +08:00
|
|
|
};
|