diff --git a/lib/torque/leaflet/torque.js b/lib/torque/leaflet/torque.js index aff87fe..71f6ee8 100644 --- a/lib/torque/leaflet/torque.js +++ b/lib/torque/leaflet/torque.js @@ -212,9 +212,14 @@ L.TorqueLayer = L.CanvasLayer.extend({ } }, - getHistogramForVisibleRegion: function(varName, start, end, bins, callback){ + getHistogramForDataset: function(varName, start, end, bins, own_filter, callback) { + var tiles = [{x: 0, y: 0, z: 0}]; + this.provider.getHistogramForTiles(varName, start, end, bins, tiles, own_filter, callback); + }, + + getHistogramForVisibleRegion: function(varName, start, end, bins, own_filter, callback) { var tiles = this.visibleTiles(); - this.provider.getHistogramForTiles(varName, start, end, bins, tiles, callback); + this.provider.getHistogramForTiles(varName, start, end, bins, tiles, own_filter, callback); }, getCategoriesForVisibleRegion: function(varName, callback){ diff --git a/lib/torque/provider/filterableJson.js b/lib/torque/provider/filterableJson.js index e7c7e4f..5398cfc 100644 --- a/lib/torque/provider/filterableJson.js +++ b/lib/torque/provider/filterableJson.js @@ -323,14 +323,6 @@ var Profiler = require('../profiler'); }).join(' and ') }, - _host: function() { - var opts = this.options; - var port = opts.sql_api_port; - var domain = ((opts.user_name || opts.user) + '.' + (opts.sql_api_domain || 'cartodb.com')) + (port ? ':' + port: ''); - var protocol = opts.sql_api_protocol || 'http'; - return this.options.url || protocol + '://' + domain + '/api/v2/sql'; - }, - url: function(subhost) { var opts = this.options; return opts.sql_api_template.replace('{user}', (opts.user_name || opts.user)).replace('{s}', subhost) + "/api/v1/sql"; @@ -421,6 +413,7 @@ var Profiler = require('../profiler'); getDataForTorquePixel: function(tile, x, y, maxNo, tolerance, applyFilters, callback){ shift = 23 - tile.z tolerance = tolerance || 20 + //TODO: use quadkey to filter var sql = [ "select * from {table}", "where (quadkey between (xyz2range({x},{y},{z})).min and (xyz2range({x},{y},{z})).max) ", @@ -617,20 +610,19 @@ var Profiler = require('../profiler'); }).join(" or ") }, - getHistogramForTiles: function(varName, start, end, bins, tiles, callback) { + getHistogramForTiles: function(varName, start, end, bins, tiles, own_filter, 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; + return f.nrows < 50000 && f.cost < 30000; }) table = table.length ? table[0].table_name: this.options.overview_tables[this.options.overview_tables.length - 1] - this._getHistogramForTiles(varName, start, end, bins, tiles, table, callback) + this._getHistogramForTiles(varName, start, end, bins, tiles, table, own_filter, callback) }.bind(this)); }, - _getHistogramForTiles: function(varName, start, end, bins, tiles, table, callback){ - + _getHistogramForTiles: function(varName, start, end, bins, tiles, table, own_filter, callback){ var sql = [ 'with source as (', @@ -648,7 +640,7 @@ var Profiler = require('../profiler'); } else { start = start === undefined ? 'min(' + varName + ')': start; end = end === undefined ? 'max(' + varName + ')': end; - bins = bins === undefined ? 100: bins; + bins = bins === undefined ? 50: bins; sql = sql.concat([ 'width as (', 'select {start} as min,', @@ -661,17 +653,18 @@ var Profiler = require('../profiler'); sql = sql.concat([ '_bw as ( select (max - min)/buckets as bw from width ),', 'histogram as (', - 'select width_bucket({varName}, min, max, buckets) as bucket,', + 'select least(buckets, width_bucket({varName}, min, max, buckets)) - 1 as bucket,', 'numrange(min({varName})::numeric, max({varName})::numeric, \'[]\') as range,', 'count(*) as freq', 'from source, width ', '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;' + 'select min + bucket*bw as start, min + (bucket+1)*bw as end, bucket as bin, lower(range) as min, upper(range) as max, freq from histogram, _bw, width;' ]); - var filters = this._generateFiltersSQL(false, [varName]) ? " and "+ this._generateFiltersSQL(false, [varName]) : "" + var ff = this._generateFiltersSQL(false, own_filter ? []: [varName]) + var filters = ff ? " and " + ff: "" var tiles_query = tiles.map(function (t, i) { return "xyz2range(" + t.x + "," + t.y + "," + t.z + ") q" + i; @@ -716,7 +709,7 @@ var Profiler = require('../profiler'); this.tablesForFilter(tilesFilter, function(tables) { // select the table with less than 100k records var table = tables.filter(function(f) { - return f.nrows < 50000; + return f.nrows < 50000 && f.cost < 30000; }) table = table.length ? table[0].table_name: this.options.overview_tables[this.options.overview_tables.length - 1] this._getCategoriesForTiles(varName, tiles, table, callback) diff --git a/lib/torque/renderer/point.js b/lib/torque/renderer/point.js index 13cd2bc..cb8bd63 100644 --- a/lib/torque/renderer/point.js +++ b/lib/torque/renderer/point.js @@ -38,6 +38,7 @@ var Filters = require('./torque_filters'); return COMP_OP_TO_CANVAS[compop] || compop; } + // // this renderer just render points depending of the value // @@ -263,7 +264,7 @@ var Filters = require('./torque_filters'); var activePixels = tile.x.length; var anchor = this.options.resolution/2; if (activePixels) { - var pixelIndex = 0;//tile.timeIndex[key]; + var pixelIndex = tile.timeIndex[key]; for(var p = 0; p < activePixels; ++p) { var posIdx = tile.renderDataPos[pixelIndex + p]; var c = tile.renderData[pixelIndex + p]; @@ -463,6 +464,8 @@ var Filters = require('./torque_filters'); } }); +PointRenderer.COMP_OP_TO_CANVAS = COMP_OP_TO_CANVAS; + // exports public api module.exports = PointRenderer; diff --git a/lib/torque/renderer/rectangle.js b/lib/torque/renderer/rectangle.js index ff027ce..b9bc4b1 100644 --- a/lib/torque/renderer/rectangle.js +++ b/lib/torque/renderer/rectangle.js @@ -23,7 +23,8 @@ torque.extend(PixelRenderer.prototype, PointRenderer.prototype, { return { width: st['marker-width'], - color: st['marker-fill'] + color: st['marker-fill'], + fill_opacity: st['marker-fill-opacity'] } }, @@ -39,7 +40,7 @@ torque.extend(PixelRenderer.prototype, PointRenderer.prototype, { var activePixels = tile.x.length; var anchor = this.options.resolution/2; if (activePixels) { - var pixelIndex = 0;//tile.timeIndex[key]; + var pixelIndex = tile.timeIndex[key]; for(var p = 0; p < activePixels; ++p) { var posIdx = tile.renderDataPos[pixelIndex + p]; var c = tile.renderData[pixelIndex + p]; @@ -50,8 +51,11 @@ torque.extend(PixelRenderer.prototype, PointRenderer.prototype, { if (sp) { var x = tile.x[posIdx]- (sp.width >> 1) + anchor; var y = tileMax - tile.y[posIdx] + anchor; // flip mercator - ctx.fillStyle = sp.color; - ctx.fillRect(x, y, sp.width, sp.width); + if (sp.fill_opacity > 0) { + ctx.globalAlpha = sp.fill_opacity + ctx.fillStyle = sp.color; + ctx.fillRect(x, y, sp.width, sp.width); + } } } }