Created
May 29, 2023 14:22
-
-
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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