Add DEM and Slope
Browse files
app.py
CHANGED
|
@@ -407,16 +407,13 @@ def process_and_display(file_obj, url_str, buffer_m, progress=gr.Progress()):
|
|
| 407 |
progress(1, desc="Done!")
|
| 408 |
return m._repr_html_(), None, stats_df, dem_html, slope_html, geometry_json, buffer_geometry_json
|
| 409 |
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
def calculate_indices(
|
| 414 |
geometry_json, buffer_geometry_json, veg_indices, evi_vars, date_range,
|
| 415 |
min_year, max_year, progress=gr.Progress()
|
| 416 |
):
|
| 417 |
"""Calculates vegetation indices based on user inputs."""
|
| 418 |
one_time_setup()
|
| 419 |
-
|
| 420 |
if not all([geometry_json, buffer_geometry_json, veg_indices]):
|
| 421 |
return "Please process a file and select at least one index first.", None, None, None
|
| 422 |
|
|
@@ -428,7 +425,7 @@ def calculate_indices(
|
|
| 428 |
# Convert to EE geometry
|
| 429 |
ee_geometry = ee.Geometry(json.loads(geometry_gdf.to_crs(4326).to_json())['features'][0]['geometry'])
|
| 430 |
buffer_ee_geometry = ee.Geometry(json.loads(buffer_geometry_gdf.to_crs(4326).to_json())['features'][0]['geometry'])
|
| 431 |
-
|
| 432 |
# Date ranges
|
| 433 |
start_day, start_month = date_range[0].day, date_range[0].month
|
| 434 |
end_day, end_month = date_range[1].day, date_range[1].month
|
|
@@ -454,35 +451,38 @@ def calculate_indices(
|
|
| 454 |
if filtered_collection.size().getInfo() == 0:
|
| 455 |
continue
|
| 456 |
|
|
|
|
| 457 |
year_val = int(start_date.split('-')[0])
|
| 458 |
row = {'Year': year_val, 'Date Range': f"{start_date}_to_{end_date}"}
|
| 459 |
|
| 460 |
for veg_index in veg_indices:
|
| 461 |
mosaic = filtered_collection.qualityMosaic(veg_index)
|
| 462 |
-
|
| 463 |
mean_val = mosaic.reduceRegion(reducer=ee.Reducer.mean(), geometry=ee_geometry, scale=10, maxPixels=1e9).get(veg_index).getInfo()
|
| 464 |
buffer_mean_val = mosaic.reduceRegion(reducer=ee.Reducer.mean(), geometry=buffer_ee_geometry, scale=10, maxPixels=1e9).get(veg_index).getInfo()
|
| 465 |
|
| 466 |
row[veg_index] = mean_val
|
| 467 |
row[f"{veg_index}_buffer"] = buffer_mean_val
|
| 468 |
-
row[f"{veg_index}_ratio"] = (mean_val / buffer_mean_val) if buffer_mean_val else np.nan
|
| 469 |
result_rows.append(row)
|
| 470 |
|
| 471 |
if not result_rows:
|
| 472 |
return "No satellite imagery found for the selected dates.", None, None, None
|
| 473 |
|
| 474 |
-
result_df = pd.DataFrame(result_rows)
|
| 475 |
result_df = result_df.round(3)
|
| 476 |
|
| 477 |
# Create plots
|
| 478 |
plots = []
|
| 479 |
for veg_index in veg_indices:
|
| 480 |
-
|
|
|
|
| 481 |
existing_plot_cols = [col for col in plot_cols if col in result_df.columns]
|
| 482 |
|
| 483 |
plot_df = result_df[['Year'] + existing_plot_cols].dropna()
|
|
|
|
| 484 |
if not plot_df.empty:
|
| 485 |
-
fig = px.line(plot_df, x=
|
| 486 |
fig.update_layout(xaxis_title="Year", yaxis_title="Index Value")
|
| 487 |
# Ensure x-axis ticks are whole numbers for years
|
| 488 |
fig.update_xaxes(dtick=1)
|
|
|
|
| 407 |
progress(1, desc="Done!")
|
| 408 |
return m._repr_html_(), None, stats_df, dem_html, slope_html, geometry_json, buffer_geometry_json
|
| 409 |
|
|
|
|
|
|
|
|
|
|
| 410 |
def calculate_indices(
|
| 411 |
geometry_json, buffer_geometry_json, veg_indices, evi_vars, date_range,
|
| 412 |
min_year, max_year, progress=gr.Progress()
|
| 413 |
):
|
| 414 |
"""Calculates vegetation indices based on user inputs."""
|
| 415 |
one_time_setup()
|
| 416 |
+
|
| 417 |
if not all([geometry_json, buffer_geometry_json, veg_indices]):
|
| 418 |
return "Please process a file and select at least one index first.", None, None, None
|
| 419 |
|
|
|
|
| 425 |
# Convert to EE geometry
|
| 426 |
ee_geometry = ee.Geometry(json.loads(geometry_gdf.to_crs(4326).to_json())['features'][0]['geometry'])
|
| 427 |
buffer_ee_geometry = ee.Geometry(json.loads(buffer_geometry_gdf.to_crs(4326).to_json())['features'][0]['geometry'])
|
| 428 |
+
|
| 429 |
# Date ranges
|
| 430 |
start_day, start_month = date_range[0].day, date_range[0].month
|
| 431 |
end_day, end_month = date_range[1].day, date_range[1].month
|
|
|
|
| 451 |
if filtered_collection.size().getInfo() == 0:
|
| 452 |
continue
|
| 453 |
|
| 454 |
+
# **MODIFIED**: Add 'Year' column data to the row
|
| 455 |
year_val = int(start_date.split('-')[0])
|
| 456 |
row = {'Year': year_val, 'Date Range': f"{start_date}_to_{end_date}"}
|
| 457 |
|
| 458 |
for veg_index in veg_indices:
|
| 459 |
mosaic = filtered_collection.qualityMosaic(veg_index)
|
| 460 |
+
|
| 461 |
mean_val = mosaic.reduceRegion(reducer=ee.Reducer.mean(), geometry=ee_geometry, scale=10, maxPixels=1e9).get(veg_index).getInfo()
|
| 462 |
buffer_mean_val = mosaic.reduceRegion(reducer=ee.Reducer.mean(), geometry=buffer_ee_geometry, scale=10, maxPixels=1e9).get(veg_index).getInfo()
|
| 463 |
|
| 464 |
row[veg_index] = mean_val
|
| 465 |
row[f"{veg_index}_buffer"] = buffer_mean_val
|
| 466 |
+
row[f"{veg_index}_ratio"] = (mean_val / buffer_mean_val) if buffer_mean_val and buffer_mean_val != 0 else np.nan
|
| 467 |
result_rows.append(row)
|
| 468 |
|
| 469 |
if not result_rows:
|
| 470 |
return "No satellite imagery found for the selected dates.", None, None, None
|
| 471 |
|
| 472 |
+
result_df = pd.DataFrame(result_rows)
|
| 473 |
result_df = result_df.round(3)
|
| 474 |
|
| 475 |
# Create plots
|
| 476 |
plots = []
|
| 477 |
for veg_index in veg_indices:
|
| 478 |
+
# **MODIFIED**: Plot using the new 'Year' column for the x-axis
|
| 479 |
+
plot_cols = [veg_index, f"{veg_index}_buffer", f"{veg_index}_ratio"]
|
| 480 |
existing_plot_cols = [col for col in plot_cols if col in result_df.columns]
|
| 481 |
|
| 482 |
plot_df = result_df[['Year'] + existing_plot_cols].dropna()
|
| 483 |
+
|
| 484 |
if not plot_df.empty:
|
| 485 |
+
fig = px.line(plot_df, x='Year', y=existing_plot_cols, markers=True, title=f"{veg_index} Time Series")
|
| 486 |
fig.update_layout(xaxis_title="Year", yaxis_title="Index Value")
|
| 487 |
# Ensure x-axis ticks are whole numbers for years
|
| 488 |
fig.update_xaxes(dtick=1)
|