Uncomment the following line to install geemap if needed.
In [1]:
Copied!
# !pip install geemap
# !pip install geemap
How to find out the greenest day of the year¶
Import libraries¶
In [2]:
Copied!
import ee
import geemap
import ee
import geemap
Create an interactive map¶
In [3]:
Copied!
Map = geemap.Map()
Map
Map = geemap.Map()
Map
Out[3]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Define a region of interest (ROI)¶
In [4]:
Copied!
countries = ee.FeatureCollection('users/giswqs/public/countries')
Map.addLayer(countries, {}, 'coutries')
countries = ee.FeatureCollection('users/giswqs/public/countries')
Map.addLayer(countries, {}, 'coutries')
In [5]:
Copied!
roi = countries.filter(ee.Filter.eq('id', 'USA'))
Map.addLayer(roi, {}, 'roi')
roi = countries.filter(ee.Filter.eq('id', 'USA'))
Map.addLayer(roi, {}, 'roi')
Filter ImageCollection¶
In [6]:
Copied!
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)
)
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)
)
In [7]:
Copied!
# print(l8.size().getInfo())
# print(l8.size().getInfo())
Create a median composite¶
In [8]:
Copied!
median = l8.median()
visParams = {
'bands': ['B4', 'B3', 'B2'],
'min': 0,
'max': 0.4,
}
Map.addLayer(median, visParams, 'Median')
median = l8.median()
visParams = {
'bands': ['B4', 'B3', 'B2'],
'min': 0,
'max': 0.4,
}
Map.addLayer(median, visParams, 'Median')
Define functions to add time bands¶
In [9]:
Copied!
def addNDVI(image):
ndvi = image.normalizedDifference(['B5', 'B4']).rename('NDVI')
return image.addBands(ndvi)
def addNDVI(image):
ndvi = image.normalizedDifference(['B5', 'B4']).rename('NDVI')
return image.addBands(ndvi)
In [10]:
Copied!
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 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())
In [11]:
Copied!
def addMonth(image):
img_date = ee.Date(image.date())
img_doy = ee.Number.parse(img_date.format('M'))
return image.addBands(ee.Image(img_doy).rename('month').toInt())
def addMonth(image):
img_date = ee.Date(image.date())
img_doy = ee.Number.parse(img_date.format('M'))
return image.addBands(ee.Image(img_doy).rename('month').toInt())
In [12]:
Copied!
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())
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¶
In [13]:
Copied!
withNDVI = l8.map(addNDVI).map(addDate).map(addMonth).map(addDOY)
withNDVI = l8.map(addNDVI).map(addDate).map(addMonth).map(addDOY)
Create a quality mosaic¶
In [14]:
Copied!
greenest = withNDVI.qualityMosaic('NDVI')
greenest = withNDVI.qualityMosaic('NDVI')
In [15]:
Copied!
greenest.bandNames().getInfo()
greenest.bandNames().getInfo()
Out[15]:
['B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B8', 'B9', 'B10', 'B11', 'BQA', 'NDVI', 'date', 'month', 'doy']
Display the max value band¶
In [16]:
Copied!
ndvi = greenest.select('NDVI')
palette = [
'#d73027',
'#f46d43',
'#fdae61',
'#fee08b',
'#d9ef8b',
'#a6d96a',
'#66bd63',
'#1a9850',
]
Map.addLayer(ndvi, {'palette': palette}, 'NDVI')
ndvi = greenest.select('NDVI')
palette = [
'#d73027',
'#f46d43',
'#fdae61',
'#fee08b',
'#d9ef8b',
'#a6d96a',
'#66bd63',
'#1a9850',
]
Map.addLayer(ndvi, {'palette': palette}, 'NDVI')
In [17]:
Copied!
Map.addLayer(greenest, visParams, 'Greenest pixel')
Map
Map.addLayer(greenest, visParams, 'Greenest pixel')
Map
Out[17]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Display time bands¶
In [18]:
Copied!
Map.addLayer(
greenest.select('month'),
{'palette': ['red', 'blue'], 'min': 1, 'max': 12},
'Greenest month',
)
Map.addLayer(
greenest.select('month'),
{'palette': ['red', 'blue'], 'min': 1, 'max': 12},
'Greenest month',
)
In [19]:
Copied!
Map.addLayer(
greenest.select('doy'),
{'palette': ['brown', 'green'], 'min': 1, 'max': 365},
'Greenest doy',
)
Map.addLayer(
greenest.select('doy'),
{'palette': ['brown', 'green'], 'min': 1, 'max': 365},
'Greenest doy',
)
Last update:
2022-03-14