代码之家  ›  专栏  ›  技术社区  ›  rhal

在Earth Engine python脚本中获取结果

  •  0
  • rhal  · 技术社区  · 7 年前

    我正在尝试使用地球引擎python API获取特征集合中每个多边形的NDVI平均值。 我认为我成功地获得了结果(特征集合中的特征集合),但我不知道如何从中获取数据。 我想要的数据是来自特征的ID和每个特征中的ndvi平均值。

    import datetime
    import ee
    ee.Initialize()
    
    #Feature collection
    fc = ee.FeatureCollection("ft:1s57dkY_Sg_E_COTe3sy1tIR_U-5Gw-BQNwHh4Xel");
    fc_filtered = fc.filter(ee.Filter.equals('NUM_DECS', 1))
    #Image collection
    Sentinel_collection1 = (ee.ImageCollection('COPERNICUS/S2')).filterBounds(fc_filtered)
    Sentinel_collection2 = Sentinel_collection1.filterDate(datetime.datetime(2017, 1, 1),datetime.datetime(2017, 8, 1))
    
    
    # NDVI function to use with ee map
    def NDVIcalc (image):
      red = image.select('B4')
      nir = image.select('B8')
      ndvi = nir.subtract(red).divide(nir.add(red)).rename('NDVI')
    
      #NDVI mean calculation with reduceRegions
      MeansFeatures = ndvi.reduceRegions(reducer= ee.Reducer.mean(),collection= fc_filtered,scale= 10)
    
      return (MeansFeatures)
    
    #Result that I don't know to get the information: Features ID and NDVI mean
    result = Sentinel_collection2.map(NDVIcalc)
    
    1 回复  |  直到 7 年前
        1
  •  3
  •   Noel Gorelick    7 年前

    如果结果很小,则使用result将它们拉入python。getInfo()。这将为您提供一个包含FeatureCollection列表的python字典(更多字典)。但是,如果结果较大或多边形覆盖较大区域,则必须导出集合。

    也就是说,您可能还想先做一些其他事情:

    1) 您可能希望展平()集合,使其不是嵌套集合。这样处理会更容易。

    2) 您可能希望为每个结果添加一个日期,以便知道结果来自什么时间。您可以在NDVIcalc函数中使用结果上的映射来实现这一点

    return MeansFeatures.map(lambda f : f.set('date', image.date().format())
    

    3) 如果您真正想要的是每个多边形(最常见)随时间变化的NDVI时间序列,那么将代码重新构造为首先在多边形上映射会更容易:

    Sentinel_collection = (ee.ImageCollection('COPERNICUS/S2')
        .filterBounds(fc_filtered)
        .filterDate(ee.Date('2017-01-01'),ee.Date('2017-08-01')))
    
    def GetSeries(feature):
      def NDVIcalc(img):
        red = img.select('B4')
        nir = img.select('B8')
        ndvi = nir.subtract(red).divide(nir.add(red)).rename(['NDVI'])
        return (feature
                .set(ndvi.reduceRegion(ee.Reducer.mean(), feature.geometry(), 10))
                .set('date', img.date().format("YYYYMMdd")))
    
      series = Sentinel_collection.map(NDVIcalc)
      // Get the time-series of values as two lists.
      list = series.reduceColumns(ee.Reducer.toList(2), ['date', 'NDVI']).get('list')
      return feature.set(ee.Dictionary(ee.List(list).flatten()))
    
    result = fc_filtered.map(GetSeries)
    print(result.getInfo())
    

    4) 最后,如果您要尝试导出结果,很可能会遇到这样一个问题,即导出表的列是从第一个功能所具有的任何列中选择的,因此最好提供一个包含所有列(时间)的“标题”功能,您可以将()与结果合并为第一个功能:

    # Get all possible dates.
    dates = ee.List(Sentinel_collection.map(function(img) {
          return ee.Feature(null, {'date': img.date().format("YYYYMMdd") })
    }).aggregate_array('date'))
    
    # Make a default value for every date.
    header = ee.Feature(null, ee.Dictionary(dates, ee.List.repeat(-1, dates.size())))
    output = header.merge(result)
    ee.batch.Export.table.toDrive(...)