【问题标题】:Get results in an Earth Engine python script在 Earth Engine python 脚本中获取结果
【发布时间】:2017-12-05 21:46:47
【问题描述】:

我正在尝试使用地球引擎 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)

【问题讨论】:

    标签: python google-earth-engine


    【解决方案1】:

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

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

    1) 你可能想要 flatten() 集合,所以它不是嵌套集合。这样会更容易处理。

    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(...)
    

    【讨论】:

    • 非常感谢!!对我来说,在字典中获取 de 值而不是导出到驱动器会更好,但我想我会这样做,因为特征集合有数千个特征。我现在唯一不能做的就是使用标题导出。我有点迷失了从 Javascript 到 python 的“翻译”。这是一段代码(请原谅缺少格式):
    • header = ee.Feature(None, ee.Dictionary(dates, ee.List.repeat(-1, dates.size()))) print "ok" output = header.merge(result ) task=ee.batch.Export.table.toDrive(collection=output,description='Tabla_stack', fileFormat='CSV') task.start() print task.status()
    猜你喜欢
    • 1970-01-01
    • 2010-10-22
    • 1970-01-01
    • 1970-01-01
    • 2020-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-01
    相关资源
    最近更新 更多