UjjwalKGupta commited on
Commit
aee1418
·
verified ·
1 Parent(s): f75123f

Merge ML Indices

Browse files
Files changed (1) hide show
  1. app.py +101 -15
app.py CHANGED
@@ -91,13 +91,21 @@ container = st.container()
91
 
92
  with st.expander("Advanced Settings"):
93
  st.write("Select the vegetation indices to calculate:")
94
- all_veg_indices = ["NDVI", "EVI", "EVI2"]
95
  formulas = {
 
96
  "NDVI": r"$\frac{NIR - Red}{NIR + Red}$",
97
  "EVI": r"$G \times \frac{NIR - Red}{NIR + C1 \times Red - C2 \times Blue + L}$",
98
  "EVI2": r"$G \times \frac{NIR - Red}{NIR + L + C \times Red}$",
 
 
 
 
 
 
 
99
  }
100
- defaults = [True, False, False]
101
  veg_indices = []
102
  for veg_index, default in zip(all_veg_indices, defaults):
103
  if st.checkbox(f"{veg_index} = {formulas[veg_index]}", value=default):
@@ -338,10 +346,11 @@ if submit:
338
  "scale": 10,
339
  "collection": ee.ImageCollection("COPERNICUS/S2_SR_HARMONIZED")
340
  .select(
341
- ["B2", "B4", "B8", "MSK_CLDPRB", "TCI_R", "TCI_G", "TCI_B"],
342
- ["Blue", "Red", "NIR", "MSK_CLDPRB", "R", "G", "B"],
343
  )
344
- .map(lambda image: add_indices(image, nir_band="NIR", red_band="Red", blue_band="Blue", evi_vars=evi_vars)),
 
345
  },
346
  }
347
  satellite = list(satellites.keys())[0]
@@ -444,11 +453,6 @@ if "result" in st.session_state:
444
  "Year 2", result_df.index, index=len(result_df.index) - 1, format_func=lambda x: daterange_str_to_year(x)
445
  )
446
 
447
- vis_params = {"min": 0, "max": 1, "palette": ["white", "green"]} # Example visualisation for Sentinel-2
448
-
449
- # Create a colormap and name it as NDVI
450
- colormap = cm.LinearColormap(colors=vis_params["palette"], vmin=vis_params["min"], vmax=vis_params["max"])
451
-
452
  for veg_index in veg_indices:
453
  st.write(f"<h3><div style='text-align: center;'>{veg_index}</div></h3>", unsafe_allow_html=True)
454
  cols = st.columns(2)
@@ -469,9 +473,9 @@ if "result" in st.session_state:
469
  else:
470
  raise ValueError(f"Unknown satellite: {satellite}")
471
 
472
- if veg_index == "NDVI":
473
  bins = [-1, 0, 0.1, 0.2, 0.3, 0.4, 0.5, 1]
474
- histogram, bin_edges = get_histogram(mosaic.select(veg_index), ee_geometry, bins)
475
  total_pix = np.sum(histogram)
476
  formatted_histogram = [f"{h*100/total_pix:.2f}" for h in histogram]
477
  print(histogram, bin_edges, bins, formatted_histogram)
@@ -496,12 +500,94 @@ if "result" in st.session_state:
496
  }
497
  m.add_layer(mosaic.select(veg_index).clip(outer_ee_geometry), ndvi_vis_params)
498
 
499
- # add colorbar
500
- # m.add_colorbar(colors=["#000000", "#00FF00"], vmin=0.0, vmax=1.0)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
501
 
502
- if veg_index != "NDVI":
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
503
  m.add_layer(mosaic.select(veg_index).clip(outer_ee_geometry), vis_params)
504
  m.add_child(colormap)
 
505
  add_geometry_to_maps([m], geometry_gdf, buffer_geometry_gdf)
506
  m.to_streamlit()
507
 
 
91
 
92
  with st.expander("Advanced Settings"):
93
  st.write("Select the vegetation indices to calculate:")
