12

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.

21

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 / π
  • 2
    wait, the OSM slippy format is the WMTS tile format? – spy Dec 15 '16 at 1:52
  • 5
    Note that the link above has implementations in many languages! (27 currently) – Cyrille Mar 13 '17 at 10:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.