Spaces:
Running
Running
update app.py
Browse files
app.py
CHANGED
@@ -79,15 +79,16 @@ elif index_choice.lower() == 'custom formula':
|
|
79 |
custom_formula = st.text_input("Enter Custom Formula (e.g., 'B5 - B4 / B5 + B4')")
|
80 |
st.write(f"Custom Formula: {custom_formula}") # Display the custom formula after the user inputs it
|
81 |
|
82 |
-
# Function to
|
83 |
def convert_to_ee_geometry(geometry):
|
84 |
-
|
85 |
-
Convert a GeoDataFrame geometry (Polygon, MultiPolygon, etc.) to an Earth Engine geometry.
|
86 |
-
"""
|
87 |
if geometry.is_valid:
|
88 |
-
|
|
|
|
|
|
|
89 |
else:
|
90 |
-
raise ValueError(
|
91 |
|
92 |
# Function to read points from CSV
|
93 |
def read_csv(file_path):
|
@@ -287,34 +288,31 @@ if file_upload:
|
|
287 |
'Calculated Value': calculated_value
|
288 |
})
|
289 |
|
290 |
-
# Check if polygons_df is populated for polygons
|
291 |
-
if polygons_df is not None:
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
# Create a LeafMap object to display the polygons
|
297 |
-
m = leafmap.Map(center=[polygons_df.geometry.centroid.y.mean(), polygons_df.geometry.centroid.x.mean()], zoom=10)
|
298 |
|
299 |
-
|
300 |
-
|
301 |
-
polygon = row['geometry']
|
302 |
-
if polygon.is_valid: # Check if the geometry is valid
|
303 |
-
# Convert geometry to Earth Engine format
|
304 |
-
roi = convert_to_ee_geometry(polygon)
|
305 |
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
#
|
310 |
-
|
|
|
|
|
|
|
|
|
311 |
|
312 |
-
|
313 |
-
|
314 |
-
|
315 |
|
316 |
-
|
317 |
-
|
318 |
|
319 |
# Process each polygon for index calculation
|
320 |
for idx, row in polygons_df.iterrows():
|
@@ -322,7 +320,11 @@ if polygons_df is not None:
|
|
322 |
location_name = row.get('name', f"Polygon_{idx}")
|
323 |
|
324 |
# Define the region of interest (ROI)
|
325 |
-
|
|
|
|
|
|
|
|
|
326 |
|
327 |
# Load Sentinel-2 image collection
|
328 |
collection = ee.ImageCollection(sub_options[sub_selection]) \
|
|
|
79 |
custom_formula = st.text_input("Enter Custom Formula (e.g., 'B5 - B4 / B5 + B4')")
|
80 |
st.write(f"Custom Formula: {custom_formula}") # Display the custom formula after the user inputs it
|
81 |
|
82 |
+
# Function to check if the polygon geometry is valid and convert it to the correct format
|
83 |
def convert_to_ee_geometry(geometry):
|
84 |
+
# Ensure the polygon geometry is in the right format
|
|
|
|
|
85 |
if geometry.is_valid:
|
86 |
+
# Convert the geometry to GeoJSON format
|
87 |
+
geojson = geometry.__geo_interface__
|
88 |
+
# Convert to Earth Engine geometry
|
89 |
+
return ee.Geometry(geojson)
|
90 |
else:
|
91 |
+
raise ValueError("Invalid geometry: The polygon geometry is not valid.")
|
92 |
|
93 |
# Function to read points from CSV
|
94 |
def read_csv(file_path):
|
|
|
288 |
'Calculated Value': calculated_value
|
289 |
})
|
290 |
|
291 |
+
# Check if polygons_df is populated for polygons
|
292 |
+
if polygons_df is not None:
|
293 |
+
# Display a preview of the polygons data
|
294 |
+
st.write("Preview of the uploaded polygons data:")
|
295 |
+
st.dataframe(polygons_df.head())
|
|
|
|
|
|
|
296 |
|
297 |
+
# Create a LeafMap object to display the polygons
|
298 |
+
m = leafmap.Map(center=[polygons_df.geometry.centroid.y.mean(), polygons_df.geometry.centroid.x.mean()], zoom=10)
|
|
|
|
|
|
|
|
|
299 |
|
300 |
+
# Add polygons to the map
|
301 |
+
for _, row in polygons_df.iterrows():
|
302 |
+
polygon = row['geometry']
|
303 |
+
if polygon.is_valid: # Check if the geometry is valid
|
304 |
+
# Create a GeoDataFrame with the single row
|
305 |
+
gdf = gpd.GeoDataFrame([row], geometry=[polygon], crs=polygons_df.crs)
|
306 |
+
|
307 |
+
# Add the valid GeoDataFrame to the map
|
308 |
+
m.add_gdf(gdf=gdf, layer_name=row.get('name', 'Unnamed Polygon'))
|
309 |
|
310 |
+
# Display map
|
311 |
+
st.write("Map of Uploaded Polygons:")
|
312 |
+
m.to_streamlit()
|
313 |
|
314 |
+
# Store the map in session_state
|
315 |
+
st.session_state.map_data = m
|
316 |
|
317 |
# Process each polygon for index calculation
|
318 |
for idx, row in polygons_df.iterrows():
|
|
|
320 |
location_name = row.get('name', f"Polygon_{idx}")
|
321 |
|
322 |
# Define the region of interest (ROI)
|
323 |
+
try:
|
324 |
+
roi = convert_to_ee_geometry(polygon)
|
325 |
+
except ValueError as e:
|
326 |
+
st.error(str(e))
|
327 |
+
continue # Skip this polygon if geometry is invalid
|
328 |
|
329 |
# Load Sentinel-2 image collection
|
330 |
collection = ee.ImageCollection(sub_options[sub_selection]) \
|