Calculate lat lon bounds for individual tile

3,964 views
Skip to first unread message

Gavin

unread,
Nov 26, 2011, 7:27:24 PM11/26/11
to MapTiler User Group
Hi all,

I have multiple tile sources I'd like to present on a map so when
presenting a tile I need to determine which source to serve it up
from, checking against the bounds.

Anyone know how to calculate the lat lon bounds for a single tile
based purely on zoom, x, and y (from the file structure generated by
gdal2tiles)? BTW: I'm using Google Maps API v3 in case there's a need
to call some functionality from the API to help with calculations.

The reason I'm restricted to purely zoom, x, and y is because the
tiles aren't just getting called by an overlay on the map but also
some custom print functionality that allows printing outside of the
map.

Thanks in advance for any help or suggestions anyone has.

Cheers,
Gavin

Klokan Petr Přidal

unread,
Nov 27, 2011, 12:07:20 PM11/27/11
to mapt...@googlegroups.com
Hi Gavin,

the math is described at:

http://www.maptiler.org/google-maps-coordinates-tile-bounds-projection/

including the source code for command line utility and an online demonstration.

It is also pretty simple math:

function tile2long(x,z) { return (x/Math.pow(2,z)*360-180); }
function tile2lat(y,z) { var n=Math.PI-2*Math.PI*y/Math.pow(2,z);
return (180/Math.PI*Math.atan(0.5*(Math.exp(n)-Math.exp(-n)))); }

Please be sure you understand difference between XYZ/Google vs TMS in
y axis (discussed several times in this mailing list).

Google Maps API V3 gives you the required functions too via
.fromPointToLatLng() of map.getProjection().
Demonstrated for example at:
http://www.maptiler.org/example-usgs-drg-grand-canyon-gtiff/v3.html

Petr Pridal
Klokan Technologies GmbH
http://www.klokantech.com/

> --
> You received this message because you are subscribed to the Google Groups "MapTiler User Group" group.
> To post to this group, send email to mapt...@googlegroups.com.
> To unsubscribe from this group, send email to maptiler+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/maptiler?hl=en.
>
>

--
http://blog.klokan.cz/
http://www.maptiler.org/
http://www.oldmapsonline.org/

Gavin

unread,
Nov 27, 2011, 4:19:57 PM11/27/11
to MapTiler User Group
Thanks Petr, very much appreciated. And thanks for sharing such useful
tools for the community to use! I've given credit to your work on
http://www.topomap.co.nz

I'd got half way there - I'd managed to convert TMS to Google (for y)
but had run out of time before having a go at converting to lat lon
yesterday.

In case it's useful for someone else - to convert y from TMS to
Google:
y = (1 << zoom) - y - 1;

One I've got the lat lon I know there's some v3 API methods to get
pixels, which I can then add 256 pixels to in each dimension and then
convert to another lat lon point to use for my tile bounds.

Thank again!

On Nov 28, 6:07 am, Klokan Petr Přidal <klo...@klokan.cz> wrote:
> Hi Gavin,
>
> the math is described at:
>
> http://www.maptiler.org/google-maps-coordinates-tile-bounds-projection/
>
> including the source code for command line utility and an online demonstration.
>
> It is also pretty simple math:
>
> function tile2long(x,z) { return (x/Math.pow(2,z)*360-180); }
> function tile2lat(y,z) { var n=Math.PI-2*Math.PI*y/Math.pow(2,z);
> return (180/Math.PI*Math.atan(0.5*(Math.exp(n)-Math.exp(-n)))); }
>
> Please be sure you understand difference between XYZ/Google vs TMS in
> y axis (discussed several times in this mailing list).
>
> Google Maps API V3 gives you the required functions too via
> .fromPointToLatLng() of map.getProjection().
> Demonstrated for example at:http://www.maptiler.org/example-usgs-drg-grand-canyon-gtiff/v3.html
>
> Petr Pridal

> Klokan Technologies GmbHhttp://www.klokantech.com/

Reply all
Reply to author
Forward
0 new messages