Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -60,7 +60,7 @@ def get_elevation_from_srtm(lat, lon):
|
|
| 60 |
if -56 <= lat <= 60:
|
| 61 |
try:
|
| 62 |
alt = elevation_data.get_elevation(lat, lon)
|
| 63 |
-
if alt and alt > 0:
|
| 64 |
return alt
|
| 65 |
except Exception:
|
| 66 |
pass
|
|
@@ -181,7 +181,7 @@ def get_weather_for_peaks_iteratively(df_peaks, min_snow_cm, max_results=20, max
|
|
| 181 |
})
|
| 182 |
|
| 183 |
except Exception as e:
|
| 184 |
-
print(f"
|
| 185 |
|
| 186 |
requests_made += 1
|
| 187 |
|
|
@@ -217,66 +217,66 @@ def format_weather_data(df):
|
|
| 217 |
|
| 218 |
def find_snowy_peaks(min_snow_cm, radius_km, lat, lon):
|
| 219 |
if lat is None or lon is None:
|
| 220 |
-
|
|
|
|
|
|
|
| 221 |
|
| 222 |
df_peaks = get_peaks_from_overpass(lat, lon, radius_km)
|
| 223 |
if df_peaks.empty:
|
| 224 |
-
|
|
|
|
|
|
|
| 225 |
|
| 226 |
df_weather = get_weather_for_peaks_iteratively(df_peaks, min_snow_cm)
|
| 227 |
if df_weather.empty:
|
| 228 |
-
|
|
|
|
|
|
|
| 229 |
|
| 230 |
df_final = format_weather_data(df_weather)
|
| 231 |
-
|
|
|
|
|
|
|
| 232 |
|
| 233 |
|
| 234 |
-
# --- MAP HELPERS ---
|
|
|
|
|
|
|
| 235 |
|
| 236 |
-
def create_map_with_center(lat, lon):
|
| 237 |
-
fig = go.Figure(go.Scattermap(lat=[lat], lon=[lon], mode="markers"))
|
| 238 |
-
fig.update_layout(map=dict(style="open-street-map", center={"lat": lat, "lon": lon}, zoom=8))
|
| 239 |
-
return fig
|
| 240 |
|
| 241 |
-
|
| 242 |
-
def create_map_with_results(lat, lon, df):
|
| 243 |
-
fig = go.Figure()
|
| 244 |
-
fig.add_trace(go.Scattermap(
|
| 245 |
-
lat=df["latitude"],
|
| 246 |
-
lon=df["longitude"],
|
| 247 |
-
mode="markers",
|
| 248 |
-
marker=dict(size=12, color="blue"),
|
| 249 |
-
customdata=df[["name", "altitude", "distance_km", "snow_depth_cm", "weather_desc", "temp_c_str"]],
|
| 250 |
-
hovertemplate=(
|
| 251 |
-
"<b>%{customdata[0]}</b><br>"
|
| 252 |
-
"Altitude: %{customdata[1]} m<br>"
|
| 253 |
-
"Distance: %{customdata[2]} km<br>"
|
| 254 |
-
"βοΈ Snow: %{customdata[3]} cm<br>"
|
| 255 |
-
"Weather: %{customdata[4]}<br>"
|
| 256 |
-
"π‘ %{customdata[5]}<extra></extra>"
|
| 257 |
-
),
|
| 258 |
-
))
|
| 259 |
-
fig.update_layout(map=dict(style="open-street-map", center={"lat": lat, "lon": lon}, zoom=9))
|
| 260 |
-
return fig
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
# --- GRADIO UI ---
|
| 264 |
|
| 265 |
with gr.Blocks(theme=gr.themes.Soft(), title="Snow Finder") as demo:
|
| 266 |
gr.Markdown("# βοΈ Snow Finder for Families")
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 275 |
|
| 276 |
search_button.click(
|
| 277 |
fn=find_snowy_peaks,
|
| 278 |
inputs=[snow_slider, radius_slider, lat_input, lon_input],
|
| 279 |
-
outputs=[map_plot,
|
| 280 |
)
|
| 281 |
|
| 282 |
if __name__ == "__main__":
|
|
|
|
| 60 |
if -56 <= lat <= 60:
|
| 61 |
try:
|
| 62 |
alt = elevation_data.get_elevation(lat, lon)
|
| 63 |
+
if alt is not None and alt > 0:
|
| 64 |
return alt
|
| 65 |
except Exception:
|
| 66 |
pass
|
|
|
|
| 181 |
})
|
| 182 |
|
| 183 |
except Exception as e:
|
| 184 |
+
print(f"Error fetching weather for {row['name']}: {e}")
|
| 185 |
|
| 186 |
requests_made += 1
|
| 187 |
|
|
|
|
| 217 |
|
| 218 |
def find_snowy_peaks(min_snow_cm, radius_km, lat, lon):
|
| 219 |
if lat is None or lon is None:
|
| 220 |
+
fig = create_map_with_center(DEFAULT_LAT, DEFAULT_LON)
|
| 221 |
+
fig.update_layout(title_text="Enter valid coordinates.")
|
| 222 |
+
return fig, "Please enter coordinates."
|
| 223 |
|
| 224 |
df_peaks = get_peaks_from_overpass(lat, lon, radius_km)
|
| 225 |
if df_peaks.empty:
|
| 226 |
+
fig = create_map_with_center(lat, lon)
|
| 227 |
+
fig.update_layout(title_text=f"No peaks found within {radius_km} km.")
|
| 228 |
+
return fig, f"No peaks found within {radius_km} km."
|
| 229 |
|
| 230 |
df_weather = get_weather_for_peaks_iteratively(df_peaks, min_snow_cm)
|
| 231 |
if df_weather.empty:
|
| 232 |
+
fig = create_map_with_center(lat, lon)
|
| 233 |
+
fig.update_layout(title_text=f"No snowy peaks β₯ {min_snow_cm} cm.")
|
| 234 |
+
return fig, f"No peaks met the β₯ {min_snow_cm} cm snow requirement."
|
| 235 |
|
| 236 |
df_final = format_weather_data(df_weather)
|
| 237 |
+
fig = create_map_with_results(lat, lon, df_final)
|
| 238 |
+
fig.update_layout(title_text=f"Found {len(df_final)} snowy peaks!")
|
| 239 |
+
return fig, f"π Showing {len(df_final)} snowy peaks with β₯ {min_snow_cm} cm of snow."
|
| 240 |
|
| 241 |
|
| 242 |
+
# --- MAP HELPERS (UNCHANGED) ---
|
| 243 |
+
# create_empty_map, create_map_with_center, create_map_with_results
|
| 244 |
+
# [identical to your original script]
|
| 245 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 246 |
|
| 247 |
+
# --- GRADIO UI (UNCHANGED) ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
|
| 249 |
with gr.Blocks(theme=gr.themes.Soft(), title="Snow Finder") as demo:
|
| 250 |
gr.Markdown("# βοΈ Snow Finder for Families")
|
| 251 |
+
gr.Markdown("Find nearby snowy peaks perfect for sledding and snowmen!")
|
| 252 |
+
|
| 253 |
+
with gr.Row():
|
| 254 |
+
with gr.Column(scale=1):
|
| 255 |
+
location_search = gr.Textbox(label="Search Location")
|
| 256 |
+
search_location_btn = gr.Button("π Find Location")
|
| 257 |
+
|
| 258 |
+
lat_input = gr.Number(value=DEFAULT_LAT, label="Latitude", precision=4)
|
| 259 |
+
lon_input = gr.Number(value=DEFAULT_LON, label="Longitude", precision=4)
|
| 260 |
+
snow_slider = gr.Radio(choices=[1, 2, 3, 4, 5, 6], value=1, label="Min Snow (cm)")
|
| 261 |
+
radius_slider = gr.Radio(choices=[10, 20, 30, 40, 50, 60], value=30, label="Radius (km)")
|
| 262 |
+
search_button = gr.Button("βοΈ Find Snow!", variant="primary")
|
| 263 |
+
status_output = gr.Textbox(lines=4, interactive=False)
|
| 264 |
+
|
| 265 |
+
with gr.Column(scale=2):
|
| 266 |
+
init_fig = create_map_with_center(DEFAULT_LAT, DEFAULT_LON)
|
| 267 |
+
init_fig.update_layout(title_text="Luxembourg City β Click 'Find Snow!' to start")
|
| 268 |
+
map_plot = gr.Plot(init_fig, label="Map")
|
| 269 |
+
|
| 270 |
+
search_location_btn.click(
|
| 271 |
+
fn=geocode_location,
|
| 272 |
+
inputs=[location_search],
|
| 273 |
+
outputs=[lat_input, lon_input, status_output],
|
| 274 |
+
)
|
| 275 |
|
| 276 |
search_button.click(
|
| 277 |
fn=find_snowy_peaks,
|
| 278 |
inputs=[snow_slider, radius_slider, lat_input, lon_input],
|
| 279 |
+
outputs=[map_plot, status_output],
|
| 280 |
)
|
| 281 |
|
| 282 |
if __name__ == "__main__":
|