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');
|
|
|
|
|
2019-09-13 22:32:37 +08:00
|
|
|
const SubstitutionTokens = require('cartodb-query-tables').utils.substitutionTokens;
|
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-29 18:21:15 +08:00
|
|
|
var MapConfig = require('windshaft').model.MapConfig;
|
|
|
|
|
2018-03-23 02:20:51 +08:00
|
|
|
const dbParamsFromReqParams = require('../../../utils/database-params');
|
|
|
|
|
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-29 18:21:15 +08:00
|
|
|
var layerId = MapConfig.getLayerId(requestMapConfig, index);
|
|
|
|
parseCartoQueue.defer(self._parseCartoCss.bind(self), user, params, layer, index, layerId);
|
2016-03-08 21:34:57 +08:00
|
|
|
});
|
|
|
|
|
2016-07-06 06:32:30 +08:00
|
|
|
parseCartoQueue.awaitAll(function (err, results) {
|
2016-03-08 21:34:57 +08:00
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
2016-07-06 06:32:30 +08:00
|
|
|
var errors = results.reduce(function(errors, result) {
|
|
|
|
if (result.error) {
|
|
|
|
errors.push(result.error);
|
|
|
|
}
|
|
|
|
return errors;
|
|
|
|
}, []);
|
|
|
|
if (errors.length > 0) {
|
|
|
|
return callback(errors);
|
|
|
|
}
|
|
|
|
|
|
|
|
requestMapConfig.layers = results.map(function(result) { return result.layer; });
|
2016-09-20 22:08:53 +08:00
|
|
|
context.turboCarto = {
|
|
|
|
layers: results.map(function(result) {
|
|
|
|
return result.meta;
|
|
|
|
})
|
|
|
|
};
|
2016-05-23 21:18:20 +08:00
|
|
|
|
|
|
|
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-29 18:21:15 +08:00
|
|
|
TurboCartoAdapter.prototype._parseCartoCss = function (username, params, layer, layerIndex, layerId, callback) {
|
2016-05-19 17:48:57 +08:00
|
|
|
if (!shouldParseLayerCartocss(layer)) {
|
2016-07-06 06:32:30 +08:00
|
|
|
return callback(null, { layer: layer });
|
2016-03-08 21:34:57 +08:00
|
|
|
}
|
|
|
|
|
2016-06-21 18:08:40 +08:00
|
|
|
var pg = new PSQL(dbParamsFromReqParams(params));
|
2016-09-20 22:08:53 +08:00
|
|
|
function processCallback(err, cartocss, meta) {
|
2016-05-13 18:10:05 +08:00
|
|
|
// Only return turbo-carto errors
|
|
|
|
if (err && err.name === 'TurboCartoError') {
|
2016-07-06 18:34:09 +08:00
|
|
|
var error = new Error(err.message);
|
2016-06-13 22:14:01 +08:00
|
|
|
error.http_status = 400;
|
2016-06-27 00:43:04 +08:00
|
|
|
error.type = 'layer';
|
2016-06-29 18:21:15 +08:00
|
|
|
error.subtype = 'turbo-carto';
|
2016-06-27 00:43:04 +08:00
|
|
|
error.layer = {
|
2016-06-29 18:21:15 +08:00
|
|
|
id: layerId,
|
|
|
|
index: layerIndex,
|
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
|
|
|
|
2016-07-06 06:32:30 +08:00
|
|
|
return callback(null, { error: 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;
|
|
|
|
}
|
2016-09-20 22:08:53 +08:00
|
|
|
return callback(null, { layer: layer, meta: meta });
|
2016-06-21 21:05:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var layerSql = layer.options.sql;
|
|
|
|
var layerRawSql = layer.options.sql_raw;
|
2018-05-18 21:29:46 +08:00
|
|
|
if (SubstitutionTokens.hasTokens(layerSql) && layerRawSql && layer.options.sql_wrap) {
|
|
|
|
// For wrapped queries we'll derive the tokens from the data extent
|
|
|
|
// instead of the whole Earth/root tile.
|
2016-06-21 21:05:44 +08:00
|
|
|
var self = this;
|
|
|
|
var tokensQuery = tokensQueryTpl({_sql: layerRawSql});
|
|
|
|
return pg.query(tokensQuery, function(err, resultSet) {
|
|
|
|
if (err) {
|
2016-07-06 18:34:09 +08:00
|
|
|
return processCallback(err);
|
2016-06-21 21:05:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|