MSU576 commited on
Commit
2ee9d4c
Β·
verified Β·
1 Parent(s): 147b972

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -32
app.py CHANGED
@@ -1325,48 +1325,49 @@ def locator_page():
1325
  pass
1326
 
1327
  # ----------------------------
1328
- # Render map and capture ROI (geemap only)
1329
- # ----------------------------
1330
- st.markdown("πŸ‘‰ Draw a polygon/rectangle/circle using the draw tool, then press **Compute Summaries**")
1331
-
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"):
1365
- roi = get_roi_from_map()
1366
  if roi is None:
1367
- st.error("⚠️ No drawn ROI found. Please draw and try again.")
1368
- return
1369
- st.success("βœ… ROI found β€” computing...")
 
 
1370
 
1371
  chosen_soil_band = None
1372
  if soil_img:
 
1325
  pass
1326
 
1327
  # ----------------------------
1328
+ # --- Render the map ---
 
 
 
 
1329
  m.to_streamlit(height=600, responsive=True)
1330
+
1331
+ # --- ROI capture ---
 
 
 
1332
  roi = None
1333
+
1334
+ # Case 1: New drawing in this run
1335
+ if m.user_roi:
1336
  try:
1337
+ roi = m.user_roi
1338
+ st.session_state["roi_geojson"] = roi.toGeoJSONString()
1339
+ st.success("βœ… ROI captured from user drawing")
 
 
 
 
1340
  except Exception as e:
1341
+ st.warning(f"Could not parse m.user_roi: {e}")
1342
+
1343
+ elif m.draw_features:
 
1344
  try:
1345
+ last_feature = m.draw_features[-1]
1346
+ geom = last_feature.get("geometry") or last_feature
1347
  roi = ee.Geometry(geom)
1348
+ st.session_state["roi_geojson"] = json.dumps(last_feature)
1349
+ st.success("βœ… ROI captured from draw_features")
1350
+ except Exception as e:
1351
+ st.warning(f"Could not parse m.draw_features: {e}")
1352
+
1353
+ # Case 2: Restore ROI from session_state if nothing new drawn
1354
+ elif "roi_geojson" in st.session_state:
1355
+ try:
1356
+ stored = json.loads(st.session_state["roi_geojson"])
1357
+ geom = stored.get("geometry") if isinstance(stored, dict) else stored
1358
+ roi = ee.Geometry(geom)
1359
+ st.info("♻️ ROI restored from session state")
1360
  except Exception as e:
1361
  st.warning(f"Could not restore ROI: {e}")
1362
+
1363
+ # --- Button to trigger computation ---
 
1364
  if st.button("Compute Summaries"):
 
1365
  if roi is None:
1366
+ st.error("⚠️ No ROI found. Please draw a polygon/rectangle/circle and try again.")
1367
+ else:
1368
+ st.success("πŸš€ ROI found β€” computing summaries...")
1369
+
1370
+
1371
 
1372
  chosen_soil_band = None
1373
  if soil_img: