Skip to content

Instantly share code, notes, and snippets.

@saleiva
Created March 5, 2012 10:58
Show Gist options
  • Save saleiva/1977875 to your computer and use it in GitHub Desktop.
Save saleiva/1977875 to your computer and use it in GitHub Desktop.
11870.com hotels density map in Madrid
SELECT count(cartodb_id) as ct, ST_Transform(ST_Buffer(ST_SnapToGrid(the_geom,0.002),0.001, ‘quad_segs=2 endcap=square’),3857) as the_geom_webmercator FROM hoteles_playground GROUP BY ST_SnapToGrid(the_geom,0.002)
#hoteles_playground{
polygon-fill:#FF6600;
polygon-opacity:.7;
}
#hoteles_playground[ct<=40]{
polygon-fill:#BD0026}
#hoteles_playground[ct<=20]{
polygon-fill:#F03B20}
#hoteles_playground[ct<=13]{
polygon-fill:#FD8D3C}
#hoteles_playground[ct<=7]{
polygon-fill:#FEB24C}
#hoteles_playground[ct<=3]{
polygon-fill:#FED976}
#hoteles_playground[ct<=2]{
polygon-fill:#FFFFB2
}
@friedbunny
Copy link

This is a fun bit of SQL, thanks! One does have to replace the smart quotes around quad_segs=2 endcap=square before the query will run, however.

Plus, this snippet works somewhat on (multi)linestrings, depending on your data's density and the styling you apply. It does run a bit slowly the first time through, depending on your radii/etc, but I suppose that could also just be a limitation of the free hosted CartoDB (which is awesome, by the way). Here's what I ended up with after some fiddling:

SELECT count(cartodb_id) as ct, ST_Transform(ST_Buffer(ST_SnapToGrid(the_geom,0.04),0.02, 'endcap=square quad_segs=4'),3857) as the_geom_webmercator FROM testing GROUP BY ST_SnapToGrid(the_geom,0.04)

... which can be seen in action here: http://bit.ly/GAVaB4

@saleiva
Copy link
Author

saleiva commented Mar 20, 2012

Hey! That's cool! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment