tyang4 commited on
Commit
8254362
·
verified ·
1 Parent(s): 83088e1

Replace with st_folium

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +20 -51
src/streamlit_app.py CHANGED
@@ -611,59 +611,28 @@ if 'date' in df.columns:
611
  if 'latitude' in df.columns and 'longitude' in df.columns:
612
  st.markdown('🗺️ Observation Locations on Map')
613
  try:
614
- coords = (
615
- df[['latitude', 'longitude']]
616
- .apply(pd.to_numeric, errors='coerce')
617
- .dropna()
618
- .rename(columns={'latitude': 'lat', 'longitude': 'lon'})
619
- )
620
- coords = coords[
621
- (coords['lat'].between(-90, 90)) &
622
- (coords['lon'].between(-180, 180))
623
- ]
624
  if len(coords) == 0:
625
- st.warning('⚠️ No valid coordinates to plot on the map.')
626
  else:
627
- # ---------- ① 视图 ----------
628
- try:
629
- vs_tmp = pdk.data_utils.compute_view(coords[['lon', 'lat']])
630
- view_state = (
631
- pdk.ViewState(**vs_tmp, pitch=0, bearing=0)
632
- if isinstance(vs_tmp, dict) else vs_tmp
633
- )
634
- view_state.pitch = 0
635
- view_state.bearing = 0
636
- except Exception:
637
- view_state = pdk.ViewState(
638
- latitude=coords['lat'].mean(),
639
- longitude=coords['lon'].mean(),
640
- zoom=5,
641
- pitch=0,
642
- bearing=0,
643
- )
644
-
645
- # ---------- ② 高亮层 ----------
646
- layer = pdk.Layer(
647
- 'ScatterplotLayer',
648
- data=coords,
649
- get_position='[lon, lat]',
650
- get_radius=50000,
651
- get_fill_color=[0, 255, 0, 200],
652
- get_line_color=[255, 255, 255],
653
- line_width_units='pixels',
654
- get_line_width=2,
655
- pickable=True,
656
- auto_highlight=True,
657
- )
658
-
659
- # ---------- ③ 组合 Deck ----------
660
- deck = pdk.Deck(
661
- layers=[layer],
662
- initial_view_state=view_state,
663
- map_style='mapbox://styles/mapbox/light-v11',
664
- tooltip={'html': '<b>Lat:</b> {lat}<br/><b>Lon:</b> {lon}'},
665
- )
666
- st.pydeck_chart(deck)
667
  except Exception as e:
668
  st.warning(f'⚠️ Could not render map: {e}')
669
  """
 
611
  if 'latitude' in df.columns and 'longitude' in df.columns:
612
  st.markdown('🗺️ Observation Locations on Map')
613
  try:
614
+ coords = df[['latitude', 'longitude']].dropna()
615
+ coords = coords[(coords['latitude'].between(-90, 90)) & (coords['longitude'].between(-180, 180))]
616
+
 
 
 
 
 
 
 
617
  if len(coords) == 0:
618
+ raise Exception('⚠️ No valid coordinates to plot on the map.')
619
  else:
620
+ # 计算中心点
621
+ center = [coords['latitude'].mean(), coords['longitude'].mean()]
622
+ m = folium.Map(location=center, zoom_start=5)
623
+
624
+ # 添加散点
625
+ for _, row in coords.iterrows():
626
+ folium.CircleMarker(
627
+ location=[row['latitude'], row['longitude']],
628
+ radius=5,
629
+ color='green',
630
+ fill=True,
631
+ fill_color='green',
632
+ fill_opacity=0.7,
633
+ ).add_to(m)
634
+
635
+ st_folium(m, width=700, height=500)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
636
  except Exception as e:
637
  st.warning(f'⚠️ Could not render map: {e}')
638
  """