94
+ all_veg_indices = ["GujEVI", "NDVI", "EVI", "EVI2", "RandomForest", "GujVDI", "MNDWI", "SAVI", "MVI", "NBR", "GCI"]
95
  formulas = {
96
+ "GujEVI": r"$0.5 \times \frac{NIR - Red}{NIR + 6 \times Red - 8.25 \times Blue + 0.01}, \text{(Optimized EVI for Gujarat)}$",
97
  "NDVI": r"$\frac{NIR - Red}{NIR + Red}$",
98
  "EVI": r"$G \times \frac{NIR - Red}{NIR + C1 \times Red - C2 \times Blue + L}$",
99
  "EVI2": r"$G \times \frac{NIR - Red}{NIR + L + C \times Red}$",
100
+ "RandomForest": "ML based Classification",
101
+ "GujVDI": r"$2.29 \times \frac{-3.98 \left(\frac{Blue}{NIR}\right) + 12.54 \left(\frac{Green}{NIR}\right) - 5.49 \left(\frac{Red}{NIR}\right) - 0.19}{-21.87 \left(\frac{Blue}{NIR}\right) + 12.4 \left(\frac{Green}{NIR}\right) + 19.98 \left(\frac{Red}{NIR}\right) + 1}$",
102
+ "MNDWI": r"$\frac{Green - SWIR}{Green + SWIR}$",
103
+ "SAVI": r"$\frac{(1 + L) \times (NIR - Red)}{NIR + Red + L}, \text{ where } L=0.5$",
104
+ "MVI": r"$\frac{NIR - (Green + SWIR)}{NIR + (Green + SWIR)}$",
105
+ "NBR": r"$\frac{NIR - SWIR2}{NIR + SWIR2}$",
106
+ "GCI": r"$\frac{NIR - Green}{Green}$",
107
  }
108
+ defaults = [True, False, False, False, False, False, False, False, False, False, False]
109
  veg_indices = []
110
  for veg_index, default in zip(all_veg_indices, defaults):
111
  if st.checkbox(f"{veg_index} = {formulas[veg_index]}", value=default):
 
346
  "scale": 10,
347
  "collection": ee.ImageCollection("COPERNICUS/S2_SR_HARMONIZED")
348
  .select(
349
+ ["B2", "B3", "B4", "B8", "B11", "B12", "MSK_CLDPRB", "TCI_R", "TCI_G", "TCI_B"],
350
+ ["Blue", "Green", "Red", "NIR", "SWIR", "SWIR2", "MSK_CLDPRB", "R", "G", "B"],
351
  )
352
+ .map(lambda image: add_indices(image, nir_band="NIR", red_band="Red", blue_band="Blue", green_band="Green",
353
+ swir_band="SWIR", swir2_band = "SWIR2", evi_vars=evi_vars)),
354
  },
355
  }
356
  satellite = list(satellites.keys())[0]
 
453
  "Year 2", result_df.index, index=len(result_df.index) - 1, format_func=lambda x: daterange_str_to_year(x)
454
  )
455
 
 
 
 
 
 
456
  for veg_index in veg_indices:
457
  st.write(f"<h3><div style='text-align: center;'>{veg_index}</div></h3>", unsafe_allow_html=True)
458
  cols = st.columns(2)
 
473
  else:
474
  raise ValueError(f"Unknown satellite: {satellite}")
475
 
476
+ if veg_index == "Test":
477
  bins = [-1, 0, 0.1, 0.2, 0.3, 0.4, 0.5, 1]
478
+ histogram, bin_edges = get_histogram(veg_index, mosaic.select(veg_index), ee_geometry, bins)
479
  total_pix = np.sum(histogram)
480
  formatted_histogram = [f"{h*100/total_pix:.2f}" for h in histogram]
481
  print(histogram, bin_edges, bins, formatted_histogram)
 
500
  }
501
  m.add_layer(mosaic.select(veg_index).clip(outer_ee_geometry), ndvi_vis_params)
502
 
503
+ elif veg_index in ["NDVI", "RandomForest", "GujVDI", "GujEVI", "EVI", "EVI2", "SAVI"]:
504
+ bins = [0,0.2,0.4,0.6,0.8,1]
505
+ histogram, bin_edges = get_histogram(veg_index, mosaic.select(veg_index), ee_geometry, bins)
506
+ total_pix = np.sum(histogram)
507
+ formatted_histogram = [f"{h*100/total_pix:.2f}" for h in histogram]
508
+ print(histogram, bin_edges, bins, formatted_histogram)
509
+ m.add_legend(
510
+ title=f"{veg_index} Class/Value",
511
+ legend_dict={
512
+ #"<0:Waterbody ({}%)".format(formatted_histogram[0]): "#0000FF",
513
+ "0-0.2: Open/Sparse Vegetation Density ({}%)".format(formatted_histogram[0]): "#FF0000",
514
+ "0.2-0.4: Low Vegetation Density ({}%)".format(formatted_histogram[1]): "#FFFF00",
515
+ "0.4-0.6: Moderate Vegetation Density ({}%)".format(formatted_histogram[2]): "#FFA500",
516
+ "0.6-0.8: Dense Vegetation ({}%)".format(formatted_histogram[3]): "#00FE00",
517
+ "0.8-1: Very Dense Vegetation ({}%)".format(formatted_histogram[4]): "#00A400",
518
+ #">0.5: Very Dense ({}%)".format(formatted_histogram[6]): "#006D00",
519
+ },
520
+ position="bottomright",
521
+ draggable=False,
522
+ )
523
+ ind_vis_params = {
524
+ "min": 0,
525
+ "max": 1,
526
+ "palette": ["#FF0000", "#FFFF00", "#FFA500", "#00FE00", "#00A400"],
527
+ }
528
+ m.add_layer(mosaic.select(veg_index).clip(outer_ee_geometry), ind_vis_params)
529
 
