2016-03-08 21:34:57 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var queue = require('queue-async');
|
2016-05-17 21:00:18 +08:00
|
|
|
var SubstitutionTokens = require('../substitution-tokens');
|
2016-03-08 21:34:57 +08:00
|
|
|
|
2016-04-26 21:59:41 +08:00
|
|
|
function TurboCartoAdapter(turboCartoParser) {
|
|
|
|
this.turboCartoParser = turboCartoParser;
|
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-04-26 21:59:41 +08:00
|
|
|
TurboCartoAdapter.prototype.getLayers = function (username, layers, callback) {
|
2016-03-08 21:34:57 +08:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
if (!layers || layers.length === 0) {
|
|
|
|
return callback(null, layers);
|
|
|
|
}
|
|
|
|
|
2016-04-26 21:59:41 +08:00
|
|
|
var parseCartoQueue = queue(layers.length);
|
2016-03-08 21:34:57 +08:00
|
|
|
|
|
|
|
layers.forEach(function(layer) {
|
2016-04-26 21:59:41 +08:00
|
|
|
parseCartoQueue.defer(self._parseCartoCss.bind(self), username, layer);
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
return callback(null, layers);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-04-26 21:59:41 +08:00
|
|
|
TurboCartoAdapter.prototype._parseCartoCss = function (username, layer, 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-05-17 21:00:18 +08:00
|
|
|
var sql = SubstitutionTokens.replace(layer.options.sql, {
|
|
|
|
bbox: 'ST_MakeEnvelope(-20037508.34,-20037508.34,20037508.34,20037508.34,3857)',
|
|
|
|
scale_denominator: '500000001',
|
|
|
|
pixel_width: '156412',
|
|
|
|
pixel_height: '156412'
|
|
|
|
});
|
|
|
|
|
|
|
|
this.turboCartoParser.process(username, layer.options.cartocss, sql, function (err, cartocss) {
|
2016-05-13 18:10:05 +08:00
|
|
|
// Only return turbo-carto errors
|
|
|
|
if (err && err.name === 'TurboCartoError') {
|
|
|
|
err = new Error('turbo-carto: ' + err.message);
|
|
|
|
err.http_status = 400;
|
|
|
|
return callback(err);
|
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-03-08 21:34:57 +08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
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
|
|
|
}
|