From 079cf439aa9a3a198133daed7f702a371abc6dab Mon Sep 17 00:00:00 2001 From: Yell0wflash Date: Sat, 2 Jun 2018 09:34:04 +0200 Subject: [PATCH] use gpxpy 'ns-namespace' branch to fix: https://github.com/tkrajina/gpxpy/issues/117 --- for_runners/gpx.py | 21 +++++--- for_runners/gpx_tools/gpx_parser.py | 24 --------- for_runners/tests/__init__.py | 0 .../tests/fixture_files/garmin_connect_1.gpx | 49 +++++++++++++++++++ for_runners/tests/test_gpx.py | 24 +++++++++ requirements/base.txt | 5 +- 6 files changed, 91 insertions(+), 32 deletions(-) delete mode 100644 for_runners/gpx_tools/gpx_parser.py create mode 100644 for_runners/tests/__init__.py create mode 100644 for_runners/tests/fixture_files/garmin_connect_1.gpx create mode 100644 for_runners/tests/test_gpx.py diff --git a/for_runners/gpx.py b/for_runners/gpx.py index 9e8fb79..ec16c97 100644 --- a/for_runners/gpx.py +++ b/for_runners/gpx.py @@ -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): @@ -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) diff --git a/for_runners/gpx_tools/gpx_parser.py b/for_runners/gpx_tools/gpx_parser.py deleted file mode 100644 index d44d9de..0000000 --- a/for_runners/gpx_tools/gpx_parser.py +++ /dev/null @@ -1,24 +0,0 @@ -""" - created 31.05.2018 by Jens Diemer - :copyleft: 2018 by the django-for-runners team, see AUTHORS for more details. - :license: GNU GPL v3 or above, see LICENSE for more details. -""" -import io -from pathlib import Path - -import gpxpy - -from for_runners.gpx_tools.garmin2gpxpy import garmin2gpxpy - - - - - -def parse_gpx_file(filepath): - assert isinstance(filepath, Path) - assert filepath.is_file() - - with filepath.open("r") as f: - content = f.read() - - return parse_gpx(content) diff --git a/for_runners/tests/__init__.py b/for_runners/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/for_runners/tests/fixture_files/garmin_connect_1.gpx b/for_runners/tests/fixture_files/garmin_connect_1.gpx new file mode 100644 index 0000000..e0bc754 --- /dev/null +++ b/for_runners/tests/fixture_files/garmin_connect_1.gpx @@ -0,0 +1,49 @@ + + + + + Garmin Connect + + + + + Foo Bar + running + + + 23.6000003814697265625 + + + + 125 + 75 + + + + + 23.6000003814697265625 + + + + 123 + 75 + + + + + 23.799999237060546875 + + + + 124 + 75 + + + + + + diff --git a/for_runners/tests/test_gpx.py b/for_runners/tests/test_gpx.py new file mode 100644 index 0000000..834be6f --- /dev/null +++ b/for_runners/tests/test_gpx.py @@ -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 diff --git a/requirements/base.txt b/requirements/base.txt index 89eecbb..ce63a42 100644 --- a/requirements/base.txt +++ b/requirements/base.txt @@ -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