Thursday, 12 May 2016

development - WMTS : Convert geolocation (lat, long) to tile index, at a given zoom level?


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.



Answer



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 / π

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...