Skip to content

Instantly share code, notes, and snippets.

Last active Mar 27, 2017
What would you like to do?
TopoJSON merge

Testing topojson.merge to merge multiple polygons into a single polygon.

<!DOCTYPE html>
<meta charset="utf-8">
.state {
fill: #ccc;
.state-boundary {
fill: none;
stroke: #fff;
.state.selected {
fill: orange;
stroke: #000;
<script src=""></script>
<script src=""></script>
var width = 1024,
height = 768;
var path = d3.geo.path()
var svg ="body").append("svg")
.attr("width", width)
.attr("height", height);
var selected = d3.set([
'ND', 'SD', 'NE', 'MN', 'IA', 'WI', 'IL', 'OH', 'MI', 'IN'
d3.json("ne_50m_usa_canada.topojson", function(error, us) {
.datum(topojson.feature(us, us.objects.usa_canada))
.attr("class", "state")
.attr("d", path);
.datum(topojson.mesh(us, us.objects.usa_canada, function(a, b) { return a !== b; }))
.attr("class", "state-boundary")
.attr("d", path);
.datum(topojson.merge(us, us.objects.usa_canada.geometries.filter(function(d) { return selected.has(; })))
.attr("class", "state selected")
.attr("d", path);
});"height", height + "px");
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment