import solara
import json
import ee
import geemap
import leafmap
# reactive variables will trigger a component rerender
# when changed.
# When you change the default (now 0), hit the embedded browser
# refresh button to reset the state
clicks = solara.reactive(0)
# Replace contents of service account json file corresponding to your account from here:
# https://console.cloud.google.com/iam-admin/serviceaccounts
json_data = '''
{
}
'''
# Preparing values
json_object = json.loads(json_data, strict=False)
service_account = json_object['client_email']
json_object = json.dumps(json_object)
# Authorising the app
credentials = ee.ServiceAccountCredentials(service_account, key_data=json_object)
print("credentials = ", credentials)
ee.Initialize(credentials)
# Load the image collection
dataset = ee.ImageCollection("CSIC/SPEI/2_9") \
.filterDate('2015-01-01', '2015-01-31') \
.mean()
# Select the 24-month analysis
spei24 = dataset.select('SPEI_24_month')
# Set the visualization parameters
# visParams = {
# 'min': -2.33,
# 'max': 2.33,
# 'palette': [
# '8b1a1a', 'de2929', 'f3641d',
# 'fdc404', '9afa94', '03f2fd',
# '12adf3', '1771de', '00008b',
# ]
# }
class POC:
Map = geemap.Map()
vegetation_image = None
evapotranspiration_image = None
common_viz_params = None
def __init__(self):
self.Map.set_center(21.2, 22.2, 2)
self.common_viz_params = {
'min': -1,
'max': 1,
# 'palette': ['blue', 'green', 'red']
'palette': [
'ffffff', 'ce7e45', 'df923d', 'f1b555', 'fcd163', '99b718', '74a901',
'66a000', '529400', '3e8601', '207401', '056201', '004c00', '023b01',
'012e01', '011d01', '011301'
]
}
def add_vegetation_layer(self):
print("in add_vegetation_layer")
# Load the image collection
dataset = ee.ImageCollection('MODIS/061/MOD13A2') \
.filterDate('2015-01-01', '2015-01-31') \
.mean()
# Select the NDVI band
ndvi = dataset.select('NDVI')
print("vegetation_image:", ndvi.getInfo())
# Visualization parameters
ndvi_viz_params = {
'min': 0,
'max': 9000,
# 'min': -1,
# 'max': 1,
'palette': [
'ffffff', 'ce7e45', 'df923d', 'f1b555', 'fcd163', '99b718', '74a901',
'66a000', '529400', '3e8601', '207401', '056201', '004c00', '023b01',
'012e01', '011d01', '011301'
]
}
try:
print("Adding NDVI vegetation layer/loading to streamlit")
# Add the NDVI layer to the map
self.vegetation_image = ndvi
print("vegetation_image:", self.vegetation_image.getInfo())
# # Map.addLayer(ndvi, ndvi_viz_params, 'NDVI vegetation layer')
## Map.addLayer(vegetation_image, ndvi_viz_params, 'NDVI vegetation layer')
# Map.addLayer(eval(ee_asset))
# st.write(Map)
# Map.to_streamlit()
self.Map.add_layer(self.vegetation_image, ndvi_viz_params, 'NDVI vegetation layer')
except Exception as e:
print(f"Error adding NDVI vegetation layer/loading to streamlit: {e}")
def add_evapotranspiration_layer(self):
print("in add_evapotranspiration_layer")
# Load the image collection
dataset = ee.ImageCollection("CSIC/SPEI/2_9") \
.filterDate('2015-01-01', '2015-01-31') \
.mean()
# Select the 24-month analysis
spei24 = dataset.select('SPEI_24_month')
# Set the visualization parameters
evapotranspiration_viz_params = {
'min': -2.33,
'max': 2.33,
'palette': [
'8b1a1a', 'de2929', 'f3641d',
'fdc404', '9afa94', '03f2fd',
'12adf3', '1771de', '00008b',
]
}
try:
# Display the SPEI 24-month layer
print("Adding SPEI 24-month evapotranspiration layer/loading to streamlit")
# evapotranspiration_image = spei24.filterBounds(regionOfInterest).first()
self.evapotranspiration_image = spei24
print("evapotranspiration_image:", self.evapotranspiration_image.getInfo())
# Map.addLayer(spei24, evapotranspiration_viz_params, 'SPEI 24 month evapotranspiration layer')
# Add the layer to the map
self.Map.add_layer(self.evapotranspiration_image, evapotranspiration_viz_params, 'SPEI 24 month evapotranspiration layer')
except Exception as e:
print(f"Error adding SPEI 24-month evapotranspiration layer/loading to streamlit: {e}")
def concat_images(self):
print("in concat_images")
try:
# Create a concat image with vegetation_image and evapotranspiration_image
composite_image = ee.Image.cat([self.evapotranspiration_image, self.vegetation_image])
print("composite_image =", composite_image)
self.Map.addLayer(composite_image, {'bands': ['SPEI_24_month', 'NDVI'], 'min': -1, 'max': 1}, 'Composite Image')
#Map.addLayer(composite_image, name='Concat Image')
evapotranspiration_viz_params = {
'bands': ['SPEI_24_month', 'NDVI'],
'min': -1,
'max': 1
# 'palette': [
# '8b1a1a', 'de2929', 'f3641d',
# 'fdc404', '9afa94', '03f2fd',
# '12adf3', '1771de', '00008b',
# ]
}
#Map.addLayer(composite_image, name='Concat Image', viz_params=evapotranspiration_viz_params)
##Map.to_streamlit()
except Exception as e:
print(f"Error creating concat image: {e}")
def getMap(self):
return self.Map
@solara.component
def Page():
pocObj = POC()
pocObj.add_vegetation_layer()
pocObj.add_evapotranspiration_layer()
# pocObj.concat_images();
solara.display(pocObj.getMap())
Page()