import os
import requests
import threading
# 替换为您自己的高德开发者账号的key值
key = "替换为您自己的高德开发者账号"
z_list = [16]
x_list = [[53881, 54003]]
y_list = [[25654, 26744]]
class DownloadThread(threading.Thread):
def __init__(self, x, y, z):
super().__init__()
self.x = x
self.y = y
self.z = z
def run(self):
url_template = "https:///appmaptile?x={x}&y={y}&z={z}&lang=zh_cn&size=1&style=7"
url = url_template.format(x=self.x, y=self.y, z=self.z)
response = requests.get(url, headers={'referer':'https:///'}, params={'key':key})
z_path = os.path.join(os.path.join(base_path, str(self.z)))
if not os.path.exists(z_path):
os.mkdir(z_path)
x_path = os.path.join(z_path, str(self.x))
if not os.path.exists(x_path):
os.mkdir(x_path)
print(self.z, self.x, self.y)
with open(os.path.join(x_path, f"{self.y}.png"), 'wb') as f:
f.write(response.content)
if __name__ == '__main__':
# 替换为需要保存地图瓦片的地址
base_path = "E:\project\scripts\get_gaode_map_wapian\\result"
threads = []
for i, z in enumerate(z_list):
for x in range(x_list[i][0], x_list[i][1] + 1):
for y in range(y_list[i][0], y_list[i][1] + 1):
thread = DownloadThread(x=x, y=y, z=z)
threads.append(thread)
# 启动所有线程
for t in threads:
t.start()
# 等待所有线程执行完毕
for t in threads:
t.join()