Last active
August 29, 2015 14:22
-
-
Save monte-hayward/353aefdf54deaede3037 to your computer and use it in GitHub Desktop.
Map using TopoJSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ne_10m_admin_0_map_subunits | |
ne_10m_populated_places |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<style> | |
.subunit.SCT { fill: #ddc; } | |
.subunit.WLS { fill: #cdd; } | |
.subunit.NIR { fill: #cdc; } | |
.subunit.ENG { fill: #dcd; } | |
.subunit-boundary { | |
fill: none; | |
stroke: #777; | |
stroke-dasharray: 2,2; | |
stoke-linejoin: round; | |
} | |
.subunit-boundary.IRL { | |
fill: #fff; | |
stroke: #aaa; | |
} | |
</style> | |
<body> | |
<script src="http://d3js.org/d3.v3.js"></script> | |
<script src="http://d3js.org/topojson.v1.js"></script> | |
<script> | |
var width = 960, height = 1160; | |
var svg = d3.select('body').append('svg') | |
.attr('width', width) | |
.attr('height', height); | |
d3.json('uk.json', function(error, uk){ | |
if(error) return console.err(error); | |
console.log(uk); | |
var subunits = topojson.feature(uk, uk.objects.subunits); | |
var projection = d3.geo.albers() | |
.center([0,55.4]) | |
.rotate([4.4, 0]) | |
.parallels([50, 60]) | |
.scale(6000) | |
.translate([width / 2, height /2]); | |
var path = d3.geo.path() | |
.projection(projection); | |
svg.append('path') | |
.datum(subunits) | |
.attr('d', path); | |
//display UK boundaries | |
svg.selectAll('.subunit') | |
.data(topojson.feature(uk, uk.objects.subunits).features) | |
.enter().append('path') | |
.attr('class', function(d) { return 'subunit ' + d.id }) | |
.attr('d', path); | |
//display Ireland boundaries | |
svg.append('path') | |
.datum(topojson.mesh(uk, uk.objects.subunits, function(a, b){ | |
return a === b && a.id === 'IRL'; | |
})) | |
.attr('d', path) | |
.attr('class', 'subunit-boundary IRL'); | |
//display places | |
svg.append('path') | |
.datum(topojson.feature(uk, uk.objects.places)) | |
.attr('d', path) | |
.attr('class', 'place'); | |
svg.selectAll('.place-label') | |
.data(topojson.feature(uk, uk.objects.places).features) | |
.enter().append('text') | |
.attr('class', 'place-label') | |
.attr('transform', function(d){ | |
return 'translate(' + projection(d.geometry.coordinates) + ')'; | |
}) | |
.attr('dy', '.35rem') | |
.text(function(d){ | |
return ' ' + d.properties.name + ' '; | |
}); | |
svg.selectAll('.place-label') | |
.attr('x', function(d){ | |
return d.geometry.coordinates[0] > -1 ? 6 : -6; | |
}) | |
.style('text-anchor', function(d){ | |
return d.geometry.coordinates[0] > -1 ? 'start' : 'end'; | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment