Skip to content

Instantly share code, notes, and snippets.

@jasondavies
Forked from mbostock/.block
Created November 16, 2012 23:20
Show Gist options
  • Save jasondavies/4091835 to your computer and use it in GitHub Desktop.
Save jasondavies/4091835 to your computer and use it in GitHub Desktop.
World Boundaries TopoJSON
Display the source blob
Display the rendered blob
Raw
Loading
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>
path {
fill: none;
stroke: #000;
}
.graticule {
fill: none;
stroke: #ccc;
stroke-width: .5px;
}
.foreground {
fill: none;
stroke: #333;
stroke-width: 1.5px;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="topojson.js"></script>
<script>
var width = 960,
height = 500;
var projection = d3.geo.orthographic()
.scale(250)
.clipAngle(90);
var path = d3.geo.path()
.projection(projection);
var graticule = d3.geo.graticule();
var λ = d3.scale.linear()
.domain([0, width])
.range([-180, 180]);
var φ = d3.scale.linear()
.domain([0, height])
.range([90, -90]);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
svg.on("mousemove", function() {
var p = d3.mouse(this);
projection.rotate([λ(p[0]), φ(p[1])]);
svg.selectAll("path:not(.foreground)").attr("d", path);
});
svg.append("path")
.attr("class", "graticule")
.datum(graticule)
.attr("d", path);
svg.append("path")
.datum(graticule.outline)
.attr("class", "foreground")
.attr("d", path);
d3.json("readme-world-admin1.json", function(error, topology) {
svg.append("path")
.datum(topojson.mesh(topology))
.attr("d", path);
});
</script>
var topojson = {
version: "0.0.1",
mesh: function(topology) {
var kx = topology.transform.scale[0],
ky = topology.transform.scale[1],
dx = topology.transform.translate[0],
dy = topology.transform.translate[1];
return {
type: "MultiLineString",
coordinates: topology.arcs.map(function(arc) {
var y0 = 0,
x0 = 0;
return arc.map(function(point) {
var x1 = x0 + point[0],
y1 = y0 + point[1];
x0 = x1;
y0 = y1;
return [x1 * kx + dx, y1 * ky + dy];
});
})
};
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment