Esmaeilkiani commited on
Commit
2bfae12
1 Parent(s): 857cf4a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -57
app.py CHANGED
@@ -1,82 +1,91 @@
1
  import streamlit as st
2
  import ee
3
- import geemap
 
4
  import datetime
5
 
6
- # Authenticate Earth Engine
7
  service_account = 'earth-engine-service-account@ee-esmaeilkiani1387.iam.gserviceaccount.com'
8
  credentials = ee.ServiceAccountCredentials(service_account, 'ee-esmaeilkiani1387-1b2c5e812a1d.json')
9
  ee.Initialize(credentials)
10
 
11
- # Define the indices and their corresponding functions
12
- INDICES = {
13
- 'NDVI': lambda img: img.normalizedDifference(['B8', 'B4']).rename('NDVI'),
14
- 'EVI': lambda img: img.expression(
15
- '2.5 * ((NIR - RED) / (NIR + 6 * RED - 7.5 * BLUE + 1))',
16
- {
17
- 'NIR': img.select('B8'),
18
- 'RED': img.select('B4'),
19
- 'BLUE': img.select('B2')
20
- }
21
- ).rename('EVI'),
22
- 'SAVI': lambda img: img.expression(
23
- '((NIR - RED) / (NIR + RED + 0.5)) * (1.5)',
24
- {
25
- 'NIR': img.select('B8'),
26
- 'RED': img.select('B4')
27
- }
28
- ).rename('SAVI'),
29
- 'NDMI': lambda img: img.normalizedDifference(['B8', 'B11']).rename('NDMI')
30
- }
31
 
32
- # Define color palettes for each index
33
- COLOR_PALETTES = {
34
  'NDVI': ['FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163', '99B718', '74A901', '66A000', '529400', '3E8601', '207401', '056201', '004C00', '023B01', '012E01', '011D01', '011301'],
35
  'EVI': ['FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163', '99B718', '74A901', '66A000', '529400', '3E8601', '207401', '056201', '004C00', '023B01', '012E01', '011D01', '011301'],
36
  'SAVI': ['#D73027', '#F46D43', '#FDAE61', '#FEE08B', '#FFFFBF', '#D9EF8B', '#A6D96A', '#66BD63', '#1A9850'],
37
  'NDMI': ['#D73027', '#F46D43', '#FDAE61', '#FEE08B', '#FFFFBF', '#D9EF8B', '#A6D96A', '#66BD63', '#1A9850']
38
  }
39
 
40
- # Streamlit app
41
- st.title('Google Earth Engine Indices Viewer')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
- # User inputs
44
- selected_indices = st.multiselect('Select two indices', list(INDICES.keys()), max_selections=2)
45
- start_date = st.date_input('Start date', datetime.date(2022, 1, 1))
46
- end_date = st.date_input('End date', datetime.date(2022, 12, 31))
47
 
48
- # Convert dates to ee.Date objects
49
- ee_start_date = ee.Date(start_date.strftime('%Y-%m-%d'))
50
- ee_end_date = ee.Date(end_date.strftime('%Y-%m-%d'))
51
 
52
- if len(selected_indices) == 2 and start_date < end_date:
53
- # Define the area of interest
54
- aoi = ee.Geometry.Point([48.7312815, 31.5200749]).buffer(5000)
55
 
56
- # Get Sentinel-2 imagery
57
- s2 = ee.ImageCollection('COPERNICUS/S2_SR') \
58
- .filterBounds(aoi) \
59
- .filterDate(ee_start_date, ee_end_date) \
60
- .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 20)) \
61
- .sort('CLOUDY_PIXEL_PERCENTAGE')
62
 
63
- # Calculate indices
64
- for index in selected_indices:
65
- s2 = s2.map(lambda img: img.addBands(INDICES[index](img)))
 
66
 
67
- # Reduce the collection to a single image (median)
68
- image = s2.select(selected_indices).median()
 
69
 
70
- # Create a map
71
- m = geemap.Map()
72
- m.centerObject(aoi, 12)
73
 
74
- # Add layers to the map
75
- for index in selected_indices:
76
- m.addLayer(image.select(index), {'min': 0, 'max': 1, 'palette': COLOR_PALETTES[index]}, index)
77
 
78
- # Add the map to the Streamlit app
79
- m.to_streamlit(height=600)
80
 
81
- else:
82
- st.warning('Please select two indices and ensure the start date is before the end date.')
 
1
  import streamlit as st
2
  import ee
3
+ import geemap.foliumap as geemap
4
+ import pandas as pd
5
  import datetime
6
 
7
+ # اعتبار سنجی و اتصال به Google Earth Engine
8
  service_account = 'earth-engine-service-account@ee-esmaeilkiani1387.iam.gserviceaccount.com'
9
  credentials = ee.ServiceAccountCredentials(service_account, 'ee-esmaeilkiani1387-1b2c5e812a1d.json')
10
  ee.Initialize(credentials)
11
 
