Skip to content

Instantly share code, notes, and snippets.

@wboykinm
wboykinm / hexgrid.sql
Created February 12, 2014 17:12
Hexgrid code
WITH hgrid AS
(SELECT CDB_HexagonGrid(ST_Expand(CDB_XYZ_Extent(/* INSERT TILE XYZ SOMEHOW */), CDB_XYZ_Resolution(/* CURRENT ZOOM */) * 15), CDB_XYZ_Resolution(/* CURRENT ZOOM */) * 15) AS cell)
SELECT hgrid.cell AS the_geom_webmercator,
count(i.cartodb_id) AS points_count,
count(i.cartodb_id)/power(15 * CDB_XYZ_Resolution(/* CURRENT ZOOM */), 2) AS points_density,
1 AS cartodb_id
FROM hgrid,
(SELECT *
FROM /* TABLE NAME HERE */) i
WHERE ST_Intersects(i.the_geom_webmercator, hgrid.cell)