Spaces:
Runtime error
Runtime error
Esmaeilkiani
commited on
Commit
•
a588cf3
1
Parent(s):
5b3a3d5
Update app.py
Browse files
app.py
CHANGED
@@ -1,31 +1,52 @@
|
|
1 |
-
import ee
|
2 |
import streamlit as st
|
|
|
3 |
import pandas as pd
|
|
|
|
|
|
|
4 |
|
5 |
# Authenticate Earth Engine
|
6 |
service_account = 'earth-engine-service-account@ee-esmaeilkiani1387.iam.gserviceaccount.com'
|
7 |
credentials = ee.ServiceAccountCredentials(service_account, 'ee-esmaeilkiani1387-1b2c5e812a1d.json')
|
8 |
ee.Initialize(credentials)
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
.
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import ee
|
3 |
import pandas as pd
|
4 |
+
from datetime import date
|
5 |
+
from google.oauth2 import service_account
|
6 |
+
import geemap
|
7 |
|
8 |
# Authenticate Earth Engine
|
9 |
service_account = 'earth-engine-service-account@ee-esmaeilkiani1387.iam.gserviceaccount.com'
|
10 |
credentials = ee.ServiceAccountCredentials(service_account, 'ee-esmaeilkiani1387-1b2c5e812a1d.json')
|
11 |
ee.Initialize(credentials)
|
12 |
|
13 |
+
@st.cache_data
|
14 |
+
def get_farm_coordinates(farm_name, dataset_url):
|
15 |
+
# بارگذاری دیتاست
|
16 |
+
df = pd.read_csv(dataset_url)
|
17 |
+
# جستجوی مزرعه
|
18 |
+
farm_data = df[df['Farm'] == farm_name]
|
19 |
+
if farm_data.empty:
|
20 |
+
return None
|
21 |
+
return farm_data[['latitude', 'longitude']].values[0]
|
22 |
+
|
23 |
+
# تابع برای محاسبه شاخص NDVI در بازه زمانی مشخص
|
24 |
+
def get_ndvi_map(latitude, longitude, start_date, end_date):
|
25 |
+
point = ee.Geometry.Point([longitude, latitude])
|
26 |
+
collection = (ee.ImageCollection("COPERNICUS/S2")
|
27 |
+
.filterBounds(point)
|
28 |
+
.filterDate(start_date, end_date)
|
29 |
+
.map(lambda img: img.normalizedDifference(['B8', 'B4']).rename('NDVI')))
|
30 |
+
ndvi_image = collection.median()
|
31 |
+
vis_params = {'min': 0, 'max': 1, 'palette': ['blue', 'white', 'green']}
|
32 |
+
return geemap.ee_to_url(ndvi_image.clip(point.buffer(10000)), vis_params)
|
33 |
+
|
34 |
+
# رابط کاربری با Streamlit
|
35 |
+
st.title("نمایش نقشه شاخص NDVI مزارع نیشکر")
|
36 |
+
farm_name = st.text_input("نام مزرعه را وارد کنید:")
|
37 |
+
dataset_url = "https://huggingface.co/datasets/Esmaeilkianii/SugarcaneDataSet/embed/viewer/default/train"
|
38 |
+
|
39 |
+
if farm_name:
|
40 |
+
coords = get_farm_coordinates(farm_name, dataset_url)
|
41 |
+
if coords is not None:
|
42 |
+
st.success(f"مزرعه یافت شد! مختصات: {coords}")
|
43 |
+
start_date = st.date_input("تاریخ شروع:", date(2023, 1, 1))
|
44 |
+
end_date = st.date_input("تاریخ پایان:", date.today())
|
45 |
|
46 |
+
if start_date < end_date:
|
47 |
+
map_url = get_ndvi_map(coords[0], coords[1], start_date.isoformat(), end_date.isoformat())
|
48 |
+
st.image(map_url, caption=f"نقشه NDVI برای مزرعه {farm_name}")
|
49 |
+
else:
|
50 |
+
st.error("تاریخ شروع باید قبل از تاریخ پایان باشد.")
|
51 |
+
else:
|
52 |
+
st.error("مزرعهای با این نام یافت نشد.")
|