Code to aggregate histograms together

This commit is contained in:
Stuart Lynn 2015-10-28 18:48:45 -04:00
parent eaba6ee4dc
commit a8c1bf7682

View File

@ -25,6 +25,8 @@ L.TorqueLayer = L.CanvasLayer.extend({
if (!torque.isBrowserSupported()) {
throw new Error("browser is not supported by torque");
}
this._histograms = []
options.tileLoader = true;
this.key = 0;
this.prevRenderedKey = 0;
@ -93,6 +95,10 @@ L.TorqueLayer = L.CanvasLayer.extend({
// don't load tiles that are not being shown
if (t.zoom !== self._map.getZoom()) return;
self._tileLoaded(t, tileData);
if(Object.keys(tileData.histogram).length > 0){
self._addTileHistogram(t.x,t.y,t.zoom, tileData.histogram)
}
self._clearTileCaches();
if (tileData) {
self.redraw();
@ -102,7 +108,66 @@ L.TorqueLayer = L.CanvasLayer.extend({
}, this);
},
_invalidateHistograms:function(){
this._histograms= undefined
},
_addTileHistogram:function(x, y, zoom, histogram){
this._histograms[zoom] = this._histograms[zoom] ? this._histograms[zoom] : []
this._histograms[zoom][x] = this._histograms[zoom][x] ? this._histograms[zoom][x] : []
this._histograms[zoom][x][y] = histogram
this.fire('histogramAdded')
},
histogramForVariable:function(variable){
histogramParts=[]
Object.keys(this._tiles).forEach(function(t){
var coord = t.split(":")
if(this._histograms[coord[2]][coord[0]][coord[1]]){
histogramParts.push(this._histograms[coord[2]][coord[0]][coord[1]][variable])
}
}.bind(this))
return this._combineHistograms(histogramParts)
},
_transfromHistogram:function(hist,zoom){
var new_hist= {
bins: {},
bounds: hist.bounds,
zoom : zoom,
x: 0
}
var zoom_diff = zoom - hist.zoom;
new_hist.x = hist.x >> zoom_diff;
for( var k in hist.bins){
var idx = (hist.x + k ) >> (zoom_diff - new_hist.x)
new_hist.bins[idx] = new_hist.bins[idx] || 0
new_hist.bins[idx] += hist.bins[k]
}
return new_hist
},
_combineHistograms:function(histograms){
var minZoom = Math.min.apply(null,histograms.map(function(h){return h.zoom}))
var mappedHistograms = histograms.map(function(h){return this._transfromHistogram(h,minZoom)}.bind(this))
var combinedHistogram = {
bins:{},
bounds: [
Math.min.apply(null,mappedHistograms.map(function(h){return h.bounds[0]})),
Math.max.apply(null,mappedHistograms.map(function(h){return h.bounds[1]}))
],
zoom: minZoom,
x: 0
}
mappedHistograms.forEach(function(histogram){
Object.keys(histogram.bins).forEach(function(bin){
combinedHistogram.bins[bin] = combinedHistogram.bins[bin] || 0
combinedHistogram.bins[bin]+= histogram.bins[bin]
})
})
return combinedHistogram
},
_clearTileCaches: function() {
var t, tile;
for(t in this._tiles) {
@ -332,6 +397,11 @@ L.TorqueLayer = L.CanvasLayer.extend({
return this.provider.getKeySpan();
},
setQueryFilter:function(variable, start,end){
this.provider.setFilter(variable,start,end)
},
/**
* set the cartocss for the current renderer
*/