Skip to content

Instantly share code, notes, and snippets.

@blaurt
Last active March 28, 2022 17:30
Show Gist options
  • Save blaurt/b0ca054a7384ebfbea2d5fce69ae9bf4 to your computer and use it in GitHub Desktop.
Save blaurt/b0ca054a7384ebfbea2d5fce69ae9bf4 to your computer and use it in GitHub Desktop.
transformAndPoint.js
/**
* @param {number} latitude in degrees
* @param {number} longitude in degrees
* @param {number} mapWidth in pixels
* @param {number} mapHeight in pixels
*/
function latLonToOffsets(latitude, longitude, mapWidth, mapHeight) {
const FE = 180; // false easting
const radius = mapWidth / (2 * Math.PI);
const latRad = degreesToRadians(latitude);
const lonRad = degreesToRadians(longitude + FE);
const x = lonRad * radius;
const yFromEquator = radius * Math.log(Math.tan(Math.PI / 4 + latRad / 2));
const y = mapHeight / 2 - yFromEquator;
return { x, y };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment