0

I have below two questions that are related together:

1- Can I iterate over items in my GEE assets in Python script? If yes, how?

2- My understanding is that all the images and features (and their collections) are server side objects and I can not define an image based on a local file on my computer and then for example clip a Landsat image in GEE repository with it, am I right?

2

Simple approach for 1)

import ee
ee.Initialize()
def iterate_over_asset(assetId):
    assets = ee.data.getList({'id':assetId})
    for asset in assets:
        asset_type = asset['type']
        asset_id = asset['id']
        if asset_type == 'ImageCollection':
            collection = ee.ImageCollection(asset_id)
            # do something with collection
        elif asset_type == 'Image':
            image = ee.Image(asset_id)
            # do something with image

iterate_over_asset('ASSET_ID')

If you need to do something over each image (no matter how 'deep' it is), you can make that function recursive:

def iterate_over_asset_images(assetId):
    assets = ee.data.getList({'id':assetId})
    for asset in assets:
        asset_type = asset['type']
        asset_id = asset['id']
        if asset_type != 'Image':
            iterate_over_asset_images(asset_id)
        else:
            image = ee.Image(asset_id)
            # do something with image

iterate_over_asset_images('ASSET_ID')

For question 2), you are right, all GEE objects are server side, so at first you could not do that. A workaround would be to get the boundaries of the local image using python and then create a geometry for clipping.

2
  • Hello again @Rodrigo, can I ask another question about first point? I also wanted to run the iteration in GEE code editor (java script), but I can not get anything out of ee.data.getList. It returns me a list but for example when I select the first entry (via get(0)) and then try to extract type and id, it returns null. – Shahriar49 Feb 20 '19 at 0:33
  • ee.data.getList is a client side function, so it returns a JS list. To get the items just use indexing: code.earthengine.google.com/d2b40eafeb14c9d70680442a2c77411e – Rodrigo E. Principe Feb 20 '19 at 10:32

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