Skip to content

Instantly share code, notes, and snippets.

@davidheyman
Forked from perrygeo/get_tiles.py
Created November 12, 2018 07:23
Show Gist options
  • Save davidheyman/e67915afa5b9594ac8115e1a1d31f3bf to your computer and use it in GitHub Desktop.
Save davidheyman/e67915afa5b9594ac8115e1a1d31f3bf to your computer and use it in GitHub Desktop.
Get all map tiles for a geographic extent and zoom level(s)
from __future__ import print_function
import mercantile
import urllib
import time
import os
def download_tile(tileurl, tile, output_dir, pause=1):
url = tileurl + '/{z}/{x}/{y}.png'.format(**tile.__dict__)
local_path = url.replace(tileurl, output_dir)
print(url, '===>', local_path)
if not os.path.exists(local_path):
try:
os.makedirs(os.path.dirname(local_path))
except OSError:
pass
urllib.urlretrieve(url, local_path)
time.sleep(pause) # be kind
if __name__ == "__main__":
zooms = [7, 8, 9, 10]
bounds = -123.40, 45.47, -122.42, 46.33 # W, S, E, N
tileurl = 'http://a.tile.openstreetmap.org'
output_dir = '/tmp/osm'
for tile in mercantile.tiles(*bounds, zooms=zooms):
download_tile(tileurl, tile, output_dir)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment