Search code examples
google-earth-engine

Get an array of specific parameters from Image Collection of Google Earth Engine


I've got an Image Collection like:

ImageCollection : {
  features : [
    0 : {
      type: Image,
      id: MODIS/006/MOD11A1/2019_01_01,
      properties : {
        LST_Day_1km   : 12345,
        LST_Night_1km : 11223,
        system:index  : "2019-01-01",
        system:asset_size: 764884189,
        system:footprint: LinearRing,
        system:time_end: 1546387200000,
        system:time_start: 1546300800000
      }, 
    1 : { ... }
    2 : { ... }
    ...
  ],
  ...
]

From this collection, how can I get an array of objects of specific properties? Like:

[
  {
    LST_Day_1km   : 12345,
    LST_Night_1km : 11223,
    system:index  : "2019-01-01"
  },
  {
    LST_Day_1km   : null,
    LST_Night_1km : 11223,
    system:index  : "2019-01-02"
  }
  ...
];

I tried ImageCollection.aggregate_array(property) but it allows only one parameter at one time.

The problem is that the length of "LST_Day_1km" is different from the length of "system:index" because "LST_Day_1km" includes empty values, so it's hard to combine arrays after get them separately.

Thanks in advance!


Solution

  • Whenever you want to extract data from a collection in Earth Engine, it is often a straightforward and efficient strategy to first arrange for the data to be present as a single property on the features/images of that collection, using map.

    var wanted = ['LST_Day_1km', 'LST_Night_1km', 'system:index'];
    var augmented = imageCollection.map(function (image) {
      return image.set('dict', image.toDictionary(wanted));
    });
    

    Then, as you're already familiar with, just use aggregate_array to extract that property's values:

    var list = augmented.aggregate_array('dict');
    print(list);
    

    Runnable complete example:

    var imageCollection = ee.ImageCollection('MODIS/006/MOD11A1')
        .filterDate('2019-01-01', '2019-01-07')
        .map(function (image) {
          // Fake values to match the question
          return image.set('LST_Day_1km', 1).set('LST_Night_1km', 2)
        });
    print(imageCollection);
    
    // Add a single property whose value is a dictionary containing
    // all the properties we want.
    var wanted = ['LST_Day_1km', 'LST_Night_1km', 'system:index'];
    var augmented = imageCollection.map(function (image) {
      return image.set('dict', image.toDictionary(wanted));
    });
    print(augmented.first());
    
    // Extract that property.
    var list = augmented.aggregate_array('dict');
    print(list);
    

    https://code.earthengine.google.com/ffe444339d484823108e23241db04629