Esmaeilkianii commited on
Commit
11a7a97
·
verified ·
1 Parent(s): b1cbf41

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -26
app.py CHANGED
@@ -1,12 +1,16 @@
1
  import streamlit as st
2
  import ee
 
3
  import pandas as pd
 
 
4
  from datetime import date, timedelta
5
  import json
6
- import geemap
7
  from fpdf import FPDF
8
  import io
9
- import matplotlib.pyplot as plt
 
10
  import plotly.graph_objects as go
11
  from plotly.subplots import make_subplots
12
  import plotly.express as px
@@ -42,6 +46,7 @@ key_json = {
42
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/dehkhodamap-e9f0da4ce9f6514021%40ee-esmaeilkiani13877.iam.gserviceaccount.com",
43
  "universe_domain": "googleapis.com"
44
  }
 
45
  # تبدیل دیکشنری به رشته JSON
46
  key_json_str = json.dumps(key_json)
47
 
@@ -75,26 +80,17 @@ def get_safe_index_value(_image, index_type, band_selection, _farm_geometry, ima
75
  return None
76
  try:
77
  if index_type == "MSAVI":
78
- nir_band = _image.select("B8")
79
- red_band = _image.select("B4")
80
- if nir_band is None or red_band is None:
81
- return None
82
  index_image = _image.expression(
83
  "(2 * NIR + 1 - sqrt((2 * NIR + 1) ** 2 - 8 * (NIR - RED))) / 2",
84
- {"NIR": nir_band, "RED": red_band}
85
  ).rename("MSAVI")
86
  else:
87
  bands = band_selection[index_type]
88
- band_exists = True
89
- for band in bands:
90
- if not _image.select(band).getInfo():
91
- band_exists = False
92
- break
93
- if band_exists:
94
  index_image = _image.normalizedDifference(bands).rename(index_type)
95
  else:
96
  return None
97
-
98
  mean_value = index_image.reduceRegion(
99
  reducer=ee.Reducer.mean(),
100
  geometry=_farm_geometry,
@@ -162,8 +158,14 @@ def create_heatmap(df, index_type):
162
  fig = px.imshow(pivot_df,
163
  labels=dict(x="تاریخ", y="نام مزرعه", color=index_type),
164
  x=pivot_df.columns,
165
- y=pivot_df.index)
166
- fig.update_layout(title=f"نقشه حرارتی {index_type}")
 
 
 
 
 
 
167
  return fig
168
 
169
  def create_comparison_chart(df, farm1, farm2, index_type):
@@ -172,12 +174,18 @@ def create_comparison_chart(df, farm1, farm2, index_type):
172
  df_farm2 = df[df['name'] == farm2]
173
 
174
  fig = go.Figure()
175
- fig.add_trace(go.Scatter(x=df_farm1['date'], y=df_farm1[index_type], name=farm1))
176
- fig.add_trace(go.Scatter(x=df_farm2['date'], y=df_farm2[index_type], name=farm2))
177
-
178
- fig.update_layout(title=f"مقایسه {index_type} بین {farm1} و {farm2}",
179
- xaxis_title="تاریخ",
180
- yaxis_title=index_type)
 
 
 
 
 
 
181
  return fig
182
 
183
  def generate_pdf_report(farm_info, index_values, lai_values, date_list):
@@ -386,11 +394,11 @@ def main():
386
 
387
  # نمایش نمودارها
388
  st.subheader("روند تغییرات شاخص‌ها در 10 روز اخیر")
389
- fig = make_subplots(rows=len(band_selection), cols=1, shared_xaxes=True)
390
  for i, (index, values) in enumerate(index_values.items(), start=1):
391
- fig.add_trace(go.Scatter(x=date_list, y=values, name=index), row=i, col=1)
392
  fig.update_yaxes(title_text=index, row=i, col=1)
393
- fig.update_layout(height=600, width=800, title_text="روند تغییرات شاخص‌ها")
394
  st.plotly_chart(fig)
395
 
396
  # نمایش نقشه NDVI
@@ -445,4 +453,5 @@ def main():
445
  st.error(f"خطا در پردازش اطلاعات: {str(e)}")
446
 
447
  if __name__ == "__main__":
448
- main()
 
 
1
  import streamlit as st
2
  import ee
3
+ import folium
4
  import pandas as pd
5
+ import numpy as np
6
+ from streamlit_folium import folium_static
7
  from datetime import date, timedelta
8
  import json
9
+ import geemap.foliumap as geemap
10
  from fpdf import FPDF
11
  import io
12
+ import base64
13
+ from PIL import Image
14
  import plotly.graph_objects as go
15
  from plotly.subplots import make_subplots
16
  import plotly.express as px
 
46
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/dehkhodamap-e9f0da4ce9f6514021%40ee-esmaeilkiani13877.iam.gserviceaccount.com",
47
  "universe_domain": "googleapis.com"
48
  }
49
+
50
  # تبدیل دیکشنری به رشته JSON
51
  key_json_str = json.dumps(key_json)
52
 
 
80
  return None
81
  try:
82
  if index_type == "MSAVI":
 
 
 
 
83
  index_image = _image.expression(
84
  "(2 * NIR + 1 - sqrt((2 * NIR + 1) ** 2 - 8 * (NIR - RED))) / 2",
85
+ {"NIR": _image.select("B8"), "RED": _image.select("B4")}
86
  ).rename("MSAVI")
87
  else:
88
  bands = band_selection[index_type]
89
+ if all(band in _image.bandNames().getInfo() for band in bands):
 
 
 
 
 
90
  index_image = _image.normalizedDifference(bands).rename(index_type)
91
  else:
92
  return None
93
+
94
  mean_value = index_image.reduceRegion(
95
  reducer=ee.Reducer.mean(),
96
  geometry=_farm_geometry,
 
158
  fig = px.imshow(pivot_df,
159
  labels=dict(x="تاریخ", y="نام مزرعه", color=index_type),
160
  x=pivot_df.columns,
161
+ y=pivot_df.index,
162
+ color_continuous_scale="RdYlGn")
163
+ fig.update_layout(
164
+ title=f"نقشه حرارتی {index_type}",
165
+ font=dict(family="Vazir"),
166
+ height=600,
167
+ margin=dict(l=50, r=50, t=50, b=50)
168
+ )
169
  return fig
170
 
171
  def create_comparison_chart(df, farm1, farm2, index_type):
 
174
  df_farm2 = df[df['name'] == farm2]
175
 
176
  fig = go.Figure()
177
+ fig.add_trace(go.Scatter(x=df_farm1['date'], y=df_farm1[index_type], name=farm1, mode='lines+markers'))
178
+ fig.add_trace(go.Scatter(x=df_farm2['date'], y=df_farm2[index_type], name=farm2, mode='lines+markers'))
179
+
180
+ fig.update_layout(
181
+ title=f"مقایسه {index_type} بین {farm1} و {farm2}",
182
+ xaxis_title="تاریخ",
183
+ yaxis_title=index_type,
184
+ font=dict(family="Vazir"),
185
+ legend_title="مزرعه",
186
+ height=500,
187
+ margin=dict(l=50, r=50, t=50, b=50)
188
+ )
189
  return fig
190
 
191
  def generate_pdf_report(farm_info, index_values, lai_values, date_list):
 
394
 
395
  # نمایش نمودارها
396
  st.subheader("روند تغییرات شاخص‌ها در 10 روز اخیر")
397
+ fig = make_subplots(rows=len(band_selection), cols=1, shared_xaxes=True, vertical_spacing=0.05)
398
  for i, (index, values) in enumerate(index_values.items(), start=1):
399
+ fig.add_trace(go.Scatter(x=date_list, y=values, name=index, mode='lines+markers'), row=i, col=1)
400
  fig.update_yaxes(title_text=index, row=i, col=1)
401
+ fig.update_layout(height=200*len(band_selection), width=800, title_text="روند تغییرات شاخص‌ها", showlegend=False)
402
  st.plotly_chart(fig)
403
 
404
  # نمایش نقشه NDVI
 
453
  st.error(f"خطا در پردازش اطلاعات: {str(e)}")
454
 
455
  if __name__ == "__main__":
456
+ main()
457
+