-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpersonal_heatmap.py
More file actions
98 lines (73 loc) · 2.37 KB
/
Copy pathpersonal_heatmap.py
File metadata and controls
98 lines (73 loc) · 2.37 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import argparse
import glob
import os
from zoneinfo import ZoneInfo
import folium
import gpxpy
import numpy as np
import pandas as pd
from fit_to_csv import collect_data
def parse_args() -> argparse.Namespace:
args = argparse.ArgumentParser()
args.add_argument(
"--dir",
help="Path to direcotry with .fit, .gpx files to process for the heatmap",
default=os.getcwd(),
)
args.add_argument(
"--timezone",
help="Timezone for timestamps, e.g. 'US/Pacific'",
default="US/Pacific",
)
args.add_argument(
"--output_path",
help="Path to write the heatmap .html to",
default="heatmap.html",
)
return args.parse_args()
def main():
args = parse_args()
gpx_files = glob.glob(args.dir + "/*.gpx")
fit_files = glob.glob(args.dir + "/*.fit")
fit_data = []
if len(fit_files):
print("Converting Garmin FIT files")
for file in fit_files:
activity_data = collect_data(file, tz=ZoneInfo(args.timezone))
df_activity_data = pd.DataFrame(activity_data)
fit_data.append(df_activity_data)
all_lat = []
all_long = []
print("Loading data")
for activity in gpx_files:
lon = []
lat = []
with open(activity, "r") as gpx_file:
gpx = gpxpy.parse(gpx_file)
for track in gpx.tracks:
for segment in track.segments:
for point in segment.points:
lat.append(point.latitude)
lon.append(point.longitude)
all_lat.append(lat)
all_long.append(lon)
for activity in fit_data:
lat = activity["position_lat"].values.tolist()
lon = activity["position_long"].values.tolist()
all_lat.append(lat)
all_long.append(lon)
central_long = np.mean(np.array(all_long).flatten())
central_lat = np.mean(np.array(all_lat).flatten())
print("Initializing map")
m = folium.Map(
location=[central_lat, central_long], tiles="Cartodb Positron", zoom_start=14.2
)
print("Plotting activities")
for i in range(len(all_lat)):
lat = all_lat[i]
lon = all_long[i]
points = list(zip(lat, lon))
folium.PolyLine(points, color="red", weight=2.5, opacity=0.5).add_to(m)
m.save(args.output_path)
if __name__ == "__main__":
main()