Published
Edited
Aug 27, 2020
2 forks
Importers
9 stars
Insert cell
Insert cell
map = html`<svg width=${width} height=${height} viewBox="0 0 ${width} ${height}" style="display:block;">
<path d="${path(graticule)}" stroke="#ccc" fill="none"></path>
<path d="${path(land)}"></path>
<path d="${path(outline)}" stroke="#000" fill="none"></path>
<g font-size="10" font-family="sans-serif">
${d3.range(-180, 180 + 1, 90).map(x => svg`
<text text-anchor="middle" transform="translate(${projection([x, 90]) + ""})" y="-6">${formatLongitude(x)}</text>
<text text-anchor="middle" transform="translate(${projection([x, -90]) + ""})" dy="0.71em" y="6">${formatLongitude(x)}</text>`)}
${d3.range(-80, 80 + 1, 10).map(y => svg`
<text text-anchor="start" transform="translate(${projection([180, y]) + ""})" dy="0.35em" x="6">${formatLatitude(y)}</text>
<text text-anchor="end" transform="translate(${projection([-180, y]) + ""})" dy="0.35em" x="-6">${formatLatitude(y)}</text>`)}
</g>
</svg>`
Insert cell
formatLatitude = y => `${Math.abs(y)}°${y < 0 ? "S" : "N"}`
Insert cell
formatLongitude = x => `${Math.abs(x)}°${x < 0 ? "W" : "E"}`
Insert cell
path = d3.geoPath(projection)
Insert cell
projection = d3.geoMiller()
Insert cell
margin = ({top: 30, right: 40, bottom: 30, left: 40})
Insert cell
height = {
const [[x0, y0], [x1, y1]] = d3.geoPath(projection.fitWidth(width, outline)).bounds(outline);
const dx = x1 - x0;
const k = (dx - margin.left - margin.right) / dx;
const dy = (y1 - y0) * k + margin.bottom + margin.top;
projection.scale(projection.scale() * k);
projection.translate([(dx + margin.left - margin.right) / 2, (dy + margin.top - margin.bottom) / 2]);
projection.precision(0.2);
return Math.round(dy);
}
Insert cell
outline = ({type: "Sphere"})
Insert cell
graticule = d3.geoGraticule10()
Insert cell
land = topojson.feature(world, world.objects.land)
Insert cell
world = FileAttachment("land-50m.json").json()
Insert cell
topojson = require("topojson-client@3")
Insert cell
d3 = require("d3-array@2", "d3-geo@2", "d3-geo-projection@3")
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