update app.py
Browse files
app.py
CHANGED
@@ -354,74 +354,74 @@ if file_upload:
|
|
354 |
else:
|
355 |
st.warning(f"No value calculated for {location_name}.")
|
356 |
|
357 |
-
|
358 |
-
# Check if polygons_df is populated for polygons
|
359 |
-
if polygons_df is not None:
|
360 |
-
st.write("Preview of the uploaded polygons data:")
|
361 |
-
st.dataframe(polygons_df.head())
|
362 |
|
363 |
-
|
|
|
|
|
|
|
|
|
|
|
364 |
|
365 |
-
|
366 |
-
|
367 |
if polygon.is_valid:
|
368 |
gdf = gpd.GeoDataFrame([row], geometry=[polygon], crs=polygons_df.crs)
|
369 |
m.add_gdf(gdf=gdf, layer_name=row.get('name', 'Unnamed Polygon'))
|
370 |
|
371 |
-
|
372 |
-
|
373 |
-
|
374 |
|
375 |
-
|
376 |
-
|
377 |
-
|
378 |
|
379 |
-
|
380 |
-
|
381 |
-
|
382 |
-
|
383 |
-
|
384 |
|
385 |
-
|
386 |
-
|
387 |
-
|
388 |
|
389 |
-
|
390 |
|
391 |
-
|
392 |
-
|
393 |
-
|
394 |
-
|
395 |
-
|
396 |
-
|
397 |
-
|
398 |
-
|
399 |
-
|
400 |
-
|
401 |
-
|
402 |
-
|
403 |
-
|
404 |
-
|
405 |
-
|
406 |
-
|
407 |
|
408 |
-
|
409 |
-
|
410 |
-
|
411 |
|
412 |
-
|
413 |
-
|
414 |
-
|
415 |
-
|
416 |
-
|
417 |
-
|
418 |
|
419 |
-
|
420 |
-
|
421 |
-
|
422 |
-
|
423 |
-
|
424 |
-
|
425 |
|
426 |
# After processing, show the results
|
427 |
if st.session_state.results:
|
|
|
354 |
else:
|
355 |
st.warning(f"No value calculated for {location_name}.")
|
356 |
|
|
|
|
|
|
|
|
|
|
|
357 |
|
358 |
+
# Check if polygons_df is populated for polygons
|
359 |
+
if polygons_df is not None:
|
360 |
+
st.write("Preview of the uploaded polygons data:")
|
361 |
+
st.dataframe(polygons_df.head())
|
362 |
+
|
363 |
+
m = leafmap.Map(center=[polygons_df.geometry.centroid.y.mean(), polygons_df.geometry.centroid.x.mean()], zoom=10)
|
364 |
|
365 |
+
for _, row in polygons_df.iterrows():
|
366 |
+
polygon = row['geometry']
|
367 |
if polygon.is_valid:
|
368 |
gdf = gpd.GeoDataFrame([row], geometry=[polygon], crs=polygons_df.crs)
|
369 |
m.add_gdf(gdf=gdf, layer_name=row.get('name', 'Unnamed Polygon'))
|
370 |
|
371 |
+
st.write("Map of Uploaded Polygons:")
|
372 |
+
m.to_streamlit()
|
373 |
+
st.session_state.map_data = m
|
374 |
|
375 |
+
for idx, row in polygons_df.iterrows():
|
376 |
+
polygon = row['geometry']
|
377 |
+
location_name = row.get('name', f"Polygon_{idx}")
|
378 |
|
379 |
+
try:
|
380 |
+
roi = convert_to_ee_geometry(polygon)
|
381 |
+
except ValueError as e:
|
382 |
+
st.error(str(e))
|
383 |
+
continue
|
384 |
|
385 |
+
collection = ee.ImageCollection(sub_options[sub_selection]) \
|
386 |
+
.filterDate(ee.Date(start_date_str), ee.Date(end_date_str)) \
|
387 |
+
.filterBounds(roi)
|
388 |
|
389 |
+
image = get_most_recent_image(collection)
|
390 |
|
391 |
+
if not image:
|
392 |
+
st.warning(f"No images found for {location_name}.")
|
393 |
+
else:
|
394 |
+
st.write(f"Found an image for {location_name}.")
|
395 |
+
result = None
|
396 |
+
if index_choice.lower() == 'ndvi':
|
397 |
+
result = calculate_ndvi(image, roi)
|
398 |
+
elif index_choice.lower() == 'ndwi':
|
399 |
+
result = calculate_ndwi(image, roi)
|
400 |
+
elif index_choice.lower() == 'average no₂':
|
401 |
+
if 'NO2' in image.bandNames().getInfo():
|
402 |
+
result = calculate_avg_no2_sentinel5p(image, roi)
|
403 |
+
else:
|
404 |
+
st.warning(f"No NO2 band found for {location_name}. Please use Sentinel-5P for NO₂ data.")
|
405 |
+
elif index_choice.lower() == 'custom formula' and custom_formula:
|
406 |
+
result = process_custom_formula(image, roi, custom_formula)
|
407 |
|
408 |
+
if result is not None:
|
409 |
+
# Initialize the calculated_value as None
|
410 |
+
calculated_value = None
|
411 |
|
412 |
+
# Check if the result is a dictionary (e.g., custom formula result)
|
413 |
+
if isinstance(result, dict) and 'CustomResult' in result:
|
414 |
+
calculated_value = result['CustomResult'] # Extract the numeric value from the dictionary
|
415 |
+
# If the result is a numeric value (e.g., NDVI, NDWI, or NO2)
|
416 |
+
elif isinstance(result, (int, float)):
|
417 |
+
calculated_value = result
|
418 |
|
419 |
+
# If a valid calculated_value is found, append the result to session_state
|
420 |
+
if calculated_value is not None:
|
421 |
+
st.session_state.results.append({
|
422 |
+
'Location Name': location_name,
|
423 |
+
'Calculated Value': calculated_value
|
424 |
+
})
|
425 |
|
426 |
# After processing, show the results
|
427 |
if st.session_state.results:
|