3

I am pretty new to GEE.

My task is to get all Sentinel 2 images from a specific time. Then I want to filter this collection by a maximum cloud coverage over an area of interest, which is imported before as a shapefile. I use this Image Collection later to calculate and export the maximum NDVI.

I know how to filter Cloud Percentage over the whole scene, but as I need only a small AOI too many images get filtered. So my Problem is to not filter the cloud probability over the whole scene, but just over the small polygon (AOI).

I am working currently in Python with geemap and the ee library.

import ee
import geemap

ee.Initialize()
ee.Authenticate()

def calculateNDVI(image):
  result = image.normalizedDifference(["B8","B4"]).rename("ndvi")
  return image.addBands(result)

geometry = #"Imported Polygon"#
      
sentinel = ee.ImageCollection("COPERNICUS/S2_SR").filterDate(
    "2020-04-01","2021-10-01").filterBounds(geometry).filter(
        ee.Filter.lte('CLOUDY_PIXEL_PERCENTAGE',10))

ndvi = sentinel.map(calculateNDVI)
maxNDVI = ndvi.max()
geemap.ee_export_image(maxNDVI, 
                       filename= maxNDVI.tif,
                       scale = 10, region = geometry, file_per_band=False,
                       crs = 'EPSG:3035')
2

1 Answer 1

2

I usually get around to this problem first clipping the collection over the region of interest and then filtering by cloud percentage. I don't know if this is something recommended or if there's something more efficient because I just started learning python and using geemap, anyway, here's the code:

import ee
import geemap

ee.Initialize()
ee.Authenticate()

def calculateNDVI(image):
  result = image.normalizedDifference(["B8","B4"]).rename("ndvi")
  return image.addBands(result)

geometry = #"Imported Polygon"#

def clipcol(image):
    clipimage = image.clip(geometry)
    return clipimage
      
sentinel = ee.ImageCollection('COPERNICUS/S2_SR') \
    .filterDate('2020-04-01','2021-10-01') \
    .filterBounds(geometry) \
    .map(clipcol) \
    .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 10)) 

ndvi = sentinel.map(calculateNDVI)
maxNDVI = ndvi.max()
geemap.ee_export_image(maxNDVI, 
                       filename= maxNDVI.tif,
                       scale = 10, region = geometry, file_per_band=False,
                       crs = 'EPSG:3035')
2
  • Wow thank you that actually works for me, what an easy and quick solution! Thank you so much!
    – maniiko
    Commented Nov 2, 2021 at 10:46
  • 2
    But aren't you using the CLOUDY_PIXEL_PERCENTAGE statistic that was computed over the whole image? Cropping the image won't update the stat!?
    – Matifou
    Commented Jun 9, 2023 at 14:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.