From 580ec38ab85aff795429a8a74b5286b1fd63444c Mon Sep 17 00:00:00 2001 From: Javier Goizueta Date: Sat, 23 Apr 2016 15:07:06 +0200 Subject: [PATCH 1/3] Adjust overview clustered point to pixel centers Fixes #240 --- scripts-available/CDB_Overviews.sql | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/scripts-available/CDB_Overviews.sql b/scripts-available/CDB_Overviews.sql index 699ca5c..a12c31b 100644 --- a/scripts-available/CDB_Overviews.sql +++ b/scripts-available/CDB_Overviews.sql @@ -652,7 +652,9 @@ AS $$ overview_rel TEXT; reduction FLOAT8; base_name TEXT; + pixel_m FLOAT8; grid_m FLOAT8; + offset_m FLOAT8; aggr_attributes TEXT; attributes TEXT; columns TEXT; @@ -678,8 +680,10 @@ AS $$ SELECT * FROM _cdb_split_table_name(reloid) INTO schema_name, table_name; - -- compute grid cell size using the overview_z dimension... - SELECT CDB_XYZ_Resolution(overview_z)*grid_px INTO grid_m; + -- pixel_m: size of a pixel in webmercator units (meters) + SELECT CDB_XYZ_Resolution(overview_z) INTO pixel_m; + -- grid size in meters + grid_m = grid_px * pixel_m; attributes := _CDB_Aggregable_Attributes_Expression(reloid); aggr_attributes := _CDB_Aggregated_Attributes_Expression(reloid); @@ -690,7 +694,12 @@ AS $$ aggr_attributes := aggr_attributes || ', '; END IF; - point_geom = Format('ST_SetSRID(ST_MakePoint(gx*%1$s + %2$s, gy*%1$s + %2$s), 3857)', grid_m, grid_m/2); + -- points are displaced to it's cell's center + offset_m := grid_m/2; + -- then corrected to be at the nearest pixel's center (by up to half a pixel) + offset_m := offset_m + pixel_m/2 - MOD((grid_m/2)::numeric, pixel_m::numeric)::float8; + + point_geom := Format('ST_SetSRID(ST_MakePoint(gx*%1$s + %2$s, gy*%1$s + %2$s), 3857)', grid_m, offset_m); -- compute the resulting columns in the same order as in the base table WITH cols AS ( From 7b2100b51e07f3ddbd07b95d7cff07e0e51cface Mon Sep 17 00:00:00 2001 From: Javier Goizueta Date: Mon, 25 Apr 2016 13:33:43 +0200 Subject: [PATCH 2/3] Adjust overview coordinates to pixel centers This makes the adjustment for all grid sizes, not only for integral number of pixels. --- scripts-available/CDB_Overviews.sql | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/scripts-available/CDB_Overviews.sql b/scripts-available/CDB_Overviews.sql index a12c31b..22703aa 100644 --- a/scripts-available/CDB_Overviews.sql +++ b/scripts-available/CDB_Overviews.sql @@ -655,6 +655,10 @@ AS $$ pixel_m FLOAT8; grid_m FLOAT8; offset_m FLOAT8; + offset_x TEXT; + offset_y TEXT; + cell_x TEXT; + cell_y TEXT; aggr_attributes TEXT; attributes TEXT; columns TEXT; @@ -694,12 +698,15 @@ AS $$ aggr_attributes := aggr_attributes || ', '; END IF; - -- points are displaced to it's cell's center - offset_m := grid_m/2; - -- then corrected to be at the nearest pixel's center (by up to half a pixel) - offset_m := offset_m + pixel_m/2 - MOD((grid_m/2)::numeric, pixel_m::numeric)::float8; + -- Center of each cell: + cell_x := Format('gx*%1$s + %2$s', grid_m, grid_m/2); + cell_y := Format('gy*%1$s + %2$s', grid_m, grid_m/2); - point_geom := Format('ST_SetSRID(ST_MakePoint(gx*%1$s + %2$s, gy*%1$s + %2$s), 3857)', grid_m, offset_m); + -- Displacement to the nearest pixel center: + offset_x := Format('%2$s/2 - MOD((%1$s)::numeric, (%2$s)::numeric)::float8', cell_x, pixel_m); + offset_y := Format('%2$s/2 - MOD((%1$s)::numeric, (%2$s)::numeric)::float8', cell_y, pixel_m); + + point_geom := Format('ST_SetSRID(ST_MakePoint(%1$s + %3$s, %2$s + %4$s), 3857)', cell_x, cell_y, offset_x, offset_y); -- compute the resulting columns in the same order as in the base table WITH cols AS ( From 3c12cf629f75e1814ea558f19bad747520906e3a Mon Sep 17 00:00:00 2001 From: Javier Goizueta Date: Mon, 25 Apr 2016 13:53:59 +0200 Subject: [PATCH 3/3] Optimize overview pixel adjustment for integer-pixel cells --- scripts-available/CDB_Overviews.sql | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/scripts-available/CDB_Overviews.sql b/scripts-available/CDB_Overviews.sql index 22703aa..ab0b606 100644 --- a/scripts-available/CDB_Overviews.sql +++ b/scripts-available/CDB_Overviews.sql @@ -703,8 +703,14 @@ AS $$ cell_y := Format('gy*%1$s + %2$s', grid_m, grid_m/2); -- Displacement to the nearest pixel center: - offset_x := Format('%2$s/2 - MOD((%1$s)::numeric, (%2$s)::numeric)::float8', cell_x, pixel_m); - offset_y := Format('%2$s/2 - MOD((%1$s)::numeric, (%2$s)::numeric)::float8', cell_y, pixel_m); + IF MOD(grid_px::numeric, 1.0::numeric) = 0 THEN + offset_m := pixel_m/2 - MOD((grid_m/2)::numeric, pixel_m::numeric)::float8; + offset_x := Format('%s', offset_m); + offset_y := Format('%s', offset_m); + ELSE + offset_x := Format('%2$s/2 - MOD((%1$s)::numeric, (%2$s)::numeric)::float8', cell_x, pixel_m); + offset_y := Format('%2$s/2 - MOD((%1$s)::numeric, (%2$s)::numeric)::float8', cell_y, pixel_m); + END IF; point_geom := Format('ST_SetSRID(ST_MakePoint(%1$s + %3$s, %2$s + %4$s), 3857)', cell_x, cell_y, offset_x, offset_y);