Search code examples
javascriptgoogle-earth-engine

Is there a way to push key value pairs in google earth engine in a loop that is using the map() function?


I am doing a spatiotemporal analysis of LULC on google earth engine. For this, I have imported Landsat 5 tier 1 TOA reflectance images and filtered them by my preference. Following this I was able to extract the id values of the features in the filtered image collection, I need to create a dictionary in order to be able to assign the unique names from ID extracted by slicing the ID's and assign a value(id itself) to each pair.

The id obtained of images in an image collection is of the type: LANDSAT/LT05/C01/T1_TOA/LT05_148045_19890509 in this, key:19890509 value:LT05_148045_19890509

both of which can be obtained from slicing the obtained ID

I have filtered the image collection and tried to create a dictionary as follows but it creates an empty dictionary.

// Create a list of image objects.
var imageList = Collection.toList(100);
print('imageList', imageList);

// Extract the ID of each image object.
var dicty = ee.Dictionary({}); //def dict for names
var id_list = imageList.map(function(item) {
    var list_nm = ee.Image(item).id();
    var lst_nm_slice = ee.Image(item).id().slice(-8,-1);
    dicty.lst_nm_slice = list_nm;
    return dicty;
});//end of map function

I expect the output of dicty to be a dictionary of key-value pairs with each key value getting assigned dynamically in the aforementioned loop so that I can call the images by using the dictionary key value pairs.


Solution

  • Generally speaking, you want to provide an iterable object to .map() and you get out an iterable like object with the original length (that function applied to each item). Earth Engine processes the function provided into .map() in parallel making it difficult to push values to a single variable in memory within that function. So, a solution to this is to set the ID values that you extract within the function to each image in the collection as a property and then outside of the function get the names and ids of the image into a dictionary. Here is a working code example:

    // Create an ImageCollection and filter by space and time
    var Collection = ee.ImageCollection('LANDSAT/LT05/C01/T1_TOA')
      .filterDate('1990-01-01','1991-01-01')
      .filterBounds(ee.Geometry.Point([86.5861,34.7304]));
    
    print('LT5 Image Collection', Collection);
    
    // Extract the ID of each image object and set as property
    // within for each image in the collection
    var Collection = Collection.map(function(img) {
        var img_id = img.id();
        var id_slice = img_id.slice(-8);
        return img.set('id',id_slice);
    });//end of map function
    
    // Get the image IDs and names as lists from the collection
    var ids = ee.List(Collection.aggregate_array('id'));
    var names = ee.List(Collection.aggregate_array('system:index'));
    
    // Build dictionary from each image ID and name
    var out_dict = ee.Dictionary.fromLists(ids,names);
    print('Output Dictionary',out_dict);