aboutsummaryrefslogtreecommitdiff
path: root/fietsboek/convert.py
diff options
context:
space:
mode:
authorDaniel Schadt <kingdread@gmx.de>2023-05-15 23:55:34 +0200
committerDaniel Schadt <kingdread@gmx.de>2023-05-15 23:55:34 +0200
commit40f766ad52b9b8ba4546a55339a9acd76c1c9896 (patch)
tree70606721a718e3e7c7232a118bdc83a93ed85ccf /fietsboek/convert.py
parente05fd862cd06d5ff1964dc3aea50222073802dca (diff)
downloadfietsboek-40f766ad52b9b8ba4546a55339a9acd76c1c9896.tar.gz
fietsboek-40f766ad52b9b8ba4546a55339a9acd76c1c9896.tar.bz2
fietsboek-40f766ad52b9b8ba4546a55339a9acd76c1c9896.zip
initial version of .fit import
Diffstat (limited to 'fietsboek/convert.py')
-rw-r--r--fietsboek/convert.py47
1 files changed, 47 insertions, 0 deletions
diff --git a/fietsboek/convert.py b/fietsboek/convert.py
new file mode 100644
index 0000000..a73883d
--- /dev/null
+++ b/fietsboek/convert.py
@@ -0,0 +1,47 @@
+"""Conversion functions to convert between various recording formats."""
+import fitparse
+from gpxpy.gpx import GPX, GPXTrack, GPXTrackPoint, GPXTrackSegment
+
+
+def semicircles_to_deg(circles: int) -> float:
+ """Convert semicircles coordinate to degree coordinate.
+
+ :param circles: The coordinate value in semicircles.
+ :return: The coordinate in degrees.
+ """
+ return circles * (180 / 2**31)
+
+
+def from_fit(data: bytes) -> GPX:
+ """Reads a .fit as GPX data.
+
+ This uses the fitparse_ library under the hood.
+
+ .. _fitparse: https://pypi.org/project/fitparse/
+
+ :param data: The input bytes.
+ :return: The converted structure.
+ """
+ fitfile = fitparse.FitFile(data)
+ points = []
+ for record in fitfile.get_messages("record"):
+ values = record.get_values()
+ try:
+ point = GPXTrackPoint(
+ latitude=semicircles_to_deg(values["position_lat"]),
+ longitude=semicircles_to_deg(values["position_long"]),
+ elevation=values["altitude"],
+ time=values["timestamp"],
+ )
+ except KeyError:
+ pass
+ else:
+ points.append(point)
+ track = GPXTrack()
+ track.segments = [GPXTrackSegment(points)]
+ gpx = GPX()
+ gpx.tracks = [track]
+ return gpx
+
+
+__all__ = ["from_fit"]