|
| 1 | +## 异步爬取切片地图 |
| 2 | +from pygeotile.tile import Tile |
| 3 | +from urllib import request |
| 4 | +import os |
| 5 | +import asyncio |
| 6 | +import time |
| 7 | +from time import sleep |
| 8 | +from asyncio import Queue |
| 9 | +import functools |
| 10 | + |
| 11 | +def create_image_path(rootpath, level): |
| 12 | + path = './%s/%d'%(rootpath, level) |
| 13 | + if not os.path.exists(path): |
| 14 | + os.makedirs(path) |
| 15 | + return path |
| 16 | + |
| 17 | +def create_image_url(minlon, minlat, maxlon, maxlat, minzoom, maxzoom, basetileurl, rootpath): |
| 18 | + for zoom in range(minzoom, maxzoom + 1): |
| 19 | + mintile = Tile.for_latitude_longitude(latitude = minlat, longitude = minlon, zoom = zoom) |
| 20 | + maxtile = Tile.for_latitude_longitude(latitude = maxlat, longitude = maxlon, zoom = zoom) |
| 21 | + |
| 22 | + print('mintile', 'X:', mintile.tms_x, 'Y:', mintile.tms_y, 'zoom:', mintile.zoom) |
| 23 | + print('maxtile', 'Y:', maxtile.tms_x, 'Y:', maxtile.tms_y, 'zoom:', maxtile.zoom) |
| 24 | + |
| 25 | + mintms_x, mintms_y = mintile.tms_x, mintile.tms_y |
| 26 | + maxtms_x, maxtms_y = maxtile.tms_x, maxtile.tms_y |
| 27 | + |
| 28 | + create_image_path(rootpath, zoom) |
| 29 | + |
| 30 | + imagelists = Queue() |
| 31 | + for x in range(mintms_x, maxtms_x + 1): |
| 32 | + for y in range(maxtms_y, mintms_y + 1): |
| 33 | + savepath = './%s/%d/%d_%d.png'%(rootpath, zoom, x, y) |
| 34 | + tileurl = basetileurl + '&x=%d&y=%d&z=%d'%(x, y, zoom) |
| 35 | + imagelists.put_nowait((tileurl, savepath)) |
| 36 | + |
| 37 | + return imagelists |
| 38 | + |
| 39 | +async def save_image(imagelists): |
| 40 | + while True: |
| 41 | + try: |
| 42 | + start = time.time() |
| 43 | + print('---- Length', imagelists.qsize()) |
| 44 | + if imagelists.empty(): |
| 45 | + print("Done!") |
| 46 | + break |
| 47 | + image = await imagelists.get() |
| 48 | + tileurl, savepath = image[0], image[1] |
| 49 | + |
| 50 | + request.urlretrieve(tileurl, savepath) |
| 51 | + print('---- PID:', os.getpid(), '--- use time: ', str(time.time() - start), tileurl) |
| 52 | + |
| 53 | + imagelists.task_done() |
| 54 | + except Exception as e: |
| 55 | + print('---- Error: {}'.format(e)) |
| 56 | + with open('./error.log', 'a') as f: |
| 57 | + f.write('---- Error: {}'.format(e)) |
| 58 | + |
| 59 | +def create_image_url_v2(minlon, minlat, maxlon, maxlat, minzoom, maxzoom, basetileurl, rootpath): |
| 60 | + for zoom in range(minzoom, maxzoom + 1): |
| 61 | + mintile = Tile.for_latitude_longitude(latitude = minlat, longitude = minlon, zoom = zoom) |
| 62 | + maxtile = Tile.for_latitude_longitude(latitude = maxlat, longitude = maxlon, zoom = zoom) |
| 63 | + |
| 64 | + print('mintile', 'X:', mintile.tms_x, 'Y:', mintile.tms_y, 'zoom:', mintile.zoom) |
| 65 | + print('maxtile', 'Y:', maxtile.tms_x, 'Y:', maxtile.tms_y, 'zoom:', maxtile.zoom) |
| 66 | + |
| 67 | + mintms_x, mintms_y = mintile.tms_x, mintile.tms_y |
| 68 | + maxtms_x, maxtms_y = maxtile.tms_x, maxtile.tms_y |
| 69 | + |
| 70 | + create_image_path(rootpath, zoom) |
| 71 | + |
| 72 | + imagelists = [] |
| 73 | + for x in range(mintms_x, maxtms_x + 1): |
| 74 | + for y in range(maxtms_y, mintms_y + 1): |
| 75 | + savepath = './%s/%d/%d_%d.png'%(rootpath, zoom, x, y) |
| 76 | + tileurl = basetileurl + '&x=%d&y=%d&z=%d'%(x, y, zoom) |
| 77 | + imagelists.append((tileurl, savepath)) |
| 78 | + |
| 79 | + return imagelists |
| 80 | + |
| 81 | +async def save_image_v2(url): |
| 82 | + tileurl, savepath = url[0], url[1] |
| 83 | + loop = asyncio.get_event_loop() |
| 84 | + try: |
| 85 | + response = await loop.run_in_executor(None, functools.partial(request.urlretrieve, url = tileurl, filename = savepath)) |
| 86 | + print('---- PID:', os.getpid(), tileurl) |
| 87 | + except Exception as e: |
| 88 | + print(e) |
| 89 | + |
| 90 | +def main(): |
| 91 | + minlon, minlat = 109.227, - 20.196 # 矩形区域左下角坐标 |
| 92 | + maxlon, maxlat = 117.182, - 25.5 # 矩形区域右上角坐标 |
| 93 | + minzoom, maxzoom = 12, 12 |
| 94 | + |
| 95 | + basetileurl = 'http://webrd02.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=8' |
| 96 | + # basetileurl = 'http://mt2.google.cn/vt/lyrs=m&hl=zh-CN&gl=cn&x=%d&y=%d&z=%d'%(x, y, zoom) |
| 97 | + # basetileurl = 'http://wprd04.is.autonavi.com/appmaptile?lang=zh_cn&size=1&style=7&x=%d&y=%d&z=%d&scl=1<ype=11'%(x, y, zoom) |
| 98 | + rootpath = './tilefile' |
| 99 | + |
| 100 | + imagelists = create_image_url_v2(minlon, minlat, maxlon, maxlat, minzoom, maxzoom, basetileurl, rootpath) |
| 101 | + |
| 102 | + tasks = [asyncio.ensure_future(save_image_v2(url)) for url in imagelists] |
| 103 | + loop = asyncio.get_event_loop() |
| 104 | + loop.run_until_complete(asyncio.wait(tasks)) |
| 105 | + loop.close() |
| 106 | + |
| 107 | +if __name__ == '__main__': |
| 108 | + main_start = time.time() |
| 109 | + main() |
| 110 | + print('---- All time: ', time.time() - main_start) |
0 commit comments