# ee.ImageCollection

Adding an Image on it's own is useful, but sometimes it's easier to search through a collection of images and filter according to their properties. &#x20;

The Data Catalog supplies Images often as part of an **ImageCollection**, which is a stack or sequence of Images. &#x20;

### Sentinel ImageCollection

Here, we're going to look through the Sentinel Collections, made available by the European Commission and European Space Agency's Copernicus Program.  It contains vast amounts of data which can be explored in the GEE Data Catalog:&#x20;

![Screenshot of the Sentinel Collection in the GEE data catalog https://developers.google.com/earth-engine/datasets/catalog/sentinel](/files/csv5nUtqCpviqRefQ74R)

Adding a Collection is straightforward - Earth Engine supplies a snippet that can be copied and used in Code Editor:&#x20;

![S2a MSI  ImageCollection landing page, GEE Data Catalog ](/files/5NpKiJU1Vi5iNZYDnFjV)

### Filtering an Image

You can filter an **ImageCollection** by dataset properties, such as dates, spatial extent, quality etc. &#x20;

For the polar regions, it is useful to filter by date to remove times without sunlight, area of interest, or cloud cover. &#x20;

You can also chain filters so you apply them all at once:&#x20;

```javascript
var s2_filter = ee.ImageCollection('COPERNICUS/S2_SR')  
                .filterDate('2022-01-01', '2022-05-01')  
                .filterBounds(ee.Geometry.Point(-68.124, -67.5679))  
                .filter('CLOUDY_PIXEL_PERCENTAGE < 50');
```

### Sorting an ImageCollection&#x20;

Sometimes it is useful to sort a collection by properties such as by observation time, cloud cover etc.&#x20;

{% hint style="info" %}
**`ee.ImageCollection('').sort('property')`**&#x20;
{% endhint %}

This can look like:&#x20;

```javascript
var s2_sort_obs = ee.ImageCollection('COPERNICUS/S2_SR')
  .filterBounds(ee.Geometry.Point(-68.124, -67.5679))
  .filterDate('2022-11-01', '2022-03-01')  
  // Sort by observation time:
  .sort('system:time_start');
```

Or by cloudiness:&#x20;

```javascript
var s2_sort_clouds = ee.ImageCollection('COPERNICUS/S2_SR')
  .filterBounds(ee.Geometry.Point(-68.124, -67.5679))
  .filterDate('2022-01-01', '2022-05-01')
  // Sort by Cloud parameter: 
  .sort('CLOUDY_PIXEL_PERCENTAGE');
```

{% hint style="success" %}
Each **ImageCollection** has different parameters, according to the instrument that captured the data.  You can read more about each **ImageCollection** in the **Data Catalog**
{% endhint %}

### Reducing Images

Filtering an **ImageCollection** often results with multiple images. &#x20;

This can be reviewed in the **CodeEditor** by printing the results in the Console:&#x20;

```javascript
var s2_filter = ee.ImageCollection('COPERNICUS/S2_SR')  
                .filterDate('2021-09-01', '2022-03-01')  
                .filterBounds(ee.Geometry.Point(-68.124, -67.5679))  
                .filter('CLOUDY_PIXEL_PERCENTAGE < 50')
                
var s2_filter_rgb = s2_filter.select(['B4', 'B3', 'B2']);

print('s2_filter_rgb', s2_filter_rgb)
```

In the **Console** we can see that this filter has resulted in 11 images:

![Screenshot of GEE Code Editor, showing the Console tab.  In this case, filtering the ImageCollection has resulted in the selection of 11 Images.  ](/files/ZkCcqBKduEYuIckuiK82)

While this is useful, it would be easier if we only had one image to manage.  We can do this by reducing the ImageCollection by value.  In this case, we can look for the Median values across each pixel.

{% hint style="info" %}
**`ee.ImageCollection.median`**
{% endhint %}

This can look like:&#x20;

```javascript
var s2_filter = ee.ImageCollection('COPERNICUS/S2_SR')  
                .filterDate('2021-09-01', '2022-03-01')  
                .filterBounds(ee.Geometry.Point(-68.124, -67.5679))  
                .filter('CLOUDY_PIXEL_PERCENTAGE < 50')


var s2_filter_rgb = s2_filter.select(['B4', 'B3', 'B2'])

var composite = s2_filter_rgb.median();

print('median composite:', composite)
```

And now the Console shows that the result is one single **Image**, composed of the 3 bands:&#x20;

![Screenshot of GEE Code Editor, showing the Console tab.  In this case, selecting the median values of the filtered ImageCollection has resulted in the selection of 1 Image.  ](/files/1PTkW0dbi8kaWW9jUvFj)

{% hint style="success" %}
Compared to the Filtered ImageCollection output, the Median Composite has less cloud-covered pixels.
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://guides.geospatial.bas.ac.uk/intro-to-google-earth-engine/how-to-use-google-earth-engine/using-the-code-editor/objects-and-methods/ee.imagecollection.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
