Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Joyrun gpx export fix for pauses and gpx segments #601

Merged
merged 3 commits into from
Jan 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 38 additions & 13 deletions run_page/joyrun_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,37 +184,61 @@ def parse_content_to_ponits(content):
return points

@staticmethod
def parse_points_to_gpx(run_points_data, start_time, end_time, interval=5):
# TODO for now kind of same as `keep` maybe refactor later
def parse_points_to_gpx(
run_points_data, start_time, end_time, pause_list, interval=5
):
"""
parse run_data content to gpx object
TODO for now kind of same as `keep` maybe refactor later

:param run_points_data: [[latitude, longitude],...]
:param pause_list: [[interval_index, pause_seconds],...]
:param interval: time interval between each point, in seconds
"""

# format data
segment_list = []
points_dict_list = []
i = 0
for point in run_points_data[:-1]:
current_time = start_time

for index, point in enumerate(run_points_data[:-1]):
points_dict = {
"latitude": point[0],
"longitude": point[1],
"time": datetime.utcfromtimestamp(start_time + interval * i),
"time": datetime.utcfromtimestamp(current_time),
}
i += 1
points_dict_list.append(points_dict)

current_time += interval
if pause_list and int(pause_list[0][0]) - 1 == index:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pause_list 的数据结构是怎样的?请在注释里标注一下,看起来是 [index, pause_seconds] ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是的
加了点注释

segment_list.append(points_dict_list[:])
points_dict_list.clear()
current_time += int(pause_list[0][1])
pause_list.pop(0)

points_dict_list.append(
{
"latitude": run_points_data[-1][0],
"longitude": run_points_data[-1][1],
"time": datetime.utcfromtimestamp(end_time),
}
)
segment_list.append(points_dict_list)

# gpx part
gpx = gpxpy.gpx.GPX()
gpx.nsmap["gpxtpx"] = "http://www.garmin.com/xmlschemas/TrackPointExtension/v1"
gpx_track = gpxpy.gpx.GPXTrack()
gpx_track.name = "gpx from joyrun"
gpx.tracks.append(gpx_track)

# Create first segment in our GPX track:
gpx_segment = gpxpy.gpx.GPXTrackSegment()
gpx_track.segments.append(gpx_segment)
for p in points_dict_list:
point = gpxpy.gpx.GPXTrackPoint(**p)
gpx_segment.points.append(point)
# add segment list to our GPX track:
for point_list in segment_list:
gpx_segment = gpxpy.gpx.GPXTrackSegment()
gpx_track.segments.append(gpx_segment)
for p in point_list:
point = gpxpy.gpx.GPXTrackPoint(**p)
gpx_segment.points.append(point)

return gpx.to_xml()

Expand All @@ -237,12 +261,13 @@ def parse_raw_data_to_nametuple(self, run_data, old_gpx_ids, with_gpx=False):

start_time = run_data["starttime"]
end_time = run_data["endtime"]
pause_list = run_data["pause"]
run_points_data = self.parse_content_to_ponits(run_data["content"])
if with_gpx:
# pass the track no points
if run_points_data:
gpx_data = self.parse_points_to_gpx(
run_points_data, start_time, end_time
run_points_data, start_time, end_time, pause_list
)
download_joyrun_gpx(gpx_data, str(joyrun_id))
try:
Expand Down
Loading