地圖瓦片始取程式
阿新 • • 發佈:2018-12-29
from urllib import request
import re
import urllib.request
import os
import random
import math
import time
import threading
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_deg,緯度
#lon_deg,經度
lat_rad = math.radians(lat_deg)#將角度轉化為弧度
n = 2.0 ** zoom#2的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 deg2num2(la,lo,zoom):
if la>90:
la=89
if lo>180:
lo=179
y = math.ceil((90-la)/(180/(2**zoom)))-1
x = math.ceil((lo+180)/(360/(2**zoom)))-1
return x,y
# 下載圖片
def getimg(Tpath, Spath, x, y):
print(Spath)
Spath_dir = "/mnt/m3/tilemap/17/"+str(x)
if not os.path.exists(Spath_dir):
os.mkdir(Spath_dir)
try:
f = open(Spath, 'wb')
req = urllib.request.Request(Tpath)
req.add_header('User-Agent', random.choice(agents)) # 換用隨機的請求頭
pic = urllib.request.urlopen(req, timeout=60)
f.write(pic.read())
f.close()
print(str(x) + '_' + str(y) + '下載成功')
except Exception:
with open("/usr/local/program/project/map_pachong/failed","a") as f:
f.writelines(str(x)+'_'+str(y)+'下載失敗')
print(str(x) + '_' + str(y) + '下載失敗,重試')
getimg(Tpath, Spath, x, y)
def main_main(lefttop,rightbottom):
start_time = time.time()
num = 0
for x in range(lefttop[0], rightbottom[0]):
for y in range(lefttop[1], rightbottom[1]):
# tilepath = "http://t3.tianditu.gov.cn/DataServer?T=cva_w&x=" + str(x) + "&y=" + str(y) + "&l=" + str(zoom)
tilepath = "http://mt3.google.cn/vt/lyrs=r&scale=2&hl=zh-CN&gl=cn&x="+str(x)+"&y="+str(y)+"&z="+str(zoom)+"&s=Galil"
print(tilepath)
# 天地圖的url 可以換成谷歌地圖的
getimg(tilepath, os.path.join("/mnt/m3/tilemap/17/",str(x)+"/"+str(y)+".png"), x, y)
# num+=1
# if num>100:
# break
# if num>100:
# break
end_time = time.time()
print('完成',end_time-start_time)
if __name__=="__main__":
path = r"/mnt/m3/tilemap/17"
if not os.path.exists(path):
os.mkdir(path)
# # zoom = 15 # 下載切片的zoom
# lefttop = deg2num(38.507126, 113.605620, zoom) # 下載切片的左上角角點
# rightbottom = deg2num(34.011111, 122.989999, zoom)
#
# print(str(lefttop[0]))
# print(str(rightbottom[0]))
# print(str(lefttop[1]))
# print(str(rightbottom[1]))
# print("共" + str(lefttop[0] - rightbottom[0]))
# print("共" + str(lefttop[1] - rightbottom[1]))
# duan_num=2
# duan_0 = int((rightbottom[0]-lefttop[0] )/duan_num)
# duan_1 = int((rightbottom[1]-lefttop[1] ) / duan_num)
# t1 = threading.Thread(target=main_main,args=(lefttop,(lefttop[0]+duan_0,lefttop[1]+duan_1)))
# t2 = threading.Thread(target=main_main,args=((lefttop[0]+duan_0,lefttop[1]+duan_1),rightbottom))
zoom = 17
start=(106898,50323)
end = (110315,52354)
# start=(1000,3000)
# end = (2500,9999)
xiancheng_num=2
start=(106898,50323)
end = (110315,52354)
duan = int((end[1]-start[1])/xiancheng_num)
#建立執行緒
ll=[]
for i in range(xiancheng_num-1):
para = ((start[0],start[1]+i*duan),(end[0],start[1]+(i+1)*duan-1))
print(para)
t = threading.Thread(target=main_main,args=para)
ll.append(t)
para = ((start[0], start[1] + (i+1) * duan), end)
t = threading.Thread(target=main_main, args=para)
ll.append(t)
print(para)
for i in ll:
i.start()
for i in ll:
i.join()
print("finish")
# x,y = deg2num(38.507126, 113.605620,zoom)
# tilepath = "http://mt3.google.cn/vt/lyrs=r&scale=2&hl=zh-CN&gl=cn&x="+str(x)+"&y="+str(y)+"&z="+str(zoom)+"&s=Galil"
# getimg(tilepath, os.path.join(path, str(x) + "_" + str(y) + ".png"), x, y)
本想通過多執行緒分塊始取,但不知哪裡出了問題,以上部分程式始取存在問題,只始取了一半瓦片。沒辦法,只好進行手動始取操作。
from urllib import request
import re
import urllib.request
import os
import random
import math
import time
import threading
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']
def getimg(Tpath, Spath, x, y):
print(Spath)
Spath_dir = "/mnt/m3/tilemap/17/"+str(x)
if not os.path.exists(Spath_dir):
os.mkdir(Spath_dir)
try:
f = open(Spath, 'wb')
req = urllib.request.Request(Tpath)
req.add_header('User-Agent', random.choice(agents)) # 換用隨機的請求頭
pic = urllib.request.urlopen(req, timeout=60)
f.write(pic.read())
f.close()
print(str(x) + '_' + str(y) + '下載成功')
except Exception:
with open("/usr/local/program/project/map_pachong/failed","a") as f:
f.writelines(str(x)+'_'+str(y)+'下載失敗')
print(str(x) + '_' + str(y) + '下載失敗,重試')
getimg(Tpath, Spath, x, y)
def main_main(lefttop,rightbottom):
start_time = time.time()
num = 0
for x in range(lefttop[0], rightbottom[0]):
for y in range(lefttop[1], rightbottom[1]):
# tilepath = "http://t3.tianditu.gov.cn/DataServer?T=cva_w&x=" + str(x) + "&y=" + str(y) + "&l=" + str(zoom)
tilepath = "http://mt3.google.cn/vt/lyrs=r&scale=2&hl=zh-CN&gl=cn&x="+str(x)+"&y="+str(y)+"&z="+str(zoom)+"&s=Galil"
print(tilepath)
# 天地圖的url 可以換成谷歌地圖的
getimg(tilepath, os.path.join("/mnt/m3/tilemap/17/",str(x)+"/"+str(y)+".png"), x, y)
if __name__=="__main__":
path = r"/mnt/m3/tilemap/17"
if not os.path.exists(path):
os.mkdir(path)
zoom = 17
# start=(106898,51337)
# end = (110315,52354)
# start=(1000,3000)
# end = (2500,9999)
xiancheng_num=2
start=(106898,51337)
end = (110315,52354)
main_main(start,end)
# duan = int((end[1]-start[1])/xiancheng_num)
# # 建立執行緒
# ll = []
# for i in range(xiancheng_num - 1):
# para = ((start[0], start[1] + i * duan), (end[0], start[1] + (i + 1) * duan - 1))
# print(para)
# t = threading.Thread(target=main_main, args=para)
# ll.append(t)
# para = ((start[0], start[1] + (i + 1) * duan), end)
# t = threading.Thread(target=main_main, args=para)
# ll.append(t)
# print(para)
# for i in ll:
# i.start()
# for i in ll:
# i.join()
# print("finish")
```