From c9e87f43652051bae728955794e0c5e36b11dae5 Mon Sep 17 00:00:00 2001 From: Stuart Lynn Date: Mon, 16 Nov 2015 18:58:16 +0000 Subject: [PATCH 1/2] Adding method to get histogram by tiles --- lib/torque/provider/filterableJson.js | 58 +++++++++++++++++++++++++-- 1 file changed, 55 insertions(+), 3 deletions(-) diff --git a/lib/torque/provider/filterableJson.js b/lib/torque/provider/filterableJson.js index 7f3cb01..19712a6 100644 --- a/lib/torque/provider/filterableJson.js +++ b/lib/torque/provider/filterableJson.js @@ -590,6 +590,57 @@ var Profiler = require('../profiler'); this._setReady(true); }, + _generateBoundsQuery:function(tiles){ + return tiles.map( function(tile){ + return "(quadkey between (xyz2range("+tile.x+","+tile.y+","+tile.z+")).min and (xyz2range("+tile.x+","+tile.y+","+tile.z+")).max)" + }).join(" or ") + }, + + getHistogramForTiles: function(varName,tiles,callback){ + + var sql = [ + 'with width as (', + 'select min({varName}) as min,', + 'max({varName}) as max,', + '{bins} as buckets', + 'from {table}', + '),', + '_bw as ( select (max - min)/buckets as bw from width ),', + 'histogram as (', + 'select width_bucket({varName}, min, max, buckets) as bucket,', + 'numrange(min({varName})::numeric, max({varName})::numeric, \'[]\') as range,', + 'count(*) as freq', + 'from {table}, width ', + 'where {bounds}', + '{filters}', + //'where trip_time_in_secs between min and max', + 'group by bucket', + 'order by bucket', + ')', + 'select bucket*bw as start, (bucket+1)*bw as end, bucket as bin, lower(range) as min, upper(range) as max, freq from histogram, _bw;' + ] + + var filters = this._generateFiltersSQL() ? " and "+ this._generateFiltersSQL() : "" + + var query = format(sql.join('\n'), this.options, { + varName: varName, + table: this.options.table, + filters: filters, + bounds: this._generateBoundsQuery(tiles), + bins: 20 + }); + + var self = this; + this.sql(query, function (data) { + if (data) { + var rows = JSON.parse(data.responseText).rows; + callback(rows); + } else { + callback(null); + } + }); + }, + getHistogram: function(varName, callback) { var sql = [ @@ -605,6 +656,7 @@ var Profiler = require('../profiler'); 'numrange(min({column})::numeric, max({column})::numeric, \'[]\') as range,', 'count(*) as freq', 'from {table}, width ', + 'where {filters}', //'where trip_time_in_secs between min and max', 'group by bucket', 'order by bucket', @@ -614,8 +666,8 @@ var Profiler = require('../profiler'); var query = format(sql.join('\n'), this.options, { - column: this.options.column, - table: this.options.table, + column: varName, + table: this.options.table, filters: this._generateFiltersSQL() }); @@ -623,7 +675,7 @@ var Profiler = require('../profiler'); this.sql(query, function (data) { if (data) { var rows = JSON.parse(data.responseText).rows; - callback(null, rows); + callback(rows); } else { callback(null); } From 880bf9870faacd2a7f885c7e8788173df58a182c Mon Sep 17 00:00:00 2001 From: Stuart Lynn Date: Mon, 16 Nov 2015 18:59:08 +0000 Subject: [PATCH 2/2] adding methods to get the histogram for the visible region of the map. One using Pauls Grandfather tile idea and the other using quad tree ranges for each visible tile --- lib/torque/leaflet/torque.js | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/lib/torque/leaflet/torque.js b/lib/torque/leaflet/torque.js index 702365b..d77cec8 100644 --- a/lib/torque/leaflet/torque.js +++ b/lib/torque/leaflet/torque.js @@ -211,6 +211,28 @@ L.TorqueLayer = L.CanvasLayer.extend({ callback(this.valuesForCatVariable(variable)) } }, + + getHistogramForVisibleRegion:function(varName,callback){ + var center = this._map.getCenter() + var zoom = this._map.getZoom() + + var xtile = parseInt(Math.floor( (center.lng + 180) / 360 * (1<> 2 + var yy = ytile >> 2 + var z = zoom - 2 + + this.provider.getHistogramForTiles(varName,[{x:xx,y:yy,z:z}],callback) + }, + + getHistogramForVisibleRegionBetter:function(varName,callback){ + var tiles= [] + for(var key in this._tiles){ + tiles.push(this._tiles[key].coord) + } + this.provider.getHistogramForTiles(varName,tiles,callback) + }, + getHistogram:function(variable,callback,noBins){ var type= this.provider._mapping[variable].type if(type=='float'){ @@ -221,6 +243,7 @@ L.TorqueLayer = L.CanvasLayer.extend({ } return this }, + histogramForCatVariable:function(variable){ var result = {} this.valuesForCatVariable(variable).forEach(function(point){ @@ -642,8 +665,8 @@ L.TorqueLayer = L.CanvasLayer.extend({ * returned format is [{ x: .., y: .., value: ...}, .. ] */ getClosestValuesFor: function(x, y, dist, step) { - var xf = x + dist, - yf = y + dist, + var xf = x + dist, + yf = y + dist, _x = x; var values = [] for(_y = y; _y < yf; _y += this.options.resolution){