Map projection converter
2024
Python
GLSL
A blazing fast, shader based image projection converter. Converts between the equirectangular and mercator projections. Can be used via a CLI or directly in Python. Check it out on on GitHub!
vec2 equi_to_merc(float u, float v)
{
// uv to equirectangular
float lon = remap(v, 0, 1, -EQUI_LON, EQUI_LON);
// equirectangular to mercator
float y = log(tan(M_PI / 4 + lon / 2));
// mercator to uv
y = remap(y, -M_PI, M_PI, 0, 1);
return vec2(u, y);
}
Using the moderngl python library, the converter runs the input image through a remapping shader. The shaders transform uv coordinates to latitude and longitude and then convert between the two projections.
Additionally, this implementation can also render with either nearest or linear texture sampling, letting you choose between a sharp or interpolated upscaling.
def merc_to_equi(u, v):
# uv to mercator
lon = remap(v, 0, 1, -math.pi, math.pi)
# mercator to equirectangular
y = 2 * math.atan(math.pow(math.e, lon)) - math.pi * 0.5
# equirectangular to uv
y = remap(y, -EQUI_LON, EQUI_LON, 0, 1)
return (u, y)
A CPU implementation using the Pillow library exists as a fallback, although with obvious downsides of poor performance and scalability.