Skip to content

Instantly share code, notes, and snippets.

@ramiroaznar
Last active April 5, 2016 15:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ramiroaznar/3c3544184c7fe916943c to your computer and use it in GitHub Desktop.
Save ramiroaznar/3c3544184c7fe916943c to your computer and use it in GitHub Desktop.
How to make 3D buildings with CartoCSS
<!DOCTYPE html>
<html>
<head>
<title>How to make 3D buildings with CartoCSS</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<link rel="shortcut icon" href="http://cartodb.com/assets/favicon.ico" />
<link rel="stylesheet" href="http://libs.cartocdn.com/cartodb.js/v3/3.15/themes/css/cartodb.css" />
<script src="http://libs.cartocdn.com/cartodb.js/v3/3.15/cartodb.js"></script>
<style>
html, body,#map {
width:100%;
height:100%;
padding: 0;
margin: 0;
}
</style>
</head>
<body>
<div id='map'></div>
<script type="text/javascript">
function main() {
var style = [
'#bk_buildings_h{',
' polygon-opacity: 0.0;',
' line-opacity:0;',
' line-color: #FFFFFF;',
' [height>0]{',
' polygon-fill:gray;',
' polygon-opacity: 0.7;',
' line-opacity:0.3;',
' line-color: #FFFFFF;',
' [zoom = 12] {polygon-opacity:.2;}',
' [zoom = 13] {polygon-opacity:.4;}',
' [zoom = 15] {polygon-opacity:.5;}',
' [zoom >16]::under{',
' building-fill-opacity: 0;',
' }',
' [zoom >16]{',
' polygon-fill:transparent;',
' line-color: rgba(0,0,0,0.3);',
' building-fill:gray;',
' building-fill-opacity: 1;',
' building-height: [height]*0.8;',
' }',
' }',
'}'
].join('\n');
var map = new L.Map('map', {
zoomControl: false,
center: [40.704512, -73.936669],
zoom: 17
});
cartodb.createLayer(map, {
user_name: 'ramirocartodb',
type: 'cartodb',
sublayers: [
{
type: "http", // add basemap without labels
urlTemplate: "http://{s}.basemaps.cartocdn.com/light_nolabels/{z}/{x}/{y}.png",
subdomains: [ "a", "b", "c" ]
},
{
type: "mapnik", // buildings
sql: 'SELECT * FROM bk_buildings_h ORDER BY ST_YMax(the_geom) DESC',
cartocss: style
},
{
type: "http", // add basemap labels
urlTemplate: "http://{s}.basemaps.cartocdn.com/light_only_labels/{z}/{x}/{y}.png",
subdomains: [ "a", "b", "c" ]
},
]
})
.addTo(map);
}
window.onload = main;
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment