Aggregation query models: bootstrap hierarchy classes

This commit is contained in:
Daniel García Aubert 2017-11-30 15:34:20 +01:00
parent e18e86f565
commit 1767b83d09
3 changed files with 117 additions and 0 deletions

View File

@ -0,0 +1,9 @@
module.exports = class BaseAggregation {
sql () {
throw new Error('Unimplemented method');
}
};
module.exports.baseQueryTemplate = ctx => `
select ${ctx} blah.., blah, blah...
`;

View File

@ -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)}
`;

View File

@ -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)}
`;