In [ ]:
import ee
import glob
import geemap
import wxee
import pandas as pd
import numpy as np
ee.Initialize()
wxee.Initialize()
In [ ]:
lmav=ee.FeatureCollection('users/hafezahmad100/LMAV').geometry()
In [ ]:
collection = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA') \
    .filterBounds(lmav) \
    .filterDate('2021-01-01', '2021-12-30')\
    .filter(ee.Filter.lt('CLOUD_COVER', 10))
In [ ]:
# here's a function that uses the CA aoi to clip an image
def clip_func(x):
    return x.clip(lmav)
# for simple functions in Python, we can also use the "lambda" convention
# this is identical to what we just defined above 
# and is nice and compact for vectorizing functions on arrays and images, etc.
clip_func = lambda x: x.clip(lmav)
# in fact, by using lambda, we dont need to define this "clip_func" function at all
# we can write it directly in our "map" function, like this:
collection= collection.map(lambda x: x.clip(lmav))
In [ ]:
count=int(collection.size().getInfo())
print(count)
124
In [ ]:
# calculate NDWI over the collection
def fun_ndvi(img):
    return img.addBands(img.normalizedDifference(['B3', 'B5']).rename('NDWI'))
ndwi = collection.map(fun_ndvi)
In [ ]:
dates = ndwi.aggregate_array('system:time_start').map(
    lambda d: ee.Date(d).format('YYYY-MM-dd')
)
#print(dates.getInfo())
In [ ]:
# extract water mask
def extract_water(img):
    ndwi = img.normalizedDifference(['B5', 'B4']) 
    water_image=ndwi.gt(0)
    return water_image
ndwi_images=collection.map(extract_water)
In [ ]:
water_images = ndwi_images.map(lambda img: img.selfMask())
In [ ]:
dates=water_images.aggregate_array('system:index').getInfo()
# export each to google drive for loop dates in description
count=int(water_images.size().getInfo())
print(count)
for i in range(0,count):
    image=ee.Image(water_images.toList(count).get(i))
    names=dates[i]
    # export to google drive
    task = ee.batch.Export.image.toDrive(image, description=names, folder='google_ras',maxPixels=1e13,fileFormat='GeoTIFF',crs='EPSG:4326',scale=30,region=lmav)
    task.start()
124
In [ ]:
geemap.ee_export_image_collection_to_drive(water_images,max_pixels=1e13,folder='google_ras',file_format='GeoTIFF',scale=30)
In [ ]:
dates=ndwi.aggregate_array('system:index').getInfo()
In [ ]: