Skip to content

Commit

Permalink
use gpxpy 'ns-namespace' branch to fix: tkrajina/gpxpy#117
Browse files Browse the repository at this point in the history
  • Loading branch information
jedie committed Jun 2, 2018
1 parent 2d2c329 commit 2e88086
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 32 deletions.
21 changes: 15 additions & 6 deletions for_runners/gpx.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
# https://github.com/jedie/django-for-runners
from for_runners.gpx_tools.garmin2gpxpy import garmin2gpxpy

Identifier = collections.namedtuple('Identifier', ('start_time, start_lat, start_lon, finish_time, finish_lat, finish_lon'))
Identifier = collections.namedtuple(
'Identifier', ('start_time, start_lat, start_lon, finish_time, finish_lat, finish_lon')
)


def get_identifier(gpxpy_instance):
Expand All @@ -36,9 +38,16 @@ def get_identifier(gpxpy_instance):


def parse_gpx(content):
if 'creator="Garmin Connect"' in content:
# work-a-round until https://github.com/tkrajina/gpxpy/issues/115#issuecomment-392798245 fixed
return garmin2gpxpy(content)
# if 'creator="Garmin Connect"' in content:
# # work-a-round until https://github.com/tkrajina/gpxpy/issues/115#issuecomment-392798245 fixed
# return garmin2gpxpy(content)

temp = io.BytesIO(content)
return gpxpy.parse(temp)
return gpxpy.parse(content)


def parse_gpx_file(filepath):
assert filepath.is_file(), "File not found: '%s'" % filepath
with filepath.open("r") as f:
content = f.read()

return parse_gpx(content)
24 changes: 0 additions & 24 deletions for_runners/gpx_tools/gpx_parser.py

This file was deleted.

Empty file added for_runners/tests/__init__.py
Empty file.
49 changes: 49 additions & 0 deletions for_runners/tests/fixture_files/garmin_connect_1.gpx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="UTF-8"?>
<gpx creator="Garmin Connect" version="1.1"
xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/11.xsd"
xmlns:ns3="http://www.garmin.com/xmlschemas/TrackPointExtension/v1"
xmlns="http://www.topografix.com/GPX/1/1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns2="http://www.garmin.com/xmlschemas/GpxExtensions/v3">
<metadata>
<link href="connect.garmin.com">
<text>Garmin Connect</text>
</link>
<time>2018-02-21T14:30:50.000Z</time>
</metadata>
<trk>
<name>Foo Bar</name>
<type>running</type>
<trkseg>
<trkpt lat="51.43788929097354412078857421875" lon="6.617012657225131988525390625">
<ele>23.6000003814697265625</ele>
<time>2018-02-21T14:30:50.000Z</time>
<extensions>
<ns3:TrackPointExtension>
<ns3:hr>125</ns3:hr>
<ns3:cad>75</ns3:cad>
</ns3:TrackPointExtension>
</extensions>
</trkpt>
<trkpt lat="51.43786800093948841094970703125" lon="6.6170061193406581878662109375">
<ele>23.6000003814697265625</ele>
<time>2018-02-21T14:30:51.000Z</time>
<extensions>
<ns3:TrackPointExtension>
<ns3:hr>123</ns3:hr>
<ns3:cad>75</ns3:cad>
</ns3:TrackPointExtension>
</extensions>
</trkpt>
<trkpt lat="51.4378472976386547088623046875" lon="6.61700570024549961090087890625">
<ele>23.799999237060546875</ele>
<time>2018-02-21T14:30:52.000Z</time>
<extensions>
<ns3:TrackPointExtension>
<ns3:hr>124</ns3:hr>
<ns3:cad>75</ns3:cad>
</ns3:TrackPointExtension>
</extensions>
</trkpt>
</trkseg>
</trk>
</gpx>
24 changes: 24 additions & 0 deletions for_runners/tests/test_gpx.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import unittest
from pathlib import Path

# https://github.com/jedie/django-for-runners
from for_runners.gpx import parse_gpx_file

BASE_PATH = Path(__file__).parent


class GpxTests(unittest.TestCase):

def test_parse_gpx(self):
filepath = Path(BASE_PATH, "fixture_files/garmin_connect_1.gpx")

gpxpy_instance = parse_gpx_file(filepath)

first_point = gpxpy_instance.tracks[0].segments[0].points[0]
self.assertEqual(first_point.latitude, 51.43788929097354412078857421875)
self.assertEqual(first_point.longitude, 6.617012657225131988525390625)

extensions = first_point.extensions
self.assertEqual(1, len(extensions))
self.assertEqual(extensions[0].getchildren()[0].text.strip(), "125") # hr
self.assertEqual(extensions[0].getchildren()[1].text.strip(), "75") # cad
5 changes: 3 additions & 2 deletions requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ django-tools
django-cms-tools

# https://github.com/tkrajina/gpxpy
gpxpy
#-e git+https://github.com/tkrajina/gpxpy.git@dev#egg=gpxpy
#gpxpy
# use branch for: https://github.com/tkrajina/gpxpy/issues/117
-e git+https://github.com/tkrajina/gpxpy.git@ns-namespace#egg=gpxpy

# https://pypi.org/project/Gpxity/
# https://github.com/wrohdewald/Gpxity
Expand Down

0 comments on commit 2e88086

Please # to comment.