Skip to content

Instantly share code, notes, and snippets.

@kuwapa
Last active April 24, 2022 14:26
Show Gist options
  • Select an option

  • Save kuwapa/72c61355bf9d707abee5acc889959a5b to your computer and use it in GitHub Desktop.

Select an option

Save kuwapa/72c61355bf9d707abee5acc889959a5b to your computer and use it in GitHub Desktop.
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.asinh(math.tan(lat_rad)) / math.pi) / 2.0 * n)
return (xtile, ytile)
def getTotalTileCount(leftBottom, rightTop, fromZoom, toZoom):
totalTileCount = 0
for zoom in range(fromZoom, toZoom + 1):
leftBottomTiles = deg2num(leftBottom, zoom)
rightTopTiles = deg2num(rightTop, zoom)
currentTileCount = (rightTopTiles[0] - leftBottomTiles[0] + 1) * (leftBottomTiles[1] - rightTopTiles[1] + 1)
print("zoom = " + str(zoom) + ", leftBottomTiles = " + str(leftBottomTiles) +
", rightTopTiles = " + str(rightTopTiles) + ", tileCount = " + str(currentTileCount))
totalTileCount += currentTileCount
return totalTileCount
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment