0

I'm trying to to make my javascript Google Earth Engine code iterable, in the sense that I run the code once and it iterates through all of my cloud assets in a given folder. Each cloud asset represents an uploaded shapefile boundary- I want the code to automatically create new files clipped to all of my cloud assets. As I have hundreds of assets, this would take a long time to do manually.

I can create a list of all my cloud assets in a given folder using:

var assets = ee.data.listAssets('projects/FOLDER NAME/assets')
print(assets)

Printing the list produces an Object of tables. The properties "id" and "name" are the full name of the asset in its folder. However, I can't seem to be able to index this Object either through using the property id or by trying to isolate even the 1st item in the list. Whenver I do I don't get an error but the console simply outputs "undefined." If I could isolate the "id" or "name" properties from the Object, I could create a simple for loop where I could iterate through all of my assets.

I can turn this list into a dictionary var dict = ee.Dictionary(assets) and then isolate the "ga" property using var dict2 = dict.get('ga'), but this seems to make matters worse by increasing the size of the hierarchical structure of the Object. When I turn the list into a dictionary, the only key in the dictionary is "ga", which isn't helpful in solving my problem.

The closest related code I've been able to find is for GEE Python from 2018, but it uses a deprecated function (ee.data.getList).

Iterate over GEE assets in Python script and mixing local and server side objects

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')

I also found this related code from 2021 which uses the Python API. While this code does not use deprecated functions, I haven't been able to get asset names when translating this code to JavaScript. Again, doing so simply produces a console output of "undefined." What are available asset types in Google Earth Engine?

def get_assets(folder=None, asset_list = []):

    # set the folder
    folder = folder if folder else ee.data.getAssetRoots()[0]['id']
    
    # loop in the assets
    for asset in ee.data.listAssets({'parent': folder})['assets']:
        if asset['type'] == 'FOLDER':
            asset_list = get_assets(asset['name'], asset_list)
        else:
            asset_list += [asset]
    
    return asset_list

1 Answer 1

0

I found the answer in a Stack Exchange question from earlier this year.

How to open a list of images from an asset as an Image Collection?

The following code does what I was looking for:

var assetList = ee.data.listAssets("projects/FOLDER NAME/assets")['assets']
                    .map(function(d) { return d.name })
7
  • If your problem is solved, accept this as answer.
    – Padmanabha
    Oct 13, 2022 at 7:33
  • Can you help me to access my all gee assets and apply NDVI LST results on each asset? I'm also trying the same thing Oct 13, 2022 at 17:14
  • 1
    I cannot accept the answer; Stack Exchange is saying that I have to wait 19 more hours.
    – user5858
    Oct 14, 2022 at 0:08
  • can you share your completed code? Oct 14, 2022 at 15:44
  • @SobanBehzad With the code solution beginning with var assetList, you're most of the way there. I just made a simple for loop to iterate over the assetList.
    – user5858
    Oct 14, 2022 at 20:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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