Fix resolution definition and use zoom as path param

This commit is contained in:
Daniel García Aubert 2019-02-27 12:42:54 +01:00
parent c3df075d91
commit b87298bad9
2 changed files with 22 additions and 20 deletions

View File

@ -33,7 +33,7 @@ module.exports = class AggregatedFeaturesLayergroupController {
}
register (mapRouter) {
mapRouter.get('/:token/:layer/cluster/:clusterId', this.middlewares());
mapRouter.get('/:token/:layer/:z/cluster/:clusterId', this.middlewares());
}
middlewares () {
@ -66,12 +66,12 @@ function getClusteredFeatures (clusterBackend) {
const { mapConfigProvider } = res.locals;
const { token } = res.locals;
const { dbuser, dbname, dbpassword, dbhost, dbport } = res.locals;
const { layer, clusterId } = req.params;
const { layer, z: zoom, clusterId } = req.params;
const params = {
token,
dbuser, dbname, dbpassword, dbhost, dbport,
layer, clusterId
layer, zoom, clusterId
};
clusterBackend.getClusterFeatures(mapConfigProvider, params, (err, features, stats = {}) => {

View File

@ -25,7 +25,7 @@ module.exports = class ClusterBackend {
return callback(error);
}
const query = layer.options.sql;
const query = layer.options.sql_raw;
const resolution = layer.options.aggregation.resolution || 1;
getColumnsName(pg, query, (err, columns) => {
@ -33,9 +33,9 @@ module.exports = class ClusterBackend {
return callback(err);
}
const { clusterId } = params;
const { zoom, clusterId } = params;
getClusterFeatures(pg, clusterId, columns, query, resolution, (err, features) => {
getClusterFeatures(pg, zoom, clusterId, columns, query, resolution, (err, features) => {
if (err) {
return callback(err);
}
@ -57,7 +57,7 @@ function getColumnsName (pg, query, callback) {
query: query
}));
debug(sql);
debug('> getColumnsName:', sql);
pg.query(sql, function (err, resultSet) {
if (err) {
@ -72,15 +72,16 @@ function getColumnsName (pg, query, callback) {
}, true);
}
function getClusterFeatures (pg, clusterId, columns, query, resolution, callback) {
function getClusterFeatures (pg, zoom, clusterId, columns, query, resolution, callback) {
const sql = replaceTokens(clusterFeaturesQuery({
zoom: zoom,
id: clusterId,
query: query,
res: resolution,
res: 256/resolution,
columns: columns
}));
debug(sql);
debug('> getClusterFeatures:', sql);
pg.query(sql, (err, data) => {
if (err) {
@ -108,10 +109,10 @@ function replaceTokens(sql, replaceValues) {
}
replaceValues = replaceValues || {
bbox: 'ST_MakeEnvelope(0,0,0,0)',
scale_denominator: '0',
pixel_width: '1',
pixel_height: '1',
bbox: 'ST_MakeEnvelope(-20037508.34,-20037508.34,20037508.34,20037508.34,3857)',
scale_denominator: '500000001',
pixel_width: '156412',
pixel_height: '156412',
var_zoom: '0',
var_bbox: '[0,0,0,0]',
var_x: '0',
@ -128,7 +129,6 @@ function replaceTokens(sql, replaceValues) {
}
const limitedQuery = ctx => `SELECT * FROM (${ctx.query}) __cdb_schema LIMIT 0`;
// const nonGeomsQuery = ctx => `SELECT ${ctx.columns.join(', ')} FROM (${ctx.query}) __cdb_non_geoms_query`;
const clusterFeaturesQuery = ctx => `
WITH
_cdb_params AS (
@ -147,9 +147,12 @@ const clusterFeaturesQuery = ctx => `
FROM (${ctx.query}) _cdb_query, _cdb_params
WHERE _cdb_query.cartodb_id = ${ctx.id}
)
SELECT ${ctx.columns.join(', ')} FROM (
SELECT _cdb_query.* FROM _cell, (${ctx.query}) _cdb_query
WHERE ST_Intersects(_cdb_query.the_geom_webmercator, _cell.bbox)
SELECT
${ctx.columns.join(', ')}
FROM (
SELECT _cdb_query.*
FROM _cell, (${ctx.query}) _cdb_query
WHERE ST_Intersects(_cdb_query.the_geom_webmercator, _cell.bbox)
) __cdb_non_geoms_query
`;
@ -162,7 +165,6 @@ const clusterFeaturesQuery = ctx => `
// inaccurate results due to rounding present in those values.
const gridResolution = ctx => {
const minimumResolution = 2*Math.PI*6378137/Math.pow(2,38);
const pixelSize = 'CDB_XYZ_Resolution(CDB_ZoomFromScale(!scale_denominator!))';
debug(ctx);
const pixelSize = `CDB_XYZ_Resolution(${ctx.zoom})`;
return `GREATEST(${256/ctx.res}*${pixelSize}, ${minimumResolution})::double precision`;
};