12
+ # خواندن فایل CSV مزارع
13
+ farms_data = pd.read_csv('Farm_Details_Export.csv')
14
+
15
+ # تعریف ناحیه مورد مطالعه با مختصات جدید
16
+ region = ee.Geometry.Polygon(
17
+ [[[48.681879, 31.417603], [48.721447, 31.413209], [48.724279, 31.420826], [48.726768, 31.427418],
18
+ [48.728228, 31.435694], [48.736382, 31.42837], [48.739557, 31.435657], [48.742261, 31.441772],
19
+ [48.752303, 31.452243], [48.75226, 31.459784], [48.759127, 31.473657], [48.766809, 31.472413],
20
+ [48.773203, 31.491188], [48.77758, 31.534579], [48.785563, 31.540797], [48.792601, 31.59696],
21
+ [48.694668, 31.60756], [48.691921, 31.603466], [48.697586, 31.534067], [48.69381, 31.507727],
22
+ [48.685226, 31.468496], [48.681879, 31.417603]]]
23
+ )
 
 
 
 
 
 
 
 
24
 
25
+ # تعریف پالت‌های رنگی برای هر شاخص
26
+ palettes = {
27
  'NDVI': ['FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163', '99B718', '74A901', '66A000', '529400', '3E8601', '207401', '056201', '004C00', '023B01', '012E01', '011D01', '011301'],
28
  'EVI': ['FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163', '99B718', '74A901', '66A000', '529400', '3E8601', '207401', '056201', '004C00', '023B01', '012E01', '011D01', '011301'],
29
  'SAVI': ['#D73027', '#F46D43', '#FDAE61', '#FEE08B', '#FFFFBF', '#D9EF8B', '#A6D96A', '#66BD63', '#1A9850'],
30
  'NDMI': ['#D73027', '#F46D43', '#FDAE61', '#FEE08B', '#FFFFBF', '#D9EF8B', '#A6D96A', '#66BD63', '#1A9850']
31
  }
32
 
33
+ # توابع برای هر شاخص
34
+ def get_index_map(index, start_date, end_date):
35
+ s2 = ee.ImageCollection('COPERNICUS/S2').filterBounds(region).filterDate(start_date, end_date)
36
+ if index == "NDVI":
37
+ image = s2.map(lambda image: image.normalizedDifference(['B8', 'B4']).rename('NDVI')).median()
38
+ elif index == "EVI":
39
+ image = s2.map(lambda image: image.expression('2.5 * ((B8 - B4) / (B8 + 6 * B4 - 7.5 * B2 + 1))', {
40
+ 'B8': image.select('B8'), 'B4': image.select('B4'), 'B2': image.select('B2')
41
+ }).rename('EVI')).median()
42
+ elif index == "SAVI":
43
+ image = s2.map(lambda image: image.expression('(1 + 0.5) * ((B8 - B4) / (B8 + B4 + 0.5))', {
44
+ 'B8': image.select('B8'), 'B4': image.select('B4')
45
+ }).rename('SAVI')).median()
46
+ elif index == "NDMI":
47
+ image = s2.map(lambda image: image.normalizedDifference(['B8', 'B11']).rename('NDMI')).median()
48
+
49
+ vis_params = {'min': -1, 'max': 1, 'palette': palettes[index]}
50
+ return image, vis_params
51
+
52
+ # ساخت برنامه Streamlit
53
+ st.title("نقشه شاخص‌های کشاورزی برای مزارع شرکت دهخدا")
54
 
55
+ # انتخاب بازه زمانی
56
+ start_date = st.date_input("تاریخ شروع", datetime.date(2023, 1, 1))
57
+ end_date = st.date_input("تاریخ پایان", datetime.date(2023, 12, 31))
 
58
 
59
+ # انتخاب شاخص
60
+ index_option = st.selectbox("شاخص مورد نظر را انتخاب کنید:", ["NDVI", "EVI", "SAVI", "NDMI"])
 
61
 
62
+ # انتخاب مزرعه از فایل CSV
63
+ farm_name = st.sidebar.selectbox("نام مزرعه را انتخاب کنید:", farms_data['farm_name'].unique())
 
64
 
65
+ # پیدا کردن مختصات و اطلاعات مزرعه انتخاب شده
66
+ selected_farm = farms_data[farms_data['farm_name'] == farm_name]
67
+ latitude = selected_farm['latitude'].values[0]
68
+ longitude = selected_farm['longitude'].values[0]
69
+ farm_age = selected_farm['age'].values[0]
70
+ farm_variety = selected_farm['variety'].values[0]
71
 
72
+ # دکمه برای نمایش نقشه
73
+ if st.button("نمایش نقشه"):
74
+ # بارگذاری نقشه شاخص
75
+ index_map, vis_params = get_index_map(index_option, start_date.isoformat(), end_date.isoformat())
76
 
77
+ # ایجاد نقشه با Geemap
78
+ map_ = geemap.Map(center=[latitude, longitude], zoom=12)
79
+ map_.addLayer(index_map, vis_params, index_option)
80
 
81
+ # افزودن نوار رنگ به نقشه
82
+ map_.add_colorbar(vis_params, label=index_option)
 
83
 
84
+ # افزودن نشان مزرعه به نقشه
85
+ map_.add_marker([latitude, longitude], popup=f"نام: {farm_name}\nسن: {farm_age}\nواریته: {farm_variety}")
 
86
 
87
+ # نمایش نقشه در Streamlit
88
+ map_.to_streamlit()
89
 
90
+ # امکان دانلود نقشه
91
+ st.download_button(label="دانلود نقشه", data=map_.to_image(), file_name="map.png", mime="image/png")