|
<!DOCTYPE html> |
|
<head> |
|
<meta charset="utf-8"> |
|
<!-- leaflet --> |
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.0.0-rc.1/leaflet.js"></script> |
|
|
|
<!-- Main tangram library --> |
|
<script src="https://mapzen.com/tangram/0.8/tangram.min.js"></script> |
|
|
|
<script src="https://cdn.jsdelivr.net/quicksettings/latest/quicksettings.min.js"></script> |
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script> |
|
<script src="https://d3js.org/d3.v4.min.js"></script> |
|
|
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.0.0-rc.1/leaflet.css" /> |
|
<style> |
|
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; } |
|
|
|
#map { |
|
height: 100%; |
|
width: 100%; |
|
position: absolute; |
|
z-index: 0; |
|
} |
|
</style> |
|
</head> |
|
|
|
<body> |
|
<div id="map"></div> |
|
|
|
<script> |
|
var map = L.map('map'); |
|
|
|
var layer = Tangram.leafletLayer({ |
|
scene: 'scene.yaml', |
|
attribution: '<a href="https://mapzen.com/tangram" target="_blank">Tangram</a> | © OSM contributors | <a href="https://mapzen.com/" target="_blank">Mapzen</a>' |
|
}); |
|
layer.addTo(map); |
|
|
|
// Keep a reference to the scene |
|
var scene = layer.scene; |
|
|
|
// World |
|
map.setView([16.0315162,8.7011004], 3); |
|
|
|
var updateConfig = function() { |
|
if(scene.config) { |
|
window.requestAnimationFrame(function() { |
|
console.log('scene.config', scene.config); |
|
scene.updateConfig(); |
|
}); |
|
} else { |
|
console.log('scene config not ready') |
|
} |
|
|
|
} |
|
|
|
updateConfig(); |
|
|
|
scene.subscribe({ |
|
error: function (e) { |
|
console.log('scene error:', e); |
|
}, |
|
warning: function (e) { |
|
console.log('scene warning:', e); |
|
}, |
|
load: function (e) { |
|
console.log('scene load complete'); |
|
|
|
setTimeout(function() { |
|
loadData(); |
|
}, 500); |
|
} |
|
}); |
|
|
|
function loadData() { |
|
//Load some data and then update the scene. |
|
d3.csv("world_population.csv", function(error, data) { |
|
|
|
// Reshape the data, and extract the population counts |
|
// for 2015. |
|
var rows = data.map(function(d){ |
|
return { |
|
'code': d['Country Code'], |
|
'name': d['Country Name'], |
|
'population': Number(d['2015']) |
|
} |
|
}) |
|
|
|
// Make a color scale |
|
// |
|
var lowCol = "hsl(62,100%,90%)"; |
|
var highCol = "hsl(222,30%,20%)"; |
|
|
|
var domain = d3.extent(rows, function(d) { |
|
if (d.population > 0) { |
|
return d.population |
|
} |
|
}); |
|
|
|
var color = d3.scaleLog() |
|
.domain(domain) |
|
.range([lowCol, highCol]) |
|
.interpolate(d3.interpolateHcl); |
|
|
|
// So it looks like the color scale can't be correctly serialized |
|
// across to the tangram renderer (which i think is running in a web |
|
// worker), so we pre-compute the colors here. |
|
var mapData = rows.reduce(function(memo, d) { |
|
d.color = color(d.population); |
|
memo[d.code] = d; |
|
return memo |
|
}, {}); |
|
|
|
attachData(scene, mapData); |
|
}); |
|
} |
|
|
|
function attachData(scene, mapData) { |
|
// Attach the data and scale functions to the scene |
|
var config = scene.config; |
|
config.global.data = mapData; |
|
config.layers.FeatureCollection.draw.polygons.color = function() { |
|
var countryData = global.data[feature.adm0_a3]; |
|
if (countryData && countryData.color) { |
|
return countryData.color; |
|
} else { |
|
return 'black' |
|
} |
|
}; |
|
|
|
updateConfig(); |
|
} |
|
|
|
</script> |
|
</body> |