77import gpxpy
88import numpy as np
99import pandas as pd
10- from geopy .geocoders import Nominatim
1110
1211from fit_to_csv import collect_data
1312
14- geolocator = Nominatim (user_agent = "heatmap_app" )
15-
1613
1714def parse_args () -> argparse .Namespace :
1815 args = argparse .ArgumentParser ()
@@ -37,9 +34,6 @@ def parse_args() -> argparse.Namespace:
3734
3835def main ():
3936 args = parse_args ()
40- location = geolocator .geocode ("Montreal Quebec" )
41- lat_check = float (location .raw ["lat" ])
42- lon_check = float (location .raw ["lon" ])
4337
4438 gpx_files = glob .glob (args .dir + "/*.gpx" )
4539 fit_files = glob .glob (args .dir + "/*.fit" )
@@ -52,17 +46,16 @@ def main():
5246 df_activity_data = pd .DataFrame (activity_data )
5347 fit_data .append (df_activity_data )
5448
55- lat = []
56- lon = []
57-
5849 all_lat = []
5950 all_long = []
6051
6152 print ("Loading data" )
6253
6354 for activity in gpx_files :
64- gpx_filename = activity
65- with open (gpx_filename , "r" ) as gpx_file :
55+ lon = []
56+ lat = []
57+
58+ with open (activity , "r" ) as gpx_file :
6659 gpx = gpxpy .parse (gpx_file )
6760
6861 for track in gpx .tracks :
@@ -71,82 +64,36 @@ def main():
7164 lat .append (point .latitude )
7265 lon .append (point .longitude )
7366
74- check1 = np .any (np .isclose (lat , lat_check , atol = 0.5 ))
75- check2 = np .any (np .isclose (lon , lon_check , atol = 0.5 ))
76-
77- if check1 and check2 :
78- all_lat .append (lat )
79- all_long .append (lon )
67+ all_lat .append (lat )
68+ all_long .append (lon )
8069
70+ for activity in fit_data :
8171 lon = []
8272 lat = []
83-
84- for activity in fit_data :
8573 for i in range (len (activity )):
8674 # TODO: use extend and .values.to_list() and avoid the loop entirely
87- lat .append (activity ["position_lat" ][i ])
88- lon .append (activity ["position_long" ][i ])
75+ lat .append (float ( activity ["position_lat" ][i ]) )
76+ lon .append (float ( activity ["position_long" ][i ]) )
8977
90- check1 = np . any ( np . isclose ( lat , lat_check , atol = 0.5 ) )
91- check2 = np . any ( np . isclose ( lon , lon_check , atol = 0.5 ) )
78+ all_lat . append ( lat )
79+ all_long . append ( lon )
9280
93- if check1 and check2 :
94- all_lat .append (lat )
95- all_long .append (lon )
96-
97- lon = []
98- lat = []
99-
100- if not all_lat or not all_long :
101- raise ValueError (
102- "No activities found within the specified location. Check lat_check/lon_check."
103- )
104-
105- all_lat = all_lat [0 ]
106- all_long = all_long [0 ]
107-
108- central_long = sum (all_long ) / len (all_long )
109- central_lat = sum (all_lat ) / len (all_lat )
81+ central_long = np .mean (np .array (all_long ).flatten ())
82+ central_lat = np .mean (np .array (all_lat ).flatten ())
11083
11184 print ("Initializing map" )
11285 m = folium .Map (
113- location = [central_lat , central_long ], tiles = "Stamen Toner " , zoom_start = 14.2
86+ location = [central_lat , central_long ], tiles = "Cartodb Positron " , zoom_start = 14.2
11487 )
11588
116- print ("Plotting gpx data" )
117-
118- for activity in gpx_files :
119- gpx_filename = activity
120- with open (gpx_filename , "r" ) as gpx_file :
121- gpx = gpxpy .parse (gpx_file )
122-
123- for track in gpx .tracks :
124- for segment in track .segments :
125- for point in segment .points :
126- lat .append (point .latitude )
127- lon .append (point .longitude )
89+ print ("Plotting activities" )
12890
91+ for i in range (len (all_lat )):
92+ lat = all_lat [i ]
93+ lon = all_long [i ]
12994 points = list (zip (lat , lon ))
13095
13196 folium .PolyLine (points , color = "red" , weight = 2.5 , opacity = 0.5 ).add_to (m )
132- lat = []
133- lon = []
134-
135- print ("Plotting csv data" )
136- color = "red"
137- hr = []
138- for activity in fit_data :
139- for i in range (len (activity )):
140- lat .append (activity ["position_lat" ][i ])
141- lon .append (activity ["position_long" ][i ])
142- if "heart_rate" in activity .columns :
143- hr .append (activity ["heart_rate" ][i ])
144- points = list (zip (lat , lon ))
145-
146- folium .PolyLine (points , color = color , weight = 2.5 , opacity = 0.5 ).add_to (m )
147- lat = []
148- lon = []
149- hr = []
15097
15198 m .save (args .output_path )
15299
0 commit comments