Uncomment the following line to install geemap if needed.
In [ ]:
Copied!
# !pip install geemap
# !pip install geemap
How to find out the greenest day of the year¶
Import libraries¶
In [ ]:
Copied!
import ee
import geemap
import ee
import geemap
Create an interactive map¶
In [ ]:
Copied!
Map = geemap.Map()
Map
Map = geemap.Map()
Map
Define a region of interest (ROI)¶
In [ ]:
Copied!
countries = ee.FeatureCollection("users/giswqs/public/countries")
Map.addLayer(countries, {}, "countries")
countries = ee.FeatureCollection("users/giswqs/public/countries")
Map.addLayer(countries, {}, "countries")
In [ ]:
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 [ ]:
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 [ ]:
Copied!
# print(l8.size().getInfo())
# print(l8.size().getInfo())
Create a median composite¶
In [ ]:
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 [ ]:
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 [ ]:
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 [ ]:
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 [ ]:
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 [ ]:
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 [ ]:
Copied!
greenest = withNDVI.qualityMosaic("NDVI")
greenest = withNDVI.qualityMosaic("NDVI")
In [ ]:
Copied!
greenest.bandNames().getInfo()
greenest.bandNames().getInfo()
Display the max value band¶
In [ ]:
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 [ ]:
Copied!
Map.addLayer(greenest, visParams, "Greenest pixel")
Map
Map.addLayer(greenest, visParams, "Greenest pixel")
Map
Display time bands¶
In [ ]:
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 [ ]:
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",
)