Skip to content

Instantly share code, notes, and snippets.

Last active September 15, 2015 06:57
Show Gist options
  • Save ckhung/7f3870769b14c921513e to your computer and use it in GitHub Desktop.
Save ckhung/7f3870769b14c921513e to your computer and use it in GitHub Desktop.
# 用向量圖磚 (vector tiles) 自製陽春離線地圖
import math, sys, argparse, os, re
def deg2tile_id(zoom,lon,lat):
lat_rad = math.radians(lat)
n = 2.0 ** zoom
xtile = int((lon + 180.0) / 360.0 * n)
ytile = int((1.0 - math.log(math.tan(lat_rad) + (1 / math.cos(lat_rad))) / math.pi) / 2.0 * n)
return (xtile, ytile)
parser = argparse.ArgumentParser(description='print tile-downloading commands',formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('-u', '--url', type=str,
# http://[abc]{}.json
# also see
help='tile server url')
parser.add_argument('-s', '--sleep', type=int, default=5,
help='# of seconds to sleep between wget\'s')
parser.add_argument('-d', '--destination', type=str, default='.',
help='(root of) destination directory')
parser.add_argument('-z', '--zoom', type=int, default='7',
help='zoom level (0 to 19')
parser.add_argument('boundary', type=float, nargs=4,
help='coords of two opposite corners of the range')
args = parser.parse_args()
if args.boundary[0] > args.boundary[2]:
(args.boundary[0], args.boundary[2]) = (args.boundary[2], args.boundary[0])
if args.boundary[1] > args.boundary[3]:
(args.boundary[1], args.boundary[3]) = (args.boundary[3], args.boundary[1])
B = {}
(B['left'], B['bottom']) = deg2tile_id(args.zoom, args.boundary[0], args.boundary[1])
(B['right'], B['top']) = deg2tile_id(args.zoom, args.boundary[2], args.boundary[3])
suffix ='\{\}(\.\w+)', args.url)
suffix =
b = args.boundary
print '# {0:s} -s {1:d} -z {2:d} -d {3:s} -u {4:s} {5:f} {6:f} {7:f} {8:f}'.format(sys.argv[0], args.sleep, args.zoom, args.destination, args.url, b[0], b[1], b[2], b[3])
print '# downloading {0:d} x {1:d} tiles'.format(B['right']+1-B['left'], B['bottom']+1-B['top'])
for x in range(B['left'], B['right']+1):
print 'mkdir -p {0:s}/{1:d}/{2:d}'.format(args.destination, args.zoom, x)
for y in range(B['top'], B['bottom']+1):
tile_id = '{0:d}/{1:d}/{2:d}'.format(args.zoom, x, y)
tile_url = args.url.replace('{}', tile_id)
print 'wget -O {0:s}/{1:s} {2:s}'.format(args.destination, tile_id+suffix, tile_url)
print 'sleep ' + str(args.sleep)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment