Kamal Hosen
Kamal Hosen
Geospatial Developer | Data Science | Python

Aug 23, 2022

Basics of Filtering and Sorting Image Collection in Google Earth Engine

/media/article-img/gee-filter-sort.jpg

I showed how to import Landsat 8 image collection in the image collection tutorial. In this tutorial, I will show you why we need to filter and sort image collection and how we can do it.

We have already collected Landsat 8 images. By default, the image collection contains all of the images available from 2013 to now worldwide. Now, we want to see image metadata. Earth engine provides a print function that prints image meta to explore images. The print function has limited to returning a maximum of 5000 elements at a time. If your image collection contains more than 5000 images, you cannot print them. Earth engine shows an error.

 

What can we do? We can filter our image collection to reduce the number of images in our collection.

Another thing, the Landsat 8 contains data from all over the world from 2013 to till now. We may not work for the entire world for all time. Rather, we may be interested in a specific location or region. So, we can focus on that particular location and filter image collection to get images for that particular location.

The filter also reduces the computation time as we collect only a limited image that requires for our work.

In this tutorial, I will show you different ways of filtering image collection.

 

A. Filter Image Collection By Date

Earthengine provides a filterDate function to filter the collection by defining a date range. In this exercise, we will filter our Landsat 8 image collection for the year 2020 only. The filterDate function requires two parameters start date and end date means the date of starting filter and the date of the ending filter. As a year starting from January 01 and ending on December 31, we can define the start date as 2020-01-01 and the end date as 2020-12-31. The date is formatted in Year-Month-Day (YYYY-MM-DD).

var myCollection = ee.ImageCollection("LANDSAT/LC08/C02/T1_TOA");


First load, Landsat 8 image collection by defining a variable myCollection. Now, filter this collection for the year 2020.

var filteredImage2020 = myCollection.filterDate(“2020-01-01, “2020-12-31”);

We can alternatively put the date in the filterDate by defining the startDate and endDate separately as a variable. For example -

var startDate = “2020-01-01” // Year-Month-Day (YYYY-MM-DD)

var endDate = “2020-12-31”

var filteredImage2020 = myCollection.filterDate(startDate, endDate);

Why separately defines the start and end date as a variable? Sometimes, it may require to do repetitive tasks. Separately defining the start and end date can save time in that cases. For example, you want to calculate NDVI for the yearly Landsat 8 images from 2015-2020. By defining the first year 2015 and the end year 2020, you can easily get each year’s image and calculate NDVI using the map function.

We can also alternatively use ee.Filter.date function rather than filterDate.

var filteredImage2020 = myCollection.filter(ee.Filter.date(startDate, endDate));

 

B. Filtering Image Collections by Geometry

Geometry is a vector feature representing the location on the earth's surface. We will filter image collection for only those geometries representing our study area.

In this tutorial, we will use a country boundary, for example, Bangladesh, to filter collection. We use country polygon data from the world boundaries dataset available in the earth engine. You can choose your data uploaded to the GEE asset folder.

var country_dataset = ee.FeatureCollection('USDOS/LSIB_SIMPLE/2017');

var bd = country_dataset.filter(ee.Filter.eq('country_na', 'Bangladesh'));

Here, at first, we define the source of the country dataset and the filter only Bangladesh.

Earthengine provides filterBounds function to filter image collection by geometries.

var filterByBangladesh = myCollection.filterBounds(bd);

Alternatively, you can use a single geometry collected by adding a marker in GEE and filter bounds by this geometry or defining our latitude and longitude.

var geometry = ee.Geometry.Point([89.50, 22.86]);

 

C. Sorting Image Collection

We reduce the number of images in our image collection by using filterDate and filterBounds functions. We know cloud cover in images is a big concern in working with earth observations. We need to find the least cloudy image or images within a range of clouds. Earth engine provides a sort function that we can use to sort images and get the least cloudy image by using the first argument.

// Filter imageCollection by date and bounds
var filterImageCollection = myCollection.filterDate(startDate, endDate).filterBounds(geometry);

// Sort image collection by cloud cover
var sortedImageCollection = filterImageCollection.sort('CLOUD_COVER');

// Select least cloudy image
var sortedLeastCloudyImage = sortedImageCollection.first();

 

The above example returns a single image from our image collection. This method works for a small area which covers by a single snap. If we work for a larger area that requires multiple snaps, this method will not work. In that scenario, we need to use the filter function and define the range of cloud cover.

// Sort images with a range of 0-25% cloud coverage
var sortedCloudyImages = filterImageCollection.filter(ee.Filter.lte('CLOUD_COVER', 25));

The above code return images which have cloud cover of less than 25%.

 

 

 

 

If you like this content, please share it with your networks.

Share To

About Author
  • Kamal Hosen
  • Kamal Hosen
    Geospatial Developer | Data Science | Python

    A passionate geospatial developer and analyst whose core interest is developing geospatial products/services to support the decision-making process in climate change and disaster risk reduction, spatial planning process, natural resources management, and land management sectors. I love learning and working with open source technologies like Python, Django, LeafletJS, PostGIS, GeoServer, and Google Earth Engine.