常用函数,中点距离等
import pandas as pd
import numpy as np
import math
from math import radians,ceil, cos, sin, asin, sqrt
#求两点中点经纬度
def mid_point(lon1, lat1, lon2, lat2):
α1 = math.radians(lon1) # 经度角α1
β1 = math.radians(lat1) # 纬度角β1
α2 = math.radians(lon2) # 经度角α2
β2 = math.radians(lat2) # 纬度角β1
dα = α2 - α1
x = math.cos(β2) * math.cos(dα)
y = math.cos(β2) * math.sin(dα)
α = α1 + math.atan2(y, math.cos(β1) + x)
β = math.atan2(math.sin(β1) + math.sin(β2), math.sqrt((math.cos(β1) + x) ** 2 + y ** 2))
return math.degrees(α), math.degrees(β)
#求两点距离
def geodistance(lon1, lat1, lon2, lat2):
lon1, lat1, lon2, lat2 = map(radians, [float(lon1), float(lat1),float(lon2),float(lat2)])# 经纬度转换成弧度
dlon=lon2-lon1
dlat=lat2-lat1
a=sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
distance=2*asin(sqrt(a))*6371*1000 # 地球平均半径,6371km
distance=round(distance,3)
return distance
#求相似距离分段
def splitlonlat(x,y):
k=[]
for i in range(len(x)):
lon1=x[i][0]
lat1=x[i][1]
k.append([lon1,lat1])
if i < len(x)-1:
lon2=x[i+1][0]
lat2=x[i+1][1]
else:
lon2=x[0][0]
lat2=x[0][1]
while geodistance(lon1, lat1, lon2, lat2) > y+100:
lon,lat=mid_point(lon1, lat1, lon2, lat2)
lon1=lon
lat1=lat
k.append([lon,lat])
m=[]
for i in range(1,len(k)):
lon1=k[i-1][0]
lat1=k[i-1][1]
m.append([lon1,lat1])
lon2=k[i][0]
lat2=k[i][1]
while geodistance(lon1, lat1, lon2, lat2) > y+100:
lon,lat=mid_point(lon1, lat1, lon2, lat2)
lon2=lon
lat2=lat
m.append([lon,lat])
return m
#函数,增加中点列
def zd(df,lon1,lat1,lon2,lat2):
df['lon_z']=0
df['lat_z']=0
for i in range(len(df)):
lon1=df[lon1].iloc[i]
lat1=df[lat1].iloc[i]
lon2=df1[lon2].iloc[i]
lat2=df1[lat2].iloc[i]
df['lon_z'].iloc[i],df['lat_z'].iloc[i]=mid_point(lon2, lat2, lon1, lat1)
return df
# 增加距离列
def jl(df,lon1,lat1,lon2,lat2):
df['d_jl']=0
for i in range(len(df)):
lon1=df[lon1].iloc[i]
lat1=df[lat1].iloc[i]
lon2=df1[lon2].iloc[i]
lat2=df1[lat2].iloc[i]
df['d_jl'].iloc[i]=geodistance(lon1, lat1, lon2, lat2)
return df