原文:quality mosaic
实践:
关键函数:greenest = withNDVI.qualityMosaic(‘NDVI’)
import ee
import geemap
geemap.set_proxy(10809)
Map =geemap.Map()
Map
Define a region of interest(ROI)
coun = "D:\learnpy\shp\countries.shp"
countries = geemap.shp_to_ee(coun)
Map.addLayer(countries, {} ,'countries')
roi = countries.filter(ee.Filter.eq('NAME','China'))
Map.addLayer(roi, {}, 'China')
Filter Collection
start_date = '2019-01-01'
end_date = '2019-12-31'
l8 = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA') \
.filterBounds(roi) \
.filterDate(start_date, end_date)
print(l8.size().getInfo())
# 10374
Create a median compoiste
median = l8.median()
visParams = {
'bands': ['B4', 'B3', 'B2'],
'min': 0,
'max': 0.4,
}
Map.addLayer(median, visParams, 'Median')
Define functions to add time bands
def addNDVI(image):
ndvi = image.normalizedDifference(['B5', 'B4']).rename('NDVI')
return image.addBands(ndvi)
def addDate(image):
img_date = ee.Date(image.date())
img_date = ee.Number.parse(img_date.format('YYYYMMdd'))
return image.addBands(ee.Image(img_date).rename('date').toInt())
def addMonth(image):
img_date = ee.Date(image.date())
img_mon = ee.Number.parse(img_date.format('M'))
return image.addBands(ee.Image(img_mon).rename('month').toInt())
def addDOY(image):
img_date = ee.Date(image.date())
img_doy = ee.Number.parse(img_date.format('D'))
return image.addBands(ee.Image(img_doy).rename('doy').toInt())
Map over an ImageCollection
withNDVI = l8.map(addNDVI).map(addDate).map(addMonth).map(addDOY)
Create a quality mosaic
greenest = withNDVI.qualityMosaic('NDVI')
greenest.bandNames().getInfo()
# ['B1', 'B2','B3','B4','B5','B6','B7','B8','B9','B10','B11', 'BQA', 'NDVI', 'date', 'month', 'doy']
Display the max value band
ndvi = greenest.select('NDVI')
palette = ['#d73027', '#f46d43', '#fdae61', '#fee08b', '#d9ef8b', '#a6d96a', '#66bd63', '#1a9850']
Map.addLayer(ndvi, {'palette': palette}, 'NDVI')
一年中最绿的月份
Map.addLayer(greenest.select('month'), {'palette': ['red', 'blue'], 'min': 1, 'max': 12}, 'Greenest month')
一年中最绿的天
Map.addLayer(greenest.select('doy'), {'palette': ['brown', 'green'], 'min': 1, 'max': 365}, 'Greenest doy')