简体   繁体   English

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

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

I have a JS Code which runs well:我有一个运行良好的 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});

I tried to convert the code to python:我试图将代码转换为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)

But gives the error:但给出了错误:

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

I've read the google guide for converting form JS to python but had no idea why this happen.我已经阅读了将表单 JS 转换为 python 的谷歌指南,但不知道为什么会发生这种情况。 Is the error duo to wrong syntax?错误是由于错误的语法吗?

In the Python EE API, use Python named argument syntax instead of dictionaries for arguments.在 Python EE API 中,使用 Python 命名参数语法而不是字典作为参数。

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

Note that a property name is not a named argument, so set() works the same way as in JavaScript;请注意,属性名称不是命名参数,因此set()工作方式与 JavaScript 中的相同; you can use a dictionary or not, but do not use = .您可以使用或不使用字典,但不要使用=

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM