Spaces:
Runtime error
Runtime error
Esmaeilkiani
commited on
Commit
•
81fc5b0
1
Parent(s):
425d71b
Update app.py
Browse files
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
|
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 |
-
#
|
12 |
-
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
-
|
|
|
19 |
|
20 |
-
#
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
if not farm.empty:
|
25 |
-
st.write(farm)
|
26 |
-
else:
|
27 |
-
st.write('Farm not found')
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
|
33 |
-
#
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
36 |
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
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 |
-
|
76 |
-
st.
|
|
|
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.')
|