def update_data(self): # Set up accumulating variables previous = False length = 0 ascent = 0 descent = 0 altitude_max = 0 altitude_min = 1000 for wp in self.waypoints.all().order_by('localtime'): if not previous: self.start_time = wp.localtime previous = wp else: # assert False, [wp.altitude,previous.altitude] # If there's an ascent, add it if wp.altitude > previous.altitude: ascent += (wp.altitude - previous.altitude) else: descent += (previous.altitude - wp.altitude) # Check if we have a new high if wp.altitude > altitude_max: altitude_max = wp.altitude # Check if we have a new low if wp.altitude < altitude_min: altitude_min = wp.altitude length += get_distance(previous, wp) # make this waypoint the previous one previous = wp # Create track here self.length = str(round(length, 5)) self.ascent = str(round(ascent, 5)) self.descent = str(round(descent, 5)) self.altitude_max = str(round(altitude_max, 5)) self.altitude_min = str(round(altitude_min, 5)) self.end_time = previous.localtime
def create_tracks(self, min_interval=30, max_interval=2700, min_length=0.1): """ min_interval = 60 max_interval = 3500 min_length = 1 """ # Our waypoints for this jaunt waypoints = self.waypoints.all().order_by('gmtime') # Set up accumulating variables previous = False length = 0 track_count = 1 # Set the first times on the trap first_wp = waypoints[0] # Create first track and 0 out variables tracks = Track.objects.filter(start_time=first_wp.localtime) if tracks: track = tracks[0] index = 0 for mt in tracks: if index > 0: mt.delete() index += 1 else: track = Track() track.name = "%s %d" % (self.name, track_count) track.description = '' track.length = 0 track.ascent = 0 track.descent = 0 track.altitude_max = 0 track.altitude_min = 1000 track.gpx_file = self track.start_time = first_wp.gmtime track.end_time = first_wp.gmtime track.save() # Loop through waypoints and measure distances for wp in waypoints: too_long = False too_far = False too_short = False if not previous: track.waypoints.add(wp) previous = wp else: secs_elapsed = wp.gmtime - previous.gmtime if secs_elapsed.seconds > max_interval or secs_elapsed.days > 0: too_long = True # assert False, [wp,previous,secs_elapsed.seconds,max_interval] if secs_elapsed.seconds < min_interval: too_short = True if too_long: # Add track if it's long enough if length > min_length: track.name = "%s %d" % (self.name, track_count) track.save() self.track_set.add(track) # Normalise altitude of first waypoint # w1 = track.waypoints.all().order_by('gmtime')[0] # w2 = track.waypoints.all().order_by('gmtime')[1] # w1.altitude = w2.altitude # if w1.gmtime == w2.gmtime and w1.latitude == w2.latitude and w1.longitude == w2.latitude: # w1.delete() # w2.save() # else: # print w1.id, w1.latitude,w1.longitude,w1.gmtime,w1.altitude # w1.save() # # # Normalise altitude of last waypoint # w1 = track.waypoints.all().order_by('-gmtime')[0] # w2 = track.waypoints.all().order_by('-gmtime')[1] # w1.altitude = w2.altitude # if w1.gmtime == w2.gmtime and w1.latitude == w2.latitude and w1.longitude == w2.latitude: # w1.delete() # w2.save() # else: # w1.save() track.update_data() else: track.delete() # track_count += 1 # Create new track and 0 out variables tracks = Track.objects.filter(start_time=wp.localtime) if tracks: track = tracks[0] index = 0 for mt in tracks: if index > 0: mt.delete() index += 1 else: track = Track() track.name = "%s %d" % (self.name, track_count) track.description = '' track.length = 0 track.ascent = 0 track.descent = 0 track.altitude_max = 0 track.altitude_min = 1000 track.start_time = wp.gmtime track.end_time = wp.gmtime track.gpx_file = self track.save() # Set up accumulating variables previous = False length = 0 track.waypoints.add(wp) previous = wp # elif too_short: # previous = previous else: length += get_distance(previous, wp) track.waypoints.add(wp) previous = wp # del(waypoints) # Add track if it's long enough if length > min_length: track_count += 1 track.name = "%s %d" % (self.name, track_count) track.save() self.track_set.add(track) track.update_data() else: track.delete()