diff --git a/lib/cartodb/models/aggregation/aggregation-query.js b/lib/cartodb/models/aggregation/aggregation-query.js index 4d964171..a455e02a 100644 --- a/lib/cartodb/models/aggregation/aggregation-query.js +++ b/lib/cartodb/models/aggregation/aggregation-query.js @@ -276,23 +276,24 @@ const spatialFilter = ` // * This queries are used for rendering and the_geom is omitted in the results for better performance // * If the MVT extent or tile buffer was 0 or a multiple of the resolution we could use directly // the bbox for them, but in general we need to find the nearest cell limits inside the bbox. -// * bbox coordinates can have an error in the last digites; we apply a small correction before -// applying CEIL or FLOOR to compensate for this. +// * bbox coordinates can have an error in the last digits; we apply a small correction before +// applying CEIL or FLOOR to compensate for this, so that coordinates closer than a small (`eps`) +// fraction of the cell size to a cell limit are moved to the exact limit. const sqlParams = (ctx) => ` _cdb_res AS ( SELECT ${gridResolution(ctx)} AS res, !bbox! AS bbox, - (2*2.220446049250313e-16::double precision) AS eps + (1E-6::double precision) AS eps ), _cdb_params AS ( SELECT res, bbox, - CEIL((ST_XMIN(bbox) - eps*ABS(ST_XMIN(bbox)))/res)*res AS xmin, - FLOOR((ST_XMAX(bbox) + eps*ABS(ST_XMAX(bbox)))/res)*res AS xmax, - CEIL((ST_YMIN(bbox) - eps*ABS(ST_YMIN(bbox)))/res)*res AS ymin, - FLOOR((ST_YMAX(bbox) + eps*ABS(ST_YMAX(bbox)))/res)*res AS ymax + CEIL((ST_XMIN(bbox) - eps*res)/res)*res AS xmin, + FLOOR((ST_XMAX(bbox) + eps*res)/res)*res AS xmax, + CEIL((ST_YMIN(bbox) - eps*res)/res)*res AS ymin, + FLOOR((ST_YMAX(bbox) + eps*res)/res)*res AS ymax FROM _cdb_res ) `;