3

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'

2 Answers 2

3

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.

5
  • Basile, thanks for the comment. I used the mean() reducer and then clip() . I now turned the ImageCollection into Image like you advised. Now when I add layer to my map I'm getting: " AttributeError: 'Image' object has no attribute 'model_id' "
    – Ofir
    Mar 30, 2020 at 17:20
  • Please edit your question to include your code
    – Basile
    Mar 30, 2020 at 17:30
  • I added my code to my question
    – Ofir
    Mar 30, 2020 at 17:56
  • 1
    @Ofir I made an example with IPython.display which shows that the image is clipped as expected, and thus, the problem is related to ipyleaflet rendering, which is a separate matter. Here's the link to Colab notebook
    – Basile
    Mar 31, 2020 at 11:17
  • 1
    I guess it's more complicated or unsupported with Ipyleaflet (in Jupyter). I ran the same code using the Qgis EarthEnngine plugin and it worked also. I learned alot from this problem, thanks for the help!
    – Ofir
    Mar 31, 2020 at 12:17
1

Just to add the Python code to Basile's answer:

collection = ee.ImageCollection("COPERNICUS/S2") \
.filterBounds(aoi) 

clipped = collection.map(lambda image: image.clip(aoi))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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