A demo of Mike Bostock’s TopoJSON using states & provinces boundaries from Natural Earth.
With simplification turned on (-s 100
), this reduces the original 57MB GeoJSON file to a mere 515KB!
A demo of Mike Bostock’s TopoJSON using states & provinces boundaries from Natural Earth.
With simplification turned on (-s 100
), this reduces the original 57MB GeoJSON file to a mere 515KB!
<!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]; | |
}); | |
}) | |
}; | |
} | |
}; |