From 1767b83d097b5c9201937694339b9ab6cf4b0a77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Thu, 30 Nov 2017 15:34:20 +0100 Subject: [PATCH] Aggregation query models: bootstrap hierarchy classes --- .../models/aggregation/base-aggregation.js | 9 ++++ .../models/aggregation/raster-aggregation.js | 54 +++++++++++++++++++ .../models/aggregation/vector-aggregation.js | 54 +++++++++++++++++++ 3 files changed, 117 insertions(+) create mode 100644 lib/cartodb/models/aggregation/base-aggregation.js create mode 100644 lib/cartodb/models/aggregation/raster-aggregation.js create mode 100644 lib/cartodb/models/aggregation/vector-aggregation.js diff --git a/lib/cartodb/models/aggregation/base-aggregation.js b/lib/cartodb/models/aggregation/base-aggregation.js new file mode 100644 index 00000000..d7f92f70 --- /dev/null +++ b/lib/cartodb/models/aggregation/base-aggregation.js @@ -0,0 +1,9 @@ +module.exports = class BaseAggregation { + sql () { + throw new Error('Unimplemented method'); + } +}; + +module.exports.baseQueryTemplate = ctx => ` + select ${ctx} blah.., blah, blah... +`; diff --git a/lib/cartodb/models/aggregation/raster-aggregation.js b/lib/cartodb/models/aggregation/raster-aggregation.js new file mode 100644 index 00000000..c0d65a37 --- /dev/null +++ b/lib/cartodb/models/aggregation/raster-aggregation.js @@ -0,0 +1,54 @@ +const BaseAggregation = require('./base-aggregation'); +const { baseQueryTemplate } = BaseAggregation; + +module.exports = class RasterAggregation extends BaseAggregation { + sql (options) { + return rasterAggregationQueryTemplate({ + source_query: options.sql, + res: options.resolution, + columns: options.columns + }); + } +}; + +const rasterAggregationQueryTemplate = ctx => ` + WITH + _cdb_source AS ( + -- original query + ${ctx.source_query} + ), + _cdb_resolution AS ( + SELECT ${ctx.res}*CDB_XYZ_Resolution(CDB_ZoomFromScale(!scale_denominator!)) + AS _cdb_grid_size + -- equivalent to: + -- ${ctx.res}*!scale_denominator!*0.00028 + ), + _cdb_gridded AS ( + SELECT + Floor(ST_X(_cdb_source.the_geom_webmercator)/_cdb_grid_size)::int AS _cdb_gx, + Floor(ST_Y(_cdb_source.the_geom_webmercator)/_cdb_grid_size)::int AS _cdb_gy, + count(*) AS _cdb_feature_count + FROM _cdb_source, _cdb_resolution + GROUP BY _cdb_gx, _cdb_gy + ), + _cdb_webmercator AS ( + SELECT + row_number() over() AS cartodb_id, + _cdb_feature_count, + ST_SetSRID( + ST_MakePoint( + _cdb_gx*_cdb_grid_size + _cdb_grid_size/2, + _cdb_gy*_cdb_grid_size + _cdb_grid_size/2 + ), + 3857 + ) AS the_geom_webmercator + FROM _cdb_gridded, _cdb_resolution + ) + SELECT + cartodb_id, + ST_Transform(the_geom_webmercator, 4326) AS the_geom, + the_geom_webmercator, + _cdb_feature_count + FROM _cdb_webmercator + ${baseQueryTemplate(ctx)} +`; diff --git a/lib/cartodb/models/aggregation/vector-aggregation.js b/lib/cartodb/models/aggregation/vector-aggregation.js new file mode 100644 index 00000000..42f070ac --- /dev/null +++ b/lib/cartodb/models/aggregation/vector-aggregation.js @@ -0,0 +1,54 @@ +const BaseAggregation = require('./base-aggregation'); +const { baseQueryTemplate } = BaseAggregation; + +module.exports = class VectorAggregation extends BaseAggregation { + sql (options) { + return vectorAggregationQueryTemplate({ + source_query: options.sql, + res: options.resolution, + columns: options.columns + }); + } +}; + +const vectorAggregationQueryTemplate = ctx => ` + WITH + _cdb_source AS ( + -- original query + ${ctx.source_query} + ), + _cdb_resolution AS ( + SELECT ${ctx.res}*CDB_XYZ_Resolution(CDB_ZoomFromScale(!scale_denominator!)) + AS _cdb_grid_size + -- equivalent to: + -- ${ctx.res}*!scale_denominator!*0.00028 + ), + _cdb_gridded AS ( + SELECT + Floor(ST_X(_cdb_source.the_geom_webmercator)/_cdb_grid_size)::int AS _cdb_gx, + Floor(ST_Y(_cdb_source.the_geom_webmercator)/_cdb_grid_size)::int AS _cdb_gy, + count(*) AS _cdb_feature_count + FROM _cdb_source, _cdb_resolution + GROUP BY _cdb_gx, _cdb_gy + ), + _cdb_webmercator AS ( + SELECT + row_number() over() AS cartodb_id, + _cdb_feature_count, + ST_SetSRID( + ST_MakePoint( + _cdb_gx*_cdb_grid_size + _cdb_grid_size/2, + _cdb_gy*_cdb_grid_size + _cdb_grid_size/2 + ), + 3857 + ) AS the_geom_webmercator + FROM _cdb_gridded, _cdb_resolution + ) + SELECT + cartodb_id, + ST_Transform(the_geom_webmercator, 4326) AS the_geom, + the_geom_webmercator, + _cdb_feature_count + FROM _cdb_webmercator + ${baseQueryTemplate(ctx)} +`;