How to find the bounding box for a web map tile in Python
Today I learnt …
When you’re working with tiled web maps you’ll often use a z/y/x coordinate system instead of geographic coordinate system like longitude and latitude. Map tiles use a grid system so, for example, you might talk about tile 14/8046/5106 — the 8,046th row and the 5,106rd column at the 14th zoom level. That particular tile corresponds to part of the Southside of Edinburgh.
Sometimes you’ll want to convert a tile’s z/y/x into a bounding box in longitude and latitude. In my particular case I needed to debug which features should appear in a tile, and I wanted the tile’s bounding box so I could pass it to PostGIS’s ST_MakeEnvelope
function.
Using the Python script below (save it to a file named bbox.py
) you can find the answer:
$ python3 bbox.py 14/8046/5106
ST_MakeEnvelope(-3.2080078125, 55.936894769039434, -3.18603515625, 55.94919982336745, 4326)
:
:
:
:
return
return / * 360.0 - 180
return
=
The script requires Python 3.7 or higher. Consider the code available under the MIT licence.