MSU576 commited on
Commit
383faa1
Β·
verified Β·
1 Parent(s): 290c0aa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -26
app.py CHANGED
@@ -1302,53 +1302,38 @@ def locator_page():
1302
 
1303
  st.markdown("πŸ‘‰ Draw a polygon/rectangle on the map (use draw tool). After drawing, click **Compute Summaries**.")
1304
 
1305
- # VITAL: Render the map and capture its state from the frontend on every script run.
1306
  map_data = m.to_streamlit(height=700, responsive=True)
1307
 
1308
- # Immediately save any newly drawn geometry to the session_state to persist it.
1309
- # geemap returns the last drawn feature in the 'last_object' key.
1310
  if map_data and map_data.get("last_object"):
1311
  st.session_state["roi_geojson"] = json.dumps(map_data["last_object"])
1312
 
1313
- # Define a robust helper function to retrieve the ROI.
1314
  def get_roi_from_map():
1315
  if "roi_geojson" in st.session_state and st.session_state["roi_geojson"]:
1316
  try:
1317
- # The data from the map is a full GeoJSON "Feature".
1318
- # We need to extract only the 'geometry' part for Earth Engine.
1319
  feature = json.loads(st.session_state["roi_geojson"])
1320
- return ee.Geometry(feature['geometry'])
1321
  except (json.JSONDecodeError, KeyError, Exception) as e:
1322
  st.warning(f"Could not parse the stored ROI. Please try drawing it again. Error: {e}")
1323
- # Clear the invalid ROI to prevent repeated errors
1324
  del st.session_state["roi_geojson"]
1325
- return None
1326
- return None # Return None if no ROI has been saved yet.
1327
 
1328
- # Initialize the button's state if it doesn't exist
1329
  if "compute_button" not in st.session_state:
1330
- st.session_state["compute_button"] = False
1331
-
1332
- # When the button is clicked, set its state to True for the subsequent rerun
1333
  if st.button("Compute Summaries"):
1334
  st.session_state["compute_button"] = True
1335
 
1336
- # This block runs ONLY if the compute button was clicked in the previous run.
1337
  if st.session_state.get("compute_button", False):
1338
- # Immediately reset the button's state to False.
1339
- # This prevents the computation from running again on the next interaction.
1340
  st.session_state["compute_button"] = False
1341
 
1342
- # Retrieve the ROI using our reliable helper function
1343
- roi = get_roi_from_map()
1344
-
1345
- if roi is None:
1346
- st.error("No drawn ROI found. Please draw a polygon or rectangle and press 'Compute Summaries' again.")
1347
- else:
1348
- # --- YOUR COMPUTATION LOGIC GOES HERE ---
1349
- # This is where you place the code that was below the original ROI check.
1350
- st.success("Polygon found β€” computing (this may take a few seconds)...")
1351
 
 
 
 
 
 
1352
 
1353
  # choose soil depth via UI (0-200cm). If soil_img is OpenLandMap provide depth selection; if SoilGrids, show its bands
1354
  chosen_soil_band = None
 
1302
 
1303
  st.markdown("πŸ‘‰ Draw a polygon/rectangle on the map (use draw tool). After drawing, click **Compute Summaries**.")
1304
 
 
1305
  map_data = m.to_streamlit(height=700, responsive=True)
1306
 
 
 
1307
  if map_data and map_data.get("last_object"):
1308
  st.session_state["roi_geojson"] = json.dumps(map_data["last_object"])
1309
 
 
1310
  def get_roi_from_map():
1311
  if "roi_geojson" in st.session_state and st.session_state["roi_geojson"]:
1312
  try:
 
 
1313
  feature = json.loads(st.session_state["roi_geojson"])
1314
+ return ee.Geometry(feature['geometry'])
1315
  except (json.JSONDecodeError, KeyError, Exception) as e:
1316
  st.warning(f"Could not parse the stored ROI. Please try drawing it again. Error: {e}")
 
1317
  del st.session_state["roi_geojson"]
1318
+ return None
1319
+ return None
1320
 
 
1321
  if "compute_button" not in st.session_state:
1322
+ st.session_state["compute_button"] = False
1323
+
 
1324
  if st.button("Compute Summaries"):
1325
  st.session_state["compute_button"] = True
1326
 
 
1327
  if st.session_state.get("compute_button", False):
 
 
1328
  st.session_state["compute_button"] = False
1329
 
1330
+ roi = get_roi_from_map()
 
 
 
 
 
 
 
 
1331
 
1332
+ if roi is None:
1333
+ st.error("No drawn ROI found. Please draw a polygon or rectangle and press 'Compute Summaries' again.")
1334
+ else:
1335
+ st.success("Polygon found β€” computing (this may take a few seconds)...")
1336
+
1337
 
1338
  # choose soil depth via UI (0-200cm). If soil_img is OpenLandMap provide depth selection; if SoilGrids, show its bands
1339
  chosen_soil_band = None