This repository has been archived on 2022-04-22. You can view files and clone it, but cannot push or open issues or pull requests.
GPXprocessor/huawei.py

73 lines
2.6 KiB
Python

import os
import gpxpy
import gpxpy.gpx
from datetime import datetime, timedelta
from geopy.distance import geodesic
start_time = ['2022-04-01 17:34']
old = '<gpx creator="Health" xsi:schemaLocation="http://www.topografix.com/GPX/1/0 \
http://www.topografix.com/GPX/1/0/gpx.xsd" version="1.0" \
xmlns="http://www.topografix.com/GPX/1/0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'
new = '<gpx xmlns="http://www.topografix.com/GPX/1/1" \
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" \
xsi:schemaLocation="http://www.topografix.com/GPX/1/1 \
http://www.topografix.com/GPX/1/1/gpx.xsd" version="1.1" \
creator="gpx.py -- https://github.com/tkrajina/gpxpy">\n'
def file_format(file):
f = open('./old/'+file, 'r', encoding='utf-8')
s = f.readline()
s = s.replace(old, new)
s = '<?xml version="1.0" encoding="UTF-8"?>\n' + s
with open('./old/'+file, 'w', encoding='utf-8') as f:
f.write(s)
def process(file, index):
gpx_file = open('./old/'+file, 'r', encoding='utf-8')
gpx = gpxpy.parse(gpx_file)
s = 0
p = None
for track in gpx.tracks:
time = float(track.extensions[0].text)
for segment in track.segments:
for point in segment.points:
if p:
s += geodesic((point.latitude, point.longitude), (p.latitude, p.longitude)).m
p = point
ngpx = gpxpy.gpx.GPX()
gpx_track = gpxpy.gpx.GPXTrack()
ngpx.tracks.append(gpx_track)
gpx_segment = gpxpy.gpx.GPXTrackSegment()
gpx_track.segments.append(gpx_segment)
p = None
time0 = datetime.strptime(start_time[index], '%Y-%m-%d %H:%M') - timedelta(hours=8)
for track in gpx.tracks:
for segment in track.segments:
for point in segment.points:
if p and point.latitude == p.latitude and point.longitude == p.longitude:
continue
if p:
dis = geodesic((point.latitude, point.longitude), (p.latitude, p.longitude)).m
dtime += time * dis / s
else:
dtime = 0
gpx_segment.points.append(gpxpy.gpx.GPXTrackPoint(point.latitude, point.longitude,
time=time0 + timedelta(seconds=dtime)))
# datetime.strftime(time0 + timedelta(seconds=dtime), '%Y-%m-%dT%H:%M:%S.%fZ')
p = point
with open('./new/'+file, 'w', encoding='utf-8') as f:
f.write(ngpx.to_xml())
files = os.listdir('./old')
for k in range(len(files)):
file_format(files[k])
process(files[k], k)
os.remove('./old/' + files[k])