huifer / tianditu-python

天地图切片(瓦片)以及POI下载
Apache License 2.0
123 stars 34 forks source link

改了一点,希望有用 #6

Closed xxzztt closed 4 years ago

xxzztt commented 4 years ago

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()

xxzztt commented 4 years ago

代码都乱了,还是分享到百度云盘吧 点击直达 链接:https://pan.baidu.com/s/1saUc_ylV7h_snJ7q1wuVOA 提取码: v4mm

huifer commented 4 years ago

好的 可以直接pr