如何在 GEE 中将 FeatureCollection 从 Javascript 转换为 Python API?

How to convert from Javascript to Python API for FeatureCollection in GEE?

我有一个运行良好的 JS 代码:

dataset = ee.Image('USGS/SRTMGL1_003');
elevation = dataset.select('elevation');

var means_of_tea = tea.map(function(field){
  var elevation_mean = elevation.reduceRegion({
    reducer: ee.Reducer.mean(),
    geometry: field.geometry(),
    scale: 30,
    maxPixels: 1e9
  });
  var slope_mean = slope.reduceRegion({
    reducer: ee.Reducer.mean(),
    geometry: field.geometry(),
    scale: 30,
    maxPixels: 1e9
  });
  return field.set({elevation:elevation_mean, slope:slope_mean});

我尝试将代码转换为 python:

def map_fc(field):
  elevation_mean = elevation.reduceRegion({
      'reducer': ee.Reducer.mean(),
      'geometry': field.geometry(),
      'scale': 30,
      'maxPixels': 1e9
  })
  return field.set({'elevation': elevation_mean})
teawithmean = tea.map(map_fc)

但是报错:

<ipython-input-36-e999072d4723> in <module>()
      9   })
     10   return field.set({'elevation': elevation_mean})
---> 11 teawithmean = tea.map(inmap)

17 frames
/usr/local/lib/python3.6/dist-packages/ee/__init__.py in init(self, *args)
    397           raise EEException(
    398               'Invalid argument for ee.{0}(): {1}.  '
--> 399               'Must be a ComputedObject.'.format(name, args))
    400         else:
    401           result = args[0]

EEException: Invalid argument for ee.Reducer(): ({'reducer': <ee.Reducer object at 0x7f3b4699e4a8>, 'geometry': ee.Geometry({
  "functionInvocationValue": {
    "functionName": "Feature.geometry",
    "arguments": {
      "feature": {
        "argumentReference": null
      }
    }
  }
}), 'scale': 30, 'maxPixels': 1000000000.0},).  Must be a ComputedObject.

我已经阅读了 google 将表单 JS 转换为 python 的指南,但不知道为什么会这样。是语法错误导致的错误吗?

在 Python EE API 中,使用 Python 命名参数语法而不是参数字典。

  elevation_mean = elevation.reduceRegion(
      reducer=ee.Reducer.mean(),
      geometry=field.geometry(),
      scale=30,
      maxPixels=1e9
  )

请注意,属性 名称不是命名参数,因此 set() 的工作方式与 JavaScript 相同;你可以使用或不使用字典,但不要使用 =.

return field.set({'elevation': elevation_mean})
# or
return field.set('elevation', elevation_mean)