有没有办法在使用 map() 函数的循环中推送 google 地球引擎中的键值对?
Is there a way to push key value pairs in google earth engine in a loop that is using the map() function?
我正在 google earth engine 上对 LULC 进行时空分析。
为此,我导入了 Landsat 5 tier 1 TOA 反射率图像,并根据我的喜好对其进行了过滤。在此之后,我能够提取过滤图像集合中特征的 id 值,我需要创建一个字典,以便能够从通过切片 ID 提取的 ID 中分配唯一名称并分配一个值(id 本身)每对。
图片集获取的图片id为LANDSAT/LT05/C01/T1_TOA/LT05_148045_19890509类型
在此,
key:19890509
value: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 的输出是键值对的字典,每个键值在上述循环中动态分配,这样我就可以使用字典键值对调用图像。
一般来说,您想向 .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);
我正在 google earth engine 上对 LULC 进行时空分析。 为此,我导入了 Landsat 5 tier 1 TOA 反射率图像,并根据我的喜好对其进行了过滤。在此之后,我能够提取过滤图像集合中特征的 id 值,我需要创建一个字典,以便能够从通过切片 ID 提取的 ID 中分配唯一名称并分配一个值(id 本身)每对。
图片集获取的图片id为LANDSAT/LT05/C01/T1_TOA/LT05_148045_19890509类型 在此, key:19890509 value: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 的输出是键值对的字典,每个键值在上述循环中动态分配,这样我就可以使用字典键值对调用图像。
一般来说,您想向 .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);