Reducing image collection
Reducing an ImageCollection
To composite images in an ImageCollection
, use imageCollection.reduce()
. This will composite all the images in the collection to a single image representing, for example, the min, max, mean or standard deviation of the images. (See the Reducers section for more information about reducers). For example, to create a median value image from a collection:
Create an interactive map¶
In [1]:
Copied!
import ee
import geemap
import ee
import geemap
In [2]:
Copied!
Map = geemap.Map()
Map
Map = geemap.Map()
Map
Out[2]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Compute a median image¶
In [3]:
Copied!
# Load a Landsat 8 collection for a single path-row.
collection = (
ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA')
.filter(ee.Filter.eq('WRS_PATH', 44))
.filter(ee.Filter.eq('WRS_ROW', 34))
.filterDate('2014-01-01', '2015-01-01')
)
# Compute a median image and display.
median = collection.median()
Map.setCenter(-122.3578, 37.7726, 12)
Map.addLayer(median, {'bands': ['B4', 'B3', 'B2'], 'max': 0.3}, 'median')
# Load a Landsat 8 collection for a single path-row.
collection = (
ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA')
.filter(ee.Filter.eq('WRS_PATH', 44))
.filter(ee.Filter.eq('WRS_ROW', 34))
.filterDate('2014-01-01', '2015-01-01')
)
# Compute a median image and display.
median = collection.median()
Map.setCenter(-122.3578, 37.7726, 12)
Map.addLayer(median, {'bands': ['B4', 'B3', 'B2'], 'max': 0.3}, 'median')
In [4]:
Copied!
collection.size().getInfo()
collection.size().getInfo()
Out[4]:
19
In [5]:
Copied!
collection.aggregate_array("system:id").getInfo()
collection.aggregate_array("system:id").getInfo()
Out[5]:
['LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140113', 'LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140129', 'LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140214', 'LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140318', 'LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140403', 'LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140419', 'LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140505', 'LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140521', 'LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140606', 'LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140622', 'LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140708', 'LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140724', 'LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140809', 'LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140825', 'LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140910', 'LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140926', 'LANDSAT/LC08/C01/T1_TOA/LC08_044034_20141012', 'LANDSAT/LC08/C01/T1_TOA/LC08_044034_20141028', 'LANDSAT/LC08/C01/T1_TOA/LC08_044034_20141231']
Use median reducer¶
At each location in the output image, in each band, the pixel value is the median of all unmasked pixels in the input imagery (the images in the collection). In the previous example, median()
is a convenience method for the following call:
In [6]:
Copied!
# Reduce the collection with a median reducer.
median = collection.reduce(ee.Reducer.median())
# Display the median image.
Map.addLayer(
median,
{'bands': ['B4_median', 'B3_median', 'B2_median'], 'max': 0.3},
'also median',
)
Map
# Reduce the collection with a median reducer.
median = collection.reduce(ee.Reducer.median())
# Display the median image.
Map.addLayer(
median,
{'bands': ['B4_median', 'B3_median', 'B2_median'], 'max': 0.3},
'also median',
)
Map
Out[6]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Create an image composite¶
In [7]:
Copied!
states = ee.FeatureCollection('TIGER/2018/States')
Map.addLayer(states, {}, "US States")
states = ee.FeatureCollection('TIGER/2018/States')
Map.addLayer(states, {}, "US States")
In [8]:
Copied!
ca = states.filter(ee.Filter.eq("NAME", "California"))
Map.addLayer(ca, {}, "California")
ca = states.filter(ee.Filter.eq("NAME", "California"))
Map.addLayer(ca, {}, "California")
In [9]:
Copied!
collection = (
ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA')
.filterBounds(ca)
.filterDate('2020-01-01', '2021-01-01')
)
collection = (
ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA')
.filterBounds(ca)
.filterDate('2020-01-01', '2021-01-01')
)
In [10]:
Copied!
collection.size().getInfo()
collection.size().getInfo()
Out[10]:
838
In [11]:
Copied!
image = collection.median().clip(ca)
Map.addLayer(image, {'bands': ['B4', 'B3', 'B2'], 'max': 0.3}, 'Landsat 2020')
image = collection.median().clip(ca)
Map.addLayer(image, {'bands': ['B4', 'B3', 'B2'], 'max': 0.3}, 'Landsat 2020')
Last update:
2022-03-25