Spaces:
Running
Running
Upload app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,5 @@
|
|
|
|
|
|
1 |
import os
|
2 |
import json
|
3 |
import pathlib
|
@@ -21,6 +23,7 @@ import openmeteo_requests
|
|
21 |
import requests_cache
|
22 |
from retry_requests import retry
|
23 |
|
|
|
24 |
from geopy.geocoders import Nominatim
|
25 |
geolocator = Nominatim(user_agent='FreeLetzWeather')
|
26 |
|
@@ -67,6 +70,37 @@ gpx_path = pathlib.Path(gpx_file)
|
|
67 |
with open('weather_icons_custom.json', 'r') as file:
|
68 |
icons = json.load(file)
|
69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
def map_icons(df):
|
71 |
|
72 |
code = df['weather_code']
|
@@ -234,7 +268,9 @@ def coor_gpx(gpx):
|
|
234 |
'elevation': p.elevation,
|
235 |
})
|
236 |
df_gpx = pd.DataFrame.from_records(points)
|
237 |
-
gpx_dict = df_gpx.iloc[-1].to_dict()
|
|
|
|
|
238 |
params['latitude'] = gpx_dict['latitude']
|
239 |
params['longitude'] = gpx_dict['longitude']
|
240 |
params['elevation'] = gpx_dict['elevation']
|
@@ -242,7 +278,7 @@ def coor_gpx(gpx):
|
|
242 |
lon = params['longitude']
|
243 |
|
244 |
if params['elevation'] == None:
|
245 |
-
params['elevation'] = int(round(
|
246 |
else:
|
247 |
params['elevation'] = int(round(params['elevation'], 0))
|
248 |
|
@@ -267,7 +303,8 @@ def coor_gpx(gpx):
|
|
267 |
|
268 |
try:
|
269 |
parse_gpx(gpx)
|
270 |
-
except:
|
|
|
271 |
parse_gpx(gpx_path)
|
272 |
global gpx_name
|
273 |
gpx_name = '<b style="color: firebrick;">ERROR: Not a valid GPX file. Upload another file.</b>'
|
|
|
1 |
+
import traceback
|
2 |
+
|
3 |
import os
|
4 |
import json
|
5 |
import pathlib
|
|
|
23 |
import requests_cache
|
24 |
from retry_requests import retry
|
25 |
|
26 |
+
from geopy import distance
|
27 |
from geopy.geocoders import Nominatim
|
28 |
geolocator = Nominatim(user_agent='FreeLetzWeather')
|
29 |
|
|
|
70 |
with open('weather_icons_custom.json', 'r') as file:
|
71 |
icons = json.load(file)
|
72 |
|
73 |
+
def mid_point(df):
|
74 |
+
|
75 |
+
def add_ele(x):
|
76 |
+
return elevation_data.get_elevation(x['latitude'], x['longitude'], 0)
|
77 |
+
|
78 |
+
df['srtm'] = df.apply(lambda x: add_ele(x), axis=1)
|
79 |
+
|
80 |
+
# Distance estimation function
|
81 |
+
|
82 |
+
def eukarney(lat1, lon1, alt1, lat2, lon2, alt2):
|
83 |
+
p1 = (lat1, lon1)
|
84 |
+
p2 = (lat2, lon2)
|
85 |
+
karney = distance.distance(p1, p2).m
|
86 |
+
return np.sqrt(karney**2 + (alt2 - alt1)**2)
|
87 |
+
|
88 |
+
# Create shifted columns in order to facilitate distance calculation
|
89 |
+
|
90 |
+
df['lat_shift'] = df['latitude'].shift(periods=-1).fillna(df['latitude'])
|
91 |
+
df['lon_shift'] = df['longitude'].shift(periods=-1).fillna(df['longitude'])
|
92 |
+
df['alt_shift'] = df['srtm'].shift(periods=-1).fillna(df['srtm'])
|
93 |
+
|
94 |
+
# Apply the distance function to the dataframe
|
95 |
+
|
96 |
+
df['distances'] = df.apply(lambda x: eukarney(x['latitude'], x['longitude'], x['srtm'], x['lat_shift'], x['lon_shift'], x['alt_shift']), axis=1).fillna(0)
|
97 |
+
df['distance'] = df['distances'].cumsum().round(decimals = 0).astype(int)
|
98 |
+
|
99 |
+
gpx_dict = df.loc[df.distance==df.distance.median()].iloc[-1].to_dict()
|
100 |
+
print(gpx_dict)
|
101 |
+
|
102 |
+
return gpx_dict
|
103 |
+
|
104 |
def map_icons(df):
|
105 |
|
106 |
code = df['weather_code']
|
|
|
268 |
'elevation': p.elevation,
|
269 |
})
|
270 |
df_gpx = pd.DataFrame.from_records(points)
|
271 |
+
#gpx_dict = df_gpx.iloc[-1].to_dict()
|
272 |
+
gpx_dict = mid_point(df_gpx)
|
273 |
+
|
274 |
params['latitude'] = gpx_dict['latitude']
|
275 |
params['longitude'] = gpx_dict['longitude']
|
276 |
params['elevation'] = gpx_dict['elevation']
|
|
|
278 |
lon = params['longitude']
|
279 |
|
280 |
if params['elevation'] == None:
|
281 |
+
params['elevation'] = int(round(gpx_dict['srtm'], 0))
|
282 |
else:
|
283 |
params['elevation'] = int(round(params['elevation'], 0))
|
284 |
|
|
|
303 |
|
304 |
try:
|
305 |
parse_gpx(gpx)
|
306 |
+
except Exception as error:
|
307 |
+
traceback.print_exc()
|
308 |
parse_gpx(gpx_path)
|
309 |
global gpx_name
|
310 |
gpx_name = '<b style="color: firebrick;">ERROR: Not a valid GPX file. Upload another file.</b>'
|