Haversine公式:经纬度计算距离 原始数据:停车开始结束时间、停车点经纬度、行政区号 处理结果:车辆行驶距离(m)、停车时间(s)
pandas数据处理,geo_distance,字符串处理
from math import radians, cos, sin, asin, sqrt
import pandas as pd
pd.set_option('display.max_columns', None)
pd.set_option('display.width', 150)
data = pd.read_csv('./newdata_small.csv', encoding='gbk')
def geo_distance(loc1, loc2):
"""
Calculate the great circle distance between two points
on the earth (specified in decimal degrees)
"""
lon1 = loc1[0]
lat1 = loc1[1]
lon2 = loc2[0]
lat2 = loc2[1]
lon1, lat1, lon2, lat2 = map(radians, map(float, [lon1, lat1, lon2, lat2]))
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 dis_cal(data):
dis_result = []
for i in range(len(data)):
hang = data[i:i + 1].values.tolist()[0]
hang_list = []
hang_time = []
for dyg in hang:
if type(dyg) == str and len(dyg) > 6:
dyg = dyg.replace('[', '').replace(']', '').replace(' ', '')
if dyg.count('-') == 2:
dyg_list = []
dyg_list.append(float(dyg.split(',')[1]))
dyg_list.append(float(dyg.split(',')[2]))
hang_list.append(dyg_list)
if dyg.count('-') == 0:
dyg_list_a = []
dyg_list_b = []
dyg_list_a.append(float(dyg.split(',')[0]))
dyg_list_a.append(float(dyg.split(',')[1]))
dyg_list_b.append(float(dyg.split(',')[2]))
dyg_list_b.append(float(dyg.split(',')[3]))
hang_list.append(dyg_list_a)
hang_list.append(dyg_list_b)
if dyg.count('-') == 4:
hang_time.append(float(dyg.split(',')[-2]))
dyg_dist = []
for i in range(0, len(hang_list) - 1, 2):
dist_1 = geo_distance(hang_list[i], hang_list[i + 1])
dyg_dist.append(dist_1)
for i in range(len(dyg_dist) - 1):
dyg_dist.insert(1 + 2 * i, hang_time[i])
dis_result.append(dyg_dist)
return dis_result
def get_col(dis, time, n):
lst_get = []
for i in range(n):
if i % 2 == 0:
lst_get.append(dis)
else:
lst_get.append(time)
return lst_get
def table_make(result, data):
dis_table = pd.DataFrame(dis_result)
N = dis_table.shape[1]
get_columns = get_col('行驶距离', '停车时间', N)
dis_table.columns = get_columns
print(dis_table)
return dis_table
dis_result = dis_cal(data)
dis_table = table_make(dis_result, data)
path = r'.\big_dis_result_3.csv'
https://idea.medeming.com/pycharm https://idea.medeming.com/jets/
|