Last active
December 27, 2019 11:18
-
-
Save felipe-araujo/84f9d20aa0e93b0c90c3e09d8be5789c to your computer and use it in GitHub Desktop.
Merge several gpx files into one
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gpxpy.parser as parser | |
import gpxpy.gpx as gpx | |
import os | |
import math | |
import datetime | |
## functions | |
def min_max_timestamp(parsed): | |
min_ts = parsed.tracks[0].segments[0].points[0].time | |
max_ts = parsed.tracks[0].segments[0].points[0].time | |
for track in parsed.tracks: | |
for segment in track.segments: | |
for point in segment.points: | |
if point.time < min_ts: | |
min_ts = point.time | |
if point.time > max_ts: | |
max_ts = point.time | |
#print("Min, max for ", parsed, "is: ") | |
#print(min_ts, max_ts) | |
return (min_ts, max_ts) | |
def flat_points(parsed): | |
points = [] | |
for track in parsed.tracks: | |
for segment in track.segments: | |
points += segment.points | |
return points | |
## end functions | |
files = [f for f in os.listdir('.') if os.path.isfile(f)] | |
print(files) | |
output = 'output.gpx' | |
if output in files: | |
files.remove(output) | |
files.remove(__file__) | |
# Get min and max timestamps | |
min_timestamp = None | |
max_timestamp = None | |
errors = [] | |
# f.tracks[0].segments[0].points[0] | |
all_points = [] | |
all_min_max_timestamps = [] | |
for file in files: | |
try: | |
p = parser.GPXParser(open(file, 'r')).parse() | |
all_min_max_timestamps.extend(min_max_timestamp(p)) | |
all_points += flat_points(p) | |
#file.close() | |
except Exception as e: | |
print("Could not parse file " , file) | |
print("Error: ", e) | |
errors.append(file) | |
print("\nAll the files that could not be loaded: ") | |
print(errors) | |
print(len(all_points)) | |
print("Min and max times") | |
all_min_max_timestamps.sort() | |
min_timestamp = all_min_max_timestamps[0] | |
max_timestamp = all_min_max_timestamps[-1] | |
print(min_timestamp, max_timestamp) | |
duration = 5 * 60* 60 # 5 hours | |
interval = math.floor(duration / len(all_points)) | |
if interval < 1: | |
interval = 1 | |
print("Interval between each point is ", interval) | |
all_points.sort(key = lambda point: point.time) | |
# overwritting this so the activity gets the current date | |
min_timestamp = datetime.datetime.now() - datetime.timedelta(seconds=duration) | |
for index, point in enumerate(all_points): | |
point.time = min_timestamp + datetime.timedelta(seconds=(index * interval)) | |
# create new gpx file | |
new_file = gpx.GPX() | |
track = gpx.GPXTrack() | |
segment = gpx.GPXTrackSegment() | |
track.segments.append(segment) | |
new_file.tracks.append(track) | |
segment.points.extend(all_points) | |
with open(output, 'a') as f: | |
f.write(new_file.to_xml()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment