Skip to content

Instantly share code, notes, and snippets.

@matteblair
Created June 27, 2016 16:59
Show Gist options
  • Save matteblair/6bfb84d18df1088b907f0f0abf5ba6ca to your computer and use it in GitHub Desktop.
Save matteblair/6bfb84d18df1088b907f0f0abf5ba6ca to your computer and use it in GitHub Desktop.
Grab some mapzen vector tiles!
import os
import urllib2
# This is a library of tile tools from Mapbox. Install with 'pip install mercantile'.
import mercantile
# Set the bounds of the area you want to download. (West, South, East, North)
bounds = (-105, 39.99, -104.99, 40)
zooms = [14]
# Set your tile api key.
api_key = "YOUR_TILE_API_KEY_HERE"
# Set your tile format.
tile_format = "topojson"
# Set the url template for tiles.
url_template = "https://vector.mapzen.com/osm/all/{z}/{x}/{y}" + ".{f}?api_key={k}".format(f=tile_format, k=api_key)
# Create a directory to store the tiles.
dirname = "tiles"
if not os.path.exists(dirname):
os.makedirs(dirname)
for tile in list(mercantile.tiles(*bounds, zooms=zooms)):
url = url_template.format(x=tile.x, y=tile.y, z=tile.z)
print "Fetching tile: %s" % (url)
data = urllib2.urlopen(url, timeout=10).readlines()
filename = "{d}/{z}_{x}_{y}.{f}".format(d=dirname, x=tile.x, y=tile.y, z=tile.z, f=tile_format)
print "Saving to: %s" % (filename)
with open(filename, 'wb') as f:
f.writelines(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment