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

Sat May 17 17:24:31 BST 2008

```This set of functions might be more readable

http://svn.openstreetmap.org/applications/routing/pyroute/tilenames.py

and covers stuff like finding the edges of tiles, as well as just the
mercator stuff

On Sat, May 17, 2008 at 4:11 PM, David MENTRE <dmentre at linux-france.org> wrote:
> Hello,
>
>> On Sat, May 17, 2008 at 2:29 PM, Nick Whitelegg
>> <Nick.Whitelegg at solent.ac.uk> wrote:
>>> 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 :-)
>
> You are not the only one. I followed the same path (pun intended) a few
> weeks ago.
>
>>> 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;
>>> }
>
> The code you give is to translate a (lat, zoom) to x coordinate, you
> have a more direct formula applying the Mercator projection to the lat
> value:
>  http://wiki.openstreetmap.org/index.php/Mercator
>
> In the above page, it helps to know that r_minor and r_major are Earth
>
> The original post helps to understand why taking into account the
> variation of radius was necessary:
>  http://osdir.com/ml/gis.openstreetmap.devel/2006-12/msg00016.html
>
> "OJ W" <ojwlists at googlemail.com> writes:
>
>> e.g. wikipedia will tell you all about how mercator itself works:
>>
>> http://en.wikipedia.org/wiki/Mercator_projection
>
> This is a very useful page.
>
>> and for the tiles, alll you do is say "select the bit of
>> mercator-projected map between two arbitrary latitudes (which happen
>> to be numerically special but you don't need to care about that) and
>> call that range the limits of your map in Y.
>>
>> you then just start dividing Y into halves and quarters etc. to do the tiles.
>
> In fact, I found explanation in
> http://wiki.openstreetmap.org/index.php/Slippy_map_tilenames sufficient
> after thinking a little about it.
>
> I hope it helps,
> Yours,
> d.
>
> Footnotes:
> [1]  http://en.wikipedia.org/wiki/Earth
>
> --
> GPG/PGP key: A3AD7A2A David MENTRE <dmentre at linux-france.org>
>  5996 CC46 4612 9CA4 3562  D7AC 6C67 9E96 A3AD 7A2A
>

```