YashMK89 commited on
Commit
cf7d33a
·
verified ·
1 Parent(s): ad87b30

update app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -11
app.py CHANGED
@@ -14,12 +14,44 @@ div.stButton > button:first-child {
14
  unsafe_allow_html=True,
15
  )
16
 
17
- # Load JSON data from file
 
 
 
 
 
 
 
 
18
  with open("sentinel_datasets.json") as f:
19
  data = json.load(f)
20
 
21
- # Display title
22
- st.title("Sentinel Dataset Selector")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
  # Step 1: Select main dataset category
25
  main_selection = st.selectbox("Select Sentinel Dataset Category", list(data.keys()))
@@ -34,10 +66,8 @@ if main_selection:
34
  bands = sub_options[sub_selection]
35
  st.write("Select up to 3 bands:")
36
 
37
- # Create a list to store selected bands
38
- selected_bands = []
39
-
40
  # Display bands in a side-by-side layout with three columns
 
41
  cols = st.columns(3)
42
  for i, band in enumerate(bands):
43
  with cols[i % 3]:
@@ -47,9 +77,50 @@ if main_selection:
47
  if len(selected_bands) == 3:
48
  break
49
 
50
- # Show a warning if more than 3 bands are selected
51
- if len(selected_bands) > 3:
52
- st.warning("You can select a maximum of 3 bands.")
53
-
54
- # Display selected bands
55
  st.write("Selected Bands:", selected_bands)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  unsafe_allow_html=True,
15
  )
16
 
17
+ import streamlit as st
18
+ import json
19
+ import ee
20
+ import geemap
21
+
22
+ # Initialize the Earth Engine library
23
+ ee.Initialize()
24
+
25
+ # Load JSON data for Sentinel dataset options
26
  with open("sentinel_datasets.json") as f:
27
  data = json.load(f)
28
 
29
+ # Define index calculation functions
30
+ def calculate_ndvi(image):
31
+ """Calculate NDVI."""
32
+ return image.normalizedDifference(['B5', 'B4']).rename('NDVI')
33
+
34
+ def calculate_ndwi(image):
35
+ """Calculate NDWI."""
36
+ return image.normalizedDifference(['B3', 'B5']).rename('NDWI')
37
+
38
+ def calculate_avg_no2(image):
39
+ """Calculate average NO2."""
40
+ return image.select('NO2').reduceRegion(
41
+ reducer=ee.Reducer.mean(),
42
+ geometry=image.geometry(),
43
+ scale=1000
44
+ ).get('NO2')
45
+
46
+ def calculate_custom_formula(image, formula):
47
+ """Apply a custom formula to the image."""
48
+ return image.expression(formula).rename('Custom Index')
49
+
50
+ # Streamlit UI - Title
51
+ st.title("Sentinel Dataset Selector and Earth Engine Index Calculator")
52
+
53
+ # Sentinel Dataset Selector Section
54
+ st.header("Sentinel Dataset Selector")
55
 
56
  # Step 1: Select main dataset category
57
  main_selection = st.selectbox("Select Sentinel Dataset Category", list(data.keys()))
 
66
  bands = sub_options[sub_selection]
67
  st.write("Select up to 3 bands:")
68
 
 
 
 
69
  # Display bands in a side-by-side layout with three columns
70
+ selected_bands = []
71
  cols = st.columns(3)
72
  for i, band in enumerate(bands):
73
  with cols[i % 3]:
 
77
  if len(selected_bands) == 3:
78
  break
79
 
80
+ # Show selected bands
 
 
 
 
81
  st.write("Selected Bands:", selected_bands)
82
+
83
+ # Earth Engine Index Calculator Section
84
+ st.header("Earth Engine Index Calculator")
85
+
86
+ # Step 1: Select Dataset for Index Calculation
87
+ dataset_options = {
88
+ "Sentinel-2": "COPERNICUS/S2",
89
+ "Landsat 8": "LANDSAT/LC08/C01/T1_SR",
90
+ "NO2 Concentration": "COPERNICUS/S5P/OFFL/L3_NO2"
91
+ }
92
+ dataset_choice = st.selectbox("Select a Dataset for Index Calculation", list(dataset_options.keys()))
93
+ dataset_id = dataset_options[dataset_choice]
94
+
95
+ # Load dataset as an EE ImageCollection
96
+ collection = ee.ImageCollection(dataset_id).filterDate('2020-01-01', '2020-12-31')
97
+
98
+ # Step 2: Select Index or Custom Formula
99
+ index_choice = st.selectbox("Select an Index or Enter Custom Formula", ['NDVI', 'NDWI', 'Average NO₂', 'Custom Formula'])
100
+
101
+ # Step 3: Custom Formula Input
102
+ custom_formula = ""
103
+ if index_choice == 'Custom Formula':
104
+ custom_formula = st.text_input("Enter Custom Formula (e.g., 'B5 - B4 / B5 + B4')")
105
+
106
+ # Step 4: Calculate and Display Results
107
+ result_image = None
108
+ if st.button("Calculate Index"):
109
+ image = collection.first() # Using the first image in the collection as an example
110
+
111
+ if index_choice == 'NDVI':
112
+ result_image = calculate_ndvi(image)
113
+ elif index_choice == 'NDWI':
114
+ result_image = calculate_ndwi(image)
115
+ elif index_choice == 'Average NO₂':
116
+ result = calculate_avg_no2(image)
117
+ st.write("Average NO₂ Concentration:", result.getInfo())
118
+ elif index_choice == 'Custom Formula' and custom_formula:
119
+ result_image = calculate_custom_formula(image, custom_formula)
120
+
121
+ # If an index image is generated, add it to the map
122
+ if result_image:
123
+ Map = geemap.Map()
124
+ Map.centerObject(image, 10)
125
+ Map.addLayer(result_image, {}, index_choice)
126
+ Map.to_streamlit(height=600)