From 43f9e75da11d10479c053e4e60d5e710476b6768 Mon Sep 17 00:00:00 2001 From: cocker-cc Date: Wed, 17 May 2023 16:50:01 +0200 Subject: [PATCH] Improve GPX-Import --- contrib/gpx-to-rec/README.md | 6 ++--- contrib/gpx-to-rec/gpx_to_rec.py | 42 ++++++++++++-------------------- 2 files changed, 19 insertions(+), 29 deletions(-) diff --git a/contrib/gpx-to-rec/README.md b/contrib/gpx-to-rec/README.md index 5607d0b..4a87a86 100644 --- a/contrib/gpx-to-rec/README.md +++ b/contrib/gpx-to-rec/README.md @@ -1,5 +1,5 @@ # gpx-to-rec.py -This script attempts to convert a `gpx` file into `rec` files to be used with `recorder`. I've only tested this script with gpx files with only one track and one segment from `Phonetrack`. +This script attempts to convert the specified `gpx` files into `rec` files to be used with `recorder`. ## Installation `gpx-to-rec.py` depends on [`gpxpy`](https://github.com/tkrajina/gpxpy) which you can install with `pip`: @@ -7,10 +7,10 @@ This script attempts to convert a `gpx` file into `rec` files to be used with `r pip install gpxpy ``` ## Usage -The script takes the `.gpx` file as an argument and creates `.rec` files in the same directory as the script. +The script takes one or more `.gpx` files as arguments and creates `.rec` files in the current working directory. Example: ``` -python3 gpx-to-rec.py my_track.gpx +python3 gpx-to-rec.py my_track.gpx [my_other_track.gpx ...] ``` You can then copy those `.rec` files to `recorder`'s storage location for the corresponding user and device. By default it's in `/var/spool/owntracks/recorder/store/rec//`. diff --git a/contrib/gpx-to-rec/gpx_to_rec.py b/contrib/gpx-to-rec/gpx_to_rec.py index f012db9..88cf7a7 100644 --- a/contrib/gpx-to-rec/gpx_to_rec.py +++ b/contrib/gpx-to-rec/gpx_to_rec.py @@ -1,34 +1,24 @@ +#!/usr/bin/env python 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 = "" +recs = [] +for file in sys.argv[1:]: + print(file) + gpx_file = open(file, 'r') + gpx = gpxpy.parse(gpx_file) + for track in gpx.tracks: + for segment in track.segments: for point in segment.points: - if point.time.strftime("%Y-%m") == month: - point_json = {'_type' : 'location', '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() + point_json = {'_type' : 'location', 'lat' : point.latitude, 'lon' : point.longitude, 'tst' : int(point.time.timestamp()), '_http' : True, 'alt' : point.elevation} + recs.append(point.time.strftime("%Y-%m-%dT%XZ") + "\t*" + (18 * " ") + "\t" + json.dumps(point_json)) +recs.sort() +for i in recs: + ts = i.split("\t")[0] + month = ts[0:7] + with open(month + '.rec', 'a') as f: + f.write(i + "\n")