[OSM-dev] Good explanation of latitude-to-pixel conversion?

Nick Whitelegg Nick.Whitelegg at solent.ac.uk
Sat May 17 14:29:50 BST 2008


Am in the process of developing a mobile client for Freemap, and 
potentially (in conjunction with one of my students) a tool for OSM as a 
whole. Part of this involves converting lat/lon to "Google" Mercator pixel 
coordinates.

I have the code to do this, but I guess I'm one of these people that like 
to understand what's actually going on :-) Am happy with the lon->y 
conversion but would like some clarification on the lat->x conversion; all 
the articles I've found so far basically just give you the formula and say 
"here, use it". Does anyone know of any good references which fully 
explain the maths behind it?

The specific formula I mean (represented in Java) is:

public static int latToY(double lat,int zoom)
{
                double f = Math.sin((Math.PI/180)*lat);
 
                int y = (int)Math.round
                        (Math.pow(2,zoom+7) + 0.5*Math.log((1+f)/(1-f)) * 
(-Math.pow(2,zoom+8)/(2*Math.PI)));
                return y;
}

Thanks,
Nick




More information about the dev mailing list