【问题标题】:Is there a way to push key value pairs in google earth engine in a loop that is using the map() function?有没有办法在使用 map() 函数的循环中推送谷歌地球引擎中的键值对?
【发布时间】:2019-06-01 16:55:45
【问题描述】:

我正在谷歌地球引擎上对 LULC 进行时空分析。 为此,我导入了 Landsat 5 tier 1 TOA 反射图像,并根据我的偏好对其进行过滤。在此之后,我能够提取过滤图像集合中特征的 id 值,我需要创建一个字典,以便能够从通过切片 ID 提取的 ID 分配唯一名称并分配一个值(id 本身)到每一对。

获取的图像集合中图像的id类型为:LANDSAT/LT05/C01/T1_TOA/LT05_148045_19890509 在这, 键:19890509 值:LT05_148045_19890509

两者都可以通过对获取的ID进行切片获得

我已过滤图像集合并尝试按如下方式创建字典,但它创建了一个空字典。

// 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

我希望 dicty 的输出是一个键值对字典,每个键值在上述循环中动态分配,以便我可以使用字典键值对调用图像。

【问题讨论】:

    标签: javascript google-earth-engine


    【解决方案1】:

    一般来说,您希望向.map() 提供一个可迭代对象,然后您会得到一个具有原始长度的类似可迭代对象(该函数应用于每个项目)。 Earth Engine 并行处理提供到.map() 中的函数,从而难以将值推送到该函数内内存中的单个变量。因此,对此的解决方案是将您在函数中提取的 ID 值设置为集合中的每个图像作为属性,然后在函数外部将图像的名称和 ID 获取到字典中。这是一个工作代码示例:

    // 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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-06
      • 1970-01-01
      • 2019-01-05
      • 1970-01-01
      • 1970-01-01
      • 2019-08-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多