【问题标题】:Iterate over Image Collection Google Earth Enigne遍历图像集合 Google 地球引擎
【发布时间】:2020-06-06 13:06:45
【问题描述】:

我编写了处理所有 Landsat 图像并计算 NDVI 的函数。但是,我有 59 个 GPS 点,我希望每个 GPS 点都有一个 NDVI 时间序列输出。运行我的代码后,似乎生成的 NDVI 值不是每个点,而是每个图像的单个值。我猜测某种边界框是自动创建并用于计算的,而不是使用 GPS 点。因此,我需要在所有 59 个点上迭代我的函数,并将输出保存到表中。

GPS 文件是一个 ESRI 点形状文件。

最好的方法是什么?

这是我的一些代码:

// import GPS locations from asset
GPS = GPS.geometry();

// Calculates the median NDVI and add value to image properties.
var meanNDVI = ndviLandsatCol.map(function(img) {
  var obs = img.reduceRegion({
    geometry: GPS,
    reducer: ee.Reducer.median(),
    scale: 30
  });
  return img.set('NDVI', obs.get('NDVI'));
});

ndviLandsatCol 变量是经过预处理的图像集合,其中 NDVI 添加为波段。 我对编码和谷歌地球引擎还是陌生的。有人可以建议如何在我所有的 GPS 点上迭代这个过程吗?我应该如何阅读 GPS 文件、字典?以及如何在不绘制点和下载随附文件的情况下将其保存到 .CSV 中。

任何帮助将不胜感激。

【问题讨论】:

    标签: javascript loops google-earth-engine


    【解决方案1】:

    如果您想从特征集合中每个点的图像中获取值,您需要reduceRegions,而不是reduceRegion。这将返回一个特征集合。比如:

    var allObs = ndviLandsatCol.map(function(img) {
      var obsAtTime = img.reduceRegions({
        collection: GPS,
        reducer: ee.Reducer.median(),
        scale: 30
      });
      return obsAtTime.map(function (feature) {
        return feature.copyProperties(img, ['system:time_start']);
      });
    }).flatten();
    

    请注意,您应该删除您拥有的GPS = GPS.geometry(); 行,因为这会丢弃特征集合的结构并将其转换为一个几何图形。

    这将为您提供一个适合导出为 CSV 的平面表,但您必须自己将行分组为时间序列。

    如果您需要在 Earth Engine 中进一步处理时间序列,那么这里有两种不同的方式来对它们进行分组:

    1. 使用ee.Join.saveAll。这会给你一个FeatureCollection,就像GPS,但有一个额外的属性,其中包含时间序列中的Features,并且每个活动都有与ndviLandsatCol中的波段相对应的属性。

      print(
        ee.Join.saveAll('series')  // 'series' is the name of the time series property.
          .apply(
            allObs.distinct('.geo').select([]),  // Determines properties of outer features (here, none but the geometry).
            allObs,
            ee.Filter.equals({leftField: '.geo', rightField: '.geo'})));
      
    2. 使用分组reducer。这将为您提供一个数字列表的字典列表,每个数字列表都是从原始ndviLandsatCol 中明确选择的一个波段的时间序列。在我看来,这有点混乱,但可能会给你一个更简单的工作。

      print(allObs.reduceColumns(
        ee.Reducer.toList().setOutputs(['NDVI'])
          .combine(ee.Reducer.toList().setOutputs(['time']))
          .group(0),
        [
          '.geo',               // select geometry for the group() operation
          'NDVI',               // first toList reducer
          'system:time_start',  // second toList reducer
        ]));
      

    【讨论】:

    • 感谢@Kevin Reid,您的见解非常有帮助。
    【解决方案2】:

    这是最终的解决方案:

    // Collect GPS, image, NDVI triplets.
    var triplets = NDVILandsatCol.map(function(image) {
      return image.select('NDVI').reduceRegions({
        collection: GPS.select(['Site_ID']), 
        reducer: ee.Reducer.mean(), 
        scale: 30
      }).filter(ee.Filter.neq('mean', null))
        .map(function(f) { 
          return f.set('imageId', image.id());
        });
    }).flatten();
    print(triplets.first());
    
    // Format a table of triplets into a 2D table of rowId x colId.
    var format = function(table, rowId, colId) {
      var rows = table.distinct(rowId);
      var joined = ee.Join.saveAll('matches').apply({
        primary: rows, 
        secondary: table, 
        condition: ee.Filter.equals({
          leftField: rowId, 
          rightField: rowId
        })
      });
    
      return joined.map(function(row) {
          var values = ee.List(row.get('matches'))
            .map(function(feature) {
              feature = ee.Feature(feature);
              return [feature.get(colId), feature.get('mean')];
            });
          return row.select([rowId]).set(ee.Dictionary(values.flatten()));
        });
    };
    
    // Export table
    var table1 = format(triplets, 'imageId', 'Site_ID');
    var desc1 = 'Table_demo'; 
    Export.table.toDrive({
      collection: table1, 
      description: desc1, 
      fileNamePrefix: desc1,
      fileFormat: 'CSV'
    });
    
    

    【讨论】:

      猜你喜欢
      • 2017-10-07
      • 1970-01-01
      • 1970-01-01
      • 2017-06-18
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多