|
5 | 5 | import requests
|
6 | 6 |
|
7 | 7 | ZOOM = 7
|
| 8 | +DELAY = 0.035 |
8 | 9 |
|
9 |
| -# Headers for request |
| 10 | +# Extra headers for request |
10 | 11 | HEADERS = {
|
11 |
| - "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", |
12 |
| - "Connection": "keep-alive", |
13 |
| - "Host": "tile.openstreetmap.org", |
14 |
| - "User-Agent": "Requests Python3.11", |
15 | 12 | "Accept-Language": "en-AU,en;q=0.9",
|
16 |
| - "Accept-Encoding": "gzip, deflate, br", |
| 13 | + "User-Agent": "Requests Python3.11", |
17 | 14 | "Priority": "u=0, i"
|
18 | 15 | }
|
19 | 16 |
|
| 17 | +# Requests session |
| 18 | +s = requests.Session() |
| 19 | +s.headers = s.headers.update(HEADERS) |
| 20 | + |
20 | 21 | # Calculate number of tiles
|
21 | 22 | X_TILES = int(2**ZOOM)
|
22 | 23 | Y_TILES = int(2**ZOOM)
|
@@ -57,18 +58,23 @@ def join_tiled_images(image_paths, x_tiles, y_tiles, output_path):
|
57 | 58 | # Download Tiles
|
58 | 59 | print("Downloading Tiles")
|
59 | 60 | image_array = []
|
| 61 | +LAST_DL_TIME = 0.25 |
60 | 62 | for x in range(X_TILES):
|
61 | 63 | for y in range(Y_TILES):
|
62 |
| - print("Downloading:", x, y) |
63 |
| - r_image = requests.get(f"http://tile.openstreetmap.org/{ZOOM}/{x}/{y}.png", |
64 |
| - timeout=20, headers=HEADERS) |
65 |
| - if not os.path.exists(f"/tmp/map-tiles/{x}"): |
66 |
| - os.makedirs(f"/tmp/map-tiles/{x}") |
67 |
| - with open(f"/tmp/map-tiles/{x}/{y}.png", "wb") as image: |
| 64 | + print(f"\rDownloading: {x:4} {y:4}. DL Time: {((X_TILES*Y_TILES)-len(image_array))*(DELAY+LAST_DL_TIME)/60:6.2f} Minutes", |
| 65 | + end="", flush=True) |
| 66 | + r_image = s.get(f"http://tile.openstreetmap.org/{ZOOM}/{x}/{y}.png", timeout=20) |
| 67 | + LAST_DL_TIME = (LAST_DL_TIME+(r_image.elapsed.microseconds/1000000))/2 |
| 68 | + if not os.path.exists(f"/tmp/map-tiles/{ZOOM}/{x}"): |
| 69 | + os.makedirs(f"/tmp/map-tiles/{ZOOM}/{x}") |
| 70 | + with open(f"/tmp/map-tiles/{ZOOM}/{x}/{y}.png", "wb") as image: |
68 | 71 | image.write(r_image.content)
|
69 |
| - image_array.append(f"/tmp/map-tiles/{x}/{y}.png") |
70 |
| - sleep(0.01) |
| 72 | + image_array.append(f"/tmp/map-tiles/{ZOOM}/{x}/{y}.png") |
| 73 | + sleep(DELAY) |
| 74 | +print('') |
71 | 75 |
|
72 | 76 | # Make Map
|
73 | 77 | print("Making image array")
|
74 | 78 | join_tiled_images(image_array, X_TILES, Y_TILES, "sdcard/ADSB/world_map.png")
|
| 79 | + |
| 80 | +s.close() |
0 commit comments