PYTHON根據兩點經緯度計算出距離

根據兩個CSV文件的經緯度計算出距離,並按一定距離提取相應的數據。

import math                               
def cal_dis(lat1,lon1,lat2,lon2):   
     lat1 = (math.pi/180.0)*lat1    
     lat2 = (math.pi/180.0)*lat2    
     lon1 = (math.pi/180.0)*lon1    
     lon2= (math.pi/180.0)*lon2     
     R = 6378.137;
     t= math.sin(lat1) * math.sin(lat2) + math.cos(lat1) * math.cos(lat2) * math.cos(lon2 - lon1)
     if t > 1.0:
         t= 1.0
     d = math.acos(t) * R
     return d


import os
import os.path
import csv
di=float(input("輸入計算多少KM內:"))
print("請耐心等待!")
f=open('./提取小區.csv',encoding='gbk')
x=csv.reader(f,delimiter=',')
n=0
with open(os.getcwd() + '\\file1.csv', 'w') as opf:
    opf.write("cell,scell,dis"+'\n')
    opf.close

for cell1,lo1,la1 in x:
    f1 = open('./基礎信息.csv', encoding='gbk')
    y = csv.reader(f1, delimiter=',')
    for cell2, lo2, la2 in y:
        if str(cell1) != str(cell2):
            dis=cal_dis(float(la1),float(lo1),float(la2),float(lo2))
            if dis<di:
               n=+1
                if n<900000:
                    with open(os.getcwd() + '\\file1.csv', 'a') as opf:
                        opf.write(cell1+chr(44)+cell2+chr(44)+str(dis)+'\n')
                        opf.close
                elif n<1800000:

                    with open(os.getcwd() + '\\file2.csv', 'a') as opf:
                        opf.write(cell1 + chr(44) + cell2 + chr(44) + str(dis) + '\n')
                        opf.close
                else:
                    with open(os.getcwd() + '\\file3.csv', 'a') as opf:
                        opf.write(cell1 + chr(44) + cell2 + chr(44) + str(dis) + '\n')
                        opf.close
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章