-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpxmanager.py
65 lines (56 loc) · 2.51 KB
/
gpxmanager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import gpxpy
from gi.repository import OsmGpsMap as osmgpsmap
from math import floor
from osmapi import OsmApi
class GPXManager:
def __init__(self):
self._gpx_track = None
self.gpx_filename = ""
self.track_point_count = 0
self._w_width = None
self._w_center = None
def open_gpx(self,fn):
"""Opens a gpx file and stores it"""
with open(fn, 'r') as gpx_file:
self._gpx_track = gpxpy.parse(gpx_file)
self.track_point_count = self.get_track_point_count()
self.gpx_filename = fn
print 'Opened', fn
def remove_gpx(self):
self._gps_track = None
def get_track_point_count(self):
count = 0
for track in self._gpx_track.tracks:
for segment in track.segments:
for point in segment.points:
count += 1
return count
def has_track(self): return self._gpx_track != None
def get_track_iter(self, w_width=None, w_center=None):
"""Returns an iterator over the points of the track. Allows a definition of a window by width and center percentage"""
if self.has_track():
if (w_width != None) and (w_center != None):
start_p, end_p = self._calc_borders_from_moving_window(w_width, w_center)
else:
start_p, end_p = 0, self.track_point_count
for track in self._gpx_track.tracks:
for segment in track.segments:
for i, point in enumerate(segment.points):
if start_p <= i < end_p:
yield (point.longitude, point.latitude, point.time)
def get_track_window_iter(self):
return self.get_track_iter(self._w_width, self._w_center)
def _calc_borders_from_moving_window(self, width, center):
"""Restricts the track to a window of given width and a center position (center in percent)"""
width = min(width, self.track_point_count)
half_width = floor(width/2.0)
center_p = int(round((self.track_point_count) * center))
#if position is smaller than half the window length at the borders disregard it
center_p = max(center_p, half_width)
center_p = min(center_p, self.track_point_count - half_width)
start_p = center_p - half_width
end_p = center_p + half_width
return (start_p, end_p)
def set_track_moving_window(self, w_width, w_center):
self._w_width = w_width
self._w_center = w_center