Public
Edited
Aug 1, 2023
9 forks
32 stars
Also listed in…
Gallery
d3-tile
Insert cell
Insert cell
map = svg`<svg viewBox="0 0 ${width} ${height}">
<defs>
<path id="land" d="${path(land)}"/>
<clipPath id="clip"><use xlink:href="${location}#land" /></clipPath>
</defs>
<g clip-path="url(${location}#clip)">
${tile().map(([x, y, z], i, {translate: [tx, ty], scale: k}) => svg`<image xlink:href="${url(x, y, z)}" x="${(x + tx) * k - 0.5}" y="${(y + ty) * k - 0.5}" width="${k + 1}" height="${k + 1}">`)}
${tile().map(([x, y, z], i, {translate: [tx, ty], scale: k}) => svg`<image xlink:href="${url(x, y, z)}" x="${(x + tx) * k}" y="${(y + ty) * k}" width="${k}" height="${k}">`)}
</g>
<use xlink:href="${location}#land" fill="none" stroke="black" stroke-width="0.5" />
</svg>`
Insert cell
url = (x, y, z) => `https://${"abc"[Math.abs(x + y) % 3]}.tiles.mapbox.com/v4/mapbox.natural-earth-2/${z}/${x}/${y}${devicePixelRatio > 1 ? "@2x" : ""}.png?access_token=pk.eyJ1IjoibWJvc3RvY2siLCJhIjoiY2s5ZWRlbTM4MDE0eDNocWJ2aXR2amNmeiJ9.LEyjnNDr_BrxRmI4UDyJAQ`
Insert cell
projection = d3.geoMercator()
Insert cell
path = d3.geoPath(projection)
Insert cell
tile = d3.tile()
.size([width, height])
.scale(projection.scale() * 2 * Math.PI)
.translate(projection([0, 0]))
Insert cell
width = 928
Insert cell
height = {
const [[x0, y0], [x1, y1]] = d3.geoPath(projection.fitWidth(width, land)).bounds(land);
const dy = Math.ceil(y1 - y0), l = Math.min(Math.ceil(x1 - x0), dy);
projection.scale(projection.scale() * (l - 1) / l).precision(0.2);
return dy;
}
Insert cell
land = FileAttachment("us.json").json()
.then(topology => topojson.feature(topology, topology.objects.land))
.then(land => ({type: "Polygon", coordinates: land.geometry.coordinates[0]}))
Insert cell
d3 = require("d3-geo@3", "d3-tile@1")
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more