Skip to content

Instantly share code, notes, and snippets.

@clhenrick
Last active November 21, 2017 21:16
Show Gist options
  • Save clhenrick/e68812d9560a35342c1bd7e0f1bcc75a to your computer and use it in GitHub Desktop.
Save clhenrick/e68812d9560a35342c1bd7e0f1bcc75a to your computer and use it in GitHub Desktop.
Raster & Vector I
license: gpl-3.0
height: 960
Display the source blob
Display the rendered blob
Raw
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>
#streets {
fill: none;
stroke: black;
stroke-width: 1.5px;
stroke-linejoin: round;
}
</style>
<svg width="960" height="960"></svg>
<script src="//d3js.org/d3.v4.min.js"></script>
<script src="https://unpkg.com/d3-tile@0.0"></script>
<script src="//d3js.org/topojson.v1.min.js"></script>
<script>
var pi = Math.PI,
tau = 2 * pi;
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height");
var projection = d3.geoMercator()
.scale((1 << 18) / tau)
.translate([width / 2, height / 2])
.center([-83.15, 42.43]);
var path = d3.geoPath()
.projection(projection);
d3.json("detroit.json", function(error, detroit) {
if (error) throw error;
var tiles = d3.tile()
.size([width, height])
.scale(projection.scale() * tau)
.translate(projection([0, 0]))
();
console.log(tiles);
svg.selectAll("image")
.data(tiles)
.enter().append("image")
.attr("xlink:href", function(d) { return "http://" + "abc"[d.y % 3] + ".tile.openstreetmap.org/" + d.z + "/" + d.x + "/" + d.y + ".png"; })
.attr("x", function(d) { return (d.x + tiles.translate[0]) * tiles.scale; })
.attr("y", function(d) { return (d.y + tiles.translate[1]) * tiles.scale; })
.attr("width", tiles.scale)
.attr("height", tiles.scale);
svg.append("path")
.attr("id", "streets")
.datum(topojson.mesh(detroit, detroit.objects.detroit))
.attr("d", path);
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment