From 9d8ce6bc44055c58f5f465d45f2dfdff5e627827 Mon Sep 17 00:00:00 2001 From: Javier Goizueta Date: Thu, 14 Dec 2017 16:51:55 +0100 Subject: [PATCH] Refactor aggregation resolution --- .../models/aggregation/aggregation-query.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/cartodb/models/aggregation/aggregation-query.js b/lib/cartodb/models/aggregation/aggregation-query.js index ca6d3431..332241d8 100644 --- a/lib/cartodb/models/aggregation/aggregation-query.js +++ b/lib/cartodb/models/aggregation/aggregation-query.js @@ -78,9 +78,14 @@ const aggregateColumnDefs = ctx => { }).join(', '); }; + +// SQL expression to compute the aggregation resolution (grid cell size). +// This is equivalent to `${256/ctx.res}*CDB_XYZ_Resolution(CDB_ZoomFromScale(!scale_denominator!))` +// This is defined by the ctx.res parameter, which is the number of grid cells per tile linear dimension +// (i.e. each tile is divided into ctx.res*ctx.res cells). +const gridResolution = ctx => `(${256*0.00028/ctx.res}*!scale_denominator!)::double precision`; + // Notes: -// * ${256*0.00028/ctx.res}*!scale_denominator! is equivalent to -// ${256/ctx.res}*CDB_XYZ_Resolution(CDB_ZoomFromScale(!scale_denominator!)) // * We need to filter spatially using !bbox! to make the queries efficient because // the filter added by Mapnik (wrapping the query) // is only applied after the aggregation. @@ -90,7 +95,7 @@ const aggregationQueryTemplates = { 'centroid': ctx => ` WITH _cdb_params AS ( SELECT - (${256*0.00028/ctx.res}*!scale_denominator!)::double precision AS res, + ${gridResolution(ctx)} AS res, !bbox! AS bbox ) SELECT @@ -112,7 +117,7 @@ const aggregationQueryTemplates = { 'point-grid': ctx => ` WITH _cdb_params AS ( SELECT - (${256*0.00028/ctx.res}*!scale_denominator!)::double precision AS res, + ${gridResolution(ctx)} AS res, !bbox! AS bbox ), _cdb_clusters AS ( @@ -133,8 +138,7 @@ const aggregationQueryTemplates = { 'point-sample': ctx => ` WITH _cdb_params AS ( SELECT - - (${256*0.00028/ctx.res}*!scale_denominator!)::double precision AS res, + ${gridResolution(ctx)} AS res, !bbox! AS bbox ), _cdb_clusters AS ( SELECT