Skip to content

Instantly share code, notes, and snippets.

@oeon
Last active December 14, 2015 02:08
Show Gist options
  • Save oeon/5010979 to your computer and use it in GitHub Desktop.
Save oeon/5010979 to your computer and use it in GitHub Desktop.
d3.geo.tile test with watersheds
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.stroke {
fill: none;
stroke: #000;
stroke-width: .5;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://d3js.org/d3.geo.tile.v0.min.js"></script>
<script src="http://d3js.org/topojson.v0.min.js"></script>
<script>
var width = 960,
height = 500;
var projection = d3.geo.mercator()
.scale(180000)
.center([-119.5, 35])
.translate([width / 2, height / 2]);
var path = d3.geo.path()
.projection(projection);
var tile = d3.geo.tile()
.scale(projection.scale())
.translate(projection([0, 0]))
.zoomDelta((window.devicePixelRatio || 1) - .5);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
d3.json("h2o.json", function(error, topology) {
var tiles = tile();
var defs = svg.append("defs");
defs.append("path")
.attr("id", "hu12")
.datum(topojson.object(topology, topology.objects.hu12))
.attr("d", path);
defs.append("clipPath")
.attr("id", "clip")
.append("use")
.attr("xlink:href", "#hu12");
svg.append("g")
.attr("clip-path", "url(#clip)")
.selectAll("image")
.data(tiles)
.enter().append("image")
.attr("xlink:href", function(d) { return "http://" + ["a", "b", "c", "d"][Math.random() * 4 | 0] + ".tiles.mapbox.com/v3/slugis.map-n849blif/" + d[2] + "/" + d[0] + "/" + d[1] + ".png"; })
.attr("width", Math.round(tiles.scale))
.attr("height", Math.round(tiles.scale))
.attr("x", function(d) { return Math.round((d[0] + tiles.translate[0]) * tiles.scale); })
.attr("y", function(d) { return Math.round((d[1] + tiles.translate[1]) * tiles.scale); });
svg.append("use")
.attr("xlink:href", "#hu12")
.attr("class", "stroke");
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment