Vertdure commited on
Commit
dfdc945
1 Parent(s): ce202c8

Update pages/12_🌲_VertXtractor.py

Browse files
Files changed (1) hide show
  1. pages/12_🌲_VertXtractor.py +20 -23
pages/12_🌲_VertXtractor.py CHANGED
@@ -1,6 +1,6 @@
1
  import streamlit as st
2
  import folium
3
- from streamlit_folium import folium_static
4
  from folium.plugins import Draw
5
  import geopandas as gpd
6
  import tempfile
@@ -351,40 +351,37 @@ draw = Draw(
351
  )
352
  draw.add_to(m)
353
 
354
- # Display the map
355
- folium_static(m)
356
 
357
  # Initialize session state for bbox
358
  if 'bbox' not in st.session_state:
359
- st.session_state.bbox = None
360
-
361
- # Get bbox from drawn rectangle
362
- draw_data = st.session_state.get("json_data")
363
- if draw_data and "features" in draw_data:
364
- feature = draw_data["features"][0]
365
- if feature["geometry"]["type"] == "Polygon":
366
- coords = feature["geometry"]["coordinates"][0]
367
- st.session_state.bbox = [
368
- min(coord[0] for coord in coords),
369
- min(coord[1] for coord in coords),
370
- max(coord[0] for coord in coords),
371
- max(coord[1] for coord in coords)
372
- ]
373
 
374
  # Display and allow editing of bounding box coordinates
375
  st.subheader("Enter Bounding Box Coordinates")
376
  col1, col2, col3, col4 = st.columns(4)
377
  with col1:
378
- xmin = st.number_input("Min Longitude", value=st.session_state.bbox[0] if st.session_state.bbox else 6.0, format="%.4f")
379
  with col2:
380
- ymin = st.number_input("Min Latitude", value=st.session_state.bbox[1] if st.session_state.bbox else 46.0, format="%.4f")
381
  with col3:
382
- xmax = st.number_input("Max Longitude", value=st.session_state.bbox[2] if st.session_state.bbox else 10.0, format="%.4f")
383
  with col4:
384
- ymax = st.number_input("Max Latitude", value=st.session_state.bbox[3] if st.session_state.bbox else 47.0, format="%.4f")
385
 
386
- if st.button("Set Bounding Box"):
387
- st.session_state.bbox = [xmin, ymin, xmax, ymax]
388
 
389
  if st.session_state.bbox:
390
  st.write(f"Selected bounding box (WGS84): {st.session_state.bbox}")
 
1
  import streamlit as st
2
  import folium
3
+ from streamlit_folium import st_folium
4
  from folium.plugins import Draw
5
  import geopandas as gpd
6
  import tempfile
 
351
  )
352
  draw.add_to(m)
353
 
354
+ # Use st_folium to render the map and get the drawn bbox
355
+ output = st_folium(m, width=700, height=500)
356
 
357
  # Initialize session state for bbox
358
  if 'bbox' not in st.session_state:
359
+ st.session_state.bbox = [6.0, 46.0, 10.0, 47.0] # Default values for Switzerland
360
+
361
+ # Update bbox if a new one is drawn
362
+ if output['last_active_drawing']:
363
+ coordinates = output['last_active_drawing']['geometry']['coordinates'][0]
364
+ st.session_state.bbox = [
365
+ min(coord[0] for coord in coordinates),
366
+ min(coord[1] for coord in coordinates),
367
+ max(coord[0] for coord in coordinates),
368
+ max(coord[1] for coord in coordinates)
369
+ ]
 
 
 
370
 
371
  # Display and allow editing of bounding box coordinates
372
  st.subheader("Enter Bounding Box Coordinates")
373
  col1, col2, col3, col4 = st.columns(4)
374
  with col1:
375
+ xmin = st.number_input("Min Longitude", value=st.session_state.bbox[0], format="%.4f", key="xmin")
376
  with col2:
377
+ ymin = st.number_input("Min Latitude", value=st.session_state.bbox[1], format="%.4f", key="ymin")
378
  with col3:
379
+ xmax = st.number_input("Max Longitude", value=st.session_state.bbox[2], format="%.4f", key="xmax")
380
  with col4:
381
+ ymax = st.number_input("Max Latitude", value=st.session_state.bbox[3], format="%.4f", key="ymax")
382
 
383
+ # Update session state if coordinates are manually changed
384
+ st.session_state.bbox = [xmin, ymin, xmax, ymax]
385
 
386
  if st.session_state.bbox:
387
  st.write(f"Selected bounding box (WGS84): {st.session_state.bbox}")