Skip to content

Commit

Permalink
#7 Work in progress:
Browse files Browse the repository at this point in the history
- Add support for extensions

[ci skip]
  • Loading branch information
FABallemand committed Oct 7, 2024
1 parent 92d6736 commit 707d787
Show file tree
Hide file tree
Showing 7 changed files with 324 additions and 265 deletions.
27 changes: 13 additions & 14 deletions ezgpx/gpx/gpx.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ def __init__(
self.gpx: Gpx = None
self._ele_data: bool = False
self._time_data: bool = False
self.precisions: Dict = None
self.time_format: str = None
self._precisions: Dict = None
self._time_format: str = None

# Parsers
self._gpx_parser: GPXParser = None
Expand All @@ -87,16 +87,15 @@ def __init__(
self.gpx = self._gpx_parser.gpx
self._ele_data = self._gpx_parser.ele_data
self._time_data = self._gpx_parser.time_data
self.precisions = self._gpx_parser.precisions
self.time_format = self._gpx_parser.time_format
self.extensions_fields = self._gpx_parser.extensions_fields
self._precisions = self._gpx_parser.precisions
self._time_format = self._gpx_parser.time_format

# KML
elif file_path.endswith(".kml"):
self._kml_parser = KMLParser(file_path, xml_schema, xml_extensions_schemas)
self.gpx = self._kml_parser.gpx
self.precisions = self._kml_parser.precisions
self.time_format = self._kml_parser.time_format
self._precisions = self._kml_parser.precisions
self._time_format = self._kml_parser.time_format

# KMZ
elif file_path.endswith(".kmz"):
Expand All @@ -109,27 +108,27 @@ def __init__(
self._write_tmp_kml("tmp.kml", kml)
self._kml_parser = KMLParser("tmp.kml", xml_schema, xml_extensions_schemas)
self.gpx = self._kml_parser.gpx
self.precisions = self._kml_parser.precisions
self.time_format = self._kml_parser.time_format
self._precisions = self._kml_parser.precisions
self._time_format = self._kml_parser.time_format
os.remove("tmp.kml")

# FIT
elif file_path.endswith(".fit"):
self._fit_parser = FitParser(file_path)
self.gpx = self._fit_parser.gpx
self.precisions = self._fit_parser.precisions
self.time_format = self._fit_parser.time_format
self._precisions = self._fit_parser.precisions
self._time_format = self._fit_parser.time_format

# NOT SUPPORTED
else:
raise ValueError(f"Unable to parse this type of file: {file_path}"
"Consider renaming your file with the proper file extension.")

self._gpx_writer: GPXWriter = GPXWriter(self.gpx,
precisions=self.precisions, time_format=self.time_format,
extensions_fields=self.extensions_fields)
precisions=self._precisions, time_format=self._time_format,
extensions_fields=self._gpx_parser.extensions_fields if self._gpx_parser else {})
self._kml_writer: KMLWriter = KMLWriter(self.gpx,
precisions=self.precisions, time_format=self.time_format)
precisions=self._precisions, time_format=self._time_format)

# Invalid file path
else:
Expand Down
66 changes: 11 additions & 55 deletions ezgpx/gpx_elements/gpx.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from importlib.resources import files
except ImportError:
from importlib_resources import files
from typing import Union, List, Tuple
from typing import Union, List, Dict, Tuple
import logging
import xmlschema
import pandas as pd
Expand All @@ -25,71 +25,27 @@ class Gpx():
def __init__(
self,
tag: str = "gpx",
creator: str = None,
xmlns: str = None,
version: str = None,
xmlns_xsi: str = None,
creator: str = None,
xsi_schema_location: List[str] = None,
xmlns_gpxtpx: str = None,
xmlns_gpxx: str = None,
xmlns_gpxtrk: str = None,
xmlns_wptx1: str = None,
xmlns: Dict = None,
metadata: Metadata = None,
wpt: List[WayPoint] = None,
rte: List[Route] = None,
trk: List[Track] = None,
extensions: Extensions = None) -> None:
"""
Initialize Gpx instance.
Parameters
----------
tag : str, optional
XML tag, by default "gpx"
creator : str, optional
Creator, by default None
xmlns : str, optional
XML xmlns, by default None
version : str, optional
Version, by default None
xmlns_xsi : str, optional
XML xmlns_xsi, by default None
xsi_schema_location : List[str], optional
XML schema location, by default None
xmlns_gpxtpx : str, optional
???, by default None
xmlns_gpxx : str, optional
???, by default None
xmlns_gpxtrk : str, optional
???, by default None
xmlns_wptx1 : str, optional
???, by default None
metadata : Metadata, optional
Metadata, by default None
wpt : List[WayPoint], optional
Way points, by default None
rte : List[Route], optional
Routes, by default None
trk : List[Track], optional
List of tracks, by default None
extensions : Extensions, optional
Extensions, by default None
"""

self.tag: str = tag
self.creator: str = creator
self.xmlns: str = xmlns
self.version: str = version

self.xmlns_xsi: str = xmlns_xsi
self.creator: str = creator
if xsi_schema_location is None:
self.xsi_schema_location: str = []
self.xsi_schema_location: List[str] = []
else:
self.xsi_schema_location: str = xsi_schema_location
self.xmlns_gpxtpx: str = xmlns_gpxtpx
self.xmlns_gpxx: str = xmlns_gpxx
self.xmlns_gpxtrk: str = xmlns_gpxtrk
self.xmlns_wptx1: str = xmlns_wptx1

self.xsi_schema_location: List[str] = xsi_schema_location
if xmlns is None:
self.xmlns: Dict = {}
else:
self.xmlns: Dict = xmlns
self.metadata: Metadata = metadata
if wpt is None:
self.wpt: List[WayPoint] = []
Expand Down
Loading

0 comments on commit 707d787

Please sign in to comment.