ZaliAiRS / app.py
Esmaeilkianii's picture
Update app.py
5dd6a8c verified
import streamlit as st
import pandas as pd
import ee
import folium
from datetime import datetime
from streamlit_folium import st_folium
# Authenticate and initialize Google Earth Engine
service_account = "[email protected]"
credentials = ee.ServiceAccountCredentials(service_account, 'ee-esmaeilkiani1387-1b2c5e812a1d.json')
ee.Initialize(credentials)
# Load farm data from CSV
csv_file = "Farm_Details_Export.csv"
data = pd.read_csv(csv_file)
# Streamlit app
st.title("نمایش شاخص‌های کشاورزی برای مزارع نیشکر دهخدا")
# Sidebar inputs
st.sidebar.header("انتخاب اطلاعات مزرعه و بازه زمانی")
selected_farm = st.sidebar.selectbox("انتخاب مزرعه", data['Farm_Name'].unique())
start_date = st.sidebar.date_input("تاریخ شروع", datetime(2024, 1, 1))
end_date = st.sidebar.date_input("تاریخ پایان", datetime(2024, 11, 30))
# Validate date range
if start_date >= end_date:
st.error("لطفاً یک بازه زمانی معتبر انتخاب کنید.")
st.stop()
# Select index
indices = {
"NDVI": lambda img: img.normalizedDifference(['B8', 'B4']).rename('NDVI'),
"NDMI": lambda img: img.normalizedDifference(['B8', 'B11']).rename('NDMI'),
"NDWI": lambda img: img.normalizedDifference(['B3', 'B8']).rename('NDWI'),
}
selected_index = st.sidebar.selectbox("انتخاب شاخص", list(indices.keys()))
# Get selected farm's geometry
farm_row = data[data['Farm_Name'] == selected_farm].iloc[0]
farm_geometry = ee.Geometry.Point([farm_row['Longitude'], farm_row['Latitude']])
# Sentinel-2 image collection
collection = ee.ImageCollection("COPERNICUS/S2") \
.filterBounds(farm_geometry) \
.filterDate(str(start_date), str(end_date)) \
.map(indices[selected_index])
# Calculate mean for the selected index
index_image = collection.mean()
# Create map
m = folium.Map(location=[farm_row['Latitude'], farm_row['Longitude']], zoom_start=12)
url = index_image.getThumbURL({"min": 0, "max": 1, "palette": ['blue', 'white', 'green']})
folium.raster_layers.ImageOverlay(
image=url,
bounds=farm_geometry.bounds().getInfo()['coordinates'][0],
opacity=0.7
).add_to(m)
# Display map
st.write(f"**نقشه شاخص {selected_index} برای مزرعه {selected_farm}**")
st_folium(m, width=700, height=500)