网站更新内容:请访问: https://bigdata.ministep.cn/

计算两点间经纬度距离

计算两点间经纬度距离.py

# -*- coding: utf-8 -*-
"""
Created on Sat Oct 20 14:51:00 2018
计算两点间经纬度距离
@author: kemi
"""
#方法一
#from geographiclib.geodesic import Geodesic
#Geodesic.WGS84.Inverse(31.410527, 121.355836, 31.398137,121.364993)
#方法二:

import pandas as pd
from math import radians, cos, sin, asin, sqrt
def haversine(lon1, lat1, lon2, lat2): # 经度1,纬度1,经度2,纬度2 (十进制度数)
    """
    Calculate the great circle distance between two points 
    on the earth (specified in decimal degrees)
    """
    # 将十进制度数转化为弧度
    lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
 
    # haversine公式
    dlon = lon2 - lon1 
    dlat = lat2 - lat1 
    a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
    c = 2 * asin(sqrt(a)) 
    r = 6371 # 地球平均半径,单位为公里
    return c * r * 1000

def read_excel(path ,sheet_name):
    xlsx = pd.ExcelFile(path)
    sheet = xlsx.parse(sheet_name)
    return(sheet)

path =  r'C:\Users\HP\Desktop\new-ok食行生鲜站点信息-20181017.xlsx'
sheet_name = 'new上海'
df= read_excel(path,sheet_name)
df.head()
df.columns=df.columns.str.strip()
df['distance'] = df.apply(lambda x:haversine(x['经度'],x['纬度'],x['lng'],x['lat']), axis=1)

df.to_excel(path,'new上海距离',index=False)

posted @ 2021-03-13 10:10  ministep88  阅读(195)  评论(0编辑  收藏  举报
网站更新内容:请访问:https://bigdata.ministep.cn/