zoukankan      html  css  js  c++  java
  • 根据范围爬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()
  • 相关阅读:
    linux下的磁盘挂载
    shell中的循环语句while
    hadoop安装和配置
    shell 命令 创建/删除 软连接 ln -s
    azkaban disable 停用部分工作流
    git dev 分支merge到master
    shell 命令 zip unzip
    git代码同步服务器代码需要注意的问题
    shell 命令 if elif else fi 用法
    python 引入本地 module
  • 原文地址:https://www.cnblogs.com/dullfish/p/8527504.html
Copyright © 2011-2022 走看看