Skip to content

Instantly share code, notes, and snippets.

@davidheyman
Created November 12, 2018 08:24
Show Gist options
  • Save davidheyman/6322d45fd0309f379d25c296bd57c371 to your computer and use it in GitHub Desktop.
Save davidheyman/6322d45fd0309f379d25c296bd57c371 to your computer and use it in GitHub Desktop.
Get tile sizes from style-optimised vector tiles on Mapbox
import mercantile
import requests
import settings
def getStyle():
r = requests.get('https://api.mapbox.com/styles/v1/axismaps/' + settings.MAPBOX_STYLE + '?access_token=' + settings.MAPBOX_TOKEN)
return r.json()['modified']
def download_tile(tileurl, tile, modified):
url = tileurl + '/{z}/{x}/{y}.mvt'.format(**tile.__dict__)
url += '?access_token=' + settings.MAPBOX_TOKEN
url += '&style=mapbox://styles/axismaps/' + settings.MAPBOX_STYLE + '@' + modified
r = requests.get(url)
return int(r.headers['Content-Length'])
if __name__ == "__main__":
zooms = [7, 8, 9, 10]
bounds = -123.40, 45.47, -122.42, 46.33 # W, S, E, N
tileurl = 'https://api.mapbox.com/v4/' + settings.MAPBOX_MAPID
modified = getStyle()
total_size = 0
for tile in mercantile.tiles(*bounds, zooms=zooms):
total_size += download_tile(tileurl, tile, modified)
print total_size
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment