Created
February 12, 2014 17:12
-
-
Save wboykinm/8959996 to your computer and use it in GitHub Desktop.
Hexgrid code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
GROUP BY hgrid.cell |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
WITH hgrid AS (SELECT CDB_HexagonGrid(ST_Expand(ST_SetSRID(ST_MakeBox2D(ST_MakePoint(-14626989.732651, 12181004.827526), ST_MakePoint(-13374645.461227, 13433349.098950)), 900913), CDB_XYZ_Resolution(12) * 15), CDB_XYZ_Resolution(12) * 15) AS cell) SELECT hgrid.cell AS wkb_geometry, count(i.id) AS points_count, count(i.id)/power(15 * CDB_XYZ_Resolution(12), 2) AS points_density, 1 AS id FROM hgrid, (SELECT * FROM households) i WHERE ST_Intersects(i.wkb_geometry, hgrid.cell) GROUP BY hgrid.cell