32

I wanted to know how to get the indexes (x,y) of a WMTS tile for a given geolocation (latitude, longitude) and zoom level.

For exemple, I have a POI located at (48.675, 2.7), I want to get the corresponding open-street-map tile for the zoom 10.

Can I do the math ? Do I need a webservice ? Precision : I have to do this programmatically.

SS_Rebelious
  • 5,621
  • 3
  • 27
  • 62
Neekobus
  • 811
  • 1
  • 7
  • 8

1 Answers1

39

The OSM wiki page is perfect : http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#Lon..2Flat._to_tile_numbers_2

Here is the extracted answer (in pseudo code) for quick reference.

Given Longitude/latitude/zoom to tile numbers :

n = 2 ^ zoom
xtile = n * ((lon_deg + 180) / 360)
ytile = n * (1 - (log(tan(lat_rad) + sec(lat_rad)) / π)) / 2

Note that log() in this pseudo code refers to natural log (often "ln()" in common math syntax, but often "log()" in many programming languages).

Given Tile numbers to longitude/latitude :

n = 2 ^ zoom
lon_deg = xtile / n * 360.0 - 180.0
lat_rad = arctan(sinh(π * (1 - 2 * ytile / n)))
lat_deg = lat_rad * 180.0 / π
ak112358
  • 812
  • 10
  • 16
Neekobus
  • 811
  • 1
  • 7
  • 8
  • 2
    wait, the OSM slippy format is the WMTS tile format? – spy Dec 15 '16 at 01:52
  • 10
    Note that the link above has implementations in many languages! (27 currently) – Cyrille Mar 13 '17 at 10:40
  • 1
    Be aware that according to this link , latitude goes from 0 to 85.0511 °N (not 90 °N) and from 0 to 85.0511 °S (not 90 °S). The number 85.0511 is the result of arctan(sinh(π)). By using this bound, the entire map becomes a (very large) square – AmirHossein Rezaei Jan 31 '21 at 11:06