根据范围爬TMS规则瓦片

因为需要简单写了一个下载地图的爬虫,代码如下:

#coding=utf-8
import urllib.request
import os
import socket
import zlib
import math

# python版本3.7
# 设置超时
socket.setdefaulttimeout(60)

def mkdir(path):
    
    # 去除首位空格
    path=path.strip()
    # 去除尾部 \ 符号
    path=path.rstrip("\\")
 
    # 判断路径是否存在
    # 存在     True
    # 不存在   False
    isExists=os.path.exists(path)
 
    # 判断结果
    if not isExists:
        # 如果不存在则创建目录
        # 创建目录操作函数
        os.makedirs(path) 
 
        print('path create success!')
        return True
    else:
        # 如果目录存在则不创建,并提示目录已存在
        print('path already exist!')
        return False
 
# 定义要创建的目录
mkpath="F:\\python\\TMS\\"
# 调用函数
#mkdir(mkpath)

def callbackfunc(blocknum, blocksize, totalsize):
    '''回调函数
    @blocknum: 已经下载的数据块
    @blocksize: 数据块的大小
    @totalsize: 远程文件的大小
    '''
    # percent = 100.0 * blocknum * blocksize / totalsize
    # if percent > 100:
        # percent = 100
    print("--")

def long2tile(lon, zoom) :
    return (math.floor((lon + 180) / 360 * math.pow(2, zoom)))

def lat2tile(lat, zoom):
    return (math.floor((1 - math.log(math.tan(lat * math.pi / 180) + 1 / math.cos(lat * math.pi / 180)) / math.pi) / 2 * math.pow(2, zoom)))

 #范围
zmin = 9
zmax = 14
south_edge = 25.2526
north_edge = 26.6384
west_edge = 118.376
east_edge = 120.512

#便利URL,获取数据
def getDataByUrl():
    for z in range(zmin,zmax):
        top_tile = lat2tile(north_edge, z)
        left_tile = long2tile(west_edge, z)
        bottom_tile = lat2tile(south_edge, z)
        right_tile = long2tile(east_edge, z)
        minLong = min(left_tile, right_tile)
        maxLong = max(left_tile, right_tile)
        minLat = min(bottom_tile, top_tile)
        maxLat = max(bottom_tile, top_tile)
        for x in range(minLong,maxLong):
                path=str(z)+"\\"+str(x)
                temppath=mkpath+path
                mkdir(temppath)
                for y in range(minLat,maxLat):
                    url=str(z) + '/' + str(x) + '/' + str(y)
                    str3='https://cartodb-basemaps-a.global.ssl.fastly.net/dark_nolabels/'+ url + '.png'
                    path2=temppath+'\\'+str(y)+'.png'
                    try:
                        urllib.request.urlretrieve(str3,path2)
                    except Exception as e:
                        print(e)

getDataByUrl()

 

转载于:https://www.cnblogs.com/dullfish/p/8527504.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值