Using Earth Engine asset ID to access ImageCollection in Earth Engine IDE for filtering
Tags: earth, googlecloud, opensource, javascript
Published: 2023-10-29 10:18:26

Google Earth Engine – a geospatial cloud platform for planetary-scale geospatial analysis with extensive computational capabilities brings an array of high-impact societal challenges including drought, disaster, deforestation, water pollution monitoring, food security, climate monitoring and environmental protection. An exciting feature of Earth Engine is the ability to host petabytes of raster data - Images then processes them within the cloud platform without downloading the enormous remote – sensing to your computer. 

imageCollection defines a stack of similar image objects. 

> It is like saying, Image + Image = imageCollection

Today we will focus on writing codes related to imageCollection in the Earth Engine Code Editor. 
We will;

1. Access raster image collections from Earth Engine data catalog
2. Filter them temporally - by a date
3. Filter spatially – by region or bound
4. Filter clouds
5. Explore some image processing functions like .max

Our true Hypothesis is that before getting into writing everybody has a verified Google Earth Engine Account. If not refer to our [previous article](https://dev.to/geedevs-nairobi/creating-an-earth-engine-cloud-project-4gja) on how to get you ready. 

### Accessing ImageCollection from Earth Engine catalog  

This is the first step, done using the Image’s Earth Engine ID.  Another approach to use ImageCollection is by creating ImageCollection from lists of images. It is possible also to create new imageCollections by merging existing collections. The `print` function will print Image information on the console. However, the console printout cannot print more than 5000 elements. Anytime you print a large collection will be correspondingly slower or result to the below error..

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rtferpf0u8ytyzud6xto.PNG)

At this time we are going to start by loading our imagineCollection from Google Earth Engine.

```
Loading the image collection using the `ImageCollection` constructor

var sent2Coll = ee.ImageCollection('COPERNICUS/S2_SR')
                //Filter dates
                  .filterDate('2010-01-01', '2016-12-31');
// print the output
print(sent2Coll);
```
Imagine you are a seasoned Pro, and you wanted to Do it yourself – create your own imageCollection. Earth Engine has this `ee.ImageCollection.fromImages()` enabling function. Please try the below code in your Earth Engine Code Editor to test it.

Create a collection with `fromImages()` function in the earth engine.

```
var collFromImages = ee.ImageCollection.fromImages(
  [ee.Image(3), ee.Image(4), ee.Image(5), ee.Image(6)]);  
print('collFromImages: ', collFromImages);
```

Another exciting hack is creating arbitrary constant images using the Image constructor. This is very useful when wanting to test a hypothesis or creating a data model to validate results from a comparing method or procedure. Let us say you’re a research fellow at World Resource Institute, and you want to create a land cover classification and cloud-free imageCollection..the following code would be handy to while trying to crack codes at Carbon lab, 

- Create imageCollection from Images 
This approach works best when we want to create arbitrary constant images with help of an Image constructor
```
var image1 = ee.Image([1, 2,3])
var image2 = ee.Image([1, 2,3])
var image3 = ee.Image([1, 2,3])
```
### Earth Engine ImageCollection Filtering Techniques
Up to this point, we have our data ready in the EE IDE ready to start our preprocessing steps – Filtering, and performing mathematical functions. As illustrated in the Image Filtering section, Earth Engine provides a range of convenient approaches to filter image collections. We will quickly start with filtering our image collection and getting the first image out of the collection. In this case `.first()` will be useful. Just see how to implement it. 

- Filter first image in ImageCollection

The `.first` method helps us get the first image in the image collection stack.
  ```
  var firstImage = sentinel2Collection.first()
  print(firstImage)
  ```
  
- Filter by bounds
  
 Filter bounds function is essential when we want to concentrate our focus in the area of interest.  
 ```
  var second = sentinel2Collection.bound()
  print(second)
 ```

- Filter Clouds in ImageCollection 
 
Imagine working on a project which is going to positively impact people living in severely cloud-affected region. How would you ensure reliability, effectiveness and efficiency of your project?  Cloud coverage is a major challenge in space-borne optical remote sensing as it hampers real-time monitoring of the earth’s surface. The code below will show you how to remove clouds from an ImageCollection. The resulting image is artifact-free 

Earth observation projects face the [data generating process challenge](https://iaes.cgiar.org/spia/news/challenges-using-earth-observation-data-impact-evaluation). As a result, developers, and researchers ought to deal with clouds while conducting land cover, or even vegetation analysis for better results.

Earth Engine provides an array of techniques for filtering image collections. Some of the frequently used filtering techniques are;  `imageCollection.filterDate()`, and `imageCollection.filterBounds()`. For general purpose filtering, use `imageCollection.filter()` with an `ee.Filter` as an argument. 
 both convenience methods and filter() to identify and remove images with high cloud cover from an ImageCollection.
Let us try to filter by date, then bounds then ensure we filter to remove all clouds - to remove all clouds we use the `ee.Filter.eq` method then assign cloud_cover as 0. 


```
var collection = ee.ImageCollection('LANDSAT/LC08/C02/T1_TOA')
  .filterDate('2016-01-01', '2018-01-01')  
  .filter(ee.Filter.calendarRange(11, 2, 'month'))  
  .filterBounds(ee.Geometry.Point(25.8544, -18.08874));  

// Filter the collection by the CLOUD_COVER property.
var filtered = collection.filter(ee.Filter.eq('CLOUD_COVER', 0));
```

#### Exploring ImageCollection Properties and Metadata

We can get to know more details of an ImageCollection by exploring its details. While data information provided in data catalog is essential, some Earth Engine functions could be handy in deeper image exploration. Let us say for example we want to print image objects to explore band names, projection information, properties, and other metadata. 

Let us try to print metadata of our image
```
// Load an image.
var image = ee.Image('COPERNICUS/S2_SR');

// Display all metadata.
print('This is Image metadata:', image);
```
We could get a list of all metadata properties using the following code.

```
var properties = image.propertyNames();
print('Metadata properties:', properties);
```

- Count the images in an ImageCollection Stack
Let us, try to count the number of images - the `.size` method used in the below code prints the total number of the images contained in the ImageCollection stack.
```
print(sentinel2Collection.size());
```

Like any other data science project, while working with in [geospatial data analysis](https://datadrivenlab.org/big-data-2/google-earth-engine-tutorial/) we can do statistical computations within Earth Engine Code Editor. The interesting part of it is that it does not infer image pixels based on statistical models but utilizes the posteriori information measured. 

The `.aggregate_stats` function in image filtering process help us to understand basic statistical information about the imageCollection. Information about standard deviation, can be understood when we use the `.aggregate_stats function`.  

Let us try it
```
// get statistics for a image coll
var Stats = dataset.aggregate_stats('SUN_ELEVATION');
print(Stats);

```
Imagine during your imageCollection preparation you wanted to get its mean value, the `.mean` method would be helpful at this point. 

I would like we try it,

```
// var mean = dataset.mean(); 
// print(mean);
```

--- and we are done for today. I will appreciate hearing from you. You can share your recommendations, suggestions or enquiries for better articles in the future.   

Writer [Nicholas Musau](https://www.linkedin.com/in/nicholas-musau/)