From 8454eef6e9aeddfc22cb716b391baaf148bcf525 Mon Sep 17 00:00:00 2001 From: Raul Marin Date: Thu, 4 Jul 2019 17:15:53 +0200 Subject: [PATCH] Aggregation: Extract the query to get the grid data --- .../models/aggregation/aggregation-query.js | 36 +++++++++++-------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/lib/cartodb/models/aggregation/aggregation-query.js b/lib/cartodb/models/aggregation/aggregation-query.js index 9e9ff805..2c3026a3 100644 --- a/lib/cartodb/models/aggregation/aggregation-query.js +++ b/lib/cartodb/models/aggregation/aggregation-query.js @@ -300,7 +300,7 @@ const havingClause = ctx => { // We limit the the minimum resolution to avoid division by zero problems. The limit used is // the pixel size of zoom level 30 (i.e. 1/2*(30+8) of the full earth web-mercator extent), which is about 0.15 mm. // -// NOTE: We'd rather use !pixel_height!, but in Mapnik this value is extent / 256 for raster +// NOTE: We'd rather use !scale_denominator!, but in Mapnik this value is extent / 256 for raster // and extent / tile_extent {4096 default} for MVT, so since aggregations are always based // on 256 we can't have the same query in both cases // As this scale change doesn't happen in !scale_denominator! we use that instead @@ -309,6 +309,26 @@ const gridResolution = ctx => { return `${256/ctx.res} * GREATEST(!scale_denominator! * 0.00028, ${minimumResolution})::double precision`; }; +// SQL query to extra the boundaries of the area to be aggregated and the grid resolution +const gridInfoQuery = ctx => { + return ` + SELECT + res, + CEIL (ST_XMIN(cdb_full_bbox) / res) * res AS cdb_xmin, + FLOOR(ST_XMAX(cdb_full_bbox) / res) * res AS cdb_xmax, + CEIL (ST_YMIN(cdb_full_bbox) / res) * res AS cdb_ymin, + FLOOR(ST_YMAX(cdb_full_bbox) / res) * res AS cdb_ymax + FROM + ( + SELECT + ${gridResolution(ctx)} AS res, + !bbox! cdb_full_bbox + OFFSET 0 + ) _cdb_input_resources +`; +}; + + // Function to generate the resulting point for a cell from the aggregated const aggregatedPoint = (ctx, aggregated) => { const placement = ctx.placement || DEFAULT_PLACEMENT; @@ -387,19 +407,7 @@ SELECT * FROM ((cdb_ymax - cdb_ymin) / res)::int as cdb_limit_y FROM ( - SELECT - res, - CEIL (ST_XMIN(cdb_full_bbox) / res) * res AS cdb_xmin, - FLOOR(ST_XMAX(cdb_full_bbox) / res) * res AS cdb_xmax, - CEIL (ST_YMIN(cdb_full_bbox) / res) * res AS cdb_ymin, - FLOOR(ST_YMAX(cdb_full_bbox) / res) * res AS cdb_ymax - FROM - ( - SELECT - ${gridResolution(ctx)} AS res, - !bbox! cdb_full_bbox - OFFSET 0 - ) _cdb_input_resources + ${gridInfoQuery(ctx)} ) _cdb_grid_bbox_margins OFFSET 0 ) __cdb_src_params WHERE the_geom_webmercator && cdb_point_bbox OFFSET 0