MSU576 commited on
Commit
f7a969f
Β·
verified Β·
1 Parent(s): e557b19

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -18
app.py CHANGED
@@ -1008,31 +1008,38 @@ def ocr_page():
1008
 
1009
  # Locator Page (Earth Engine integration)
1010
 
 
 
1011
  import ee
1012
  import os
1013
- import json
1014
 
1015
- # Load EE service account credentials from Hugging Face secret
 
 
1016
  EE_JSON = os.getenv("EARTHENGINE_TOKEN")
1017
 
1018
  if EE_JSON:
1019
- creds = ee.ServiceAccountCredentials(
1020
- email='YOUR_SERVICE_ACCOUNT_EMAIL',
1021
- key_data=json.loads(EE_JSON)
1022
- )
1023
- ee.Initialize(creds)
 
 
 
1024
  else:
1025
- ee.Initialize() # fallback if running locally with `earthengine authenticate`
 
 
 
 
 
1026
  def locator_page():
1027
  st.header("🌍 Locator β€” Interactive Selection")
1028
  st.markdown("➑️ Draw a point, rectangle, or polygon on the map to select your area of interest.")
1029
 
1030
  site = st.session_state["sites"][st.session_state["active_site"]]
1031
 
1032
- if not EE_JSON:
1033
- st.error("❌ Earth Engine/Geemap not available in this environment. Please add `geemap` & `earthengine-api` to requirements.txt.")
1034
- return
1035
-
1036
  try:
1037
  # ----------------------------
1038
  # Create interactive map
@@ -1046,12 +1053,13 @@ def locator_page():
1046
  # Save map HTML for display
1047
  map_file = "/tmp/locator_draw.html"
1048
  m.to_html(map_file)
1049
- map_html = open(map_file, "r").read()
1050
- st.components.v1.html(map_html, height=600)
1051
 
 
1052
  # Button to process drawn geometry
 
1053
  if st.button("Fetch Earth Data (EE)"):
1054
- drawn = m.user_roi # get user drawn geometry
1055
  if drawn is None:
1056
  st.warning("⚠️ No region selected. Please draw a point or polygon on the map.")
1057
  return
@@ -1075,7 +1083,7 @@ def locator_page():
1075
  site["Flood Data"] = f"Flood occurrence probability: {flood_val:.1f}%" if flood_val else "No flood data"
1076
 
1077
  # ----------------------------
1078
- # Soil (OpenLandMap b200 = 100–200 cm)
1079
  # ----------------------------
1080
  soil = ee.Image("OpenLandMap/SOL/SOL_CLAY-WFRACTION_USDA-3A1A1A_M/v02").select("b200")
1081
  soil_sample = soil.sample(region=geom, scale=250).first()
@@ -1129,14 +1137,13 @@ def locator_page():
1129
  m2.addLayer(gsw, {"min": 0, "max": 100, "palette": ["white", "blue"]}, "Flood Occurrence")
1130
  m2.addLayer(soil, {"min": 0.0, "max": 0.6, "palette": ["#ffffcc","#c2e699","#78c679","#31a354"]}, "Soil Clay (100–200 cm)")
1131
  m2.addLayer(seismic, {"min": 0, "max": 1, "palette": ["green", "yellow", "red"]}, "Seismic Hazard")
1132
- m2.addLayer(landcover, {"min": 10, "max": 100, "palette": ["006400","ffbb22","ffff4c","f096ff","fa0000","b4b4b4","f0f0f0","0064c8","0096a0","00cf75","fae6a0"]}, "Landcover")
1133
  m2.addLayer(forest, {"palette": ["white", "green"]}, "Forest Loss")
1134
  m2.addLayer(urban, {"min": 0, "max": 1, "palette": ["white", "black"]}, "Urban Footprint")
1135
 
1136
  m2.addLayer(drawn, {}, "Selected AOI")
1137
  m2.addLayerControl()
1138
 
1139
- # Save final map
1140
  map_file2 = "/tmp/locator_results.html"
1141
  m2.to_html(map_file2)
1142
  st.components.v1.html(open(map_file2, "r").read(), height=600)
 
1008
 
1009
  # Locator Page (Earth Engine integration)
1010
 
1011
+ import streamlit as st
1012
+ import geemap.foliumap as geemap
1013
  import ee
1014
  import os
 
1015
 
1016
+ # ----------------------------
1017
+ # Authenticate Earth Engine
1018
+ # ----------------------------
1019
  EE_JSON = os.getenv("EARTHENGINE_TOKEN")
1020
 
1021
  if EE_JSON:
1022
+ try:
1023
+ creds = ee.ServiceAccountCredentials(
1024
+ email=os.getenv("SERVICE_ACCOUNT"), # <-- replace with your service account email
1025
+ key_data=EE_JSON # pass raw JSON string, not dict
1026
+ )
1027
+ ee.Initialize(creds)
1028
+ except Exception as e:
1029
+ st.error(f"❌ Failed to initialize Earth Engine: {e}")
1030
  else:
1031
+ try:
1032
+ ee.Initialize() # fallback if running locally
1033
+ except Exception as e:
1034
+ st.error(f"❌ Local EE init failed: {e}")
1035
+
1036
+
1037
  def locator_page():
1038
  st.header("🌍 Locator β€” Interactive Selection")
1039
  st.markdown("➑️ Draw a point, rectangle, or polygon on the map to select your area of interest.")
1040
 
1041
  site = st.session_state["sites"][st.session_state["active_site"]]
1042
 
 
 
 
 
1043
  try:
1044
  # ----------------------------
1045
  # Create interactive map
 
1053
  # Save map HTML for display
1054
  map_file = "/tmp/locator_draw.html"
1055
  m.to_html(map_file)
1056
+ st.components.v1.html(open(map_file, "r").read(), height=600)
 
1057
 
1058
+ # ----------------------------
1059
  # Button to process drawn geometry
1060
+ # ----------------------------
1061
  if st.button("Fetch Earth Data (EE)"):
1062
+ drawn = m.user_roi
1063
  if drawn is None:
1064
  st.warning("⚠️ No region selected. Please draw a point or polygon on the map.")
1065
  return
 
1083
  site["Flood Data"] = f"Flood occurrence probability: {flood_val:.1f}%" if flood_val else "No flood data"
1084
 
1085
  # ----------------------------
1086
+ # Soil (OpenLandMap, b200 = 100–200 cm depth)
1087
  # ----------------------------
1088
  soil = ee.Image("OpenLandMap/SOL/SOL_CLAY-WFRACTION_USDA-3A1A1A_M/v02").select("b200")
1089
  soil_sample = soil.sample(region=geom, scale=250).first()
 
1137
  m2.addLayer(gsw, {"min": 0, "max": 100, "palette": ["white", "blue"]}, "Flood Occurrence")
1138
  m2.addLayer(soil, {"min": 0.0, "max": 0.6, "palette": ["#ffffcc","#c2e699","#78c679","#31a354"]}, "Soil Clay (100–200 cm)")
1139
  m2.addLayer(seismic, {"min": 0, "max": 1, "palette": ["green", "yellow", "red"]}, "Seismic Hazard")
1140
+ m2.addLayer(landcover, {"min": 10, "max": 100}, "Landcover")
1141
  m2.addLayer(forest, {"palette": ["white", "green"]}, "Forest Loss")
1142
  m2.addLayer(urban, {"min": 0, "max": 1, "palette": ["white", "black"]}, "Urban Footprint")
1143
 
1144
  m2.addLayer(drawn, {}, "Selected AOI")
1145
  m2.addLayerControl()
1146
 
 
1147
  map_file2 = "/tmp/locator_results.html"
1148
  m2.to_html(map_file2)
1149
  st.components.v1.html(open(map_file2, "r").read(), height=600)