Skip to content

Instantly share code, notes, and snippets.

@plmrry
Last active June 4, 2021 17:30
Show Gist options
  • Save plmrry/5934a4ac85774aa8a080b7c9e08f54d0 to your computer and use it in GitHub Desktop.
Save plmrry/5934a4ac85774aa8a080b7c9e08f54d0 to your computer and use it in GitHub Desktop.
proj4 to D3 projection
function radiansToDegrees(radians) {
return (radians * 180) / Math.PI;
}
function degreesToRadians(degrees) {
return degrees * (Math.PI / 180);
}
function proj4toD3(proj4String) {
const proj4 = require("proj4");
const d3 = require("d3");
const proj = proj4(proj4String);
const project = function(lambda, phi) {
return proj.forward([lambda, phi].map(radiansToDegrees));
};
project.invert = function(x, y) {
return proj.inverse([x, y]).map(degreesToRadians);
};
const projection = d3.geoProjection(project);
return projection;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment