Skip to content

Commit

Permalink
#3 #2 Work in progress:
Browse files Browse the repository at this point in the history
- Parsers and writers relocation
- Fix importlib issue
- Fix export methods
- Update tests

[ci skip]
  • Loading branch information
FABallemand committed Jul 5, 2024
1 parent 553677d commit cf0f4f5
Show file tree
Hide file tree
Showing 27 changed files with 1,915 additions and 63 deletions.
6 changes: 0 additions & 6 deletions ezgpx/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
from .fit_parser import *
from .gpx import *
from .gpx_elements import *
from .gpx_parser import *
from .gpx_writer import *
from .kml_parser import *
from .kml_writer import *
from .parser import *
from .utils import *
from .writer import *
from .xml_parser import *
15 changes: 6 additions & 9 deletions ezgpx/gpx/gpx.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,9 @@
import pandas as pd
import numpy as np

from fitparse import FitFile

import matplotlib
import matplotlib.pyplot as plt
from matplotlib.axes import Axes
from matplotlib.figure import Figure
from mpl_toolkits.basemap import Basemap
import matplotlib.animation as animation

Expand All @@ -29,11 +26,11 @@
from papermap import PaperMap

from ..gpx_elements import Bounds, Copyright, Email, Extensions, Gpx, Link, Metadata, Person, PointSegment, Point, Route, TrackSegment, Track, WayPoint
from ..gpx_parser import GPXParser
from ..kml_parser import KMLParser
from ..fit_parser import FitParser
from ..gpx_writer import GPXWriter
from ..kml_writer import KMLWriter
from ..parser.gpx_parser import GPXParser
from ..parser.kml_parser import KMLParser
from ..parser.fit_parser import FitParser
from ..writer.gpx_writer import GPXWriter
from ..writer.kml_writer import KMLWriter
from ..utils import EARTH_RADIUS

GPX = NewType("GPX", object) # GPX forward declaration for type hint
Expand Down Expand Up @@ -727,7 +724,7 @@ def to_csv(
str
CSV like string if path is set to None.
"""
return self.gpx.to_csv(path, sep, values, header, index)
return self.gpx.to_csv(path, values, sep, header, index)

def to_gpx(
self,
Expand Down
25 changes: 2 additions & 23 deletions ezgpx/gpx_elements/gpx.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
try:
from importlib.resources import files
except ImportError:
Expand Down Expand Up @@ -1060,26 +1059,6 @@ def to_csv(
CSV like string if path is set to None.
"""
if values is None:
values = ["lat", "lon"]
values = ["lat", "lon"]

elevation = False
time = False
speed = False
pace = False
ascent_rate = False
ascent_speed = False

if "ele" in values:
elevation = True
if "time":
time = True
if "speed" in values:
speed = True
if "pace" in values:
pace = True
if "ascent_rate" in values:
ascent_rate = True
if "ascent_speed" in values:
ascent_speed = True

return self.to_dataframe(elevation, time, speed, pace, ascent_rate, ascent_speed).to_csv(path, sep=sep, columns=values, header=header, index=index)
return self.to_dataframe(values).to_csv(path, sep=sep, header=header, index=index)
4 changes: 4 additions & 0 deletions ezgpx/parser/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
from .fit_parser import *
from .gpx_parser import *
from .kml_parser import *
from .xml_parser import *
from .parser import *
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import os
from typing import Optional, Union
from typing import Optional
import logging
import pandas as pd
from datetime import datetime
from math import pi

from fitparse import FitFile

from ..parser import Parser, DEFAULT_PRECISION
from ..gpx_elements import Bounds, Copyright, Email, Extensions, Gpx, Link, Metadata, Person, Point, PointSegment, Route, TrackSegment, Track, WayPoint
from ...gpx_elements import Bounds, Copyright, Email, Extensions, Gpx, Link, Metadata, Person, Point, PointSegment, Route, TrackSegment, Track, WayPoint

class FitParser(Parser):
"""
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import xml.etree.ElementTree as ET

from ..xml_parser import XMLParser
from ..gpx_elements import Bounds, Copyright, Email, Extensions, Gpx, Link, Metadata, Person, Point, PointSegment, Route, TrackSegment, Track, WayPoint
from ...gpx_elements import Bounds, Copyright, Email, Extensions, Gpx, Link, Metadata, Person, Point, PointSegment, Route, TrackSegment, Track, WayPoint

class GPXParser(XMLParser):
"""
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import os
from typing import Optional, Union, List, Dict
import logging
from datetime import datetime
import xml.etree.ElementTree as ET

from ..xml_parser import XMLParser
from ..gpx_elements import Bounds, Copyright, Email, Extensions, Gpx, Link, Metadata, Person, Point, PointSegment, Route, TrackSegment, Track, WayPoint
from ...gpx_elements import Bounds, Copyright, Email, Extensions, Gpx, Link, Metadata, Person, Point, PointSegment, Route, TrackSegment, Track, WayPoint

class KMLParser(XMLParser):
"""
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions ezgpx/writer/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
from .gpx_writer import *
from .kml_writer import *
from .writer import *
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import os
import errno
import warnings
from typing import Optional, Union, List, Tuple, Dict
from typing import List, Dict
import logging
import xml.etree.ElementTree as ET
from datetime import datetime

from ..writer import Writer
from ..gpx_elements import Bounds, Copyright, Email, Extensions, Gpx, Link, Metadata, Person, PointSegment, Point, Route, TrackSegment, Track, WayPoint
from ...gpx_elements import Bounds, Copyright, Email, Extensions, Gpx, Link, Metadata, Person, PointSegment, Point, Route, TrackSegment, Track, WayPoint
from .gpx_writer_method_behavior_creator import GPXWriterMethodBehaviorCreator

class GPXWriter(Writer):
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import os
from typing import Optional, Union, List, Tuple, Dict
from typing import Optional, List, Tuple, Dict
import logging
import xml.etree.ElementTree as ET
import xmlschema
from datetime import datetime

from ..writer import Writer
from ..gpx_elements import Gpx
from ...gpx_elements import Gpx

DEFAULT_NORMAL_STYLE = {
"color": "ff0000ff",
Expand Down Expand Up @@ -208,9 +206,9 @@ def add_linestring(self, element: ET.Element) -> ET.Element:
linestring_ = ET.SubElement(element, "LineString")
linestring_, _ = self.add_subelement_number(linestring_, "tessellate", 1)
if self.ele:
coordinates = self.gpx.to_csv(path=None, columns=["lon", "lat", "ele"], header=False).replace("\n", " ")
coordinates = self.gpx.to_csv(path=None, values=["lon", "lat", "ele"], header=False).replace("\n", " ")
else:
coordinates = self.gpx.to_csv(path=None, columns=["lon", "lat"], header=False).replace("\n", " ")
coordinates = self.gpx.to_csv(path=None, values=["lon", "lat"], header=False).replace("\n", " ")
linestring_, _ = self.add_subelement(linestring_, "coordinates", coordinates)
return element

Expand Down
16 changes: 8 additions & 8 deletions tests/test_GPX.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,31 +238,31 @@ def test_max_ascent_speed(self):
def test_to_dataframe(self):
# Parse GPX Files
gpx = GPX(os.path.join(FILES_DIRECTORY, "strava_run_1.gpx"))
df = gpx.to_dataframe(elevation=True, time=True)
reference_df = pd.read_csv(os.path.join(REFERENCE_FILES_DIRECTORY, "strava_run_1_test.csv"))
df = gpx.to_dataframe(values=["lat", "lon", "ele", "time"])
reference_df = pd.read_csv(os.path.join(REFERENCE_FILES_DIRECTORY, "strava_run_1.csv"))
# Test
assert(reference_df.equals(df))

def test_to_gpx(self):
# Parse GPX Files
gpx = GPX(os.path.join(FILES_DIRECTORY, "strava_run_1.gpx"))
gpx.to_gpx("tmp/strava_run_1.gpx", xml_schema=False)
gpx.to_gpx("tmp/strava_run_1_test.gpx", xml_schema=False)
# Test
assert(filecmp.cmp("tmp/strava_run_1.gpx", os.path.join(REFERENCE_FILES_DIRECTORY, "strava_run_1_test.gpx"), False))
assert(filecmp.cmp("tmp/strava_run_1_test.gpx", os.path.join(REFERENCE_FILES_DIRECTORY, "strava_run_1.gpx"), False))

def test_to_kml(self):
# Parse GPX Files
gpx = GPX(os.path.join(FILES_DIRECTORY, "strava_run_1.gpx"))
gpx.to_kml("tmp/strava_run_1_test.kml", styles=None, xml_schema=False)
# Test
assert(filecmp.cmp("tmp/strava_run_1_test.kml", os.path.join(REFERENCE_FILES_DIRECTORY, "strava_run_1_test.kml"), False))
assert(filecmp.cmp("tmp/strava_run_1_test.kml", os.path.join(REFERENCE_FILES_DIRECTORY, "strava_run_1.kml"), False))

def test_to_csv(self):
# Parse GPX Files
gpx = GPX(os.path.join(FILES_DIRECTORY, "strava_run_1.gpx"))
gpx.to_csv("tmp/strava_run_1.csv", columns=["lat", "lon", "ele", "time"])
gpx.to_csv("tmp/strava_run_1_test.csv", values=["lat", "lon", "ele", "time"])
# Test
assert(filecmp.cmp("tmp/strava_run_1.csv", os.path.join(REFERENCE_FILES_DIRECTORY, "strava_run_1_test.csv"), False))
assert(filecmp.cmp("tmp/strava_run_1_test.csv", os.path.join(REFERENCE_FILES_DIRECTORY, "strava_run_1.csv"), False))

#==== Plots ==============================================================#

Expand Down Expand Up @@ -349,7 +349,7 @@ def test_folium_plot(self):

#==== Destroy ============================================================#

def test_destroy(self, remove_tmp: bool = True):
def test_destroy(self, remove_tmp: bool = False):
# Remove temporary folder
if remove_tmp:
rmtree("tmp", True)
Expand Down
Loading

0 comments on commit cf0f4f5

Please sign in to comment.