UjjwalKGupta commited on
Commit
9e5397a
·
verified ·
1 Parent(s): 940075f

Fix DEM and Slope

Browse files
Files changed (1) hide show
  1. app.py +27 -86
app.py CHANGED
@@ -187,21 +187,33 @@ def get_wayback_data():
187
 
188
  layer_data = []
189
  for layer in layers:
190
- title = layer.find("ows:Title", ns)
191
- resource = layer.find("wmts:ResourceURL", ns) # Tile URL template
192
 
193
- title_text = title.text if title is not None else "N/A"
194
- url_template = resource.get("template") if resource is not None else "N/A"
 
 
 
 
195
 
196
  layer_data.append({"Title": title_text, "ResourceURL_Template": url_template})
197
 
198
  wayback_df = pd.DataFrame(layer_data)
 
 
 
 
 
199
  wayback_df["date"] = pd.to_datetime(wayback_df["Title"].str.extract(r"(\d{4}-\d{2}-\d{2})").squeeze(), errors="coerce")
 
 
 
200
  wayback_df.set_index("date", inplace=True)
201
  wayback_df.sort_index(ascending=False, inplace=True) # Sort with the latest first
202
  return wayback_df
203
 
204
- except (requests.exceptions.RequestException, ET.ParseError, Exception) as e:
205
  print(f"Could not fetch or parse Wayback data: {e}")
206
  return pd.DataFrame() # Return empty dataframe on failure
207
 
