Mapping over image collection
Mapping over an ImageCollection
Reference: https://developers.google.com/earth-engine/guides/ic_mapping
To apply a function to every Image
in an ImageCollection
use imageCollection.map()
. The only argument to map()
is a function which takes one parameter: an ee.Image
.
In [1]:
Copied!
import ee
import geemap
import ee
import geemap
In [2]:
Copied!
Map = geemap.Map()
Map = geemap.Map()
Define a function¶
In [3]:
Copied!
def minus1000(num):
result = ee.Number(num).subtract(1000)
return result
def minus1000(num):
result = ee.Number(num).subtract(1000)
return result
In [4]:
Copied!
result = minus1000(2000)
print(result.getInfo())
result = minus1000(2000)
print(result.getInfo())
1000
Apply the map function¶
In [5]:
Copied!
years = ee.List.sequence(2000, 2010)
print(years.getInfo())
years = ee.List.sequence(2000, 2010)
print(years.getInfo())
[2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010]
In [6]:
Copied!
results = years.map(minus1000)
print(results.getInfo())
results = years.map(minus1000)
print(results.getInfo())
[1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009, 1010]
Use anonymous/lambda function¶
In [7]:
Copied!
years = ee.List.sequence(2000, 2010)
results = years.map(lambda x: ee.Number(x).subtract(1000))
print(results.getInfo())
years = ee.List.sequence(2000, 2010)
results = years.map(lambda x: ee.Number(x).subtract(1000))
print(results.getInfo())
[1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009, 1010]
Filter an ImageCollection¶
In [8]:
Copied!
roi = ee.Geometry.Point(-83.92, 35.96) # Knoxville, Tennessee
collection = (
ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')
.filterBounds(roi)
.filterDate("2019-01-01", "2020-01-01")
.sort("CLOUD_COVER")
)
print(collection.size().getInfo())
roi = ee.Geometry.Point(-83.92, 35.96) # Knoxville, Tennessee
collection = (
ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')
.filterBounds(roi)
.filterDate("2019-01-01", "2020-01-01")
.sort("CLOUD_COVER")
)
print(collection.size().getInfo())
17
In [9]:
Copied!
image = collection.first()
image = collection.first()
In [10]:
Copied!
# image.getInfo()
# image.getInfo()
In [11]:
Copied!
geemap.image_props(image).getInfo()
geemap.image_props(image).getInfo()
Out[11]:
{'CLOUD_COVER': 0.06, 'CLOUD_COVER_LAND': 0.06, 'EARTH_SUN_DISTANCE': 0.994981, 'ESPA_VERSION': '2_23_0_1b', 'GEOMETRIC_RMSE_MODEL': 7.624, 'GEOMETRIC_RMSE_MODEL_X': 5.066, 'GEOMETRIC_RMSE_MODEL_Y': 5.698, 'IMAGE_DATE': '2019-03-17', 'IMAGE_QUALITY_OLI': 9, 'IMAGE_QUALITY_TIRS': 9, 'LANDSAT_ID': 'LC08_L1TP_019035_20190317_20190325_01_T1', 'LEVEL1_PRODUCTION_DATE': 1553541347000, 'NOMINAL_SCALE': 30, 'PIXEL_QA_VERSION': 'generate_pixel_qa_1.6.0', 'SATELLITE': 'LANDSAT_8', 'SENSING_TIME': '2019-03-17T16:11:33.5001919Z', 'SOLAR_AZIMUTH_ANGLE': 144.944595, 'SOLAR_ZENITH_ANGLE': 43.08416, 'SR_APP_VERSION': 'LaSRC_1.3.0', 'WRS_PATH': 19, 'WRS_ROW': 35, 'system:asset_size': '641.99612 MB', 'system:band_names': ['B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B10', 'B11', 'sr_aerosol', 'pixel_qa', 'radsat_qa'], 'system:id': 'LANDSAT/LC08/C01/T1_SR/LC08_019035_20190317', 'system:index': 'LC08_019035_20190317', 'system:time_end': '2019-03-17 16:11:33', 'system:time_start': '2019-03-17 16:11:33', 'system:version': 1563396666124498}
In [12]:
Copied!
collection.aggregate_array("CLOUD_COVER").getInfo()
collection.aggregate_array("CLOUD_COVER").getInfo()
Out[12]:
[0.06, 0.42, 0.57, 0.76, 1.26, 6.67, 9.69, 13.14, 22.55, 26.67, 29.89, 33.11, 51.76, 74.72, 78.86, 85.73, 95.02]
In [13]:
Copied!
vis_params = {'bands': ['B5', 'B4', 'B3'], 'min': 0, 'max': 3000, 'gamma': 1.0}
Map.addLayer(image, vis_params, "First image")
Map.centerObject(image)
Map
vis_params = {'bands': ['B5', 'B4', 'B3'], 'min': 0, 'max': 3000, 'gamma': 1.0}
Map.addLayer(image, vis_params, "First image")
Map.centerObject(image)
Map
Out[13]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Select the best image each year¶
In [14]:
Copied!
roi = ee.Geometry.Point(-83.92, 35.96) # Knoxville, Tennessee
def best_image(year):
start_date = ee.Date.fromYMD(year, 1, 1)
end_date = start_date.advance(1, 'year')
image = (
ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')
.filterBounds(roi)
.filterDate(start_date, end_date)
.sort("CLOUD_COVER")
.first()
)
return image
roi = ee.Geometry.Point(-83.92, 35.96) # Knoxville, Tennessee
def best_image(year):
start_date = ee.Date.fromYMD(year, 1, 1)
end_date = start_date.advance(1, 'year')
image = (
ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')
.filterBounds(roi)
.filterDate(start_date, end_date)
.sort("CLOUD_COVER")
.first()
)
return image
In [15]:
Copied!
start_year = 2013
end_year = 2020
years = ee.List.sequence(start_year, end_year)
year_list = years.getInfo()
print(year_list)
start_year = 2013
end_year = 2020
years = ee.List.sequence(start_year, end_year)
year_list = years.getInfo()
print(year_list)
[2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020]
In [16]:
Copied!
images = years.map(best_image)
images = years.map(best_image)
In [17]:
Copied!
count = images.size().getInfo()
print(count)
count = images.size().getInfo()
print(count)
8
In [18]:
Copied!
ee.ImageCollection(images).aggregate_array("CLOUD_COVER").getInfo()
ee.ImageCollection(images).aggregate_array("CLOUD_COVER").getInfo()
Out[18]:
[0.09, 0.13, 0.03, 0.01, 0.03, 0.01, 0.06, 4.61]
In [19]:
Copied!
for index in range(0, count):
image = ee.Image(images.get(index))
layer_name = "Image " + str(year_list[index])
Map.addLayer(image, vis_params, layer_name, False)
for index in range(0, count):
image = ee.Image(images.get(index))
layer_name = "Image " + str(year_list[index])
Map.addLayer(image, vis_params, layer_name, False)
In [20]:
Copied!
Map
Map
Out[20]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Set image properties¶
In [21]:
Copied!
collection = ee.ImageCollection(images)
collection = ee.ImageCollection(images)
In [22]:
Copied!
collection.aggregate_array("system:time_start").getInfo()
collection.aggregate_array("system:time_start").getInfo()
Out[22]:
[1382804022390, 1399392695740, 1442247113710, 1454688719630, 1494778283540, 1525191067880, 1552839093500, 1591546292340]
In [23]:
Copied!
collection = collection.map(
lambda img: img.set(
{"DATE": ee.Date(img.get("system:time_start")).format("YYYY-MM-dd")}
)
)
collection = collection.map(
lambda img: img.set(
{"DATE": ee.Date(img.get("system:time_start")).format("YYYY-MM-dd")}
)
)
In [24]:
Copied!
collection.aggregate_array("DATE").getInfo()
collection.aggregate_array("DATE").getInfo()
Out[24]:
['2013-10-26', '2014-05-06', '2015-09-14', '2016-02-05', '2017-05-14', '2018-05-01', '2019-03-17', '2020-06-07']
Last update:
2022-03-25