Coder Social home page Coder Social logo

tianditu-python's Introduction

天地图切片地图分类提取

开发环境

  • python 3.6
  • Pillow==5.4.1
  • requests==2.21.0
  • opencv-python==4.1.0.25

开发文档

中文文档

功能

License

Apache

tianditu-python's People

Contributors

dependabot[bot] avatar huifer avatar imgbotapp avatar liuxinfengabc avatar zeofura avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

tianditu-python's Issues

改了一点,希望有用

file: 天地图经纬度转换切片索引

#ez_title_index.py
import math

def lng_lat_to_title_index(lng, lat, level):
"""
天地图经纬度转换切片索引
:param lng: 经度
:param lat: 纬度
:param level: 放大级别
:return: (切片的x索引,切片的y索引)
"""
tdtScale = {18: 0.597164283559817, 17: 1.19432856685505, 16: 2.38865713397468, 15: 4.77731426794937,
14: 9.55462853563415, 13: 19.1092570712683, 12: 38.2185141425366, 11: 76.4370282850732,
10: 152.8740565704, 9: 305.7481128, 8: 611.49622628138, 7: 1222.99245256249, 6: 2445.98490512499,
5: 4891.96981024998, 4: 9783.93962049996, 3: 19567.8792409999, 2: 39135.7584820001,
1: 78271.5169639999}
coef = tdtScale[level] * 256
topTileFromX = -20037508.3427892
topTileFromY = 20037508.3427892
lon = lon2Mercator(lng)
lat = lat2Mercator(lat)
x_num =math.floor((lon - topTileFromX) / coef)
print(x_num)
y_num = math.floor((topTileFromY - lat) / coef)
x = (lng + 180) / 360

# title_X = math.floor(x * math.pow(2, level))
# lat_rad = lat * math.pi / 180
# y = (1 - math.log(math.tan(lat_rad) + 1 / math.cos(lat_rad)) / math.pi) / 2
# title_Y = math.floor(y * math.pow(2, level))
return (x_num, y_num)

#经度转墨卡托投影坐标
def lon2Mercator(px):
x = px * 20037508.3427892 / 180
return x

#纬度转墨卡托投影坐标
def lat2Mercator(py):
y = math.log(math.tan((90 + py) * math.pi / 360)) / (math.pi / 180);
y = y * 20037508.3427892 / 180;
return y;

def main():
z = 16
xy = [121.320642828941,30.7022824355605]
aaa = lng_lat_to_title_index(xy[0], xy[1], z)
url = 'http://t2.tianditu.gov.cn/DataServer?T=img_w&x={}&y={}&l={}&tk=a4ee5c551598a1889adfabff55a5fc27'.format(
aaa[0], aaa[1], z)
print(url)

if name == 'main':
main()

分目录下载瓦片,可以直接用openlayer加载

天地图切片下载优化方案 ez_tdt_title_download.py

from ez_title_index import lng_lat_to_title_index
import requests
#Pillow
from PIL import Image
import os

文件存放位置设置

BASE_PATH = os.path.join(os.path.abspath(os.curdir), 'disc')
BASE_PATH_res = os.path.join(os.path.abspath(os.curdir), 'result')

简单反爬虫 , 可以不写

headers = {
"Connection": "keep-alive",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36",
}

#下载瓦片
def download_pic(x, y, z):
"""
下载地图
:param x: x 范围
:param y: y 范围
:param z: int
"""
try:
# 下载图片
key = 'a4ee5c551598a1889adfabff55a5fc27'
for xi in x:
xpathlayer = os.path.join(BASE_PATH, str(z))#地图层目录
if (os.path.exists(xpathlayer) == False):
os.mkdir(xpathlayer)
xpath = os.path.join(xpathlayer, str(xi))#地图X坐标目录
if(os.path.exists(xpath)==False):
os.mkdir(xpath)

        for yi in y:
            #url = "http://t2.tianditu.gov.cn/DataServer?T=img_w&x={}&y={}&l={}&tk={}".format(xi, yi, z, key)
            url = "http://t3.tianditu.gov.cn/DataServer?T=img_w&x={}&y={}&l={}&tk={}".format(xi, yi, z, key)
            # 保存文件名称
            fileName = os.path.join(xpath, "{}.png".format(yi))
            # 具体下载操作
            if (os.path.exists(fileName)) == False:
                r = requests.get(url=url, headers=headers)
                if r.status_code == 200:
                    with open(fileName, 'wb') as f:
                        for chunk in r:
                            f.write(chunk)
                else:
                    print("访问异常")
except Exception as e:
    print(e)
    pass

#合并瓦片
def merge_pic(x, y, z):
"""
合并下载地图
:param x: x 范围
:param y: y 范围
:param z: int
:return:
"""
picSize = 256
try:
# 构造平图矩阵
li = []

    for xi in x:
        xpathlayer = os.path.join(BASE_PATH, str(z))  # 地图层目录
        xpath = os.path.join(xpathlayer, str(xi))  # 地图X坐标目录
        lis = []
        for yi in y:
            fileName = os.path.join(xpath, "{}.png".format(yi))
            lis.append(fileName)

        li.append(lis)

    oca = len(x)
    ocb = len(y)

    toImage = Image.new('RGBA', (oca * picSize, ocb * picSize))

    for i in range(oca):
        for j in range(ocb):
            fromImge = Image.open(li[i][j])
            picx = 256 * i
            picy = 256 * j
            loc = (picx, picy)
            toImage.paste(fromImge, loc)

    toImage.save(os.path.join(BASE_PATH_res, "rs{}.png".format(z)))
    print("构造完成输出图片")

except Exception as e:
    print(e)
    pass

def run_spider(z, minx, maxx, miny, maxy):
"""
下载切片地图
:param z:放大级别
:param minx: 最小x
:param maxx: 最大x
:param miny: 最小y
:param maxy: 最大y
:return:
"""
#z = 18
xy = [minx, miny]#左下角 经纬度坐标
xxyy = [maxx, maxy]#右上角 经纬度坐标

minxy = lng_lat_to_title_index(xy[0], xy[1], z) #转换后的瓦片坐标
maxxy = lng_lat_to_title_index(xxyy[0], xxyy[1], z) #转换后的瓦片坐标
xr = range(minxy[0], maxxy[0]+1)
yr = range( maxxy[1],minxy[1]+1)
download_pic(xr, yr, z)#下载瓦片
merge_pic(xr, yr, z)#合并瓦片成为大图

if name == 'main':
run_spider(z=15, minx=121.315498352051, maxx=121.338307857513, miny=30.6949805750931, maxy=30.7056264386052)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.