Improve GPX-Import

This commit is contained in:
cocker-cc
2023-05-17 16:50:01 +02:00
parent 6ed31799a5
commit 43f9e75da1
2 changed files with 19 additions and 29 deletions
+3 -3
View File
@@ -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/<username>/<device>`.
+16 -26
View File
@@ -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")