Esmaeilkiani commited on
Commit
81fc5b0
1 Parent(s): 425d71b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -60
app.py CHANGED
@@ -1,76 +1,78 @@
1
  import streamlit as st
2
- import pandas as pd
3
  import ee
4
  import geemap
 
5
 
6
- # Earth Engine authentication
7
  service_account = 'earth-engine-service-account@ee-esmaeilkiani1387.iam.gserviceaccount.com'
8
  credentials = ee.ServiceAccountCredentials(service_account, 'ee-esmaeilkiani1387-1b2c5e812a1d.json')
9
  ee.Initialize(credentials)
10
 
11
- # Load the CSV file
12
- @st.cache_data
13
- def load_data():
14
- return pd.read_csv('Farm_Details_Export.csv')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
- df = load_data()
 
 
 
 
 
 
17
 
18
- st.title('Farm Finder and Sentinel-2 Imagery Viewer')
 
19
 
20
- # Farm search functionality
21
- farm_name = st.text_input('Enter farm name:')
22
- if farm_name:
23
- farm = df[df['farm_name'].str.contains(farm_name, case=False)]
24
- if not farm.empty:
25
- st.write(farm)
26
- else:
27
- st.write('Farm not found')
28
 
29
- # Sentinel-2 index selection
30
- indices = {'NDVI': 'NDVI', 'EVI': 'EVI', 'NDWI': '(B3 - B8) / (B3 + B8)'}
31
- selected_index = st.selectbox('Select Sentinel-2 index:', list(indices.keys()))
32
 
33
- # Date range selection
34
- start_date = st.date_input('Start date')
35
- end_date = st.date_input('End date')
 
 
 
36
 
37
- if st.button('Show Map'):
38
- # Create a map centered on the mean coordinates of all farms
39
- center_lat = df['latitude'].mean()
40
- center_lon = df['longitude'].mean()
41
- m = geemap.Map(center=[center_lat, center_lon], zoom=10)
42
-
43
- # Add Sentinel-2 imagery with selected index
44
- s2_collection = (ee.ImageCollection('COPERNICUS/S2_SR')
45
- .filterDate(start_date, end_date)
46
- .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 20)))
47
-
48
- if selected_index == 'NDVI':
49
- vis_params = {'min': 0, 'max': 1, 'palette': ['red', 'yellow', 'green']}
50
- elif selected_index == 'EVI':
51
- vis_params = {'min': -1, 'max': 1, 'palette': ['blue', 'white', 'green']}
52
- else: # NDWI
53
- vis_params = {'min': -1, 'max': 1, 'palette': ['red', 'white', 'blue']}
54
-
55
- def add_index(image):
56
- index = image.expression(indices[selected_index], {
57
- 'B3': image.select('B3'),
58
- 'B4': image.select('B4'),
59
- 'B8': image.select('B8'),
60
- }).rename(selected_index)
61
- return image.addBands(index)
62
-
63
- s2_with_index = s2_collection.map(add_index)
64
- median_index = s2_with_index.select(selected_index).median()
65
- m.addLayer(median_index, vis_params, selected_index)
66
-
67
- # Add farm locations to the map
68
- for idx, row in df.iterrows():
69
- info = f"Farm: {row['farm_name']}<br>Age: {row['age']}<br>Variety: {row['variety']}"
70
- m.add_marker(location=[row['latitude'], row['longitude']], popup=info)
71
-
72
- # Display the map
73
  m.to_streamlit(height=600)
74
 
75
- if __name__ == '__main__':
76
- st.write('App is running')
 
1
  import streamlit as st
 
2
  import ee
3
  import geemap
4
+ import datetime
5
 
6
+ # Authenticate Earth Engine
7
  service_account = 'earth-engine-service-account@ee-esmaeilkiani1387.iam.gserviceaccount.com'
8
  credentials = ee.ServiceAccountCredentials(service_account, 'ee-esmaeilkiani1387-1b2c5e812a1d.json')
9
  ee.Initialize(credentials)
10
 
11
+ # Define the indices and their corresponding functions
12
+ INDICES = {
13
+ 'NDVI': lambda img: img.normalizedDifference(['B8', 'B4']).rename('NDVI'),
14
+ 'EVI': lambda img: img.expression(
15
+ '2.5 * ((NIR - RED) / (NIR + 6 * RED - 7.5 * BLUE + 1))',
16
+ {
17
+ 'NIR': img.select('B8'),
18
+ 'RED': img.select('B4'),
19
+ 'BLUE': img.select('B2')
20
+ }
21
+ ).rename('EVI'),
22
+ 'SAVI': lambda img: img.expression(
23
+ '((NIR - RED) / (NIR + RED + 0.5)) * (1.5)',
24
+ {
25
+ 'NIR': img.select('B8'),
26
+ 'RED': img.select('B4')
27
+ }
28
+ ).rename('SAVI'),
29
+ 'NDMI': lambda img: img.normalizedDifference(['B8', 'B11']).rename('NDMI')
30
+ }
31
 
32
+ # Define color palettes for each index
33
+ COLOR_PALETTES = {
34
+ 'NDVI': ['FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163', '99B718', '74A901', '66A000', '529400', '3E8601', '207401', '056201', '004C00', '023B01', '012E01', '011D01', '011301'],
35
+ 'EVI': ['FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163', '99B718', '74A901', '66A000', '529400', '3E8601', '207401', '056201', '004C00', '023B01', '012E01', '011D01', '011301'],
36
+ 'SAVI': ['#D73027', '#F46D43', '#FDAE61', '#FEE08B', '#FFFFBF', '#D9EF8B', '#A6D96A', '#66BD63', '#1A9850'],
37
+ 'NDMI': ['#D73027', '#F46D43', '#FDAE61', '#FEE08B', '#FFFFBF', '#D9EF8B', '#A6D96A', '#66BD63', '#1A9850']
38
+ }
39
 
40
+ # Streamlit app
41
+ st.title('Google Earth Engine Indices Viewer')
42
 
43
+ # User inputs
44
+ selected_indices = st.multiselect('Select two indices', list(INDICES.keys()), max_selections=2)
45
+ start_date = st.date_input('Start date', datetime.date(2022, 1, 1))
46
+ end_date = st.date_input('End date', datetime.date(2022, 12, 31))
 
 
 
 
47
 
48
+ if len(selected_indices) == 2 and start_date < end_date:
49
+ # Define the area of interest
50
+ aoi = ee.Geometry.Point([48.7312815, 31.5200749]).buffer(5000)
51
 
52
+ # Get Sentinel-2 imagery
53
+ s2 = ee.ImageCollection('COPERNICUS/S2_SR') \
54
+ .filterBounds(aoi) \
55
+ .filterDate(start_date, end_date) \
56
+ .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 20)) \
57
+ .sort('CLOUDY_PIXEL_PERCENTAGE')
58
 
59
+ # Calculate indices
60
+ for index in selected_indices:
61
+ s2 = s2.map(lambda img: img.addBands(INDICES[index](img)))
62
+
63
+ # Reduce the collection to a single image (median)
64
+ image = s2.select(selected_indices).median()
65
+
66
+ # Create a map
67
+ m = geemap.Map()
68
+ m.centerObject(aoi, 12)
69
+
70
+ # Add layers to the map
71
+ for index in selected_indices:
72
+ m.addLayer(image.select(index), {'min': 0, 'max': 1, 'palette': COLOR_PALETTES[index]}, index)
73
+
74
+ # Add the map to the Streamlit app
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
  m.to_streamlit(height=600)
76
 
77
+ else:
78
+ st.warning('Please select two indices and ensure the start date is before the end date.')