File size: 2,665 Bytes
a269448
 
1ec7272
a588cf3
a269448
1ec7272
a269448
 
1ec7272
a269448
1ec7272
a588cf3
1ec7272
 
 
 
 
 
a588cf3
1ec7272
 
a588cf3
1ec7272
 
 
a588cf3
1ec7272
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
import streamlit as st
import pandas as pd
import ee
import geemap

# Earth Engine authentication
service_account = 'earth-engine-service-account@ee-esmaeilkiani1387.iam.gserviceaccount.com'
credentials = ee.ServiceAccountCredentials(service_account, 'ee-esmaeilkiani1387-1b2c5e812a1d.json')
ee.Initialize(credentials)

# Load the CSV file
@st.cache_data
def load_data():
    return pd.read_csv('/home/user/Farm_Details_Export.csv')

df = load_data()

st.title('Farm Finder and Sentinel-2 Imagery Viewer')

# Farm search functionality
farm_name = st.text_input('Enter farm name:')
if farm_name:
    farm = df[df['farm_name'].str.contains(farm_name, case=False)]
    if not farm.empty:
        st.write(farm)
    else:
        st.write('Farm not found')

# Sentinel-2 index selection
indices = {'NDVI': 'NDVI', 'EVI': 'EVI', 'NDWI': '(B3 - B8) / (B3 + B8)'}
selected_index = st.selectbox('Select Sentinel-2 index:', list(indices.keys()))

# Date range selection
start_date = st.date_input('Start date')
end_date = st.date_input('End date')

if st.button('Show Map'):
    # Create a map centered on the mean coordinates of all farms
    center_lat = df['latitude'].mean()
    center_lon = df['longitude'].mean()
    m = geemap.Map(center=[center_lat, center_lon], zoom=10)
    
    # Add Sentinel-2 imagery with selected index
    s2_collection = (ee.ImageCollection('COPERNICUS/S2_SR')
                     .filterDate(start_date, end_date)
                     .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 20)))
    
    if selected_index == 'NDVI':
        vis_params = {'min': 0, 'max': 1, 'palette': ['red', 'yellow', 'green']}
    elif selected_index == 'EVI':
        vis_params = {'min': -1, 'max': 1, 'palette': ['blue', 'white', 'green']}
    else:  # NDWI
        vis_params = {'min': -1, 'max': 1, 'palette': ['red', 'white', 'blue']}
    
    def add_index(image):
        index = image.expression(indices[selected_index], {
            'B3': image.select('B3'),
            'B4': image.select('B4'),
            'B8': image.select('B8'),
        }).rename(selected_index)
        return image.addBands(index)
    
    s2_with_index = s2_collection.map(add_index)
    median_index = s2_with_index.select(selected_index).median()
    m.addLayer(median_index, vis_params, selected_index)
    
    # Add farm locations to the map
    for idx, row in df.iterrows():
        info = f"Farm: {row['farm_name']}<br>Age: {row['age']}<br>Variety: {row['variety']}"
        m.add_marker(location=[row['latitude'], row['longitude']], popup=info)
    
    # Display the map
    m.to_streamlit(height=600)

if __name__ == '__main__':
    st.write('App is running')