Update app.py
Browse files
app.py
CHANGED
|
@@ -1325,48 +1325,49 @@ def locator_page():
|
|
| 1325 |
pass
|
| 1326 |
|
| 1327 |
# ----------------------------
|
| 1328 |
-
# Render map
|
| 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 |
-
# ---
|
| 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 |
-
|
|
|
|
|
|
|
| 1341 |
try:
|
| 1342 |
-
|
| 1343 |
-
|
| 1344 |
-
|
| 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.
|
| 1351 |
-
|
| 1352 |
-
|
| 1353 |
-
if roi is None and "roi_geojson" in st.session_state:
|
| 1354 |
try:
|
| 1355 |
-
|
| 1356 |
-
geom =
|
| 1357 |
roi = ee.Geometry(geom)
|
| 1358 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1359 |
except Exception as e:
|
| 1360 |
st.warning(f"Could not restore ROI: {e}")
|
| 1361 |
-
|
| 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
|
| 1368 |
-
|
| 1369 |
-
|
|
|
|
|
|
|
| 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:
|