# [OSM-dev] How to translate coordinate to pixel for a tile ?

Jon Burgess jburgess777 at googlemail.com
Thu Nov 15 22:51:22 GMT 2007

```On Thu, 2007-11-15 at 22:50 +0100, Rune Baggetun wrote:
> This is how I tested the perl code:
>
> #!/usr/bin/perl
> use Math::Trig;
> my \$lon = 60.3434;
> my \$lat = 5.2834;
>
> my \$z = 15;
> my \$x = (\$lon+180)/360 *2**\$z * 256;
> my \$y = (1 - log(tan(\$lat*pi/180) + sec(\$lat*pi/180))/pi)/2 *2**\$z * 256;
> my \$xtile = int(\$x/256);
> my \$ytile = int(\$y/256);
> my \$xpixel = \$x % 256;
> print "\$xpixel\n";
> my \$ypixel = \$y % 256;
> print "\$ypixel\n";
>
>
> This is the output:
>
> 151
> 105
>
> This is actually the first piece of perl code I've written (or copied)
> so I am not surprised if I did something wrong. I do most my
> 'amateurish' coding in java and python.

Attached is a similar example written in python which shows what you get
with the lat/lon swapped:

\$ ./coords.py
BAD
get( (60.343400000000003, 5.2834000000000003) 15 )
Tile: z( 15 ) x( 21876 ) y( 15902 ), offset( 151.0 , 105.0 )
Good
get( (5.2834000000000003, 60.343400000000003) 15 )
Tile: z( 15 ) x( 16864 ) y( 9452 ), offset( 232.0 , 249.0 )

Jon

-------------- next part --------------
A non-text attachment was scrubbed...
Name: coords.py
Type: text/x-python
Size: 1453 bytes
Desc: not available
URL: <http://lists.openstreetmap.org/pipermail/dev/attachments/20071115/c9dbb8ad/attachment.py>
```

More information about the dev mailing list