cumulative WIP

This commit is contained in:
javi 2013-11-20 08:19:32 +01:00
parent 037c83655e
commit f3e45f523e
4 changed files with 77 additions and 40 deletions

43
lib/torque/common.js Normal file
View File

@ -0,0 +1,43 @@
//
// common functionallity for torque layers
//
(function(exports) {
function TorqueLayer() {}
TorqueLayer.prototype = {
};
TorqueLayer.optionsFromLayer = function(mapConfig) {
var opts = {};
if (!mapConfig) return opts;
var attrs = {
'buffer-size': 'buffer-size',
'-torque-steps': 'steps',
'-torque-resolution': 'resolution',
'-torque-animation-duration': 'animationDuration',
'-torque-aggregation-function': 'countby',
'-torque-time-attribute': 'column',
'-torque-data-aggregation': 'data_aggregation'
};
for (var i in attrs) {
var v = mapConfig.eval(i);
if (v !== undefined) {
var a = attrs[i];
opts[a] = v;
}
}
return opts;
};
TorqueLayer.optionsFromCartoCSS = function(cartocss) {
var shader = new carto.RendererJS().render(cartocss);
var mapConfig = shader.findLayer({ name: 'Map' });
return L.TorqueLayer.optionsFromLayer(mapConfig);
};
exports.torque.common = torque.common || {};
exports.torque.common.TorqueLayer = TorqueLayer;
})(typeof exports === "undefined" ? this : exports);

View File

@ -8,7 +8,7 @@ function GMapsTorqueLayer(options) {
throw new Error("browser is not supported by torque");
}
this.key = 0;
this.cartocss = null;
this.shader = null;
this.ready = false;
this.options = _.extend({}, options);
_.defaults(this.options, {
@ -18,6 +18,10 @@ function GMapsTorqueLayer(options) {
steps: 100,
visible: true
});
if (options.cartocss) {
_.extend(this.options,
torque.common.TorqueLayer.optionsFromCartoCSS(options.cartocss));
}
this.animator = new torque.Animator(function(time) {
var k = time | 0;
@ -86,8 +90,8 @@ GMapsTorqueLayer.prototype = _.extend({},
this._initTileLoader(this.options.map, this.getProjection());
if (this.cartocss) {
this.renderer.setCartoCSS(this.cartocss);
if (this.shader) {
this.renderer.setShader(this.shader);
}
},
@ -227,11 +231,24 @@ GMapsTorqueLayer.prototype = _.extend({},
* set the cartocss for the current renderer
*/
setCartoCSS: function(cartocss) {
if (!this.renderer) {
this.cartocss = cartocss;
return this;
var shader = new carto.RendererJS().render(cartocss);
this.shader = shader;
if (this.renderer) {
this.renderer.setShader(shader);
}
this.renderer.setCartoCSS(cartocss);
// provider options
var options = torque.common.TorqueLayer.optionsFromLayer(shader.findLayer({ name: 'Map' }));
if(this.provider && this.provider.setOptions(options)) {
this._reloadTiles();
}
_.extend(this.options, options);
// animator options
if (options.animationDuration) {
this.animator.duration(options.animationDuration);
}
this.redraw();
return this;
},

View File

@ -22,7 +22,7 @@ L.TorqueLayer = L.CanvasLayer.extend({
options.tileLoader = true;
this.key = 0;
if (options.cartocss) {
_.extend(options, L.TorqueLayer.optionsFromCartoCSS(options.cartocss));
_.extend(options, torque.common.TorqueLayer.optionsFromCartoCSS(options.cartocss));
}
options.resolution = options.resolution || 2;
@ -215,8 +215,11 @@ L.TorqueLayer = L.CanvasLayer.extend({
this.renderer.setShader(shader);
// provider options
var options = L.TorqueLayer.optionsFromLayer(shader.findLayer({ name: 'Map' }));
this.provider.setOptions(options);
var options = torque.common.TorqueLayer.optionsFromLayer(shader.findLayer({ name: 'Map' }));
if(this.provider.setOptions(options)) {
this._reloadTiles();
}
_.extend(this.options, options);
// animator options
if (options.animationDuration) {
@ -229,34 +232,6 @@ L.TorqueLayer = L.CanvasLayer.extend({
});
L.TorqueLayer.optionsFromLayer = function(mapConfig) {
var opts = {};
if (!mapConfig) return opts;
var attrs = {
'buffer-size': 'buffer-size',
'-torque-steps': 'steps',
'-torque-resolution': 'resolution',
'-torque-animation-duration': 'animationDuration',
'-torque-aggregation-function': 'countby',
'-torque-time-attribute': 'column',
'-torque-data-aggregation': 'data_aggregation'
};
for (var i in attrs) {
var v = mapConfig.eval(i);
if (v !== undefined) {
var a = attrs[i];
opts[a] = v;
}
}
return opts;
};
L.TorqueLayer.optionsFromCartoCSS = function(cartocss) {
var shader = new carto.RendererJS().render(cartocss);
var mapConfig = shader.findLayer({ name: 'Map' });
return L.TorqueLayer.optionsFromLayer(mapConfig);
};
L.TiledTorqueLayer = L.TileLayer.Canvas.extend({

View File

@ -342,6 +342,7 @@
this.options.resolution = res;
},
// return true if tiles has been changed
setOptions: function(opt) {
var refresh = false;
@ -361,6 +362,7 @@
}
if (refresh) this.reload();
return refresh;
},
@ -381,12 +383,12 @@
},
setSteps: function(steps, opt) {
opt = opt || {}
opt = opt || {};
if (this.options.steps !== steps) {
this.options.steps = steps;
this.options.step = (this.options.end - this.options.start)/this.getSteps();
this.options.step = this.options.step || 1;
if (!opt.silent) this.reload()
if (!opt.silent) this.reload();
}
},