[OSM-dev] Algorithm for converting lat/lon into Mercator as used in OSM PostGIS?

Nick Whitelegg nick at hogweed.org
Sat Mar 10 22:25:57 GMT 2007


Hello everyone,

Can anyone point me to any code which will do the conversion from WGS84 
lat/lon into the Mercator system used in the OSM PostGIS database? 

I've tried using the Mercator conversion code present in various places on 
SVN, mostly for utilities written pre  Mapnik, e.g:

double mercator(double normalLat)
{
    double a = log (tan ( (M_PI/4.0) + (normalLat/180.0*M_PI/2.0))) *
            180/M_PI;
    return a  * 20037508.34 / 180;
}

but this gives me the wrong answer (out by about 30km at 51 N).

I've also tried examining the PROJ.4 code used by Mapnik but it's hideously 
convoluted. If there's a quicker route I'd be grateful of it :-)

I'm aware Mapnik can do this for me, but (in Freemap), to avoid server load 
and unnecessary projection conversions on the server I want to have the bbox 
of the Freemap renderer in Mercator, and do all my conversions client side.

Thanks,
Nick




More information about the dev mailing list