Skip to content

Instantly share code, notes, and snippets.

@perrygeo
Last active December 14, 2021 15:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save perrygeo/c52185612b4b1869a35a to your computer and use it in GitHub Desktop.
Save perrygeo/c52185612b4b1869a35a 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