Exploring colab and the earth engine

using the python earth engine API

Step 1: open notebook :

Step 2: click add code 

Step 3: add the line below and hit run. This will install the earthengine map library

!pip install geemap

Step 4: add the line below to install the geemap package and authenticate and initialize Earth Engine.

# Installs geemap package
import google.colab
import geemap.eefolium as geemap
# Authenticates and initializes Earth Engine
import ee
ee.Authenticate()
ee.Initialize()

Step 5: Create an interactive Map

Map = geemap.Map(center=[12,105], zoom= 8)
Map

Step 6: Add earth engine python script

# Import the Sentinel 2 collection as sentinel 2
# Import the boundary of Cambodia as bnd_cambodia
sentinel2 = ee.ImageCollection("COPERNICUS/S2");
bnd_cambodia = ee.FeatureCollection("users/nyeinsoethwal/Cambodia/cambodia_boundary");
 
# Filter the image collection using filterBounds() and filterDate() method.
# Sort the collection by cloud cover metadata
# Create the mosaic image and clip it to Cambodia boundary
image_dry = sentinel2.filterBounds(bnd_cambodia) \
 .filterDate('2019-01-01', '2019-04-30') \
 .sort('CLOUDY_PIXEL_PERCENTAGE', False) \
 .mosaic() \
 .clip(bnd_cambodia)
 
image_wet = sentinel2.filterDate('2019-06-01', '2019-10-31') \
 .filterBounds(bnd_cambodia) \
 .sort('CLOUDY_PIXEL_PERCENTAGE', False) \
 .mosaic() \
 .clip(bnd_cambodia)
 
# Calculate NDWI                 
ndwi_dry = image_dry.normalizedDifference(['B3', 'B8']);
ndwi_wet = image_wet.normalizedDifference(['B3', 'B8']);
 
# Palettes for Visualization
trueColor_palette = {'bands': ['B4', 'B3', 'B2'], 'min': 0, 'max': 3000};
waterPalette = ['red', 'yellow', 'green', 'blue'];

Step 7: Display Earth Engine True Color Images layers in Dry Season

Map = geemap.Map(center=[12,105], zoom= 8)
Map.addLayer(image_dry, trueColor_palette, '2019 Dry season true color');
Map.addLayerControl()
Map

Step 8: Display Earth Engine True Color Images layers in Wet Season

Map = geemap.Map(center=[12,105], zoom= 8)
Map.addLayer(image_wet, trueColor_palette, '2019 Wet season true color');
Map

Step 9: Display Earth Engine NDWI Image layer with a color palette in Dry Season

Map = geemap.Map(center=[12,105], zoom= 8)
Map.addLayer(ndwi_dry, {'min': -1, 'max': 0.5, 'palette': waterPalette}, '2019 Dry season NDWI');
Map

Step 10: Display Earth Engine NDWI Image layer with a color palette in Wet Season

Map = geemap.Map(center=[12,105], zoom= 8)
# Display NDWI Wet
Map.addLayer(ndwi_wet, {'min': -1, 'max': 0.5, 'palette': waterPalette}, '2019 Wet season NDWI');
Map

Find the full colab here

Leave a Reply