Caching and integration of histogram tiles

This commit is contained in:
Stuart Lynn 2015-10-26 09:05:57 -04:00
parent d873bd5bcb
commit e485ca4d99

View File

@ -12,6 +12,7 @@ L.TorqueLayer = L.CanvasLayer.extend({
'sql_api': torque.providers.json,
'url_template': torque.providers.JsonArray,
'windshaft': torque.providers.windshaft,
'jsonHistogram': torque.providers.jsonHistogram,
'tileJSON': torque.providers.tileJSON
},
@ -32,6 +33,10 @@ L.TorqueLayer = L.CanvasLayer.extend({
torque.extend(options, torque.common.TorqueLayer.optionsFromCartoCSS(options.cartocss));
}
self.tileHistograms = []
self.mapFilters = []
self.histFilters = []
options.resolution = options.resolution || 2;
options.steps = options.steps || 100;
options.visible = options.visible === undefined ? true: options.visible;
@ -72,6 +77,8 @@ L.TorqueLayer = L.CanvasLayer.extend({
this.provider = new this.providers[this.options.provider](options);
this.renderer = new this.renderers[this.options.renderer](this.getCanvas(), options);
this.provider.getHeader()
options.ready = function() {
self.fire("change:bounds", {
bounds: self.provider.getBounds()
@ -86,7 +93,6 @@ L.TorqueLayer = L.CanvasLayer.extend({
this.renderer.on("allIconsLoaded", this.render.bind(this));
// for each tile shown on the map request the data
this.on('tileAdded', function(t) {
var tileData = this.provider.getTileData(t, t.zoom, function(tileData) {
@ -98,9 +104,65 @@ L.TorqueLayer = L.CanvasLayer.extend({
self.redraw();
}
self.fire('tileLoaded');
self.provider.getTileHistograms(t,t.zoom,function(histData){
if(t.zoom !== self._map.getZoom()) return
this.tileHistograms[t.zoom] = this.tileHistograms[t.zoom] || []
this.tileHistograms[t.zoom][t.x]= this.tileHistograms[t.zoom][t.x] || []
this.tileHistograms[t.zoom][t.x][t.y]= histData
this.fire('histLoaded')
}.bind(self))
});
}, this);
},
setMapFilters:function(filters){
this.mapFilters = filters;
this.provider.setMapFilters(filters)
this._clearCaches()
this.redraw();
},
setHistFilters:function(filters){
this.histFilters = filters
this._clearCaches()
},
totalHistogramFor:function(variable,step){
var start = new Date()
step = step === undefined ? this.key: step;
var t, tile;
var bins = []
var vals = []
for(t in this._tiles) {
tile = this._tiles[t].coord;
tileHist = this.tileHistograms[tile.z][tile.x][tile.y]
tileBins = tileHist[variable+"_bins"]
tileVals = tileHist[variable+"_vals"]
for(var i in tileBins){
tileBin = tileBins[i]
bins[tileBin] = bins[tileBin] || tileBin
vals[tileBin] = vals[tileBin] || 0
vals[tileBin] = vals[tileBin] + tileVals[i]
}
}
result = []
for(var i=0; i < bins.length; i++){
result[i] = [bins[i] || i , vals[i] || 0 ]
}
var end = new Date()
console.log("total hist took ", (end-start)/1000.0)
return result;
},
_clearTileCaches: function() {
@ -113,9 +175,14 @@ L.TorqueLayer = L.CanvasLayer.extend({
}
},
_clearHistCaches:function(){
this.tileHistograms = [];
},
_clearCaches: function() {
this.renderer && this.renderer.clearSpriteCache();
this._clearTileCaches();
this._clearHistCaches();
},
onAdd: function (map) {