Update app.py
Browse files
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 |
-
#
|
| 1337 |
-
|
| 1338 |
-
def get_roi_from_map():
|
| 1339 |
-
"""Return ee.Geometry if ROI exists, otherwise None"""
|
| 1340 |
-
roi = None
|
| 1341 |
|
| 1342 |
-
|
| 1343 |
-
|
| 1344 |
-
|
| 1345 |
-
|
| 1346 |
-
|
| 1347 |
-
|
| 1348 |
-
|
| 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 |
-
|
| 1362 |
-
st.
|
| 1363 |
-
|
| 1364 |
-
|
| 1365 |
-
return roi
|
| 1366 |
|
| 1367 |
-
#
|
| 1368 |
-
roi
|
| 1369 |
-
|
| 1370 |
-
|
| 1371 |
-
|
| 1372 |
-
|
| 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"):
|