Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,91 +1,208 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
import plotly.express as px
|
|
|
|
| 5 |
import requests
|
| 6 |
-
import json
|
| 7 |
-
from datetime import datetime
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
# تابع برای خواندن دادهها
|
| 13 |
@st.cache_data
|
| 14 |
-
def
|
| 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 |
-
|
| 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 |
-
st.
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
import ee
|
| 5 |
+
import geemap
|
| 6 |
+
import geopandas as gpd
|
| 7 |
+
from datetime import datetime, timedelta
|
| 8 |
+
import folium
|
| 9 |
+
from streamlit_folium import folium_static
|
| 10 |
import plotly.express as px
|
| 11 |
+
import io
|
| 12 |
import requests
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
# Initialize Earth Engine
|
| 15 |
+
def initialize_ee():
|
| 16 |
+
try:
|
| 17 |
+
service_account = 'earth-engine-service-account@ee-esmaeilkiani1387.iam.gserviceaccount.com'
|
| 18 |
+
credentials = ee.ServiceAccountCredentials(service_account, 'ee-esmaeilkiani1387-1b2c5e812a1d.json')
|
| 19 |
+
ee.Initialize(credentials)
|
| 20 |
+
return True
|
| 21 |
+
except Exception as e:
|
| 22 |
+
st.error(f"خطا در راهاندازی Earth Engine: {str(e)}")
|
| 23 |
+
return False
|
| 24 |
+
|
| 25 |
+
# Set page config
|
| 26 |
+
st.set_page_config(
|
| 27 |
+
page_title="تحلیل پوشش گیاهی مزارع",
|
| 28 |
+
layout="wide",
|
| 29 |
+
initial_sidebar_state="expanded"
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
# Custom styling
|
| 33 |
+
st.markdown("""
|
| 34 |
+
<style>
|
| 35 |
+
.main {
|
| 36 |
+
background-color: #f5f5f5;
|
| 37 |
+
}
|
| 38 |
+
.stButton>button {
|
| 39 |
+
background-color: #2E4057;
|
| 40 |
+
color: white;
|
| 41 |
+
border-radius: 8px;
|
| 42 |
+
padding: 0.75rem 1.5rem;
|
| 43 |
+
font-weight: 600;
|
| 44 |
+
}
|
| 45 |
+
.stSelectbox {
|
| 46 |
+
background-color: white;
|
| 47 |
+
border-radius: 8px;
|
| 48 |
+
}
|
| 49 |
+
.plot-container {
|
| 50 |
+
background-color: white;
|
| 51 |
+
border-radius: 15px;
|
| 52 |
+
padding: 1rem;
|
| 53 |
+
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
|
| 54 |
+
}
|
| 55 |
+
body {
|
| 56 |
+
direction: rtl;
|
| 57 |
+
font-family: 'Vazirmatn', sans-serif;
|
| 58 |
+
}
|
| 59 |
+
</style>
|
| 60 |
+
""", unsafe_allow_html=True)
|
| 61 |
+
|
| 62 |
+
# Constants
|
| 63 |
+
DEFAULT_LOCATION = {
|
| 64 |
+
"latitude": 31.534442,
|
| 65 |
+
"longitude": 48.724416,
|
| 66 |
+
"name": "شرکت کشت و صنعت نیشکر دهخدا"
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
NDVI_PALETTE = [
|
| 70 |
+
'#d73027', '#f46d43', '#fdae61', '#fee08b', '#ffffbf',
|
| 71 |
+
'#d9ef8b', '#a6d96a', '#66bd63', '#1a9850'
|
| 72 |
+
]
|
| 73 |
|
|
|
|
| 74 |
@st.cache_data
|
| 75 |
+
def load_farm_data():
|
| 76 |
+
try:
|
| 77 |
+
CSV_URL = "https://hebbkx1anhila5yf.public.blob.vercel-storage.com/tableConvert.com_wftamx-1UC3HLMMTZ9WN2ePEhHQLU2Knn0ACl.csv"
|
| 78 |
+
response = requests.get(CSV_URL)
|
| 79 |
+
response.raise_for_status()
|
| 80 |
+
csv_content = response.content.decode('utf-8')
|
| 81 |
+
df = pd.read_csv(io.StringIO(csv_content))
|
| 82 |
+
return df
|
| 83 |
+
except Exception as e:
|
| 84 |
+
st.error(f"خطا در بارگیری دادههای مزارع: {str(e)}")
|
| 85 |
+
return None
|
| 86 |
+
|
| 87 |
+
def calculate_ndvi(image):
|
| 88 |
+
ndvi = image.normalizedDifference(['B8', 'B4']).rename('NDVI')
|
| 89 |
+
return image.addBands(ndvi)
|
| 90 |
+
|
| 91 |
+
def get_sentinel_collection(start_date, end_date, geometry):
|
| 92 |
+
return (ee.ImageCollection('COPERNICUS/S2_SR')
|
| 93 |
+
.filterDate(start_date, end_date)
|
| 94 |
+
.filterBounds(geometry)
|
| 95 |
+
.map(calculate_ndvi)
|
| 96 |
+
.select('NDVI'))
|
| 97 |
+
|
| 98 |
+
def create_ndvi_visualization(start_date, end_date, wheat_only=False):
|
| 99 |
+
try:
|
| 100 |
+
# Create geometry from default location
|
| 101 |
+
point = ee.Geometry.Point([DEFAULT_LOCATION['longitude'], DEFAULT_LOCATION['latitude']])
|
| 102 |
+
region = point.buffer(5000) # 5km buffer
|
| 103 |
+
|
| 104 |
+
# Get Sentinel-2 collection
|
| 105 |
+
collection = get_sentinel_collection(start_date, end_date, region)
|
| 106 |
+
|
| 107 |
+
# Calculate mean NDVI
|
| 108 |
+
ndvi = collection.mean()
|
| 109 |
+
|
| 110 |
+
# Visualization parameters
|
| 111 |
+
vis_params = {
|
| 112 |
+
'min': -1,
|
| 113 |
+
'max': 1,
|
| 114 |
+
'palette': NDVI_PALETTE
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
if wheat_only:
|
| 118 |
+
# Load wheat farm locations
|
| 119 |
+
farms_df = load_farm_data()
|
| 120 |
+
wheat_farms = farms_df[farms_df['crop_type'] == 'wheat']
|
| 121 |
+
|
| 122 |
+
# Create mask for wheat farms
|
| 123 |
+
wheat_mask = ee.FeatureCollection(
|
| 124 |
+
wheat_farms.apply(
|
| 125 |
+
lambda x: ee.Feature(
|
| 126 |
+
ee.Geometry.Point([x['longitude'], x['latitude']])
|
| 127 |
+
),
|
| 128 |
+
axis=1
|
| 129 |
+
).tolist()
|
| 130 |
+
).geometry().buffer(100) # 100m buffer around wheat farms
|
| 131 |
+
|
| 132 |
+
# Apply mask
|
| 133 |
+
ndvi = ndvi.updateMask(wheat_mask)
|
| 134 |
+
|
| 135 |
+
# Create the map
|
| 136 |
+
Map = geemap.Map(center=[DEFAULT_LOCATION['latitude'], DEFAULT_LOCATION['longitude']], zoom=12)
|
| 137 |
+
Map.add_layer(ndvi, vis_params, 'NDVI')
|
| 138 |
+
|
| 139 |
+
# Add legend
|
| 140 |
+
Map.add_legend(title="NDVI Values", colors=NDVI_PALETTE,
|
| 141 |
+
labels=[f"{i:.1f}" for i in np.linspace(-1, 1, len(NDVI_PALETTE))])
|
| 142 |
+
|
| 143 |
+
return Map
|
| 144 |
+
|
| 145 |
+
except Exception as e:
|
| 146 |
+
st.error(f"خطا در محاسبه NDVI: {str(e)}")
|
| 147 |
+
return None
|
| 148 |
+
|
| 149 |
+
def main():
|
| 150 |
+
# Initialize Earth Engine
|
| 151 |
+
if not initialize_ee():
|
| 152 |
+
return
|
| 153 |
+
|
| 154 |
+
st.title("تحلیل شاخص NDVI مزارع")
|
| 155 |
+
|
| 156 |
+
# Date selection
|
| 157 |
+
col1, col2 = st.columns(2)
|
| 158 |
+
with col1:
|
| 159 |
+
start_date = st.date_input(
|
| 160 |
+
"تاریخ شروع",
|
| 161 |
+
datetime.now() - timedelta(days=30),
|
| 162 |
+
help="تاریخ شروع برای محاسبه NDVI"
|
| 163 |
+
)
|
| 164 |
+
with col2:
|
| 165 |
+
end_date = st.date_input(
|
| 166 |
+
"تاریخ پایان",
|
| 167 |
+
datetime.now(),
|
| 168 |
+
help="تاریخ پایان برای محاسبه NDVI"
|
| 169 |
+
)
|
| 170 |
+
|
| 171 |
+
# Wheat farms filter
|
| 172 |
+
wheat_only = st.checkbox("نمایش فقط مزارع گندم", help="فیلتر کردن نقشه برای نمایش فقط مزارع گندم")
|
| 173 |
+
|
| 174 |
+
if st.button("محاسبه NDVI"):
|
| 175 |
+
with st.spinner("در حال محاسبه NDVI..."):
|
| 176 |
+
start_time = datetime.now()
|
| 177 |
+
|
| 178 |
+
# Create NDVI visualization
|
| 179 |
+
Map = create_ndvi_visualization(
|
| 180 |
+
start_date.strftime("%Y-%m-%d"),
|
| 181 |
+
end_date.strftime("%Y-%m-%d"),
|
| 182 |
+
wheat_only
|
| 183 |
+
)
|
| 184 |
+
|
| 185 |
+
if Map:
|
| 186 |
+
# Display the map
|
| 187 |
+
Map.to_streamlit(height=600)
|
| 188 |
+
|
| 189 |
+
# Display processing time
|
| 190 |
+
end_time = datetime.now()
|
| 191 |
+
processing_time = (end_time - start_time).total_seconds()
|
| 192 |
+
st.info(f"زمان پردازش: {processing_time:.2f} ثانیه")
|
| 193 |
+
|
| 194 |
+
# Display NDVI information
|
| 195 |
+
st.markdown("""
|
| 196 |
+
<div class="plot-container">
|
| 197 |
+
<h3>راهنمای تفسیر NDVI</h3>
|
| 198 |
+
<ul>
|
| 199 |
+
<li>مقادیر نزدیک به 1 (سبز تیره): پوشش گیاهی متراکم</li>
|
| 200 |
+
<li>مقادیر متوسط (زرد تا سبز روشن): پوشش گیاهی متوسط</li>
|
| 201 |
+
<li>مقادیر نزدیک به 0 (نارنجی): پوشش گیاهی کم یا خاک لخت</li>
|
| 202 |
+
<li>مقادیر منفی (قرمز): آب یا ابر</li>
|
| 203 |
+
</ul>
|
| 204 |
+
</div>
|
| 205 |
+
""", unsafe_allow_html=True)
|
| 206 |
+
|
| 207 |
+
if __name__ == "__main__":
|
| 208 |
+
main()
|