Update app.py
Browse files
app.py
CHANGED
|
@@ -1331,37 +1331,46 @@ def locator_page():
|
|
| 1331 |
|
| 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
|
|
|
|
|
|
|
|
|
|
| 1340 |
if m.draw_features:
|
| 1341 |
try:
|
| 1342 |
-
last_feature = m.draw_features[-1]
|
| 1343 |
if isinstance(last_feature, dict):
|
| 1344 |
geom = last_feature.get("geometry") or last_feature
|
|
|
|
|
|
|
| 1345 |
st.session_state["roi_geojson"] = json.dumps(last_feature)
|
| 1346 |
-
return ee.Geometry(geom)
|
| 1347 |
except Exception as e:
|
| 1348 |
-
st.warning(f"Could not parse drawn ROI: {e}")
|
| 1349 |
-
|
|
|
|
|
|
|
| 1350 |
elif "roi_geojson" in st.session_state:
|
| 1351 |
-
# Restore ROI if already saved in session
|
| 1352 |
try:
|
| 1353 |
-
|
| 1354 |
-
|
| 1355 |
-
|
| 1356 |
-
|
| 1357 |
-
|
| 1358 |
-
|
| 1359 |
-
|
|
|
|
|
|
|
|
|
|
| 1360 |
roi = get_roi_from_map()
|
| 1361 |
if roi:
|
| 1362 |
-
st.success("ROI
|
| 1363 |
-
|
| 1364 |
-
|
|
|
|
| 1365 |
|
| 1366 |
# --- Compute Summaries ---
|
| 1367 |
if st.button("Compute Summaries"):
|
|
|
|
| 1331 |
|
| 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"):
|