Skip to content

Instantly share code, notes, and snippets.

@weaming
Created October 5, 2024 13:41
Show Gist options
  • Save weaming/092c5c9c23f5f7c963701545861b2fb7 to your computer and use it in GitHub Desktop.
Save weaming/092c5c9c23f5f7c963701545861b2fb7 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# created at 2024.10.05
import argparse
from decimal import Decimal as D
from data_process.io_csv import read_csv, write_csv # python3 -m pip install data-process
def cal_distance(lat1, lon1, lat2, lon2):
"""
Calculate the great circle distance between two points
on the earth (specified in decimal degrees)
"""
from math import radians, sin, cos, sqrt, atan2
# approximate radius of earth in km
R = 6371.0
lat1 = radians(lat1)
lon1 = radians(lon1)
lat2 = radians(lat2)
lon2 = radians(lon2)
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat / 2) ** 2 + cos(lat1) * cos(lat2) * sin(dlon / 2) ** 2
c = 2 * atan2(sqrt(a), sqrt(1 - a))
return R * c * 1000
def convert(line, line_last=None):
"""
input:
geoTime,latitude,longitude,altitude,course,horizontalAccuracy,verticalAccuracy,speed,dayTime,groupTime,isSplit,isMerge,isAdd,network,networkName
1722052544489,23.3622444,116.7555586,6.5,-1.0,33.9,30.0,-1.0,1722009600,1722052544,0,0,0,0,
output:
dataTime,locType,longitude,latitude,heading,accuracy,speed,distance,isBackForeground,stepType,altitude
1649562060,1,113.895717,22.577525,90.165062,35.000000,-1.000000,0.000000,1,0,30.193341
"""
distance = 0
if line_last:
distance = cal_distance(
D(line_last["latitude"]),
D(line_last["longitude"]),
D(line["latitude"]),
D(line["longitude"]),
)
return dict(
dataTime=int(line["geoTime"]) // 1000,
locType=1,
longitude=line["longitude"],
latitude=line["latitude"],
heading=line["course"],
accuracy=max(line["horizontalAccuracy"], line["verticalAccuracy"]),
speed=line["speed"],
distance=round(distance, 5),
isBackForeground=1,
stepType=0,
altitude=line["altitude"],
)
parser = argparse.ArgumentParser()
parser.add_argument("-i", help="输入路径,灵感足迹 App 导出的 CSV 文件")
parser.add_argument("-o", help="输出路径,一生足迹 App 兼容的 CSV 文件")
args = parser.parse_args()
lines = read_csv(args.i)
output = []
for i, line in enumerate(lines):
output.append(convert(line, lines[i - 1] if i > 0 else None))
write_csv(output, args.o, fields=list(output[0].keys()))
print('done')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment