google-earth-engine – Clipping a GEE Image with the Python API

clipgoogle-earth-enginegoogle-earth-engine-python-api

I am looking for a way to clip image collection by a given polygon geometry with Python API,

The GEE JavaScript equivalent is simply ImageCollection.clip(geom)
(https://developers.google.com/earth-engine/image_visualization#clipping)

With the python API I get the error: 'ImageCollection' object has no attribute 'clip'.


I'm adding my code after using .mean() as a reducer

My code:

set the boundary polygon

my_boundary = ee.Geometry.Polygon([ [[-5, 40],[65, 40],[65, 60],[-5, 60],[-5, 60]]] ,proj=None)

#set the ImageCollection and Image
img_Collection = ee.ImageCollection('COPERNICUS/S5P/NRTI/L3_NO2').filterDate('2020-03-01', '2020-03-27').select('NO2_column_number_density')

my_Image=img_Collection.mean().clip(my_boundary)

#Create Ipyleaflet map
my_map = ipyleaflet.Map(zoom=7)

my_map.add_layer(layer=my_Image)

my_map

error:
AttributeError: 'Image' object has no attribute 'model_id'

Best Answer

No matter the API, clip() is a function on images, but not image collections. So in order to do it, you have to either turn your ImageCollection into an Image using reducers like mean(), mosaic() etc. or map clipping procedure over your ImageCollection, i.e.

imageCollection.map(function(image) { return image.clip(geometry); });

In pyhton API, use lambda function in such case.

Related Question