Skip to content

Instantly share code, notes, and snippets.

@jmwenda
Last active August 30, 2016 15:17
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jmwenda/b472b0a932fc5b895a5e59dadac18192 to your computer and use it in GitHub Desktop.
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<style>html, body, #map { height: 100%; padding: 0; margin: 0 }</style>
</head>
<body>
<div id="map"></div>
<!-- include cartodb css -->
<link rel="stylesheet" href="https://cartodb-libs.global.ssl.fastly.net/cartodb.js/v3/3.15/themes/css/cartodb.css" />
<!-- include CARTO.js library -->
<script src="https://cartodb-libs.global.ssl.fastly.net/cartodb.js/v3/3.15/cartodb.js"></script>
<script type="text/javascript">
var layer;
function main() {
var map = L.map('map', {
zoomControl: false,
center: [0, 0],
zoom: 3
});
// add a nice baselayer from Stamen
L.tileLayer('http://{s}.tile.stamen.com/toner/{z}/{x}/{y}.png', {
attribution: 'Stamen'
}).addTo(map);
cartodb.createLayer(map, { user_name: 'nation',
type: 'cartodb',
sublayers: [{
sql: "WITH hgridA AS (SELECT CDB_HexagonGrid(ST_Expand(!bbox!, greatest(!pixel_width!,!pixel_height!) * 48), greatest(!pixel_width!,!pixel_height!) * 48) as cell), bigs AS (SELECT * FROM (SELECT ST_Centroid(ST_Collect(i.the_geom_webmercator)) as the_geom_webmercator, count(i.cartodb_id) as points_count, 1 as cartodb_id, array_agg(cartodb_id) AS id_list FROM hgridA, (select * from reports WHERE published = true) i where ST_Intersects(i.the_geom_webmercator, hgridA.cell) GROUP BY hgridA.cell) t WHERE points_count > 100 ), hgridB AS (SELECT CDB_HexagonGrid(ST_Expand(!bbox!, greatest(!pixel_width!,!pixel_height!) * 36), greatest(!pixel_width!,!pixel_height!) * 36) as cell), mids AS (SELECT * FROM (SELECT ST_Centroid(ST_Collect(i.the_geom_webmercator)) as the_geom_webmercator, count(i.cartodb_id) as points_count, 1 as cartodb_id, array_agg(cartodb_id) AS id_list FROM hgridB, (select * from reports WHERE published = true) i where ST_Intersects(i.the_geom_webmercator, hgridB.cell) AND cartodb_id NOT IN (SELECT unnest(id_list) FROM bigs) GROUP BY hgridB.cell) t WHERE points_count > 25 ), hgridC AS (SELECT CDB_HexagonGrid(ST_Expand(!bbox!, greatest(!pixel_width!,!pixel_height!) * 24), greatest(!pixel_width!,!pixel_height!) * 24) as cell), smalls AS (SELECT * FROM (SELECT ST_Centroid(ST_Collect(i.the_geom_webmercator)) as the_geom_webmercator, count(i.cartodb_id) as points_count, 1 as cartodb_id, array_agg(cartodb_id) AS id_list FROM hgridC, (select * from reports WHERE published = true) i where ST_Intersects(i.the_geom_webmercator, hgridC.cell) AND cartodb_id NOT IN (SELECT unnest(id_list) FROM bigs) AND cartodb_id NOT IN (SELECT unnest(id_list) FROM mids) GROUP BY hgridC.cell) t WHERE points_count > 5 ) SELECT the_geom_webmercator, 1 points_count, cartodb_id, ARRAY[cartodb_id] as id_list, 'origin' as src, cartodb_id::text cdb_list FROM reports WHERE published = true AND cartodb_id NOT IN (select unnest(id_list) FROM bigs) AND cartodb_id NOT IN (select unnest(id_list) FROM mids) AND cartodb_id NOT IN (select unnest(id_list) FROM smalls) UNION ALL SELECT *, 'bigs' as src, array_to_string(id_list, ',') FROM bigs UNION ALL SELECT *, 'mids' as src, array_to_string(id_list, ',') FROM mids UNION ALL SELECT *, 'smalls' as src, array_to_string(id_list, ',') FROM smalls",
https: 'force https',
cartocss: "#reports {marker-width: 12;marker-fill: #109DCD;marker-opacity: 0.6;marker-line-width: 0;marker-allow-overlap: true;marker-comp-op: dst-atop;[src = 'smalls'] {marker-width: 20; }[src = 'mids'] {marker-width: 44;}[src = 'bigs'] { marker-width: 64; }[zoom>11]{marker-width: 52;}}#reports::lables {text-size: 0;text-fill: black;text-opacity: 0.8;text-name: [points_count];text-face-name: 'DejaVu Sans Book';text-halo-fill: #fff;text-halo-radius: 0;[src = 'smalls'] {text-size: 18; text-halo-radius: 1; }[src = 'mids'] {text-size: 26; text-halo-radius: 1; }[src = 'bigs'] { text-size: 32; text-halo-radius: 1; } text-allow-overlap: true;[zoom>11] {text-size: 36;}}",
interactivity: 'cartodb_id, cdb_list',
},{
sql: "WITH hgridA AS (SELECT CDB_HexagonGrid(ST_Expand(!bbox!, greatest(!pixel_width!,!pixel_height!) * 48), greatest(!pixel_width!,!pixel_height!) * 48) as cell), bigs AS (SELECT * FROM (SELECT ST_Centroid(ST_Collect(i.the_geom_webmercator)) as the_geom_webmercator, count(i.cartodb_id) as points_count, 1 as cartodb_id, array_agg(cartodb_id) AS id_list FROM hgridA, (select * from reports WHERE verified = true and published = false) i where ST_Intersects(i.the_geom_webmercator, hgridA.cell) GROUP BY hgridA.cell) t WHERE points_count > 100 ), hgridB AS (SELECT CDB_HexagonGrid(ST_Expand(!bbox!, greatest(!pixel_width!,!pixel_height!) * 36), greatest(!pixel_width!,!pixel_height!) * 36) as cell), mids AS (SELECT * FROM (SELECT ST_Centroid(ST_Collect(i.the_geom_webmercator)) as the_geom_webmercator, count(i.cartodb_id) as points_count, 1 as cartodb_id, array_agg(cartodb_id) AS id_list FROM hgridB, (select * from reports WHERE verified = true and published = false) i where ST_Intersects(i.the_geom_webmercator, hgridB.cell) AND cartodb_id NOT IN (SELECT unnest(id_list) FROM bigs) GROUP BY hgridB.cell) t WHERE points_count > 25 ), hgridC AS (SELECT CDB_HexagonGrid(ST_Expand(!bbox!, greatest(!pixel_width!,!pixel_height!) * 24), greatest(!pixel_width!,!pixel_height!) * 24) as cell), smalls AS (SELECT * FROM (SELECT ST_Centroid(ST_Collect(i.the_geom_webmercator)) as the_geom_webmercator, count(i.cartodb_id) as points_count, 1 as cartodb_id, array_agg(cartodb_id) AS id_list FROM hgridC, (select * from reports WHERE verified = true and published = false) i where ST_Intersects(i.the_geom_webmercator, hgridC.cell) AND cartodb_id NOT IN (SELECT unnest(id_list) FROM bigs) AND cartodb_id NOT IN (SELECT unnest(id_list) FROM mids) GROUP BY hgridC.cell) t WHERE points_count > 5 ) SELECT the_geom_webmercator, 1 points_count, cartodb_id, ARRAY[cartodb_id] as id_list, 'origin' as src, cartodb_id::text cdb_list FROM reports WHERE verified = true AND published = false AND cartodb_id NOT IN (select unnest(id_list) FROM bigs) AND cartodb_id NOT IN (select unnest(id_list) FROM mids) AND cartodb_id NOT IN (select unnest(id_list) FROM smalls) UNION ALL SELECT *, 'bigs' as src, array_to_string(id_list, ',') FROM bigs UNION ALL SELECT *, 'mids' as src, array_to_string(id_list, ',') FROM mids UNION ALL SELECT *, 'smalls' as src, array_to_string(id_list, ',') FROM smalls",
https: 'force https',
cartocss: "#reports {marker-width: 12;marker-fill: #99FF00;marker-opacity: 0.6;marker-line-width: 0;marker-allow-overlap: true;marker-comp-op: dst-atop;[src = 'smalls'] {marker-width: 20; }[src = 'mids'] {marker-width: 44;}[src = 'bigs'] { marker-width: 64; }[zoom>11]{marker-width: 52;}}#reports::lables {text-size: 0;text-fill: black;text-opacity: 0.8;text-name: [points_count];text-face-name: 'DejaVu Sans Book';text-halo-fill: #fff;text-halo-radius: 0;[src = 'smalls'] {text-size: 18; text-halo-radius: 1; }[src = 'mids'] {text-size: 26; text-halo-radius: 1; }[src = 'bigs'] { text-size: 32; text-halo-radius: 1; } text-allow-overlap: true;[zoom>11] {text-size: 36;}}",
interactivity: 'cartodb_id, cdb_list',
},{
sql: "WITH hgridA AS (SELECT CDB_HexagonGrid(ST_Expand(!bbox!, greatest(!pixel_width!,!pixel_height!) * 48), greatest(!pixel_width!,!pixel_height!) * 48) as cell),bigs AS (SELECT * FROM (SELECT ST_Centroid(ST_Collect(i.the_geom_webmercator)) as the_geom_webmercator, count(i.cartodb_id) as points_count, 1 as cartodb_id, array_agg(cartodb_id) AS id_list FROM hgridA, (select * from reports WHERE verified = false and published = false) i where ST_Intersects(i.the_geom_webmercator, hgridA.cell) GROUP BY hgridA.cell) t WHERE points_count > 100 ), hgridB AS (SELECT CDB_HexagonGrid(ST_Expand(!bbox!, greatest(!pixel_width!,!pixel_height!) * 36), greatest(!pixel_width!,!pixel_height!) * 36) as cell), mids AS (SELECT * FROM (SELECT ST_Centroid(ST_Collect(i.the_geom_webmercator)) as the_geom_webmercator, count(i.cartodb_id) as points_count, 1 as cartodb_id, array_agg(cartodb_id) AS id_list FROM hgridB, (select * from reports WHERE verified = false and published = false) i where ST_Intersects(i.the_geom_webmercator, hgridB.cell) AND cartodb_id NOT IN (SELECT unnest(id_list) FROM bigs) GROUP BY hgridB.cell) t WHERE points_count > 25 ), hgridC AS (SELECT CDB_HexagonGrid(ST_Expand(!bbox!, greatest(!pixel_width!,!pixel_height!) * 24), greatest(!pixel_width!,!pixel_height!) * 24) as cell), smalls AS (SELECT * FROM (SELECT ST_Centroid(ST_Collect(i.the_geom_webmercator)) as the_geom_webmercator, count(i.cartodb_id) as points_count, 1 as cartodb_id, array_agg(cartodb_id) AS id_list FROM hgridC, (select * from reports WHERE verified = false and published = false) i where ST_Intersects(i.the_geom_webmercator, hgridC.cell) AND cartodb_id NOT IN (SELECT unnest(id_list) FROM bigs) AND cartodb_id NOT IN (SELECT unnest(id_list) FROM mids) GROUP BY hgridC.cell) t WHERE points_count > 5 ) SELECT the_geom_webmercator, 1 points_count, cartodb_id, ARRAY[cartodb_id] as id_list, 'origin' as src, cartodb_id::text cdb_list FROM reports WHERE verified = false AND published = false AND cartodb_id NOT IN (select unnest(id_list) FROM bigs) AND cartodb_id NOT IN (select unnest(id_list) FROM mids) AND cartodb_id NOT IN (select unnest(id_list) FROM smalls) UNION ALL SELECT *, 'bigs' as src, array_to_string(id_list, ',') FROM bigs UNION ALL SELECT *, 'mids' as src, array_to_string(id_list, ',') FROM mids UNION ALL SELECT *, 'smalls' as src, array_to_string(id_list, ',') FROM smalls",
https: 'force https',
cartocss: "#reports {marker-width: 12;marker-fill: #8b0000;marker-opacity: 0.6;marker-line-width: 0;marker-allow-overlap: true;marker-comp-op: dst-atop;[src = 'smalls'] {marker-width: 20; }[src = 'mids'] {marker-width: 44;}[src = 'bigs'] { marker-width: 64; }[zoom>11]{marker-width: 52;}}#reports::lables {text-size: 0;text-fill: black;text-opacity: 0.8;text-name: [points_count];text-face-name: 'DejaVu Sans Book';text-halo-fill: #fff;text-halo-radius: 0;[src = 'smalls'] {text-size: 18; text-halo-radius: 1; }[src = 'mids'] {text-size: 26; text-halo-radius: 1; }[src = 'bigs'] { text-size: 32; text-halo-radius: 1; } text-allow-overlap: true;[zoom>11] {text-size: 36;}}",
interactivity: 'cartodb_id, cdb_list',
}]
})
.addTo(map)
.on('done', function(layer) {
for (var i = 0; i < layer.getSubLayerCount(); ++i ) {
cdb.vis.Vis.addInfowindow(map,layer.getSubLayer(i),['cartodb_id','cdb_list'],{triggerEvent: 'featureOver'});
}
}).on('error', function() {
console.log("some error occurred");
});
}
window.onload = main;
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment