Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,112 +1,159 @@
|
|
1 |
-
import ee
|
2 |
-
import json
|
3 |
-
import pandas as pd
|
4 |
import streamlit as st
|
5 |
-
import
|
6 |
-
from branca.colormap import linear
|
7 |
import plotly.express as px
|
8 |
-
import
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
#
|
11 |
-
|
|
|
12 |
ee.Initialize(credentials)
|
13 |
|
14 |
-
#
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
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 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
def
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import pandas as pd
|
|
|
3 |
import plotly.express as px
|
4 |
+
import folium
|
5 |
+
from streamlit_folium import folium_static
|
6 |
+
import ee
|
7 |
+
import datetime
|
8 |
+
import os
|
9 |
|
10 |
+
# Set up Google Earth Engine
|
11 |
+
service_account = 'dehkhodamap-e9f0da4ce9f6514021@ee-esmaeilkiani13877.iam.gserviceaccount.com'
|
12 |
+
credentials = ee.ServiceAccountCredentials(service_account, 'ee-esmaeilkiani13877-cfdea6eaf411.json')
|
13 |
ee.Initialize(credentials)
|
14 |
|
15 |
+
# Load farm data
|
16 |
+
@st.cache_data
|
17 |
+
def load_farm_data():
|
18 |
+
return pd.read_csv('farm_coordinates.csv')
|
19 |
+
|
20 |
+
farms = load_farm_data()
|
21 |
+
|
22 |
+
# Streamlit app
|
23 |
+
st.title('Sugarcane Farm Dashboard')
|
24 |
+
|
25 |
+
# Sidebar
|
26 |
+
st.sidebar.title('Navigation')
|
27 |
+
page = st.sidebar.radio('Go to', ['Map', 'Comparative Charts', 'Weekly Reports'])
|
28 |
+
|
29 |
+
if page == 'Map':
|
30 |
+
st.header('Sugarcane Farm Map')
|
31 |
+
|
32 |
+
# Create map
|
33 |
+
m = folium.Map(location=[farms['latitude'].mean(), farms['longitude'].mean()], zoom_start=10)
|
34 |
+
|
35 |
+
# Add markers for each farm
|
36 |
+
for idx, row in farms.iterrows():
|
37 |
+
folium.Marker(
|
38 |
+
location=[row['latitude'], row['longitude']],
|
39 |
+
popup=f"Farm: {row['name']}, Age: {row['age']}, Variety: {row['variety']}",
|
40 |
+
icon=folium.Icon(color='green', icon='leaf')
|
41 |
+
).add_to(m)
|
42 |
+
|
43 |
+
# Display map
|
44 |
+
folium_static(m)
|
45 |
+
|
46 |
+
# Farm search
|
47 |
+
search_farm = st.text_input('Search for a farm:')
|
48 |
+
if search_farm:
|
49 |
+
farm = farms[farms['name'].str.contains(search_farm, case=False)]
|
50 |
+
if not farm.empty:
|
51 |
+
st.write(f"Farm found: {farm['name'].values[0]}")
|
52 |
+
st.map(farm)
|
53 |
+
else:
|
54 |
+
st.write("Farm not found.")
|
55 |
+
|
56 |
+
elif page == 'Comparative Charts':
|
57 |
+
st.header('Comparative Charts')
|
58 |
+
|
59 |
+
# Function to get NDVI data
|
60 |
+
def get_ndvi_data(geometry, start_date, end_date):
|
61 |
+
collection = ee.ImageCollection('COPERNICUS/S2_SR') \
|
62 |
+
.filterBounds(geometry) \
|
63 |
+
.filterDate(start_date, end_date) \
|
64 |
+
.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 20))
|
65 |
+
|
66 |
+
def addNDVI(image):
|
67 |
+
ndvi = image.normalizedDifference(['B8', 'B4']).rename('NDVI')
|
68 |
+
return image.addBands(ndvi)
|
69 |
+
|
70 |
+
ndvi_collection = collection.map(addNDVI)
|
71 |
+
ndvi_values = ndvi_collection.select('NDVI').mean().reduceRegion(
|
72 |
+
reducer=ee.Reducer.mean(),
|
73 |
+
geometry=geometry,
|
74 |
+
scale=10
|
75 |
+
).get('NDVI')
|
76 |
+
|
77 |
+
return ndvi_values.getInfo()
|
78 |
+
|
79 |
+
# Function to get LAI data
|
80 |
+
def get_lai_data(geometry, start_date, end_date):
|
81 |
+
collection = ee.ImageCollection('MODIS/006/MOD15A2H') \
|
82 |
+
.filterBounds(geometry) \
|
83 |
+
.filterDate(start_date, end_date)
|
84 |
+
|
85 |
+
lai_values = collection.select('Lai_500m').mean().reduceRegion(
|
86 |
+
reducer=ee.Reducer.mean(),
|
87 |
+
geometry=geometry,
|
88 |
+
scale=500
|
89 |
+
).get('Lai_500m')
|
90 |
+
|
91 |
+
return lai_values.getInfo()
|
92 |
+
|
93 |
+
# Date range selection
|
94 |
+
start_date = st.date_input('Start date', datetime.date(2023, 1, 1))
|
95 |
+
end_date = st.date_input('End date', datetime.date.today())
|
96 |
+
|
97 |
+
if start_date < end_date:
|
98 |
+
# Get NDVI and LAI data for each farm
|
99 |
+
ndvi_data = []
|
100 |
+
lai_data = []
|
101 |
+
for idx, row in farms.iterrows():
|
102 |
+
geometry = ee.Geometry.Point([row['longitude'], row['latitude']])
|
103 |
+
ndvi = get_ndvi_data(geometry, start_date.isoformat(), end_date.isoformat())
|
104 |
+
lai = get_lai_data(geometry, start_date.isoformat(), end_date.isoformat())
|
105 |
+
ndvi_data.append({'Farm': row['name'], 'NDVI': ndvi})
|
106 |
+
lai_data.append({'Farm': row['name'], 'LAI': lai})
|
107 |
+
|
108 |
+
ndvi_df = pd.DataFrame(ndvi_data)
|
109 |
+
lai_df = pd.DataFrame(lai_data)
|
110 |
+
|
111 |
+
# NDVI chart
|
112 |
+
fig_ndvi = px.bar(ndvi_df, x='Farm', y='NDVI', title='NDVI Comparison')
|
113 |
+
st.plotly_chart(fig_ndvi)
|
114 |
+
|
115 |
+
# LAI chart
|
116 |
+
fig_lai = px.bar(lai_df, x='Farm', y='LAI', title='LAI Comparison')
|
117 |
+
st.plotly_chart(fig_lai)
|
118 |
+
else:
|
119 |
+
st.error('Error: End date must fall after start date.')
|
120 |
+
|
121 |
+
elif page == 'Weekly Reports':
|
122 |
+
st.header('Weekly Reports')
|
123 |
+
|
124 |
+
# Farm selection
|
125 |
+
selected_farm = st.selectbox('Select a farm:', farms['name'])
|
126 |
+
|
127 |
+
# Week selection
|
128 |
+
week = st.slider('Select week:', 1, 52, 1)
|
129 |
+
|
130 |
+
# Simulate weekly data (replace this with actual data processing)
|
131 |
+
def get_weekly_data(farm, week):
|
132 |
+
return {
|
133 |
+
'NDVI': 0.5 + 0.1 * week, # Simulated increasing NDVI
|
134 |
+
'LAI': 2 + 0.2 * week, # Simulated increasing LAI
|
135 |
+
}
|
136 |
+
|
137 |
+
data = get_weekly_data(selected_farm, week)
|
138 |
+
|
139 |
+
# Display weekly report
|
140 |
+
st.subheader(f'Weekly Report for {selected_farm} - Week {week}')
|
141 |
+
|
142 |
+
col1, col2 = st.columns(2)
|
143 |
+
with col1:
|
144 |
+
st.metric(label="NDVI 🌱", value=f"{data['NDVI']:.2f}")
|
145 |
+
with col2:
|
146 |
+
st.metric(label="LAI 🌡", value=f"{data['LAI']:.2f}")
|
147 |
+
|
148 |
+
# Trend chart
|
149 |
+
weeks = list(range(1, week + 1))
|
150 |
+
trend_data = [get_weekly_data(selected_farm, w) for w in weeks]
|
151 |
+
trend_df = pd.DataFrame(trend_data)
|
152 |
+
trend_df['Week'] = weeks
|
153 |
+
|
154 |
+
fig = px.line(trend_df, x='Week', y=['NDVI', 'LAI'], title=f'Trend for {selected_farm}')
|
155 |
+
st.plotly_chart(fig)
|
156 |
+
|
157 |
+
# Footer
|
158 |
+
st.sidebar.markdown('---')
|
159 |
+
st.sidebar.text('Dashboard created with Streamlit')
|