Spaces:
Configuration error
Configuration error
Delete ee_utils.py
Browse files- ee_utils.py +0 -144
ee_utils.py
DELETED
@@ -1,144 +0,0 @@
|
|
1 |
-
import ee
|
2 |
-
import datetime
|
3 |
-
import streamlit as st
|
4 |
-
|
5 |
-
@st.cache_resource
|
6 |
-
def initialize_gee():
|
7 |
-
"""Initialize Google Earth Engine with service account credentials"""
|
8 |
-
try:
|
9 |
-
service_account = 'dehkhodamap-e9f0da4ce9f6514021@ee-esmaeilkiani13877.iam.gserviceaccount.com'
|
10 |
-
credentials_file = 'ee-esmaeilkiani13877-cfdea6eaf411 (4).json'
|
11 |
-
credentials = ee.ServiceAccountCredentials(service_account, credentials_file)
|
12 |
-
ee.Initialize(credentials)
|
13 |
-
return True
|
14 |
-
except Exception as e:
|
15 |
-
st.error(f"Error connecting to Google Earth Engine: {e}")
|
16 |
-
return False
|
17 |
-
|
18 |
-
def calculate_indices(image):
|
19 |
-
"""Calculate various vegetation and moisture indices from Sentinel-2 imagery"""
|
20 |
-
# NDVI (Normalized Difference Vegetation Index)
|
21 |
-
ndvi = image.normalizedDifference(['B8', 'B4']).rename('NDVI')
|
22 |
-
|
23 |
-
# EVI (Enhanced Vegetation Index)
|
24 |
-
evi = image.expression(
|
25 |
-
'2.5 * ((NIR - RED) / (NIR + 6 * RED - 7.5 * BLUE + 1))',
|
26 |
-
{
|
27 |
-
'NIR': image.select('B8'),
|
28 |
-
'RED': image.select('B4'),
|
29 |
-
'BLUE': image.select('B2')
|
30 |
-
}
|
31 |
-
).rename('EVI')
|
32 |
-
|
33 |
-
# NDMI (Normalized Difference Moisture Index)
|
34 |
-
ndmi = image.normalizedDifference(['B8', 'B11']).rename('NDMI')
|
35 |
-
|
36 |
-
# LAI (Leaf Area Index) - simplified model
|
37 |
-
lai = image.expression(
|
38 |
-
'3.618 * EVI - 0.118',
|
39 |
-
{
|
40 |
-
'EVI': evi
|
41 |
-
}
|
42 |
-
).rename('LAI')
|
43 |
-
|
44 |
-
# Biomass - simplified model based on LAI
|
45 |
-
biomass = image.expression(
|
46 |
-
'0.8 * LAI + 0.2',
|
47 |
-
{
|
48 |
-
'LAI': lai
|
49 |
-
}
|
50 |
-
).rename('Biomass')
|
51 |
-
|
52 |
-
# MSI (Moisture Stress Index)
|
53 |
-
msi = image.expression(
|
54 |
-
'SWIR1 / NIR',
|
55 |
-
{
|
56 |
-
'SWIR1': image.select('B11'),
|
57 |
-
'NIR': image.select('B8')
|
58 |
-
}
|
59 |
-
).rename('MSI')
|
60 |
-
|
61 |
-
# Chlorophyll Index
|
62 |
-
chlorophyll = image.expression(
|
63 |
-
'NIR / RED - 1',
|
64 |
-
{
|
65 |
-
'NIR': image.select('B8'),
|
66 |
-
'RED': image.select('B4')
|
67 |
-
}
|
68 |
-
).rename('Chlorophyll')
|
69 |
-
|
70 |
-
# Add all indices to the image
|
71 |
-
return image.addBands([ndvi, evi, ndmi, lai, biomass, msi, chlorophyll])
|
72 |
-
|
73 |
-
def get_sentinel_imagery(start_date, end_date, aoi):
|
74 |
-
"""Retrieve and process Sentinel-2 imagery for the specified time period and area"""
|
75 |
-
# Filter Sentinel-2 collection
|
76 |
-
s2 = ee.ImageCollection('COPERNICUS/S2_SR') \
|
77 |
-
.filterDate(start_date, end_date) \
|
78 |
-
.filterBounds(aoi) \
|
79 |
-
.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 20))
|
80 |
-
|
81 |
-
if s2.size().getInfo() == 0:
|
82 |
-
return None
|
83 |
-
|
84 |
-
# Calculate median image
|
85 |
-
s2_median = s2.median()
|
86 |
-
|
87 |
-
# Calculate indices
|
88 |
-
s2_indices = calculate_indices(s2_median)
|
89 |
-
|
90 |
-
return s2_indices
|
91 |
-
|
92 |
-
def create_time_series(aoi, index_name, start_date, end_date, interval='day'):
|
93 |
-
"""Create a time series of index values for the specified area and time period"""
|
94 |
-
# Define time intervals
|
95 |
-
if interval == 'day':
|
96 |
-
step = 1
|
97 |
-
unit = 'day'
|
98 |
-
elif interval == 'week':
|
99 |
-
step = 7
|
100 |
-
unit = 'day'
|
101 |
-
elif interval == 'month':
|
102 |
-
step = 1
|
103 |
-
unit = 'month'
|
104 |
-
|
105 |
-
# Create lists for dates and values
|
106 |
-
dates = []
|
107 |
-
values = []
|
108 |
-
|
109 |
-
current_date = start_date
|
110 |
-
while current_date <= end_date:
|
111 |
-
next_date = current_date + datetime.timedelta(days=step) if unit == 'day' else \
|
112 |
-
datetime.date(current_date.year + (current_date.month + step - 1) // 12,
|
113 |
-
(current_date.month + step - 1) % 12 + 1,
|
114 |
-
1)
|
115 |
-
|
116 |
-
# Get imagery for this time period
|
117 |
-
image = get_sentinel_imagery(current_date.strftime('%Y-%m-%d'),
|
118 |
-
next_date.strftime('%Y-%m-%d'),
|
119 |
-
aoi)
|
120 |
-
|
121 |
-
if image is not None:
|
122 |
-
# Calculate mean index value for the area
|
123 |
-
mean_value = image.select(index_name).reduceRegion(
|
124 |
-
reducer=ee.Reducer.mean(),
|
125 |
-
geometry=aoi,
|
126 |
-
scale=10
|
127 |
-
).get(index_name).getInfo()
|
128 |
-
|
129 |
-
if mean_value is not None:
|
130 |
-
dates.append(current_date.strftime('%Y-%m-%d'))
|
131 |
-
values.append(mean_value)
|
132 |
-
|
133 |
-
current_date = next_date
|
134 |
-
|
135 |
-
# Create dataframe for plotting
|
136 |
-
if len(dates) > 0:
|
137 |
-
import pandas as pd
|
138 |
-
df = pd.DataFrame({
|
139 |
-
'Date': dates,
|
140 |
-
index_name: values
|
141 |
-
})
|
142 |
-
return df
|
143 |
-
else:
|
144 |
-
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|