26
loading...
This website collects cookies to deliver better user experience
# for pip users
pip install earthengine-api --upgrade
pip install folium
pip install colorcet
# for conda users
conda update -c conda-forge earthengine-api
conda install folium -c conda-forge
conda install colorcet
import ee
import colorcet as cc
import folium
ee.Authenticate()
ee.Initialize()
def add_ee_layer(self, ee_image_object, vis_params, name):
map_id_dict = ee.Image(ee_image_object).getMapId(vis_params)
folium.raster_layers.TileLayer(
tiles=map_id_dict['tile_fetcher'].url_format,
attr='Map Data © <a href="https://earthengine.google.com/">Google Earth Engine</a>',
name=name,
overlay=True,
control=True
).add_to(self)
folium.Map.add_ee_layer = add_ee_layer
image.select
and uses both the cloud and cirrus bit-masks to generate a robust idea of where the resulting image is obscured by cloud cover.def maskS2clouds(image):
qa = image.select('QA60');
# Bits 10 and 11 are clouds and cirrus, respectively.
cloudBitMask = 1 << 10
cirrusBitMask = 1 << 11
# Both flags should be set to zero, indicating clear conditions.
mask = qa.bitwiseAnd(cloudBitMask).eq(0) and qa.bitwiseAnd(cirrusBitMask).eq(0)
return image.updateMask(mask).divide(10000)
imageCollection = ee.ImageCollection('COPERNICUS/S2_SR')
before = imageCollection.filterDate('2020-01-30','2020-02-28')
after = imageCollection.filterDate('2021-08-01','2021-08-28')
before = before.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE',20)).map(maskS2clouds)
after = after.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE',20)).map(maskS2clouds)
.expression()
function to calculate and return the resultant image. (.expression()
is typically used to handle more complex math expressions, while simpler operations have their own functions, such as .add()
, .power()
, etc)def calc_bais2(image):
return ee.Image(image.expression(
'(1 - ((R2*R4*R4)/(R4))**(0.5))*((SWIR2 - R4)/((SWIR2 + R4)**(0.5)) + 1)', {
'RED': image.select('B4'),
'R2': image.select('B6'),
'R3': image.select('B7'),
'SWIR2': image.select('B12'),
'R4': image.select('B8A'),
}))
'(1 - ((R2*R4*R4)/(R4))**(0.5))*((SWIR2 - R4)/((SWIR2 + R4)**(0.5)) + 1)'
'RED': image.select('B4')
etc re-maps the Sentinel-2 imaging bands to be referenced by RED, R2, etc for each band.bais2_after = after.map(calc_bais2)
bais2_before = before.map(calc_bais2)
.subtract()
functiondelta_bais2 = bais2_after.mean().subtract(bais2_before.mean())
lat, lon = 38.829592, 23.344883
my_map = folium.Map(location=[lat, lon], zoom_start=10)
my_map.add_ee_layer(delta_bais2.updateMask(waterMask), visualization, '')
display(my_map)