Aggregation query models: bootstrap hierarchy classes
This commit is contained in:
parent
e18e86f565
commit
1767b83d09
9
lib/cartodb/models/aggregation/base-aggregation.js
Normal file
9
lib/cartodb/models/aggregation/base-aggregation.js
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
module.exports = class BaseAggregation {
|
||||||
|
sql () {
|
||||||
|
throw new Error('Unimplemented method');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports.baseQueryTemplate = ctx => `
|
||||||
|
select ${ctx} blah.., blah, blah...
|
||||||
|
`;
|
54
lib/cartodb/models/aggregation/raster-aggregation.js
Normal file
54
lib/cartodb/models/aggregation/raster-aggregation.js
Normal 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)}
|
||||||
|
`;
|
54
lib/cartodb/models/aggregation/vector-aggregation.js
Normal file
54
lib/cartodb/models/aggregation/vector-aggregation.js
Normal 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)}
|
||||||
|
`;
|
Loading…
Reference in New Issue
Block a user