Spaces:
Runtime error
Runtime error
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 | |
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') | |