import streamlit as st import ee import geemap import datetime # Authenticate Earth Engine service_account = 'earth-engine-service-account@ee-esmaeilkiani1387.iam.gserviceaccount.com' credentials = ee.ServiceAccountCredentials(service_account, 'ee-esmaeilkiani1387-1b2c5e812a1d.json') ee.Initialize(credentials) # Define the indices and their corresponding functions INDICES = { 'NDVI': lambda img: img.normalizedDifference(['B8', 'B4']).rename('NDVI'), 'EVI': lambda img: img.expression( '2.5 * ((NIR - RED) / (NIR + 6 * RED - 7.5 * BLUE + 1))', { 'NIR': img.select('B8'), 'RED': img.select('B4'), 'BLUE': img.select('B2') } ).rename('EVI'), 'SAVI': lambda img: img.expression( '((NIR - RED) / (NIR + RED + 0.5)) * (1.5)', { 'NIR': img.select('B8'), 'RED': img.select('B4') } ).rename('SAVI'), 'NDMI': lambda img: img.normalizedDifference(['B8', 'B11']).rename('NDMI') } # Define color palettes for each index COLOR_PALETTES = { 'NDVI': ['FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163', '99B718', '74A901', '66A000', '529400', '3E8601', '207401', '056201', '004C00', '023B01', '012E01', '011D01', '011301'], 'EVI': ['FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163', '99B718', '74A901', '66A000', '529400', '3E8601', '207401', '056201', '004C00', '023B01', '012E01', '011D01', '011301'], 'SAVI': ['#D73027', '#F46D43', '#FDAE61', '#FEE08B', '#FFFFBF', '#D9EF8B', '#A6D96A', '#66BD63', '#1A9850'], 'NDMI': ['#D73027', '#F46D43', '#FDAE61', '#FEE08B', '#FFFFBF', '#D9EF8B', '#A6D96A', '#66BD63', '#1A9850'] } # Streamlit app st.title('Google Earth Engine Indices Viewer') # User inputs selected_indices = st.multiselect('Select two indices', list(INDICES.keys()), max_selections=2) start_date = st.date_input('Start date', datetime.date(2022, 1, 1)) end_date = st.date_input('End date', datetime.date(2022, 12, 31)) if len(selected_indices) == 2 and start_date < end_date: # Define the area of interest aoi = ee.Geometry.Point([48.7312815, 31.5200749]).buffer(5000) # Get Sentinel-2 imagery s2 = ee.ImageCollection('COPERNICUS/S2_SR') \ .filterBounds(aoi) \ .filterDate(start_date, end_date) \ .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 20)) \ .sort('CLOUDY_PIXEL_PERCENTAGE') # Calculate indices for index in selected_indices: s2 = s2.map(lambda img: img.addBands(INDICES[index](img))) # Reduce the collection to a single image (median) image = s2.select(selected_indices).median() # Create a map m = geemap.Map() m.centerObject(aoi, 12) # Add layers to the map for index in selected_indices: m.addLayer(image.select(index), {'min': 0, 'max': 1, 'palette': COLOR_PALETTES[index]}, index) # Add the map to the Streamlit app m.to_streamlit(height=600) else: st.warning('Please select two indices and ensure the start date is before the end date.')