From 59e533af10bd714362a34e853fd372daa6ec1ed6 Mon Sep 17 00:00:00 2001 From: javi Date: Wed, 18 Nov 2015 11:50:11 +0100 Subject: [PATCH] histograms for category --- lib/torque/leaflet/torque.js | 38 +++------------------------ lib/torque/provider/filterableJson.js | 35 +++++++++++++++++++++--- 2 files changed, 35 insertions(+), 38 deletions(-) diff --git a/lib/torque/leaflet/torque.js b/lib/torque/leaflet/torque.js index f806dcc..aff87fe 100644 --- a/lib/torque/leaflet/torque.js +++ b/lib/torque/leaflet/torque.js @@ -217,43 +217,11 @@ L.TorqueLayer = L.CanvasLayer.extend({ this.provider.getHistogramForTiles(varName, start, end, bins, tiles, callback); }, - getHistogram:function(variable,callback,noBins){ - var type= this.provider._mapping[variable].type - if(type=='float'){ - callback(this.histogramForRangeVariable(variable,noBins)) - } - else if(type='cat'){ - callback(this.histogramForCatVariable(variable)) - } - return this + getCategoriesForVisibleRegion: function(varName, callback){ + var tiles = this.visibleTiles(); + this.provider.getCategoriesForTiles(varName, tiles, callback); }, - histogramForCatVariable:function(variable){ - var result = {} - this.valuesForCatVariable(variable).forEach(function(point){ - Object.keys(point).forEach(function(key){ - result[key] = result[key] || 0 - result[key] += point[key] - }) - }) - return result - }, - histogramForRangeVariable:function(variable,noBins){ - noBins = noBins || 10 - - var vals = this.valuesForRangeVariable(variable) - - var min = Math.min.apply(null, vals) - var max = Math.max.apply(null, vals) - var binSize = (max-min)/noBins - var result = [] - vals.forEach(function(val){ - var bin = (val -min)/binSize - result[bin]= result[bin] || 0 - result[bin] += val - }) - return result - }, onAdd: function (map) { map.on({ 'zoomend': this._clearCaches, diff --git a/lib/torque/provider/filterableJson.js b/lib/torque/provider/filterableJson.js index dd911a5..e7c7e4f 100644 --- a/lib/torque/provider/filterableJson.js +++ b/lib/torque/provider/filterableJson.js @@ -711,19 +711,48 @@ var Profiler = require('../profiler'); }); }, - getCatHistogramForTiles: function(varName, tiles, table, callback){ + getCategoriesForTiles: function(varName, tiles, callback){ + var tilesFilter = this._generateBoundsQuery(tiles); + this.tablesForFilter(tilesFilter, function(tables) { + // select the table with less than 100k records + var table = tables.filter(function(f) { + return f.nrows < 50000; + }) + table = table.length ? table[0].table_name: this.options.overview_tables[this.options.overview_tables.length - 1] + this._getCategoriesForTiles(varName, tiles, table, callback) + }.bind(this)); + }, + + _getCategoriesForTiles: function(varName, tiles, table, callback){ var sql = [ - 'select {varName}, count(1) from {table} where ({bounds}) {filters} group by 1 order by 2 desc limit {num_cats}', + 'select {varName} category, count(1) as "value" from {table} {tiles} where {bounds} {filters} group by 1 order by 2 desc limit {num_cats}', ] var filters = this._generateFiltersSQL(false, [varName]) ? " and "+ this._generateFiltersSQL(false, [varName]) : "" + var tiles_query = tiles.map(function (t, i) { + return "xyz2range(" + t.x + "," + t.y + "," + t.z + ") q" + i; + }).join(',') + + if (tiles_query) { + tiles_query = "," + tiles_query + } + + var bounds = tiles.map(function(t, i) { + return format("(quadkey between q{i}.min and q{i}.max)", { i: i }) + }).join('or') + + if (bounds) { + bounds = '(' + bounds + ')'; + } + var query = format(sql.join('\n'), { varName: varName, table: table, filters: filters, - bounds: this._generateBoundsQuery(tiles), + tiles: tiles_query, + bounds: bounds, num_cats: 20 });