Skip to content

Instantly share code, notes, and snippets.

@chelm
Created March 4, 2014 20:16
Show Gist options
  • Select an option

  • Save chelm/9354732 to your computer and use it in GitHub Desktop.

Select an option

Save chelm/9354732 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
margin: 0;
}
.map {
position: relative;
overflow: hidden;
}
.world {
position:absolute;
top: 0px;
z-index: -1;
}
.layer {
position: absolute;
}
.tile {
position: absolute;
width: 256px;
height: 256px;
}
.tile path {
fill: #33f0f0;
stroke: none;
stroke-linejoin: round;
stroke-linecap: round;
}
.tile path.c1 {
fill: #2166ac;
}
.tile path.c2 {
fill: #4393c3;
}
.tile path.c3 {
fill:#92c5de;
}
.tile path.c4 {
fill:#d1e5f0;
}
.tile path.c5 {
fill:#f7f7f7;
}
.tile path.c6 {
fill:#fddbc7;
}
.tile path.c7 {
fill:#f4a582;
}
.tile path.c8 {
fill:#d6604d;
}
.tile path.c9 {
fill:#b2182b;
}
.tile path.c10 {
fill:#fa8072;
}
.tile {
opacity:0.8;
}
.info {
position: absolute;
bottom: 10px;
left: 10px;
}
path {
fill: #444;
stroke:#888;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://d3js.org/topojson.v1.min.js"></script>
<script src="http://d3js.org/d3.geo.tile.v0.min.js"></script>
<script>
var width = Math.max(960, window.innerWidth),
height = Math.max(500, window.innerHeight),
prefix = prefixMatch(["webkit", "ms", "Moz", "O"]);
var tile = d3.geo.tile()
.size([width, height]);
var tileProjection = d3.geo.mercator();
var tilePath = d3.geo.path()
.projection(tileProjection);
var zoom = d3.behavior.zoom()
.scale(1 << 10)
.scaleExtent([1 << 9, 1 << 23])
.translate([width / 2, height / 2])
.on("zoom", zoomed);
var map = d3.select("body").append("div")
.attr("class", "map")
.style("width", width + "px")
.style("height", height + "px")
.call(zoom);
//svg map
var projection = d3.geo.mercator()
.scale(zoom.scale() / 2 / Math.PI)
.translate(zoom.translate());
var path = d3.geo.path().projection( projection );
var svg = d3.select("body").append("svg")
.attr('class', 'world')
.style("width", width + "px")
.style("height", height + "px")
.call(zoom);
d3.json("world-110m.json", function(error, world) {
console.log('world', world);
svg.append("g")
.attr("class", "states")
.selectAll("path")
.data(topojson.feature(world, world.objects.countries).features)
.enter().append("path")
.attr("d", path);
});
var layer = map.append("div")
.attr("class", "layer");
var info = map.append("div")
.attr("class", "info");
var scale = d3.scale.quantile()
.domain([-20,70])
.range(['c1','c2','c3','c4','c5','c6','c7','c8','c9','c10']);
zoomed();
function zoomed() {
var tiles = tile
.scale(zoom.scale())
.translate(zoom.translate())
();
var image = layer
.style(prefix + "transform", matrix3d(tiles.scale, tiles.translate))
.selectAll(".tile")
.data(tiles, function(d) { return d; });
image.exit()
.each(function(d) { this._xhr.abort(); })
.remove();
image.enter().append("svg")
.attr("class", "tile")
.style("left", function(d) { return d[0] * 256 + "px"; })
.style("top", function(d) { return d[1] * 256 + "px"; })
.each(function(d) {
var svg = d3.select(this);
var host = "http://koop.dc.esri.com/climate/temperature/tiles/";
this._xhr = d3.json(host + d[2] + "/" + d[0] + "/" + d[1] + ".json", function(error, json) {
var k = Math.pow(2, d[2]) * 256; // size of the world in pixels
tilePath.projection()
.translate([k / 2 - d[0] * 256, k / 2 - d[1] * 256]) // [0°,0°] in pixels
.scale(k / 2 / Math.PI);
svg.selectAll("path")
.data(json.features) //.sort(function(a, b) { return a.properties.sort_key - b.properties.sort_key; }))
.enter().append("path")
.attr("class", function(d) { return scale(d.properties.temp-272.15); })
.attr("d", tilePath);
});
});
projection
.scale(zoom.scale() / 2 / Math.PI)
.translate(zoom.translate());
svg.selectAll("path").attr("d", path);
}
function matrix3d(scale, translate) {
var k = scale / 256, r = scale % 1 ? Number : Math.round;
return "matrix3d(" + [k, 0, 0, 0, 0, k, 0, 0, 0, 0, k, 0, r(translate[0] * scale), r(translate[1] * scale), 0, 1 ] + ")";
}
function prefixMatch(p) {
var i = -1, n = p.length, s = document.body.style;
while (++i < n) if (p[i] + "Transform" in s) return "-" + p[i].toLowerCase() + "-";
return "";
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment