From 3314b08f89486712b8797df8bfcfd265692bce2b Mon Sep 17 00:00:00 2001 From: Zeko9381 Date: Fri, 25 Mar 2022 09:24:02 +0100 Subject: [PATCH] new file: contrib/gpx-to-rec/gpx_to_owntracks.py --- contrib/gpx-to-rec/gpx_to_owntracks.py | 34 ++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 contrib/gpx-to-rec/gpx_to_owntracks.py diff --git a/contrib/gpx-to-rec/gpx_to_owntracks.py b/contrib/gpx-to-rec/gpx_to_owntracks.py new file mode 100644 index 0000000..66888e4 --- /dev/null +++ b/contrib/gpx-to-rec/gpx_to_owntracks.py @@ -0,0 +1,34 @@ +import gpxpy +import json +import time +import datetime +import sys + +if len(sys.argv) < 2: + print("Please pass a .gpx file to parse") + quit() + +gpx_file = open(sys.argv[1], 'r') + +print("Parsing ", sys.argv[1], "...", sep="") +gpx = gpxpy.parse(gpx_file) + +for track in gpx.tracks: + for segment in track.segments: + # Creates a set of every month and year the points were recorded in + months = set() + for point in segment.points: + months.add(point.time.strftime("%Y-%m")) + + # Generates the contents for every month and saves them to a .rec file (eg. 2022-03.rec) + for month in months: + output = "" + for point in segment.points: + if point.time.strftime("%Y-%m") == month: + point_json = {'_type' : point.type, 'lat' : point.latitude, 'lon' : point.longitude, 'tst' : int(point.time.timestamp()), '_http' : True, 'alt' : point.elevation} + output += point.time.strftime("%Y-%m-%dT%XZ") + "\t*" + (18 * " ") + "\t" + json.dumps(point_json) + "\n" + rec_file = open(month + ".rec", "w") + print("Saving file ", month, ".rec...", sep="") + rec_file.write(output) + rec_file.close() +