-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
83 lines (61 loc) · 2.77 KB
/
Copy pathmain.py
File metadata and controls
83 lines (61 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import mercantile
import requests
import os
from concurrent.futures import ThreadPoolExecutor
# --- CONFIGURATION ---
# 1. Tiles type
style = str(input("tiles style: "))
# 2. Bounding box [west, south, east, north]. Get from http://bboxfinder.com/
bbox = [8.934631, 45.328962, 9.725475, 45.722420] # Example: New York City
# 3. Zoom levels to download (inclusive)
min_zoom = int(input("min zoom: "))
max_zoom = int(input("max zoom: "))
zoom_levels = range(min_zoom, max_zoom + 1)
# 4. Tiles url
tile_url_template = "http://localhost:8080/styles/" + style + "/256/{z}/{x}/{y}.png"
# ---------------------
downloaded_count = 0
total_tiles_to_download = 0
def print_progress_bar():
global downloaded_count
global total_tiles_to_download
percentage = (downloaded_count / total_tiles_to_download) * 100 if total_tiles_to_download > 0 else 0
bar_length = 50
filled_length = int(bar_length * percentage // 100)
bar = '█' * filled_length + '-' * (bar_length - filled_length)
print(f'\rProgress: |{bar}| {percentage:.2f}% ({downloaded_count}/{total_tiles_to_download})', end='')
def download_tile(url, path):
global downloaded_count
try:
# Create directory if it doesn't exist
os.makedirs(os.path.dirname(path), exist_ok=True)
response = requests.get(url, stream=True)
response.raise_for_status() # Raise an exception for bad status codes
with open(path, 'wb') as f:
for chunk in response.iter_content(chunk_size=8192):
f.write(chunk)
downloaded_count += 1
print_progress_bar()
return path
except requests.exceptions.RequestException as e:
# We don't increment downloaded_count here as it wasn't successful
print(f"\nFailed to download {url}: {e}") # Print on a new line to not interfere with progress bar
return None
# --- SCRIPT START ---
if __name__ == "__main__":
all_tiles = []
for zoom in zoom_levels:
for tile in mercantile.tiles(bbox[0], bbox[1], bbox[2], bbox[3], zooms=[zoom]):
all_tiles.append(tile)
total_tiles_to_download = len(all_tiles)
print(f"Total tiles to download: {total_tiles_to_download}")
print_progress_bar() # Initial progress bar
with ThreadPoolExecutor(max_workers=10) as executor: # Adjust max_workers as needed
urls_and_paths = []
for tile in all_tiles:
url = tile_url_template.format(z=tile.z, x=tile.x, y=tile.y)
path = os.path.join("maps\\" + style, str(tile.z), str(tile.x), f"{tile.y}.png")
urls_and_paths.append((url, path))
# Map the download function to the URLs and paths
executor.map(lambda p: download_tile(*p), urls_and_paths)
print("\n--- Download complete! ---") # New line after the final progress bar