1

我正在尝试编写一个函数,该函数将使用 Sentinel 2 数据从图像集合中创建一个字典,该字典将包含标签/值对,其中标签来自图像的 MGRS_TILE 属性,值将包含所有图像的列表相同的 MGRS_TILE id。标签值必须不同。我希望输出是这样的: {'label' : 'tileid1', 'values':[ image1, image2 ...] 'label' : 'tileid2', 'values':[图像3,图像4 ...]}

下面是我的代码:interestImageCollection 是我过滤后的 imageCollection 对象 tileIDS 是一个 ee.List 类型对象,包含所有不同的 tile id,字段是我感兴趣的图像属性的名称,在本例中为“MGRS_TILE”。

var build_selectZT = function(interestImageCollection, tileIDS, field){

  //this line returns a list which contains the unique tile ids thanks to the keys function
  //var field_list = ee.Dictionary(interestImageCollection.aggregate_histogram(field)).keys();

  //.map must always return something
  var a = tileIDS.map(function(tileId) {
    var partialList=ee.List([]);
    var partialImage = interestImageCollection.map(function(image){
      return ee.Algorithms.If(ee.Image(image).get(field)==tileId, image, null);
    });
    partialList.add(partialImage);
    return ee.Dictionary({'label': tileId, 'value': partialList});
  }).getInfo();
  return a;
};

不幸的是,上面的函数给了我这个结果: {'label' : 'tileid1', 'values':[], 'label' : 'tileid2', 'values':[]}

4

1 回答 1

0

我认为您可以使用过滤功能而不是使用 if。如果您需要以列表形式使用它,则可以使用 toList 函数将其更改为列表。

var build_selectZT = function(interestImageCollection, tileIDS, field){
  //.map must always return something
  var a = tileIDS.map(function(tileId) {
    var partialList=ee.List([]);
    // get subset of image collection where images have specific tileId
    var subsetCollection = interestImageCollection.filter(ee.Filter.eq(field, tileId));
    // convert the collection to list
    var partialImage = subsetCollection.toList(subsetCollection.size())
    partialList.add(partialImage);
    return ee.Dictionary({'label': tileId, 'value': partialList});
  }).getInfo();
  return a;
};

但这实际上会给你一个字典列表

[{'label':'id1','value':[image1]},{'label':'id2','value':[image2,image3]......}]

如果您想使用ee.Algorithms.If就像您在代码中所做的那样,那么您的错误就在“ee.Image(image).get(field)==tileId”部分。由于 .get(field) 返回服务器端对象,因此您不能使用 == 将其等同于某些东西,因为它是一个字符串类型,您需要使用 compareTo 来代替。但是,如果字符串相同,则返回 0,并且由于 0 被视为 false,因此您可以在条件为 false 时返回图像。

return ee.Algorithms.If(ee.String(ee.Image(image).get(field)).compareTo(tileId), null, image);

我仍然认为这是一个不好的方法,因为你会得到一个充满 null 的数组,比如

[{'label':'id1','value':[image1, null, null, null, .....]},{'label':'id2','value':[null,image2,image3, null,....]......}]
于 2019-07-24T00:55:47.070 回答