-
Notifications
You must be signed in to change notification settings - Fork 1
/
personal_heatmap.py
148 lines (117 loc) · 3.88 KB
/
personal_heatmap.py
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import os
import glob
import folium
import gpxpy
import numpy as np
import pandas as pd
from geopy.geocoders import Nominatim
geolocator = Nominatim()
location = geolocator.geocode(
"Montreal Quebec"
) # Change this to change location centering
lat_check = float(location.raw["lat"])
lon_check = float(location.raw["lon"])
data = glob.glob("*.gpx")
fitdata = glob.glob("*.fit")
if not len(fitdata) == 0:
print("Converting Garmin FIT files")
os.system("python fit_to_csv.py")
os.system("mkdir fit_files")
os.system("mv *.fit ./fit_files")
csvdata = glob.glob("*.csv")
lat = []
lon = []
all_lat = []
all_long = []
print("Loading data")
for activity in data:
gpx_filename = activity
gpx_file = open(gpx_filename, "r")
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)
check1 = np.any(
np.isclose(lat, lat_check, atol=0.5)
) # Change the tolerance 'atol' to include a larger or smaller area around the centering point
check2 = np.any(
np.isclose(lon, lon_check, atol=0.5)
) # Change the tolerance 'atol' to include a larger or smaller area around the centering point
if check1 and check2:
all_lat.append(lat)
all_long.append(lon)
lon = []
lat = []
for activity in csvdata:
csv_filename = activity
csv_file = pd.read_csv(csv_filename)
for i in range(len(csv_file)):
lat.append(csv_file["position_lat"][i])
lon.append(csv_file["position_long"][i])
check1 = np.any(
np.isclose(lat, lat_check, atol=0.5)
) # Change the tolerance 'atol' to include a larger or smaller area around the centering point
check2 = np.any(
np.isclose(lon, lon_check, atol=0.5)
) # Change the tolerance 'atol' to include a larger or smaller area around the centering point
if check1 and check2:
all_lat.append(lat)
all_long.append(lon)
lon = []
lat = []
all_lat = all_lat[0]
all_long = all_long[0]
central_long = sum(all_long) / float(len(all_long))
central_lat = sum(all_lat) / float(len(all_lat))
print("Initializing map")
m = folium.Map(
location=[central_lat, central_long], tiles="Stamen Toner", zoom_start=14.2
) # Recommended map styles are "Stamen Terrain", "Stamen Toner"
print("Plotting gpx data")
for activity in data:
gpx_filename = activity
gpx_file = open(gpx_filename, "r")
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)
points = zip(lat, lon)
points = [item for item in zip(lat, lon)]
folium.PolyLine(points, color="red", weight=2.5, opacity=0.5).add_to(m)
lat = []
lon = []
print("Plotting csv data")
color = "red"
hr = []
for activity in csvdata:
csv_filename = activity
csv_file = pd.read_csv(csv_filename)
for i in range(len(csv_file)):
lat.append(csv_file["position_lat"][i])
lon.append(csv_file["position_long"][i])
hr.append(csv_file["heart_rate"][i])
points = zip(lat, lon)
points = [item for item in zip(lat, lon)]
# color = []
# print('heart_rate',csv_file['heart_rate'])
# hr = hr / max(hr)
# for value in hr:
# if value < 0.2:
# color.append("darkred")
# elif value >= 0.2 and value < 0.4:
# color.append("red")
# elif value >= 0.4 and value < 0.6:
# color.append("lightred")
# elif value >= 0.6 and value < 0.8:
# color.append("lightyellow")
# elif value >= 0.6:
# color.append("yellow")
folium.PolyLine(points, color=color, weight=2.5, opacity=0.5).add_to(m)
lat = []
lon = []
hr = []
m.save("heatmap.html")