-
-
Save Andrew-Reid/e3f242b645f167cb17d470246764ef4d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
<script src="https://d3js.org/d3-tile.v0.0.min.js"></script> | |
<script src="https://unpkg.com/topojson@3"></script> | |
</head> | |
<body> | |
<div id="mapplaceholder"></div> | |
<script> | |
var map; | |
var width = 800, | |
height = 600; | |
var pi = Math.PI, | |
tau = 2 * pi; | |
var projection = d3.geoMercator() | |
.scale(1 / tau) | |
.translate([0, 0]); | |
var path = d3.geoPath() | |
.projection(projection); | |
map = d3.select("#mapplaceholder") | |
.append("svg") | |
.attr("width", width) | |
.attr("height", height); | |
var tile = d3.tile() | |
.size([width, height]); | |
var zoom = d3.zoom() | |
.scaleExtent([1 << 15, 1 << 30]) | |
.on("zoom", zoomed); | |
var raster = map.append("g"); | |
var vector; | |
d3.json('data.json', function (error, data) { | |
if (error) throw error; | |
var topo = topojson.topology({foo: data}); | |
vector = map.selectAll("path") | |
.data(topojson.feature(topo, topo.objects.foo).features) | |
.enter() | |
.append("path"); | |
var center = projection([36.8, -2.3]); | |
map | |
.call(zoom) | |
.call(zoom.transform, d3.zoomIdentity | |
.translate(width / 2, height / 2) | |
.scale(1 << 19) | |
.translate(-center[0], -center[1])); | |
}); | |
function zoomed(){ | |
var transform = d3.event.transform; | |
var tiles = tile | |
.scale(transform.k) | |
.translate([transform.x, transform.y]) | |
(); | |
projection | |
.scale(transform.k / tau) | |
.translate([transform.x, transform.y]); | |
vector | |
.attr("d", path); | |
var image = raster | |
.attr("transform", stringify(tiles.scale, tiles.translate)) | |
.selectAll("image") | |
.data(tiles, function(d) { return d; }); | |
image.exit().remove(); | |
image.enter().append("image") | |
.attr("xlink:href", function(d) { return "http://" + "abc"[d[1] % 3] + ".tile.openstreetmap.org/" + d[2] + "/" + d[0] + "/" + d[1] + ".png"; }) | |
.attr("x", function(d) { return d[0] * 256; }) | |
.attr("y", function(d) { return d[1] * 256; }) | |
.attr("width", 256) | |
.attr("height", 256); | |
} | |
function stringify(scale, translate) { | |
var k = scale / 256, r = scale % 1 ? Number : Math.round; | |
return "translate(" + r(translate[0] * scale) + "," + r(translate[1] * scale) + ") scale(" + k + ")"; | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment