Skip to content

Instantly share code, notes, and snippets.

@edgarogh
Created May 29, 2023 14:22
Show Gist options
  • Save edgarogh/e624449d7f1f648832ffb64ffeeb007f to your computer and use it in GitHub Desktop.
Save edgarogh/e624449d7f1f648832ffb64ffeeb007f to your computer and use it in GitHub Desktop.
Google Web Mercator pixel coordinates to latitude & longitude, in GLSL / https://fredriknoren.github.io/glsl-repl/
#version 300 es
precision highp float;
out vec2 res;
#define TILE_SIZE 256u
#define PI 3.141592653589793
#define EARTH_RADIUS 6378137.0
vec2 webMercatorToLatLng(vec2 pixelCoord, uint zoomLevel) {
float initialResolution = 2.0 * PI * EARTH_RADIUS / float(TILE_SIZE);
float originShift = PI * EARTH_RADIUS;
float res = initialResolution / float(1 << zoomLevel);
float mx = (pixelCoord.x * res) - originShift;
float my = -((pixelCoord.y * res) - originShift);
float lon = degrees(mx / EARTH_RADIUS);
float lat = degrees(my / EARTH_RADIUS);
lat = degrees(2.0 * atan(exp(radians(lat)))) - 90.0;
return vec2(lat, lon);
}
void main() {
vec2 pixelCoordinate = vec2(131, 190);
res = webMercatorToLatLng(pixelCoordinate, uint(1));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment