update app.py
Browse files
app.py
CHANGED
@@ -173,6 +173,14 @@ def calculate_custom_formula(image, geometry, formula):
|
|
173 |
)
|
174 |
return result.get('Custom Index')
|
175 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
176 |
# Process each point or polygon
|
177 |
if file_upload:
|
178 |
locations_df = None # Initialize locations_df to None
|
@@ -277,39 +285,36 @@ if file_upload:
|
|
277 |
'Calculated Value': calculated_value
|
278 |
})
|
279 |
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
# Create a LeafMap object to display the polygons
|
287 |
-
m = leafmap.Map(center=[polygons_df.geometry.centroid.y.mean(), polygons_df.geometry.centroid.x.mean()], zoom=10)
|
288 |
-
|
289 |
-
# Add polygons to the map
|
290 |
-
for _, row in polygons_df.iterrows():
|
291 |
-
polygon = row['geometry']
|
292 |
-
if polygon.is_valid: # Check if the geometry is valid
|
293 |
-
# Create a GeoDataFrame with the single row
|
294 |
-
gdf = gpd.GeoDataFrame([row], geometry=[polygon], crs=polygons_df.crs)
|
295 |
-
|
296 |
-
# Add the valid GeoDataFrame to the map
|
297 |
-
m.add_gdf(gdf=gdf, layer_name=row.get('name', 'Unnamed Polygon'))
|
298 |
|
299 |
-
|
300 |
-
|
301 |
-
m.to_streamlit()
|
302 |
|
303 |
-
|
304 |
-
|
|
|
|
|
|
|
305 |
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
location_name = row.get('name', f"Polygon_{idx}")
|
310 |
|
311 |
-
|
312 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
313 |
|
314 |
# Load Sentinel-2 image collection
|
315 |
collection = ee.ImageCollection(sub_options[sub_selection]) \
|
@@ -347,7 +352,9 @@ if file_upload:
|
|
347 |
'Location Name': location_name,
|
348 |
'Calculated Value': calculated_value
|
349 |
})
|
350 |
-
|
|
|
|
|
351 |
# After processing, show the results
|
352 |
if st.session_state.results:
|
353 |
# Convert the results to a DataFrame for better visualization
|
@@ -368,4 +375,4 @@ if st.session_state.results:
|
|
368 |
data=result_df.to_csv(index=False).encode('utf-8'),
|
369 |
file_name=filename,
|
370 |
mime='text/csv'
|
371 |
-
)
|
|
|
173 |
)
|
174 |
return result.get('Custom Index')
|
175 |
|
176 |
+
# Helper function to ensure valid geometry for EE processing
|
177 |
+
def convert_to_ee_geometry(geometry):
|
178 |
+
if geometry.is_valid:
|
179 |
+
geojson = geometry.__geo_interface__ # Convert geometry to GeoJSON format
|
180 |
+
return ee.Geometry(geojson)
|
181 |
+
else:
|
182 |
+
raise ValueError("Invalid geometry: The polygon geometry is not valid.")
|
183 |
+
|
184 |
# Process each point or polygon
|
185 |
if file_upload:
|
186 |
locations_df = None # Initialize locations_df to None
|
|
|
285 |
'Calculated Value': calculated_value
|
286 |
})
|
287 |
|
288 |
+
# Process polygons
|
289 |
+
if polygons_df is not None:
|
290 |
+
# Display a preview of the polygons data
|
291 |
+
st.write("Preview of the uploaded polygons data:")
|
292 |
+
st.dataframe(polygons_df.head())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
293 |
|
294 |
+
# Create a LeafMap object to display the polygons
|
295 |
+
m = leafmap.Map(center=[polygons_df.geometry.centroid.y.mean(), polygons_df.geometry.centroid.x.mean()], zoom=10)
|
|
|
296 |
|
297 |
+
# Add polygons to the map
|
298 |
+
for _, row in polygons_df.iterrows():
|
299 |
+
polygon = row['geometry']
|
300 |
+
if polygon.is_valid: # Check if the geometry is valid
|
301 |
+
m.add_gdf(gdf=row, layer_name=row.get('name', 'Unnamed Polygon'))
|
302 |
|
303 |
+
# Display map
|
304 |
+
st.write("Map of Uploaded Polygons:")
|
305 |
+
m.to_streamlit()
|
|
|
306 |
|
307 |
+
# Store the map in session_state
|
308 |
+
st.session_state.map_data = m
|
309 |
+
|
310 |
+
# Process each polygon for index calculation
|
311 |
+
for idx, row in polygons_df.iterrows():
|
312 |
+
polygon = row['geometry']
|
313 |
+
location_name = row.get('name', f"Polygon_{idx}")
|
314 |
+
|
315 |
+
try:
|
316 |
+
# Convert polygon to Earth Engine geometry
|
317 |
+
roi = convert_to_ee_geometry(polygon)
|
318 |
|
319 |
# Load Sentinel-2 image collection
|
320 |
collection = ee.ImageCollection(sub_options[sub_selection]) \
|
|
|
352 |
'Location Name': location_name,
|
353 |
'Calculated Value': calculated_value
|
354 |
})
|
355 |
+
except ValueError as e:
|
356 |
+
st.error(f"Error processing {location_name}: {str(e)}")
|
357 |
+
|
358 |
# After processing, show the results
|
359 |
if st.session_state.results:
|
360 |
# Convert the results to a DataFrame for better visualization
|
|
|
375 |
data=result_df.to_csv(index=False).encode('utf-8'),
|
376 |
file_name=filename,
|
377 |
mime='text/csv'
|
378 |
+
)
|