|
| 1 | +import argparse |
1 | 2 | import glob |
2 | 3 | import os |
3 | 4 |
|
|
8 | 9 | from geopy.geocoders import Nominatim |
9 | 10 |
|
10 | 11 | geolocator = Nominatim(user_agent="heatmap_app") |
11 | | -location = geolocator.geocode("Montreal Quebec") |
12 | | -lat_check = float(location.raw["lat"]) |
13 | | -lon_check = float(location.raw["lon"]) |
14 | 12 |
|
15 | | -data = glob.glob("*.gpx") |
16 | | -fitdata = glob.glob("*.fit") |
17 | 13 |
|
18 | | -if len(fitdata) != 0: |
19 | | - print("Converting Garmin FIT files") |
20 | | - os.system("python fit_to_csv.py") |
21 | | - os.system("mkdir fit_files") |
22 | | - os.system("mv *.fit ./fit_files") |
| 14 | +def parse_args() -> argparse.Namespace: |
| 15 | + args = argparse.ArgumentParser() |
| 16 | + args.add_argument( |
| 17 | + "--dir", |
| 18 | + help="Path to direcotry with .fit, .gpx files to process for the heatmap", |
| 19 | + default=os.getcwd(), |
| 20 | + ) |
| 21 | + args.add_argument( |
| 22 | + "--output_path", |
| 23 | + help="Path to write the heatmap .html to", |
| 24 | + default="heatmap.html", |
| 25 | + ) |
23 | 26 |
|
24 | | -csvdata = glob.glob("*.csv") |
| 27 | + return args.parse_args() |
25 | 28 |
|
26 | | -lat = [] |
27 | | -lon = [] |
28 | 29 |
|
29 | | -all_lat = [] |
30 | | -all_long = [] |
| 30 | +def main(): |
| 31 | + args = parse_args() |
| 32 | + location = geolocator.geocode("Montreal Quebec") |
| 33 | + lat_check = float(location.raw["lat"]) |
| 34 | + lon_check = float(location.raw["lon"]) |
31 | 35 |
|
32 | | -print("Loading data") |
| 36 | + gpx_files = glob.glob(args.dir + "/*.gpx") |
| 37 | + fit_files = glob.glob(args.dir + "/*.fit") |
33 | 38 |
|
34 | | -for activity in data: |
35 | | - gpx_filename = activity |
36 | | - with open(gpx_filename, "r") as gpx_file: |
37 | | - gpx = gpxpy.parse(gpx_file) |
| 39 | + if len(fit_files) != 0: |
| 40 | + print("Converting Garmin FIT files") |
| 41 | + os.system("python fit_to_csv.py") |
| 42 | + os.system("mkdir fit_files") |
| 43 | + os.system("mv *.fit ./fit_files") |
38 | 44 |
|
39 | | - for track in gpx.tracks: |
40 | | - for segment in track.segments: |
41 | | - for point in segment.points: |
42 | | - lat.append(point.latitude) |
43 | | - lon.append(point.longitude) |
| 45 | + csvdata = glob.glob("*.csv") |
44 | 46 |
|
45 | | - check1 = np.any(np.isclose(lat, lat_check, atol=0.5)) |
46 | | - check2 = np.any(np.isclose(lon, lon_check, atol=0.5)) |
| 47 | + lat = [] |
| 48 | + lon = [] |
47 | 49 |
|
48 | | - if check1 and check2: |
49 | | - all_lat.append(lat) |
50 | | - all_long.append(lon) |
| 50 | + all_lat = [] |
| 51 | + all_long = [] |
51 | 52 |
|
52 | | - lon = [] |
53 | | - lat = [] |
| 53 | + print("Loading data") |
54 | 54 |
|
55 | | -for activity in csvdata: |
56 | | - csv_filename = activity |
57 | | - csv_file = pd.read_csv(csv_filename) |
| 55 | + for activity in gpx_files: |
| 56 | + gpx_filename = activity |
| 57 | + with open(gpx_filename, "r") as gpx_file: |
| 58 | + gpx = gpxpy.parse(gpx_file) |
58 | 59 |
|
59 | | - for i in range(len(csv_file)): |
60 | | - lat.append(csv_file["position_lat"][i]) |
61 | | - lon.append(csv_file["position_long"][i]) |
| 60 | + for track in gpx.tracks: |
| 61 | + for segment in track.segments: |
| 62 | + for point in segment.points: |
| 63 | + lat.append(point.latitude) |
| 64 | + lon.append(point.longitude) |
62 | 65 |
|
63 | | - check1 = np.any(np.isclose(lat, lat_check, atol=0.5)) |
64 | | - check2 = np.any(np.isclose(lon, lon_check, atol=0.5)) |
| 66 | + check1 = np.any(np.isclose(lat, lat_check, atol=0.5)) |
| 67 | + check2 = np.any(np.isclose(lon, lon_check, atol=0.5)) |
65 | 68 |
|
66 | | - if check1 and check2: |
67 | | - all_lat.append(lat) |
68 | | - all_long.append(lon) |
| 69 | + if check1 and check2: |
| 70 | + all_lat.append(lat) |
| 71 | + all_long.append(lon) |
69 | 72 |
|
70 | | - lon = [] |
71 | | - lat = [] |
| 73 | + lon = [] |
| 74 | + lat = [] |
72 | 75 |
|
73 | | -if not all_lat or not all_long: |
74 | | - raise ValueError( |
75 | | - "No activities found within the specified location. Check lat_check/lon_check." |
76 | | - ) |
| 76 | + for activity in csvdata: |
| 77 | + csv_filename = activity |
| 78 | + csv_file = pd.read_csv(csv_filename) |
77 | 79 |
|
78 | | -all_lat = all_lat[0] |
79 | | -all_long = all_long[0] |
| 80 | + for i in range(len(csv_file)): |
| 81 | + lat.append(csv_file["position_lat"][i]) |
| 82 | + lon.append(csv_file["position_long"][i]) |
80 | 83 |
|
81 | | -central_long = sum(all_long) / len(all_long) |
82 | | -central_lat = sum(all_lat) / len(all_lat) |
| 84 | + check1 = np.any(np.isclose(lat, lat_check, atol=0.5)) |
| 85 | + check2 = np.any(np.isclose(lon, lon_check, atol=0.5)) |
83 | 86 |
|
84 | | -print("Initializing map") |
85 | | -m = folium.Map( |
86 | | - location=[central_lat, central_long], tiles="Stamen Toner", zoom_start=14.2 |
87 | | -) |
| 87 | + if check1 and check2: |
| 88 | + all_lat.append(lat) |
| 89 | + all_long.append(lon) |
88 | 90 |
|
89 | | -print("Plotting gpx data") |
| 91 | + lon = [] |
| 92 | + lat = [] |
90 | 93 |
|
91 | | -for activity in data: |
92 | | - gpx_filename = activity |
93 | | - with open(gpx_filename, "r") as gpx_file: |
94 | | - gpx = gpxpy.parse(gpx_file) |
| 94 | + if not all_lat or not all_long: |
| 95 | + raise ValueError( |
| 96 | + "No activities found within the specified location. Check lat_check/lon_check." |
| 97 | + ) |
95 | 98 |
|
96 | | - for track in gpx.tracks: |
97 | | - for segment in track.segments: |
98 | | - for point in segment.points: |
99 | | - lat.append(point.latitude) |
100 | | - lon.append(point.longitude) |
| 99 | + all_lat = all_lat[0] |
| 100 | + all_long = all_long[0] |
101 | 101 |
|
102 | | - points = list(zip(lat, lon)) |
| 102 | + central_long = sum(all_long) / len(all_long) |
| 103 | + central_lat = sum(all_lat) / len(all_lat) |
103 | 104 |
|
104 | | - folium.PolyLine(points, color="red", weight=2.5, opacity=0.5).add_to(m) |
105 | | - lat = [] |
106 | | - lon = [] |
| 105 | + print("Initializing map") |
| 106 | + m = folium.Map( |
| 107 | + location=[central_lat, central_long], tiles="Stamen Toner", zoom_start=14.2 |
| 108 | + ) |
107 | 109 |
|
108 | | -print("Plotting csv data") |
109 | | -color = "red" |
110 | | -hr = [] |
111 | | -for activity in csvdata: |
112 | | - csv_filename = activity |
113 | | - csv_file = pd.read_csv(csv_filename) |
114 | | - for i in range(len(csv_file)): |
115 | | - lat.append(csv_file["position_lat"][i]) |
116 | | - lon.append(csv_file["position_long"][i]) |
117 | | - if "heart_rate" in csv_file.columns: |
118 | | - hr.append(csv_file["heart_rate"][i]) |
119 | | - points = list(zip(lat, lon)) |
120 | | - |
121 | | - folium.PolyLine(points, color=color, weight=2.5, opacity=0.5).add_to(m) |
122 | | - lat = [] |
123 | | - lon = [] |
124 | | - hr = [] |
| 110 | + print("Plotting gpx data") |
125 | 111 |
|
126 | | -m.save("heatmap.html") |
| 112 | + for activity in gpx_files: |
| 113 | + gpx_filename = activity |
| 114 | + with open(gpx_filename, "r") as gpx_file: |
| 115 | + gpx = gpxpy.parse(gpx_file) |
| 116 | + |
| 117 | + for track in gpx.tracks: |
| 118 | + for segment in track.segments: |
| 119 | + for point in segment.points: |
| 120 | + lat.append(point.latitude) |
| 121 | + lon.append(point.longitude) |
| 122 | + |
| 123 | + points = list(zip(lat, lon)) |
| 124 | + |
| 125 | + folium.PolyLine(points, color="red", weight=2.5, opacity=0.5).add_to(m) |
| 126 | + lat = [] |
| 127 | + lon = [] |
| 128 | + |
| 129 | + print("Plotting csv data") |
| 130 | + color = "red" |
| 131 | + hr = [] |
| 132 | + for activity in csvdata: |
| 133 | + csv_filename = activity |
| 134 | + csv_file = pd.read_csv(csv_filename) |
| 135 | + for i in range(len(csv_file)): |
| 136 | + lat.append(csv_file["position_lat"][i]) |
| 137 | + lon.append(csv_file["position_long"][i]) |
| 138 | + if "heart_rate" in csv_file.columns: |
| 139 | + hr.append(csv_file["heart_rate"][i]) |
| 140 | + points = list(zip(lat, lon)) |
| 141 | + |
| 142 | + folium.PolyLine(points, color=color, weight=2.5, opacity=0.5).add_to(m) |
| 143 | + lat = [] |
| 144 | + lon = [] |
| 145 | + hr = [] |
| 146 | + |
| 147 | + m.save("heatmap.html") |
| 148 | + |
| 149 | + |
| 150 | +if __name__ == "__main__": |
| 151 | + main() |
0 commit comments