@@ -244,6 +256,7 @@ def get_dem_slope_maps(ee_geometry, map_center, zoom=12, wayback_url=None, wayba
244
 
245
  slope_map_html = "<div>No Slope data available for this area.</div>"
246
  try:
 
247
  slope_layer = ee.Terrain.slope(dem_layer).clip(ee_geometry)
248
  slope_vis_params = {"min": 0, "max": 60, "palette": ['#00FF00', '#FFFF00', '#FFA500', '#FF0000']}
249
  slope_map.addLayer(slope_layer, slope_vis_params, "Slope")
@@ -395,84 +408,7 @@ def process_and_display(file_obj, url_str, buffer_m, progress=gr.Progress()):
395
  return m._repr_html_(), None, stats_df, dem_html, slope_html, geometry_json, buffer_geometry_json
396
 
397
 
398
- def calculate_indices(
399
- geometry_json, buffer_geometry_json, veg_indices, evi_vars, date_range,
400
- min_year, max_year, progress=gr.Progress()
401
- ):
402
- """Calculates vegetation indices based on user inputs."""
403
- one_time_setup()
404
-
405
- if not all([geometry_json, buffer_geometry_json, veg_indices]):
406
- return "Please process a file and select at least one index first.", None, None, None
407
-
408
- try:
409
- # Recreate GDFs from JSON
410
- geometry_gdf = gpd.read_file(geometry_json)
411
- buffer_geometry_gdf = gpd.read_file(buffer_json)
412
-
413
- # Convert to EE geometry
414
- ee_geometry = ee.Geometry(json.loads(geometry_gdf.to_crs(4326).to_json())['features'][0]['geometry'])
415
- buffer_ee_geometry = ee.Geometry(json.loads(buffer_geometry_gdf.to_crs(4326).to_json())['features'][0]['geometry'])
416
-
417
- # Date ranges
418
- start_day, start_month = date_range[0].day, date_range[0].month
419
- end_day, end_month = date_range[1].day, date_range[1].month
420
- dates = [
421
- (f"{year}-{start_month:02d}-{start_day:02d}", f"{year}-{end_month:02d}-{end_day:02d}")
422
- for year in range(min_year, max_year + 1)
423
- ]
424
-
425
- # GEE processing
426
- collection = (
427
- ee.ImageCollection("COPERNICUS/S2_SR_HARMONIZED")
428
- .select(
429
- ["B2", "B3", "B4", "B8", "MSK_CLDPRB"],
430
- ["Blue", "Green", "Red", "NIR", "MSK_CLDPRB"]
431
- )
432
- .map(lambda img: add_indices(img, 'NIR', 'Red', 'Blue', 'Green', evi_vars))
433
- )
434
-
435
- result_rows = []
436
- for i, (start_date, end_date) in enumerate(dates):
437
- progress((i + 1) / len(dates), desc=f"Processing {start_date} to {end_date}")
438
- filtered_collection = collection.filterDate(start_date, end_date).filterBounds(ee_geometry)
439
- if filtered_collection.size().getInfo() == 0:
440
- continue
441
-
442
- row = {'daterange': f"{start_date.split('-')[0]}"}
443
- for veg_index in veg_indices:
444
- mosaic = filtered_collection.qualityMosaic(veg_index)
445
-
446
- mean_val = mosaic.reduceRegion(reducer=ee.Reducer.mean(), geometry=ee_geometry, scale=10, maxPixels=1e9).get(veg_index).getInfo()
447
- buffer_mean_val = mosaic.reduceRegion(reducer=ee.Reducer.mean(), geometry=buffer_ee_geometry, scale=10, maxPixels=1e9).get(veg_index).getInfo()
448
-
449
- row[veg_index] = mean_val
450
- row[f"{veg_index}_buffer"] = buffer_mean_val
451
- row[f"{veg_index}_ratio"] = (mean_val / buffer_mean_val) if buffer_mean_val else np.nan
452
- result_rows.append(row)
453
-
454
- if not result_rows:
455
- return "No satellite imagery found for the selected dates.", None, None, None
456
-
457
- result_df = pd.DataFrame(result_rows).set_index('daterange')
458
- result_df = result_df.round(3)
459
-
460
- # Create plots
461
- plots = []
462
- for veg_index in veg_indices:
463
- plot_cols = [col for col in [veg_index, f"{veg_index}_buffer", f"{veg_index}_ratio"] if col in result_df.columns]
464
- plot_df = result_df[plot_cols].dropna()
465
- if not plot_df.empty:
466
- fig = px.line(plot_df, x=plot_df.index, y=plot_df.columns, markers=True, title=f"{veg_index} Time Series")
467
- fig.update_layout(xaxis_title="Year", yaxis_title="Index Value")
468
- plots.append(fig)
469
-
470
- return None, result_df, plots, "Calculation complete."
471
 
472
- except Exception as e:
473
- import traceback
474
- traceback.print_exc()
475
- return f"An error occurred during calculation: {e}", None, None, None
476
 
477
  def calculate_indices(
478
  geometry_json, buffer_geometry_json, veg_indices, evi_vars, date_range,
@@ -487,7 +423,6 @@ def calculate_indices(
487
  try:
488
  # Recreate GDFs from JSON
489
  geometry_gdf = gpd.read_file(geometry_json)
490
- # CORRECTED LINE: Use the correct variable name defined in the function signature
491
  buffer_geometry_gdf = gpd.read_file(buffer_geometry_json)
492
 
493
  # Convert to EE geometry
@@ -519,7 +454,9 @@ def calculate_indices(
519
  if filtered_collection.size().getInfo() == 0:
520
  continue
521
 
522
- row = {'daterange': f"{start_date.split('-')[0]}"}
 
 
523
  for veg_index in veg_indices:
524
  mosaic = filtered_collection.qualityMosaic(veg_index)
525
 
@@ -540,11 +477,15 @@ def calculate_indices(
540
  # Create plots
541
  plots = []
542
  for veg_index in veg_indices:
543
- plot_cols = [col for col in [veg_index, f"{veg_index}_buffer", f"{veg_index}_ratio"] if col in result_df.columns]
544
- plot_df = result_df[plot_cols].dropna()
 
 
545
  if not plot_df.empty:
546
  fig = px.line(plot_df, x=plot_df.index, y=plot_df.columns, markers=True, title=f"{veg_index} Time Series")
547
  fig.update_layout(xaxis_title="Year", yaxis_title="Index Value")
 
 
548
  plots.append(fig)
549
 
550
  return None, result_df, plots, "Calculation complete."
 
187
 
188
  layer_data = []
189
  for layer in layers:
190
+ title_element = layer.find("ows:Title", ns)
191
+ resource_element = layer.find("wmts:ResourceURL", ns)
192
 
193
+ # Skip layer if essential elements are missing
194
+ if title_element is None or resource_element is None:
195
+ continue
196
+
197
+ title_text = title_element.text
198
+ url_template = resource_element.get("template")
199
 
200
  layer_data.append({"Title": title_text, "ResourceURL_Template": url_template})
201
 
202
  wayback_df = pd.DataFrame(layer_data)
203
+
204
+ if wayback_df.empty:
205
+ print("Warning: No valid Wayback layers were found in the XML data.")
206
+ return wayback_df
207
+
208
  wayback_df["date"] = pd.to_datetime(wayback_df["Title"].str.extract(r"(\d{4}-\d{2}-\d{2})").squeeze(), errors="coerce")
209
+ # Drop rows where a date could not be parsed
210
+ wayback_df.dropna(subset=['date'], inplace=True)
211
+
212
  wayback_df.set_index("date", inplace=True)
213
  wayback_df.sort_index(ascending=False, inplace=True) # Sort with the latest first
214
  return wayback_df
215
 
216
+ except (requests.exceptions.RequestException, ET.ParseError, KeyError) as e:
217
  print(f"Could not fetch or parse Wayback data: {e}")
218
  return pd.DataFrame() # Return empty dataframe on failure
219
 
 
256
 
257
  slope_map_html = "<div>No Slope data available for this area.</div>"
258
  try:
259
+ dem_layer = ee.Image("USGS/SRTMGL1_003").resample("bilinear").reproject(crs="EPSG:4326", scale=30)
260
  slope_layer = ee.Terrain.slope(dem_layer).clip(ee_geometry)
261
  slope_vis_params = {"min": 0, "max": 60, "palette": ['#00FF00', '#FFFF00', '#FFA500', '#FF0000']}
262
  slope_map.addLayer(slope_layer, slope_vis_params, "Slope")
 
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,
 
423
  try:
424
  # Recreate GDFs from JSON
425
  geometry_gdf = gpd.read_file(geometry_json)
 
426
  buffer_geometry_gdf = gpd.read_file(buffer_geometry_json)
427
 
428
  # Convert to EE geometry
 
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
 
 
477
  # Create plots
478
  plots = []
479
  for veg_index in veg_indices:
480
+ plot_cols = [veg_index, f"{veg_index}_buffer", f"{veg_index}_ratio"]
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=plot_df.index, y=plot_df.columns, 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)
489
  plots.append(fig)
490
 
491
  return None, result_df, plots, "Calculation complete."