Optimize the gridded clustering strategy

The internal grid_px parameter is adjusted for best results with default symbol size
This commit is contained in:
Javier Goizueta 2015-12-22 17:59:49 +01:00
parent c8a1ef6f68
commit 5a78ee2896

View File

@ -175,7 +175,7 @@ AS $$
overview_rel TEXT; overview_rel TEXT;
reduction FLOAT8; reduction FLOAT8;
base_name TEXT; base_name TEXT;
grid_px FLOAT8 = 3.0; grid_px FLOAT8 = 7.5; -- Grid size in pixels at Z level overview_z
grid_m FLOAT8; grid_m FLOAT8;
aggr_attributes TEXT; aggr_attributes TEXT;
attributes TEXT; attributes TEXT;
@ -193,27 +193,31 @@ AS $$
aggr_attributes := ''; aggr_attributes := '';
EXECUTE Format('DROP TABLE IF EXISTS %s CASCADE;', overview_rel); EXECUTE Format('DROP TABLE IF EXISTS %s CASCADE;', overview_rel);
-- Now we cluster the data using a grid of size grid_m
-- and selecte the centroid (average coordinates) of each cluster.
-- If we had a selected numeric attribute of interest we could use it
-- as a weight for the average coordinates.
EXECUTE Format(' EXECUTE Format('
CREATE TABLE %3$s AS CREATE TABLE %3$s AS
WITH clusters AS ( WITH clusters AS (
SELECT SELECT
first_value(f.cartodb_id) OVER ( %5$s
PARTITION BY count(*) AS n,
Floor(ST_X(f.the_geom_webmercator)/%2$s)::int, SUM(ST_X(f.the_geom_webmercator)) AS sx,
Floor(ST_Y(f.the_geom_webmercator)/%2$s)::int SUM(ST_Y(f.the_geom_webmercator)) AS sy,
) AS cartodb_id, Floor(ST_X(f.the_geom_webmercator)/%2$s)::int AS gx,
%4$s Floor(ST_Y(f.the_geom_webmercator)/%2$s)::int AS gy,
the_geom, row_number() OVER () AS cartodb_id
the_geom_webmercator
FROM %1$s f FROM %1$s f
GROUP BY gx, gy
) )
SELECT SELECT
%4$s
cartodb_id, cartodb_id,
ST_Centroid(ST_Collect(clusters.the_geom)) AS the_geom, ST_SetSRID(ST_MakePoint(sx/n, sy/n), 3857) AS the_geom_webmercator,
%5$s ST_Transform(ST_SetSRID(ST_MakePoint(sx/n, sy/n), 3857), 4326) AS the_geom
ST_Centroid(ST_Collect(clusters.the_geom_webmercator)) AS the_geom_webmercator
FROM clusters FROM clusters
GROUP BY cartodb_id;
', reloid::text, grid_m, overview_rel, attributes, aggr_attributes); ', reloid::text, grid_m, overview_rel, attributes, aggr_attributes);
RETURN overview_rel; RETURN overview_rel;