Skip to content

Instantly share code, notes, and snippets.

@toja
Last active September 18, 2017 19:10
Show Gist options
  • Save toja/b3f1aaf5877eb9fc3f481dc03dc6a8c3 to your computer and use it in GitHub Desktop.
Save toja/b3f1aaf5877eb9fc3f481dc03dc6a8c3 to your computer and use it in GitHub Desktop.
World - Selected Projections
license: gpl-3.0
height: 540
border: no
Display the source blob
Display the rendered blob
Raw
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">
<script src="//d3js.org/d3.v4.min.js"></script>
<script src="//d3js.org/d3-geo-projection.v1.min.js"></script>
<script src="//d3js.org/topojson.v2.min.js"></script>
<style>
.stroke {
fill: none;
stroke: #000;
stroke-width: 1px;
}
.fill {
fill: #fff;
}
.graticule {
fill: none;
stroke: #777;
stroke-width: .5px;
stroke-opacity: .5;
}
.land {
fill: #222;
}
.countries {
fill: none;
stroke: #fff;
stroke-width: .8px;
}
</style>
<body>
<div id="map"></div>
<select name="proj" autocomplete="off" onchange="update(this.value);">
<option value="0">Natural Earth</option>
<option value="1">Robinson</option>
<option value="2">Kavraisky VII</option>
<option value="3">Winkel Tripel</option>
<option value="4">Times</option>
<option value="5">Patterson</option>
<option value="6">Braun</option>
<option value="7">Miller</option>
</select>
<script>
var width = 960,
height = 500;
var projections = [
d3.geoNaturalEarth,
d3.geoRobinson,
d3.geoKavrayskiy7,
d3.geoWinkel3,
d3.geoTimes,
d3.geoPatterson,
d3.geoCylindricalStereographic,
d3.geoMiller
];
var proj = projections[0]();
var path = d3.geoPath()
.projection(proj);
var graticule = d3.geoGraticule();
var svg = d3.select("#map").append("svg")
.attr("width", width)
.attr("height", height);
svg.append("defs").append("path")
.datum({type: "Sphere"})
.attr("id", "sphere")
.attr("d", path);
svg.append("use")
.attr("class", "stroke")
.attr("xlink:href", "#sphere");
svg.append("use")
.attr("class", "fill")
.attr("xlink:href", "#sphere");
svg.append("path")
.datum(graticule)
.attr("class", "graticule")
.attr("d", path);
d3.json("50m.json", function (error, world) {
if (error) throw error;
var land = topojson.feature(world, world.objects.land);
var countries = topojson.mesh(world, world.objects.countries, function(a, b) { return a !== b; });
svg.append("path")
.datum(land)
.attr("class", "land")
.attr("d", path);
svg.append("path")
.datum(countries)
.attr("class", "countries")
.attr("d", path);
});
var update = function(i) {
paths = svg.selectAll("path");
proj = projections[i]();
path.projection(proj);
paths.attr("d", path);
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment