#NEWLINE# def smooth( self , vertical = True , horizontal = False , remove_extremes = False ) : #NEWLINE# #INDENT# ' "Smooths" the elevation graph. Can be called multiple times. ' #NEWLINE# if ( len ( self . points ) <= 3 ) : #NEWLINE# #INDENT# return #NEWLINE# #UNINDENT# elevations = [ ] #NEWLINE# latitudes = [ ] #NEWLINE# longitudes = [ ] #NEWLINE# for point in self . points : #NEWLINE# #INDENT# elevations . append ( point . elevation ) #NEWLINE# latitudes . append ( point . latitude ) #NEWLINE# longitudes . append ( point . longitude ) #NEWLINE# #UNINDENT# avg_distance = 0 #NEWLINE# avg_elevation_delta = 1 #NEWLINE# if remove_extremes : #NEWLINE# #INDENT# distances = [ ] #NEWLINE# elevations_delta = [ ] #NEWLINE# for i in range ( len ( self . points ) ) [ 1 : ] : #NEWLINE# #INDENT# distances . append ( self . points [ i ] . distance_2d ( self . points [ ( i - 1 ) ] ) ) #NEWLINE# elevation_1 = self . points [ i ] . elevation #NEWLINE# elevation_2 = self . points [ ( i - 1 ) ] . elevation #NEWLINE# if ( ( elevation_1 is not None ) and ( elevation_2 is not None ) ) : #NEWLINE# #INDENT# elevations_delta . append ( abs ( ( elevation_1 - elevation_2 ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# if distances : #NEWLINE# #INDENT# avg_distance = ( ( 1.0 * sum ( distances ) ) / len ( distances ) ) #NEWLINE# #UNINDENT# if elevations_delta : #NEWLINE# #INDENT# avg_elevation_delta = ( ( 1.0 * sum ( elevations_delta ) ) / len ( elevations_delta ) ) #NEWLINE# #UNINDENT# #UNINDENT# remove_2d_extremes_threshold = ( 1.75 * avg_distance ) #NEWLINE# remove_elevation_extremes_threshold = ( avg_elevation_delta * 5 ) #NEWLINE# new_track_points = [ self . points [ 0 ] ] #NEWLINE# for i in range ( len ( self . points ) ) [ 1 : ( - 1 ) ] : #NEWLINE# #INDENT# new_point = None #NEWLINE# point_removed = False #NEWLINE# if ( vertical and elevations [ ( i - 1 ) ] and elevations [ i ] and elevations [ ( i + 1 ) ] ) : #NEWLINE# #INDENT# old_elevation = self . points [ i ] . elevation #NEWLINE# new_elevation = ( ( ( SMOOTHING_RATIO [ 0 ] * elevations [ ( i - 1 ) ] ) + ( SMOOTHING_RATIO [ 1 ] * elevations [ i ] ) ) + ( SMOOTHING_RATIO [ 2 ] * elevations [ ( i + 1 ) ] ) ) #NEWLINE# if ( not remove_extremes ) : #NEWLINE# #INDENT# self . points [ i ] . elevation = new_elevation #NEWLINE# #UNINDENT# if remove_extremes : #NEWLINE# #INDENT# d1 = abs ( ( old_elevation - elevations [ ( i - 1 ) ] ) ) #NEWLINE# d2 = abs ( ( old_elevation - elevations [ ( i + 1 ) ] ) ) #NEWLINE# if ( ( min ( d1 , d2 ) < remove_elevation_extremes_threshold ) and ( abs ( ( old_elevation - new_elevation ) ) < remove_2d_extremes_threshold ) ) : #NEWLINE# #INDENT# new_point = self . points [ i ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# point_removed = True #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# new_point = self . points [ i ] #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# new_point = self . points [ i ] #NEWLINE# #UNINDENT# if horizontal : #NEWLINE# #INDENT# old_latitude = self . points [ i ] . latitude #NEWLINE# new_latitude = ( ( ( SMOOTHING_RATIO [ 0 ] * latitudes [ ( i - 1 ) ] ) + ( SMOOTHING_RATIO [ 1 ] * latitudes [ i ] ) ) + ( SMOOTHING_RATIO [ 2 ] * latitudes [ ( i + 1 ) ] ) ) #NEWLINE# old_longitude = self . points [ i ] . longitude #NEWLINE# new_longitude = ( ( ( SMOOTHING_RATIO [ 0 ] * longitudes [ ( i - 1 ) ] ) + ( SMOOTHING_RATIO [ 1 ] * longitudes [ i ] ) ) + ( SMOOTHING_RATIO [ 2 ] * longitudes [ ( i + 1 ) ] ) ) #NEWLINE# if ( not remove_extremes ) : #NEWLINE# #INDENT# self . points [ i ] . latitude = new_latitude #NEWLINE# self . points [ i ] . longitude = new_longitude #NEWLINE# #UNINDENT# d1 = mod_geo . distance ( latitudes [ ( i - 1 ) ] , longitudes [ ( i - 1 ) ] , None , latitudes [ i ] , longitudes [ i ] , None ) #NEWLINE# d2 = mod_geo . distance ( latitudes [ ( i + 1 ) ] , longitudes [ ( i + 1 ) ] , None , latitudes [ i ] , longitudes [ i ] , None ) #NEWLINE# d = mod_geo . distance ( latitudes [ ( i - 1 ) ] , longitudes [ ( i - 1 ) ] , None , latitudes [ ( i + 1 ) ] , longitudes [ ( i + 1 ) ] , None ) #NEWLINE# if ( ( ( d1 + d2 ) > ( d * 1.5 ) ) and remove_extremes ) : #NEWLINE# #INDENT# d = mod_geo . distance ( old_latitude , old_longitude , None , new_latitude , new_longitude , None ) #NEWLINE# if ( d < remove_2d_extremes_threshold ) : #NEWLINE# #INDENT# new_point = self . points [ i ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# point_removed = True #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# new_point = self . points [ i ] #NEWLINE# #UNINDENT# #UNINDENT# if ( new_point and ( not point_removed ) ) : #NEWLINE# #INDENT# new_track_points . append ( new_point ) #NEWLINE# #UNINDENT# #UNINDENT# new_track_points . append ( self . points [ ( - 1 ) ] ) #NEWLINE# self . points = new_track_points