histograms for category

This commit is contained in:
javi 2015-11-18 11:50:11 +01:00
parent d95f044ffc
commit 59e533af10
2 changed files with 35 additions and 38 deletions

View File

@ -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,

View File

@ -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
});