MSU576 commited on
Commit
147b972
·
verified ·
1 Parent(s): c8a3ac7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -36
app.py CHANGED
@@ -1332,45 +1332,33 @@ def locator_page():
1332
  # Render the map inside Streamlit
1333
  m.to_streamlit(height=600, responsive=True)
1334
 
1335
- # ----------------------------
1336
- # ROI capture helper
1337
- # ----------------------------
1338
- def get_roi_from_map():
1339
- """Return ee.Geometry if ROI exists, otherwise None"""
1340
- roi = None
1341
 
1342
- # Case 1: User just drew something on the map
1343
- if m.draw_features:
1344
- try:
1345
- last_feature = m.draw_features[-1]
1346
- if isinstance(last_feature, dict):
1347
- geom = last_feature.get("geometry") or last_feature
1348
- roi = ee.Geometry(geom)
1349
- # Save permanently in session state
1350
- st.session_state["roi_geojson"] = json.dumps(last_feature)
1351
- except Exception as e:
1352
- st.warning(f"⚠️ Could not parse drawn ROI: {e}")
1353
- roi = None
1354
-
1355
- # Case 2: Nothing drawn this run, but we saved ROI earlier
1356
- elif "roi_geojson" in st.session_state:
1357
- try:
1358
- saved = json.loads(st.session_state["roi_geojson"])
1359
- geom = saved.get("geometry") if isinstance(saved, dict) else saved
1360
  roi = ee.Geometry(geom)
1361
- except Exception as e:
1362
- st.warning(f"⚠️ Failed to restore ROI: {e}")
1363
- roi = None
1364
-
1365
- return roi
1366
 
1367
- # Update ROI immediately (persistent between runs)
1368
- roi = get_roi_from_map()
1369
- if roi:
1370
- st.success("✅ ROI is available and ready for computations")
1371
- else:
1372
- st.info("✍️ Please draw a polygon/rectangle/circle before pressing **Compute Summaries**")
1373
-
 
 
 
1374
 
1375
  # --- Compute Summaries ---
1376
  if st.button("Compute Summaries"):
 
1332
  # Render the map inside Streamlit
1333
  m.to_streamlit(height=600, responsive=True)
1334
 
1335
+ # --- Render map and capture drawings ---
1336
+ # st_map() is the correct way to sync drawn features into Streamlit
1337
+ drawn_features = m.st_map(width=1200, height=600)
 
 
 
1338
 
1339
+ roi = None
1340
+ if drawn_features:
1341
+ try:
1342
+ # Get last drawn shape
1343
+ last_feature = drawn_features[-1]
1344
+ geom = last_feature.get("geometry")
1345
+ if geom:
 
 
 
 
 
 
 
 
 
 
 
1346
  roi = ee.Geometry(geom)
1347
+ st.session_state["roi_geojson"] = json.dumps(last_feature)
1348
+ st.success(" ROI captured successfully")
1349
+ except Exception as e:
1350
+ st.error(f"Failed to parse drawn feature: {e}")
 
1351
 
1352
+ # Restore ROI from session_state if nothing drawn this run
1353
+ if roi is None and "roi_geojson" in st.session_state:
1354
+ try:
1355
+ saved = json.loads(st.session_state["roi_geojson"])
1356
+ geom = saved.get("geometry") if isinstance(saved, dict) else saved
1357
+ roi = ee.Geometry(geom)
1358
+ st.info("♻️ Restored ROI from session state")
1359
+ except Exception as e:
1360
+ st.warning(f"Could not restore ROI: {e}")
1361
+ roi = None
1362
 
1363
  # --- Compute Summaries ---
1364
  if st.button("Compute Summaries"):