Windshaft-cartodb/lib/cartodb/models/aggregation/aggregation-mapconfig.js

125 lines
3.6 KiB
JavaScript
Raw Normal View History

2017-12-19 02:06:01 +08:00
const MapConfig = require('windshaft').model.MapConfig;
const aggregationQuery = require('./aggregation-query');
2017-12-19 03:42:26 +08:00
const { SUPPORTED_AGGREGATE_FUNCTIONS } = require('./aggregation-query');
const aggregationValidator = require('./aggregation-validator');
const {
2017-12-19 02:51:55 +08:00
createPositiveNumberValidator,
2017-12-19 03:42:26 +08:00
createIncludesValueValidator,
createAggregationColumnsValidator
2017-12-19 17:54:20 +08:00
} = aggregationValidator;
2017-12-19 02:06:01 +08:00
module.exports = class AggregationMapConfig extends MapConfig {
static get PLACEMENTS () {
return [
'centroid',
'point-grid',
'point-sample'
];
}
static get PLACEMENT () {
return AggregationMapConfig.PLACEMENTS[0];
}
static get THRESHOLD () {
return 1e5; // 100K
}
static get RESOLUTION () {
return 1;
}
static get SUPPORTED_GEOMETRY_TYPES () {
return [
'ST_Point'
];
}
static supportsGeometryType(geometryType) {
return AggregationMapConfig.SUPPORTED_GEOMETRY_TYPES.includes(geometryType);
}
constructor (config, datasource) {
super(config, datasource);
2017-12-19 02:19:02 +08:00
const validate = aggregationValidator(this);
2017-12-19 03:42:26 +08:00
const positiveNumberValidator = createPositiveNumberValidator(this);
2017-12-19 02:19:02 +08:00
const includesValidPlacementsValidator = createIncludesValueValidator(this, AggregationMapConfig.PLACEMENTS);
2017-12-19 03:42:26 +08:00
const aggregationColumnsValidator = createAggregationColumnsValidator(this, SUPPORTED_AGGREGATE_FUNCTIONS);
2017-12-19 02:19:02 +08:00
2017-12-19 03:42:26 +08:00
validate('resolution', positiveNumberValidator);
2017-12-19 02:19:02 +08:00
validate('placement', includesValidPlacementsValidator);
2017-12-19 03:42:26 +08:00
validate('threshold', positiveNumberValidator);
validate('columns', aggregationColumnsValidator);
2017-12-19 02:06:01 +08:00
}
getAggregatedQuery (index) {
const { sql_raw, sql } = this.getLayer(index).options;
const {
resolution = AggregationMapConfig.RESOLUTION,
threshold = AggregationMapConfig.THRESHOLD,
placement = AggregationMapConfig.PLACEMENT,
columns = {},
dimmensions = {}
} = this.getAggregation(index);
return aggregationQuery({
query: sql_raw || sql,
resolution,
threshold,
placement,
columns,
dimmensions
});
}
isAggregationMapConfig () {
return this.isVectorOnlyMapConfig() || this.hasAnyLayerAggregation();
}
isAggregationLayer (index) {
return this.isVectorOnlyMapConfig() || this.hasLayerAggregation(index);
}
hasAnyLayerAggregation () {
const layers = this.getLayers();
for (let index = 0; index < layers.length; index++) {
if (this.hasLayerAggregation(index)) {
return true;
}
}
return false;
}
hasLayerAggregation (index) {
const layer = this.getLayer(index);
const { aggregation } = layer.options;
return aggregation !== undefined && (typeof aggregation === 'object' || typeof aggregation === 'boolean');
}
getAggregation (index) {
if (!this.hasLayerAggregation(index)) {
return;
}
const { aggregation } = this.getLayer(index).options;
if (typeof aggregation === 'boolean') {
return {};
}
return aggregation;
}
doesLayerReachThreshold(index, featureCount) {
const threshold = this.getAggregation(index) && this.getAggregation(index).threshold ?
this.getAggregation(index).threshold :
AggregationMapConfig.THRESHOLD;
return featureCount >= threshold;
}
};