Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Given a WGS84 bounding box and an OSM tile zoom range calculates a total number of tiles.
#!/usr/bin/python
# This script should be considered CC0 licensed
# the deg2num function is from http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#lon.2Flat_to_tile_numbers_2
import math
def deg2num(lat_deg, lon_deg, zoom):
lat_rad = math.radians(lat_deg)
n = 2.0 ** zoom
xtile = int((lon_deg + 180.0) / 360.0 * n)
ytile = int((1.0 - math.log(math.tan(lat_rad) + (1 / math.cos(lat_rad))) / math.pi) / 2.0 * n)
return (xtile, ytile)
def countTiles(minZoom, maxZoom, x0, y0, x1, y1):
tileCount = 0;
for z in range(minZoom, maxZoom + 1):
tiles_dx = ((deg2num(0, x1, z)[0] - deg2num(0, x0, z)[0]) + 1);
tiles_dy = ((deg2num(y0, 0, z)[1] - deg2num(y1, 0, z)[1]) + 1);
tileCount += tiles_dx * tiles_dy
#print z, ": ", tiles_dx, ", ", tiles_dy
return tileCount;
print countTiles(12, 18, 12.96524048, 47.77163739, 13.06549072, 47.81684332)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment