Py.Cafe

vedhavora/

earth-engine-geemap-webapp

Geospatial Analysis with Earth Engine and Geemap

DocsPricing
  • app.py
  • requirements.txt
app.py
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
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()