Update app.py
Browse files
app.py
CHANGED
|
@@ -741,13 +741,6 @@ if "sites" not in st.session_state:
|
|
| 741 |
if "active_site" not in st.session_state:
|
| 742 |
st.session_state["active_site"] = 0
|
| 743 |
|
| 744 |
-
# -------------------- MODEL PLACEHOLDER --------------------
|
| 745 |
-
# Load soil recognizer model here (replace with your trained PyTorch model)
|
| 746 |
-
# model = torch.load("soil_model.pth", map_location="cpu")
|
| 747 |
-
# model.eval()
|
| 748 |
-
|
| 749 |
-
# Placeholder soil classes
|
| 750 |
-
soil_classes = ["Gravel", "Sand", "Silt", "Clay", "Loam"]
|
| 751 |
|
| 752 |
# -------------------- LANDING PAGE --------------------
|
| 753 |
def landing_page():
|
|
@@ -1359,51 +1352,79 @@ def locator_page():
|
|
| 1359 |
|
| 1360 |
# ----------------------------
|
| 1361 |
from streamlit_folium import st_folium
|
|
|
|
| 1362 |
|
| 1363 |
-
#
|
| 1364 |
-
|
| 1365 |
-
# Render geemap map
|
| 1366 |
m.to_streamlit(height=600, responsive=True)
|
| 1367 |
-
|
| 1368 |
-
#
|
| 1369 |
-
|
| 1370 |
-
|
| 1371 |
-
|
| 1372 |
-
# Convert your geemap map to a folium map object for st_folium
|
| 1373 |
-
fol_map = m.folium_map # geemap.foliumap.Map wraps a base folium.Map
|
| 1374 |
-
result = st_folium(fol_map, width=700, height=600, returned_objects=["last_active_drawing"])
|
| 1375 |
-
|
| 1376 |
-
# Attempt to get ROI from drawn feature
|
| 1377 |
roi = None
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1378 |
if result and "last_active_drawing" in result and result["last_active_drawing"]:
|
| 1379 |
feat = result["last_active_drawing"]
|
| 1380 |
geom = feat.get("geometry")
|
| 1381 |
if geom:
|
| 1382 |
try:
|
| 1383 |
roi = ee.Geometry(geom)
|
|
|
|
| 1384 |
st.session_state["roi_geojson"] = feat
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1385 |
st.success("✅ ROI captured!")
|
| 1386 |
except Exception as e:
|
| 1387 |
st.error(f"Failed to convert drawn geometry to ee.Geometry: {e}")
|
| 1388 |
-
|
| 1389 |
-
# Restore from session if none drawn
|
| 1390 |
if roi is None and "roi_geojson" in st.session_state:
|
| 1391 |
saved = st.session_state["roi_geojson"]
|
| 1392 |
try:
|
| 1393 |
geom = saved.get("geometry")
|
| 1394 |
if geom:
|
| 1395 |
roi = ee.Geometry(geom)
|
| 1396 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1397 |
except Exception as e:
|
| 1398 |
st.warning(f"Could not restore ROI: {e}")
|
| 1399 |
-
|
| 1400 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1401 |
if st.button("Compute Summaries"):
|
| 1402 |
if roi is None:
|
| 1403 |
st.error("⚠️ No ROI found. Please draw a polygon/rectangle/circle and try again.")
|
| 1404 |
else:
|
| 1405 |
st.success("ROI ready — performing computations...")
|
| 1406 |
-
|
| 1407 |
chosen_soil_band = None
|
| 1408 |
if soil_img:
|
| 1409 |
bands = soil_img.bandNames().getInfo()
|
|
|
|
| 741 |
if "active_site" not in st.session_state:
|
| 742 |
st.session_state["active_site"] = 0
|
| 743 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 744 |
|
| 745 |
# -------------------- LANDING PAGE --------------------
|
| 746 |
def landing_page():
|
|
|
|
| 1352 |
|
| 1353 |
# ----------------------------
|
| 1354 |
from streamlit_folium import st_folium
|
| 1355 |
+
import json
|
| 1356 |
|
| 1357 |
+
# --- Render geemap map ---
|
|
|
|
|
|
|
| 1358 |
m.to_streamlit(height=600, responsive=True)
|
| 1359 |
+
|
| 1360 |
+
# Capture drawn ROI
|
| 1361 |
+
result = st_folium(m, width=700, height=600, returned_objects=["last_active_drawing"])
|
| 1362 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1363 |
roi = None
|
| 1364 |
+
coords = None
|
| 1365 |
+
flat_coords = None
|
| 1366 |
+
|
| 1367 |
+
# --- Get ROI from drawn feature ---
|
| 1368 |
if result and "last_active_drawing" in result and result["last_active_drawing"]:
|
| 1369 |
feat = result["last_active_drawing"]
|
| 1370 |
geom = feat.get("geometry")
|
| 1371 |
if geom:
|
| 1372 |
try:
|
| 1373 |
roi = ee.Geometry(geom)
|
| 1374 |
+
coords = geom.get("coordinates", None)
|
| 1375 |
st.session_state["roi_geojson"] = feat
|
| 1376 |
+
|
| 1377 |
+
# Flatten coordinates
|
| 1378 |
+
if coords:
|
| 1379 |
+
if geom["type"] in ["Polygon", "MultiPolygon"]:
|
| 1380 |
+
flat_coords = [(lat, lon) for ring in coords for lon, lat in ring]
|
| 1381 |
+
elif geom["type"] == "Point":
|
| 1382 |
+
lon, lat = coords
|
| 1383 |
+
flat_coords = [(lat, lon)]
|
| 1384 |
+
elif geom["type"] == "LineString":
|
| 1385 |
+
flat_coords = [(lat, lon) for lon, lat in coords]
|
| 1386 |
+
|
| 1387 |
+
if flat_coords:
|
| 1388 |
+
st.session_state["roi_coords"] = flat_coords # ✅ Save for reuse
|
| 1389 |
st.success("✅ ROI captured!")
|
| 1390 |
except Exception as e:
|
| 1391 |
st.error(f"Failed to convert drawn geometry to ee.Geometry: {e}")
|
| 1392 |
+
|
| 1393 |
+
# --- Restore ROI from session if none drawn ---
|
| 1394 |
if roi is None and "roi_geojson" in st.session_state:
|
| 1395 |
saved = st.session_state["roi_geojson"]
|
| 1396 |
try:
|
| 1397 |
geom = saved.get("geometry")
|
| 1398 |
if geom:
|
| 1399 |
roi = ee.Geometry(geom)
|
| 1400 |
+
coords = geom.get("coordinates", None)
|
| 1401 |
+
if coords:
|
| 1402 |
+
if geom["type"] in ["Polygon", "MultiPolygon"]:
|
| 1403 |
+
flat_coords = [(lat, lon) for ring in coords for lon, lat in ring]
|
| 1404 |
+
elif geom["type"] == "Point":
|
| 1405 |
+
lon, lat = coords
|
| 1406 |
+
flat_coords = [(lat, lon)]
|
| 1407 |
+
elif geom["type"] == "LineString":
|
| 1408 |
+
flat_coords = [(lat, lon) for lon, lat in coords]
|
| 1409 |
+
|
| 1410 |
+
if flat_coords:
|
| 1411 |
+
st.session_state["roi_coords"] = flat_coords # ✅ Restore saved
|
| 1412 |
+
st.info("♻️ ROI restored from earlier session")
|
| 1413 |
except Exception as e:
|
| 1414 |
st.warning(f"Could not restore ROI: {e}")
|
| 1415 |
+
|
| 1416 |
+
# --- Show coordinates below the map ---
|
| 1417 |
+
if "roi_coords" in st.session_state:
|
| 1418 |
+
st.markdown("### 📍 ROI Coordinates (Lat, Lon)")
|
| 1419 |
+
st.write(st.session_state["roi_coords"])
|
| 1420 |
+
|
| 1421 |
+
# --- Compute summaries ---
|
| 1422 |
if st.button("Compute Summaries"):
|
| 1423 |
if roi is None:
|
| 1424 |
st.error("⚠️ No ROI found. Please draw a polygon/rectangle/circle and try again.")
|
| 1425 |
else:
|
| 1426 |
st.success("ROI ready — performing computations...")
|
| 1427 |
+
# Here you can now use st.session_state["roi_coords"] for summaries
|
| 1428 |
chosen_soil_band = None
|
| 1429 |
if soil_img:
|
| 1430 |
bands = soil_img.bandNames().getInfo()
|