Fetch the repository succeeded.
This action will force synchronization from 且行且珍惜/python爬取地图, which will overwrite any changes that you have made since you forked the repository, and can not be recovered!!!
Synchronous operation will process in the background and will refresh the page when finishing processing. Please be patient.
from urllib import request
import re
import urllib.request
import os
import random
import math
import json
agents = [
'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36',
'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/532.5 (KHTML, like Gecko) Chrome/4.0.249.0 Safari/532.5',
'Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US) AppleWebKit/532.9 (KHTML, like Gecko) Chrome/5.0.310.0 Safari/532.9',
'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.7 (KHTML, like Gecko) Chrome/7.0.514.0 Safari/534.7',
'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) AppleWebKit/534.14 (KHTML, like Gecko) Chrome/9.0.601.0 Safari/534.14',
'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.14 (KHTML, like Gecko) Chrome/10.0.601.0 Safari/534.14',
'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.20 (KHTML, like Gecko) Chrome/11.0.672.2 Safari/534.20',
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.27 (KHTML, like Gecko) Chrome/12.0.712.0 Safari/534.27',
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.24 Safari/535.1']
# 经纬度反算切片行列号 3857坐标系
def deg2num(lat_deg, lon_deg, zoom):
lat_rad = math.radians(lat_deg)
n = 2.0 ** zoom
xtile = int((lon_deg + 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)
# 下载图片
def getimg(Tpath, Spath, x, y):
try:
f = open(Spath, 'wb')
req = urllib.request.Request(Tpath)
req.add_header('User-Agent', random.choice(agents)) # 换用随机的请求头
# req.add_header('x-client-data','CIS2yQEIo7bJAQjBtskBCKmdygEIlqzKAQj4x8oBCJ+KywEI5JzLAQipncsBCOmdywEIoKDLARjgmssB')
pic = urllib.request.urlopen(req, timeout=60)
f.write(pic.read())
f.close()
print(str(x) + '_' + str(y) + '下载成功')
except Exception:
print(str(x) + '_' + str(y) + '下载失败,重试')
getimg(Tpath, Spath, x, y)
if __name__ == '__main__':
zoom = 17 # 下载切片的zoom
lefttop = deg2num(34.917511,116.396120, zoom) # 下载切片的左上角角点
rightbottom = deg2num(30.627973,122.074959, zoom)
for x in range(lefttop[0], rightbottom[0]):
for y in range(lefttop[1], rightbottom[1]):
s = random.randint(0,7);
# 标签服务
# tilepath = "http://t3.tianditu.gov.cn/DataServer?T=cva_w&x=" + str(x) + "&y=" + str(y) + "&l=" + str(zoom)+"&tk=28b495e4df789d971d2ae77b01a55a55"
# tilepath ="https://t"+str(s)+".tianditu.gov.cn/vec_w/wmts?SERVICE=WMTS&REQUEST=GetTile&version=1.0.0&LAYER=vec&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TileRow="+str(y)+"&TileCol="+str(x)+"&TILEMATRIX="+str(zoom)+"&tk=28b495e4df789d971d2ae77b01a55a55"
tilepath ="https://khms0.google.com/kh/v=902?x="+str(x)+"&y="+str(y)+"&z="+str(zoom)
# 天地图的url 可以换成谷歌地图的
print(tilepath)
path = "E:/google/江苏/"+str(zoom)
if not os.path.exists(path):
os.mkdir(path)
path = "E:/google/江苏/"+str(zoom)+"/"+str(x)
if not os.path.exists(path):
os.mkdir(path)
getimg(tilepath, os.path.join(path,str(y) + ".png"), x, y)
print('完成')
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。