Skip to content

Instantly share code, notes, and snippets.

@mbostock
Last active November 15, 2016 18:03
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 mbostock/1e32984cf0587a86c806779a5464b0cc to your computer and use it in GitHub Desktop.
Save mbostock/1e32984cf0587a86c806779a5464b0cc to your computer and use it in GitHub Desktop.
Clipped Conic Conformal
license: gpl-3.0
border: no
<!DOCTYPE html>
<meta charset="utf-8">
<svg width="960" height="500"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://d3js.org/topojson.v2.min.js"></script>
<script>
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height");
var graticule = d3.geoGraticule().extentMajor([[-135, 0], [135, 90]]),
outline = graticule.outline(),
projection = d3.geoConicConformal().fitSize([width, height], outline),
path = d3.geoPath().projection(projection);
var defs = svg.append("defs");
defs.append("path")
.attr("id", "extent")
.attr("d", path(outline));
defs.append("clipPath")
.attr("id", "clip")
.append("use")
.attr("xlink:href", "#extent");
var g = svg.append("g")
.attr("clip-path", "url(#clip)");
g.append("path")
.attr("id", "graticule")
.attr("fill", "none")
.attr("stroke", "#777")
.attr("stroke-width", 0.5)
.attr("stroke-opacity", 0.5)
.attr("d", path(graticule()));
g.append("use")
.attr("fill", "none")
.attr("stroke", "#000")
.attr("xlink:href", "#extent");
d3.json("https://d3js.org/world-50m.v1.json", function(error, world) {
if (error) throw error;
g.insert("path", "#graticule")
.attr("fill", "#222")
.attr("d", path(topojson.feature(world, world.objects.land)));
g.insert("path", "#graticule")
.attr("fill", "none")
.attr("stroke", "#fff")
.attr("stroke-width", 0.5)
.attr("d", path(topojson.mesh(world, world.objects.countries, function(a, b) { return a !== b; })));
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment