FreyGeospatial
FreyGeospatial

Reputation: 549

Trying to filter sentinel 2 images by percent cloud cover

I am trying to filter Sentinel 2 images by percent of cloud cover (say, 20%) and then perform some image arithmetic on the output.

I am trying to do implement what is found here:gis.stackexchange thread (https://gis.stackexchange.com/questions/303344/filter-landsat-images-cloud-cover). Unfortunately, the function ee.Algorithms.Landsat... does not work with Sentinel 2 images, which is required for what I am doing.

My code thus far is below.

var myCollection = ee.ImageCollection('COPERNICUS/S2');

var dataset2 = ee.ImageCollection(
  myCollection.filterBounds(point) //use only one image that contains the POI
  .filterDate('2015-06-23', '2019-04-25') //filter by date range
);


var ds2_cloudiness = dataset2.map(function(image){
  var cloud = ee.Algorithms.Landsat.simpleCloudScore(image).select('cloud');
  var cloudiness = cloud.reduceRegion({
    reducer: 'median'
  });
  return image.set(cloudiness);
});

var filteredCollection = ds2_cloudiness.filter(ee.Filter.lt('cloud', 20));

Map.addLayer(filteredCollection, {min: -.2, max:.2}, 'test')

This outputs an error: Landsat.simpleCloudScore: Image is not a Landsat scene or is missing SENSOR_ID metadata. Any nudge in the right direction would be appreciated.

Upvotes: 2

Views: 3302

Answers (1)

Nishanta Khanal
Nishanta Khanal

Reputation: 334

I think there is a simpler approach if you just want to filter using cloud cover percentage. You can do this by filtering based on the image metadata.

var myCollection = ee.ImageCollection('COPERNICUS/S2');
print(myCollection.first())

If you inspect the first image in the Sentinel-2 imageCollection you can actually see its metadata (only for that image). Since, you are working with a homogeneous and well maintained image collection, you can expect the other images to have similar porperties. From here, you can do the following

myCollection = myCollection.filter(ee.Filter.lte('CLOUDY_PIXEL_PERCENTAGE',20));
print(myCollection.first());

This particular code will filter the image collection to find images with cloud cover less than or equal to 20. You can verify this by either once again checking the first image or checking the size of the collection which should have narrowed.

However, if you are looking for a separate algorithm to calculate cloud over an image, you'll probably have to write one for Sentinel (yet).

Upvotes: 3

Related Questions