Using d3-tile to display raster image tiles underneath some TopoJSON vectors. Compare to the zoomable version.
Tiles copyright OpenStreetMap contributors.
forked from mbostock's block: Raster & Vector I
xxxxxxxxxx
<meta charset="utf-8">
<style>
#streets {
fill: none;
stroke: black;
stroke-width: 1.5px;
stroke-linejoin: round;
}
body {
position: absolute;
margin: 0px;
font: 16px sans-serif;
}
svg {
background-color: #4682b4;
}
.info {
color: #000;
position: absolute;
top: 450px;
left: 800px;
}
path {
fill: #555555;
stroke: #aaaaaa;
}
path.quake {
fill: crimson;
}
</style>
<svg width="960" height="960"></svg>
<script src="//d3js.org/d3.v4.min.js"></script>
<script src="//d3js.org/d3-tile.v0.0.min.js"></script>
<script src="//d3js.org/topojson.v1.min.js"></script>
<script>
// Set the map projection centered on Oklahoma
var projection = d3.geoAlbersUsa()
.scale(7543)
.translate([702, -186]);
var path = d3.geoPath()
.projection(projection);
var pi = Math.PI,
tau = 2 * pi;
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height");
var url ="https://raw.githubusercontent.com/deldersveld/topojson/master/countries/us-states/OK-40-oklahoma-counties.json"
d3.json(url, function(error, oklahoma) {
if (error) throw error;
var tiles = d3.tile()
.size([width, height])
.scale(projection.scale() * tau)
.translate(projection([0, 0]))
();
svg.selectAll("image")
.data(tiles)
.enter().append("image")
.attr("xlink:href", function(d) { return "https://" + "abc"[d[1] % 3] + ".tile.openstreetmap.org/" + d[2] + "/" + d[0] + "/" + d[1] + ".png"; })
.attr("x", function(d) { return (d[0] + tiles.translate[0]) * tiles.scale; })
.attr("y", function(d) { return (d[1] + tiles.translate[1]) * tiles.scale; })
.attr("width", tiles.scale)
.attr("height", tiles.scale);
svg.append("path")
.attr("id", "streets")
.datum(topojson.mesh(oklahoma, oklahoma.objects.oklahoma))
.attr("d", path);
});
</script>
https://d3js.org/d3.v4.min.js
https://d3js.org/d3-tile.v0.0.min.js
https://d3js.org/topojson.v1.min.js