Skip to content

Commit 1a62209

Browse files
ddddzhben-29
andauthored
Refactor parse_points_to_gpx() of joyrun_sync.py to modularize GPX instance construction for extension data (#783)
* [Refactor] Create classes to handle pause data in parse_points_to_gpx() * [Refactor] Reorder GPX instances creation * [Refactor] Use gpxpy native classes to construct GPX data in `parse_points_to_gpx()` * fix: joyrun duplicate check logic (#784) * [Update] Address the Pause and Pause list class type conversion issue --------- Co-authored-by: Ben Bell <[email protected]>
1 parent faad27e commit 1a62209

File tree

1 file changed

+62
-38
lines changed

1 file changed

+62
-38
lines changed

run_page/joyrun_sync.py

+62-38
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from collections import namedtuple
99
from datetime import datetime, timedelta, timezone
1010
from hashlib import md5
11+
from typing import List
1112
from urllib.parse import quote
1213

1314
import gpxpy
@@ -187,6 +188,23 @@ def parse_content_to_ponits(content):
187188
points = []
188189
return points
189190

191+
class Pause:
192+
def __init__(self, pause_data_point: List[str]):
193+
self.index = int(pause_data_point[0])
194+
self.duration = int(pause_data_point[1])
195+
196+
def __repr__(self):
197+
return f"Pause(index=${self.index}, duration=${self.duration})"
198+
199+
class PauseList:
200+
def __init__(self, pause_list: List[List[str]]):
201+
self._list = []
202+
for pause in pause_list:
203+
self._list.append(Joyrun.Pause(pause))
204+
205+
def next(self) -> "Joyrun.Pause":
206+
return self._list.pop(0) if self._list else None
207+
190208
@staticmethod
191209
def parse_points_to_gpx(
192210
run_points_data, start_time, end_time, pause_list, interval=5
@@ -200,49 +218,55 @@ def parse_points_to_gpx(
200218
:param interval: time interval between each point, in seconds
201219
"""
202220

203-
# format data
204-
segment_list = []
205-
points_dict_list = []
206-
current_time = start_time
221+
# GPX instance
222+
gpx = gpxpy.gpx.GPX()
223+
gpx.nsmap["gpxtpx"] = "http://www.garmin.com/xmlschemas/TrackPointExtension/v1"
224+
225+
# GPX Track
226+
track = gpxpy.gpx.GPXTrack()
227+
track.name = f"gpx from joyrun {start_time}"
228+
gpx.tracks.append(track)
229+
230+
# GPX Track Segment
231+
track_segment = gpxpy.gpx.GPXTrackSegment()
232+
track.segments.append(track_segment)
207233

234+
# Initialize Pause
235+
pause_list = Joyrun.PauseList(pause_list)
236+
pause = pause_list.next()
237+
238+
current_time = start_time
208239
for index, point in enumerate(run_points_data[:-1]):
209-
points_dict = {
210-
"latitude": point[0],
211-
"longitude": point[1],
212-
"time": datetime.fromtimestamp(current_time, tz=timezone.utc),
213-
}
214-
points_dict_list.append(points_dict)
240+
# New Track Point
241+
track_point = gpxpy.gpx.GPXTrackPoint(
242+
latitude=point[0],
243+
longitude=point[1],
244+
time=datetime.fromtimestamp(current_time, tz=timezone.utc),
245+
)
246+
track_segment.points.append(track_point)
215247

248+
# Increment time
216249
current_time += interval
217-
if pause_list and int(pause_list[0][0]) - 1 == index:
218-
segment_list.append(points_dict_list[:])
219-
points_dict_list.clear()
220-
current_time += int(pause_list[0][1])
221-
pause_list.pop(0)
222-
223-
points_dict_list.append(
224-
{
225-
"latitude": run_points_data[-1][0],
226-
"longitude": run_points_data[-1][1],
227-
"time": datetime.fromtimestamp(end_time, tz=timezone.utc),
228-
}
229-
)
230-
segment_list.append(points_dict_list)
231250

232-
# gpx part
233-
gpx = gpxpy.gpx.GPX()
234-
gpx.nsmap["gpxtpx"] = "http://www.garmin.com/xmlschemas/TrackPointExtension/v1"
235-
gpx_track = gpxpy.gpx.GPXTrack()
236-
gpx_track.name = "gpx from joyrun"
237-
gpx.tracks.append(gpx_track)
238-
239-
# add segment list to our GPX track:
240-
for point_list in segment_list:
241-
gpx_segment = gpxpy.gpx.GPXTrackSegment()
242-
gpx_track.segments.append(gpx_segment)
243-
for p in point_list:
244-
point = gpxpy.gpx.GPXTrackPoint(**p)
245-
gpx_segment.points.append(point)
251+
# Check pause
252+
if pause and pause.index - 1 == index:
253+
# New Segment
254+
track_segment = gpxpy.gpx.GPXTrackSegment()
255+
track.segments.append(track_segment)
256+
# Add paused duration
257+
current_time += pause.duration
258+
# Next pause
259+
pause = pause_list.next()
260+
261+
# Last Track Point uses end_time
262+
last_point = run_points_data[-1]
263+
track_segment.points.append(
264+
gpxpy.gpx.GPXTrackPoint(
265+
latitude=last_point[0],
266+
longitude=last_point[1],
267+
time=datetime.fromtimestamp(end_time, tz=timezone.utc),
268+
)
269+
)
246270

247271
return gpx.to_xml()
248272

0 commit comments

Comments
 (0)