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

你可能感兴趣的:(PYTHON)