Skip to content

Instantly share code, notes, and snippets.

@loganwilliams
Last active April 17, 2019 20:54
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 loganwilliams/eb0a474557a32d64f6b57d409e40e486 to your computer and use it in GitHub Desktop.
Save loganwilliams/eb0a474557a32d64f6b57d409e40e486 to your computer and use it in GitHub Desktop.
projection debugging
license: mit
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v5.min.js"></script>
<style>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
</style>
</head>
<body>
<canvas width=960 height=500>
<script>
let width = 960;
let height = 500;
let context = d3.select("canvas").node().getContext('2d');
let path1 = d3.geoPath();
let path2 = d3.geoPath();
let degree = 360/(2*Math.PI);
let skew = 0.07;
let transform = (lng, lat) => {
let x = d3.scaleLinear().domain([-180, 180]).range([0, width])(lng);
let y = d3.scaleLinear().domain([-90, 90]).range([height, 0])(lat);
return {x: x + y * skew, y: y + x* skew}
};
let projection0 = d3.geoTransform({
point: function(x,y) {
const t = transform(x, y);
this.stream.point(t.x, t.y);
}
});
let translate_offset = transform(0,0);
let projection1 = d3.geoProjection((x,y) => {
const t = transform(x*degree, y*degree);
return [t.x, -t.y];
}).scale(1).translate([translate_offset.x, translate_offset.y]);
d3.json("world_countries.json").then(d => {
path1.projection(projection1).context(context);
context.strokeStyle = "#F00";
context.beginPath();
path1(d)
context.stroke();
path2.projection(projection0).context(context);
context.strokeStyle = "#00F";
context.beginPath();
path2(d)
context.stroke();
})
</script>
</body>
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment