Skip to content

Instantly share code, notes, and snippets.

@Fil
Last active June 19, 2017 20:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fil/1b26d81094571160975490e8a47a3215 to your computer and use it in GitHub Desktop.
Save Fil/1b26d81094571160975490e8a47a3215 to your computer and use it in GitHub Desktop.
Centroid Counties [UNLISTED]
license: gpl-3.0
<!DOCTYPE html>
<meta charset="utf-8">
<style>
path {
fill: none;
stroke: steelblue;
}
</style>
<body>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://d3js.org/topojson.v2.min.js"></script>
<script>
var width = 960,
height = 500;
var projection = d3.geoAlbersUsa()
.scale(1000)
.translate([width / 2, height / 2]);
var path = d3.geoPath()
.projection(projection);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
d3.json("https://gist.githubusercontent.com/mbostock/4090846/raw/d534aba169207548a8a3d670c9c2cc719ff05c47/us.json", function(error, us) {
if (error) throw error;
var centroids = topojson.feature(us, us.objects.counties).features
.filter(function(d) { return d.geometry && d.geometry.coordinates.length; })
.map(d3.geoCentroid);
console.log(centroids)
svg.append("path")
.attr("d", path({
type: "MultiPoint",
coordinates: centroids
}));
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment