TransWikia.com

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

Geographic Information Systems Asked by Neekobus on August 30, 2021

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.

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

Correct answer by Neekobus on August 30, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP