Last active
December 14, 2021 15:04
-
-
Save perrygeo/c52185612b4b1869a35a to your computer and use it in GitHub Desktop.
Get all map tiles for a geographic extent and zoom level(s)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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