2016-03-08 21:34:57 +08:00
|
|
|
'use strict';
|
|
|
|
|
2016-05-19 18:13:49 +08:00
|
|
|
var dot = require('dot');
|
|
|
|
dot.templateSettings.strip = false;
|
2016-03-08 21:34:57 +08:00
|
|
|
var queue = require('queue-async');
|
2016-06-21 18:08:40 +08:00
|
|
|
var PSQL = require('cartodb-psql');
|
|
|
|
var turboCarto = require('turbo-carto');
|
|
|
|
|
2016-05-23 20:18:58 +08:00
|
|
|
var SubstitutionTokens = require('../../../utils/substitution-tokens');
|
2016-06-21 18:08:40 +08:00
|
|
|
var PostgresDatasource = require('../../../backends/turbo-carto-postgres-datasource');
|
2016-03-08 21:34:57 +08:00
|
|
|
|
2016-06-21 18:08:40 +08:00
|
|
|
function TurboCartoAdapter() {
|
2016-03-08 21:34:57 +08:00
|
|
|
}
|
|
|
|
|
2016-04-26 21:59:41 +08:00
|
|
|
module.exports = TurboCartoAdapter;
|
2016-03-08 21:34:57 +08:00
|
|
|
|
2016-05-23 22:50:26 +08:00
|
|
|
TurboCartoAdapter.prototype.getMapConfig = function (user, requestMapConfig, params, context, callback) {
|
2016-03-08 21:34:57 +08:00
|
|
|
var self = this;
|
|
|
|
|
2016-05-23 21:18:20 +08:00
|
|
|
var layers = requestMapConfig.layers;
|
|
|
|
|
2016-03-08 21:34:57 +08:00
|
|
|
if (!layers || layers.length === 0) {
|
2016-05-23 21:18:20 +08:00
|
|
|
return callback(null, requestMapConfig);
|
2016-03-08 21:34:57 +08:00
|
|
|
}
|
|
|
|
|
2016-04-26 21:59:41 +08:00
|
|
|
var parseCartoQueue = queue(layers.length);
|
2016-03-08 21:34:57 +08:00
|
|
|
|
2016-06-13 18:20:56 +08:00
|
|
|
layers.forEach(function(layer, index) {
|
2016-06-21 18:08:40 +08:00
|
|
|
parseCartoQueue.defer(self._parseCartoCss.bind(self), user, params, layer, index);
|
2016-03-08 21:34:57 +08:00
|
|
|
});
|
|
|
|
|
2016-04-26 21:59:41 +08:00
|
|
|
parseCartoQueue.awaitAll(function (err, layers) {
|
2016-03-08 21:34:57 +08:00
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
2016-05-23 21:18:20 +08:00
|
|
|
requestMapConfig.layers = layers;
|
|
|
|
|
|
|
|
return callback(null, requestMapConfig);
|
2016-03-08 21:34:57 +08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-06-21 21:05:44 +08:00
|
|
|
var tokensQueryTpl = dot.template([
|
|
|
|
'WITH input_query AS (',
|
|
|
|
' {{=it._sql}}',
|
|
|
|
'),',
|
|
|
|
'bbox_query AS (',
|
|
|
|
' SELECT ST_SetSRID(ST_Extent(the_geom_webmercator), 3857) as bbox from input_query',
|
|
|
|
'),',
|
|
|
|
'zoom_query as (',
|
|
|
|
' SELECT GREATEST(',
|
|
|
|
' ceil(log(40075017000 / 256 / GREATEST(ST_XMax(bbox) - ST_XMin(bbox), ST_YMax(bbox) - ST_YMin(bbox)))/log(2)),',
|
|
|
|
' 0) as zoom',
|
|
|
|
' FROM bbox_query',
|
|
|
|
'),',
|
|
|
|
'pixel_size_query as (',
|
|
|
|
' SELECT 40075017 * cos(radians(ST_Y(ST_Transform(ST_Centroid(bbox), 4326)))) / 2 ^ ((zoom) + 8) as pixel_size',
|
|
|
|
' FROM bbox_query, zoom_query',
|
|
|
|
'),',
|
|
|
|
'scale_denominator_query as (',
|
|
|
|
' SELECT (pixel_size / 0.00028)::numeric as scale_denominator',
|
|
|
|
' FROM pixel_size_query',
|
|
|
|
')',
|
|
|
|
'select ST_AsText(bbox) bbox, pixel_size, scale_denominator, zoom',
|
|
|
|
'from bbox_query, pixel_size_query, scale_denominator_query, zoom_query'
|
2016-05-19 18:13:49 +08:00
|
|
|
].join('\n'));
|
|
|
|
|
2016-06-21 18:08:40 +08:00
|
|
|
TurboCartoAdapter.prototype._parseCartoCss = function (username, params, layer, index, callback) {
|
2016-05-19 17:48:57 +08:00
|
|
|
if (!shouldParseLayerCartocss(layer)) {
|
|
|
|
return callback(null, layer);
|
2016-03-08 21:34:57 +08:00
|
|
|
}
|
|
|
|
|
2016-06-21 18:08:40 +08:00
|
|
|
var pg = new PSQL(dbParamsFromReqParams(params));
|
2016-06-21 21:05:44 +08:00
|
|
|
function processCallback(err, cartocss) {
|
2016-05-13 18:10:05 +08:00
|
|
|
// Only return turbo-carto errors
|
|
|
|
if (err && err.name === 'TurboCartoError') {
|
2016-06-13 22:14:01 +08:00
|
|
|
var error = new Error('turbo-carto: ' + err.message);
|
|
|
|
error.http_status = 400;
|
2016-06-27 00:43:04 +08:00
|
|
|
error.type = 'layer';
|
|
|
|
error.layer = {
|
2016-06-13 22:14:01 +08:00
|
|
|
index: index,
|
2016-06-27 00:43:04 +08:00
|
|
|
type: layer.type,
|
|
|
|
context: err.context
|
2016-06-13 18:20:56 +08:00
|
|
|
};
|
2016-06-13 22:14:01 +08:00
|
|
|
|
|
|
|
return callback(error);
|
2016-03-08 21:34:57 +08:00
|
|
|
}
|
|
|
|
|
2016-05-13 18:10:05 +08:00
|
|
|
// Try to continue in the rest of the cases
|
|
|
|
if (cartocss) {
|
|
|
|
layer.options.cartocss = cartocss;
|
|
|
|
}
|
|
|
|
return callback(null, layer);
|
2016-06-21 21:05:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var layerSql = layer.options.sql;
|
|
|
|
var layerRawSql = layer.options.sql_raw;
|
|
|
|
if (SubstitutionTokens.hasTokens(layerSql) && layerRawSql) {
|
|
|
|
var self = this;
|
|
|
|
var tokensQuery = tokensQueryTpl({_sql: layerRawSql});
|
|
|
|
return pg.query(tokensQuery, function(err, resultSet) {
|
|
|
|
if (err) {
|
|
|
|
var error = new Error('turbo-carto: ' + err.message);
|
|
|
|
error.type = 'turbo-carto';
|
|
|
|
return callback(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
resultSet = resultSet || {};
|
|
|
|
var rows = resultSet.rows || [];
|
|
|
|
var result = rows[0] || {};
|
|
|
|
|
|
|
|
var tokens = {
|
|
|
|
bbox: 'ST_SetSRID(ST_GeomFromText(\'' + result.bbox + '\'), 3857)',
|
|
|
|
scale_denominator: result.scale_denominator,
|
|
|
|
pixel_width: result.pixel_size,
|
|
|
|
pixel_height: result.pixel_size
|
|
|
|
};
|
|
|
|
|
|
|
|
var sql = SubstitutionTokens.replace(layerSql, tokens);
|
|
|
|
self.process(pg, layer.options.cartocss, sql, processCallback);
|
|
|
|
}, true); // use read-only transaction
|
|
|
|
}
|
|
|
|
|
|
|
|
var tokens = {
|
|
|
|
bbox: 'ST_MakeEnvelope(-20037508.34,-20037508.34,20037508.34,20037508.34,3857)',
|
|
|
|
scale_denominator: '500000001',
|
|
|
|
pixel_width: '156412',
|
|
|
|
pixel_height: '156412'
|
|
|
|
};
|
|
|
|
|
|
|
|
var sql = SubstitutionTokens.replace(layerSql, tokens);
|
|
|
|
this.process(pg, layer.options.cartocss, sql, processCallback);
|
2016-03-08 21:34:57 +08:00
|
|
|
};
|
|
|
|
|
2016-06-21 18:08:40 +08:00
|
|
|
TurboCartoAdapter.prototype.process = function (psql, cartocss, sql, callback) {
|
|
|
|
var datasource = new PostgresDatasource(psql, sql);
|
|
|
|
turboCarto(cartocss, datasource, callback);
|
|
|
|
};
|
|
|
|
|
2016-05-19 17:48:57 +08:00
|
|
|
function shouldParseLayerCartocss(layer) {
|
|
|
|
return layer && layer.options && layer.options.cartocss && layer.options.sql;
|
2016-03-08 21:34:57 +08:00
|
|
|
}
|
2016-06-21 18:08:40 +08:00
|
|
|
|
|
|
|
function dbParamsFromReqParams(params) {
|
|
|
|
var dbParams = {};
|
|
|
|
if ( params.dbuser ) {
|
|
|
|
dbParams.user = params.dbuser;
|
|
|
|
}
|
|
|
|
if ( params.dbpassword ) {
|
|
|
|
dbParams.pass = params.dbpassword;
|
|
|
|
}
|
|
|
|
if ( params.dbhost ) {
|
|
|
|
dbParams.host = params.dbhost;
|
|
|
|
}
|
|
|
|
if ( params.dbport ) {
|
|
|
|
dbParams.port = params.dbport;
|
|
|
|
}
|
|
|
|
if ( params.dbname ) {
|
|
|
|
dbParams.dbname = params.dbname;
|
|
|
|
}
|
|
|
|
return dbParams;
|
|
|
|
}
|