用python 來實現經緯度的測算與輸出

本文采用高德地圖地理標識規範

首先導入兩個分析包,math和pandas,如果有需要還有numpy,mathplotlib等常用數據分析包

import pandas as pd
import math

測算一經度和一緯度的距離

# 地球半徑
r  = 12713.504 / 2
# 1緯度長
wd = (2 * math.pi * r) / 180
# 1經度長
jd = (5 * math.sin(math.atan2(x2-x1,y2-y1))) / (wd * math.cos(x))
圖片來源百度圖片
該圖片來源於百度圖片

讀寫原excel文件並輸出

df = pd.read_excel('jwd.xlsx')
# df = df.head()
# make a new col
df['res1'] = df['a_road']
res1 = [[0,1]]


# get index
for i in range(df.iloc[:,0].size):
    x1 = df.latA[i]
    y1 = df.lngA[i]
    x2 = df.latB[i]
    y2 = df.lngB[i]
    # 通過三角函數進行求值
    # 1緯度 111km 
    # 1經度 111*cos緯度 km
    x  = (5 * math.cos(math.atan2(x2-x1,y2-y1))) / wd
    y  = (5 * math.sin(math.atan2(x2-x1,y2-y1))) / (wd * math.cos(x))
    #print(x,y)
    for j in range(0,int(df.distance[i] / 5) + 1):
        if j == int(df.distance[i] / 5):
            num = df.distance[i] % 5
            x  = (num * math.cos(math.atan2(x2-x1,y2-y1))) / wd
            y  = (num * math.sin(math.atan2(x2-x1,y2-y1))) / (wd * math.cos(x))
            res1.append([x2 - x,y2 - y])
        elif j == 0:
            res1[j][0] = x1 + x 
            res1[j][1] = y1 + y 
        else:
            res1.append([x1 + x * (j + 1),y1 + y * (j + 1)])
    df.loc[i,'res1'] = str(res1)

df.to_excel('經緯度結果表.xlsx',index=False,sheet_name = 'df1')

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章