530
+ elif veg_index in ["MNDWI"]:
531
+ bins = [-0.8, -0.6, -0.4, -0.2, 0.0, 0.2, 1]
532
+ histogram, bin_edges = get_histogram(veg_index, mosaic.select(veg_index), ee_geometry, bins)
533
+ total_pix = np.sum(histogram)
534
+ formatted_histogram = [f"{h*100/total_pix:.2f}" for h in histogram]
535
+ print(histogram, bin_edges, bins, formatted_histogram)
536
+ m.add_legend(
537
+ title=f"{veg_index} Class/Value",
538
+ legend_dict={
539
+ #"<0:Waterbody ({}%)".format(formatted_histogram[0]): "#0000FF",
540
+ "-0.8 to -0.6: ({}%)".format(formatted_histogram[0]): "#FF0000",
541
+ "-0.6 to -0.4: ({}%)".format(formatted_histogram[1]): "#FFFF00",
542
+ "-0.4 to -0.2: ({}%)".format(formatted_histogram[2]): "#FFA500",
543
+ "-0.2 to 0.0: ({}%)".format(formatted_histogram[3]): "#00FE00",
544
+ "0.0 to 0.2: ({}%)".format(formatted_histogram[4]): "#00A400",
545
+ ">0.2: Very Dense ({}%)".format(formatted_histogram[5]): "#006D00",
546
+ },
547
+ position="bottomright",
548
+ draggable=False,
549
+ )
550
+ ind_vis_params = {
551
+ "min": -0.8,
552
+ "max": 1,
553
+ "palette": ["#FF0000", "#FFFF00", "#FFA500", "#00FE00", "#00A400", "#006D00"],
554
+ }
555
+ m.add_layer(mosaic.select(veg_index).clip(outer_ee_geometry), ind_vis_params)
556
+
557
+ elif veg_index in ["NBR"]:
558
+ bins = [-1, 0.1, 0.275, 0.45, 0.65, 1]
559
+ histogram, bin_edges = get_histogram(veg_index, mosaic.select(veg_index), ee_geometry, bins)
560
+ total_pix = np.sum(histogram)
561
+ formatted_histogram = [f"{h*100/total_pix:.2f}" for h in histogram]
562
+ print(histogram, bin_edges, bins, formatted_histogram)
563
+ m.add_legend(
564
+ title=f"{veg_index} Class/Value",
565
+ legend_dict={
566
+ #"<0:Waterbody ({}%)".format(formatted_histogram[0]): "#0000FF",
567
+ "-1 to 0.1: Unburned ({}%)".format(formatted_histogram[0]): "#00A400" ,
568
+ "0.1 to 0.275: Low-severity burn ({}%)".format(formatted_histogram[1]): "#00FE00",
569
+ "0.275 to 0.45: Moderate-to-low severity burn ({}%)".format(formatted_histogram[2]): "#FFA500",
570
+ "0.45 to 0.66: Moderate-to-high severity burn ({}%)".format(formatted_histogram[3]): "#FFFF00",
571
+ "0.66 to 1: High-severity burn ({}%)".format(formatted_histogram[4]): "#FF0000",
572
+ },
573
+ position="bottomright",
574
+ draggable=False,
575
+ )
576
+ ind_vis_params = {
577
+ "min": -1,
578
+ "max": 1,
579
+ "palette": ["#FF0000", "#FFFF00", "#FFA500", "#00FE00", "#00A400", "#006D00"],
580
+ }
581
+ m.add_layer(mosaic.select(veg_index).clip(outer_ee_geometry), ind_vis_params)
582
+
583
+
584
+ else:
585
+ # For GCI
586
+ vis_params = {"min": 1, "max": 10, "palette": ["white", "green"]}
587
+ colormap = cm.LinearColormap(colors=vis_params["palette"], vmin=vis_params["min"], vmax=vis_params["max"])
588
  m.add_layer(mosaic.select(veg_index).clip(outer_ee_geometry), vis_params)
589
  m.add_child(colormap)
590
+
591
  add_geometry_to_maps([m], geometry_gdf, buffer_geometry_gdf)
592
  m.to_streamlit()
593