Skip to content

Instantly share code, notes, and snippets.

@donSchoe
Created July 2, 2015 07:24
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 donSchoe/f12fffb3584ee2b9042d to your computer and use it in GitHub Desktop.
Save donSchoe/f12fffb3584ee2b9042d to your computer and use it in GitHub Desktop.
epsg4326 latLonToPixels xy
// JavaScript
// converts lat/lon to pixel x/y at zoom level 0 for 256*256 tile size , inverts y coord
function latLonToPixels(lat, lon) { // lat, lon: epsg:4326
var sinLat = Math.sin(lat * Math.PI / 180.0);
var pixelX = ((lon + 180) / 360) * 256;
var pixelY = (0.5 - Math.log((1 + sinLat) / (1 - sinLat)) / (Math.PI * 4)) * 256;
return { x: pixelX, y: pixelY }; // object with .x and .y
}
@donSchoe
Copy link
Author

donSchoe commented Jul 2, 2015

ps. multiplying with * 256 can be skipped, range [0,1] is good to work with, too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment