[python]天地地图瓦片下载

 

之前下载了一个.net GMap来下载地图发现速度太慢。而且经常会闪退。所以写了个脚本去获取。(亲测可用,直接复制吧。)

 

#!/usr/bin/python
# _*_coding:utf-8_*_

from urllib3 import request

import re

import urllib.request

import os

import random

import math

import sys

count = 0

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']
resolution={18:5.36441802978515E-06,
    17:1.07288360595703E-05,
    16:2.1457672119140625E-05,
    15:4.29153442382814E-05,
    14:8.58306884765629E-05,
    13:0.000171661376953125,
    12:0.00034332275390625,
    11:0.0006866455078125,
    10:0.001373291015625,
    9:0.00274658203125,
    8:0.0054931640625,
    7:0.010986328125,
    6:0.02197265625,
    5:0.0439453125,
    4:0.087890625,
    3:0.17578125,
    2:0.3515625,
    1:0.703125}

# 经纬度反算切片行列号 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)
#   xtile = math.floor((lon_deg+180.0) / (resolution[zoom]*256))
#   ytile = math.floor((90.0-lat_deg) / (resolution[zoom]*256))
    return (xtile, ytile)
# 下载图片
def getimg(Tpath, Spath, x, y):
    global count
    count = 0 #清零
    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:
        print(str(x),'_',str(y),'下载失败,重试')
        getimg(Tpath, Spath, x, y)
        count = count + 1
    print(count)
    if count > 5:
        return

#zoom =16  # 下载切片的zoom
#lefttop = deg2num( 28.317231,115.846340, zoom)  # 下载切片的左上角角点
#rightbottom = deg2num( 23.567119,120.722095, zoom)

#def GetMapTile(lat1, lon1, lat2, lon2):
#   """根据经纬度范围获取背景"""
#   zooms = []
#   for i in range(1, 19):
#       l = deg2num(lat1, lon1, i)
#       r = deg2num(lat2, lon2, i)
#       if l[0] - r[0] == 0 or l[1] - r[1] == 0:
#           continue
 #          else:
#           zooms.append(i)
    # 根据有瓦片数据的地方下载瓦片
#zoom = zooms[-1]
    # 根据经纬度确定瓦片位置
#lefttop = deg2num(lat1, lon1, zoom)
#rightbottom = deg2num(lat2, lon2, zoom)


def  download(k,LTlat,LTlon,RBlat,RBlon):
    zoom = k
    LT_lat = LTlat
    LT_lon = LTlon
    RB_lat = RBlat
    RB_lon = RBlon
    lefttop = deg2num(LT_lat,LT_lon, zoom)
    rightbottom = deg2num(RB_lat,RB_lon, zoom)
    print(str(zoom))
    print(str(lefttop[0]))
    print(str(rightbottom[0]))
    print(str(lefttop[0]))
    print(str(rightbottom[0]))
    print("共" + str(lefttop[0] - rightbottom[0]))
    print("共" + str(lefttop[1] - rightbottom[1]))
    #rootDir = "/root/ditupython/World_Annotation/TiandituSatelliteMap/"
    rootDir = "D:\ditu"
    for x in range(lefttop[0], rightbottom[0]):
        path = rootDir + str(zoom) + "/" + str(x)
        if not os.path.exists(path):
                        os.makedirs(path)
        for y in range(lefttop[1], rightbottom[1]):
            #天地图-影像
            #tilepath = "http://t0.tianditu.gov.cn/DataServer?T=img_w&x=" + str(x) + "&y=" + str(y) + "&l=" + str(zoom) + "&tk=28b495e4df789d971d2ae77b01a55a55"
            tilepath = 'http://t0.tianditu.gov.cn/DataServer?T=img_w&x=' + str(x) + '&y=' + str(y) + '&l=' + str(zoom) + '&tk=28b495e4df789d971d2ae77b01a55a55'
            #tilepath = "http://t6.tianditu.gov.cn/DataServer?T=cia_w&x=" + str(x) + "&y=" + str(y) + "&l=" + str(zoom) + "&tk=5d22d49fdc586cb5caed68bfb12d1e6b"
            filepath = path + "/" + str(y) + ".png"
            if not os.path.isfile(filepath):
                getimg(tilepath,os.path.join(path,str(y) + ".png"),x,y)
            
'''
for x in range(lefttop[0], rightbottom[0]):
    for y in range(lefttop[1], rightbottom[1]):
        path = "/root/ditupython/World_Annotation/" + str(zoom) + "/" + str(x)
        if not os.path.exists(path):
            os.makedirs(path)
                #   tilepath = "http://t0.tianditu.gov.cn/DataServer?T=cia_c&x=" + str(x) + "&y=" + str(y) + "&l=" + str(zoom) + "&tk=28b495e4df789d971d2ae77b01a55a55"
            tilepath = "http://t0.tianditu.gov.cn/DataServer?T=img_w&x=" + str(x) + "&y=" + str(y) + "&l=" + str(zoom) + "&tk=5d22d49fdc586cb5caed68bfb12d1e6b"
# 天地图的url 可以换成谷歌地图的
            getimg(tilepath, path + "\\" +  str(y) +  ".png", x, y)
'''

print('完成')
arg1 = sys.argv[1]
arg2 = sys.argv[2]
#湖南
#LT_lat = 30.2
#LT_lon = 108.7
#RB_lat = 24.5
#RB_lon = 114.7

#全国
LT_lat = 53.64464
LT_lon = 73.82812
RB_lat = 24.53713
RB_lon = 114.78516

level1 = int(arg1)
level2 = int(arg2)
for i in range(level1,level2):
    delta_lat = LT_lat - RB_lat
    delta_lon = RB_lon - LT_lon
    if i > 15:
        print(str(i))
        LT_lat = LT_lat -delta_lat * 1/4
        LT_lon = LT_lon + delta_lon * 1/4
        RB_lat = RB_lat + delta_lat * 1/4
        RB_lon = RB_lon - delta_lon * 1/4
    print(str(LT_lat))
    print(str(LT_lon))
    print(str(RB_lat))
    print(str(RB_lon))
    download(i,LT_lat,LT_lon,RB_lat,RB_lon)

 

 

你可能感兴趣的:(python,运维,服务器,1024程序员节)