1

I try to convert a javascript google earth engine workflow into python and I face some strange errors. More specifically, I use the below script in order to calculate the mean elevation of a predefined area:

feature_geometry = {
    'type': 'MultiPolygon',
    'coordinates': [[[
        [-113.11777746091163,35.924059850042575],
        [-112.43662511716161,35.924059850042575],
        [-112.43662511716161, 36.52671462113273],
        [-113.11777746091163, 36.52671462113273],
        [-113.11777746091163,35.924059850042575]
    ]]]
}

#Compute the mean elevation in the polygon.
meanDict = srtm.reduceRegion(
  reducer= ee.Reducer.mean(),
  geometry= feature_geometry,
  scale= 90
)


mean = meanDict.get('elevation');
print(mean)

When I execute the above, I get back a dictionary like the one below:

ee.ComputedObject({
  "type": "Invocation",
  "arguments": {
    "dictionary": {
      "type": "Invocation",
      "arguments": {
        "image": {
          "type": "Invocation",
          "arguments": {
            "id": "CGIAR/SRTM90_V4"
          },
          "functionName": "Image.load"
        },
        "reducer": {
          "type": "Invocation",
          "arguments": {},
          "functionName": "Reducer.mean"
        },
        "geometry": {
          "type": "MultiPolygon",
          "coordinates": [
            [
              [
                [
                  -113.11777746091163,
                  35.924059850042575
                ],
                [
                  -112.43662511716161,
                  35.924059850042575
                ],
                [
                  -112.43662511716161,
                  36.52671462113273
                ],
                [
                  -113.11777746091163,
                  36.52671462113273
                ],
                [
                  -113.11777746091163,
                  35.924059850042575
                ]
              ]
            ]
          ]
        },
        "scale": 90
      },
      "functionName": "Image.reduceRegion"
    },
    "key": "elevation"
  },
  "functionName": "Dictionary.get"
})

Instead the javascript code from this tutorial returns a string value with the result.

What is the correct way to do this in python?

user1919
  • 3,818
  • 17
  • 62
  • 97

1 Answers1

1

In the python earth engine API, print does not execute the server side code and return the value like it does in javascript. From https://developers.google.com/earth-engine/deferred_execution (an aside near the bottom):

(In Python, it's necessary to call getInfo() on the object being printed; otherwise the request JSON is printed).

So to get the value, you need to explicitly call .getInfo(), like this:

mean = meanDict.get('elevation').getInfo();
print(mean)

To get a better grasp of what's happening I would recommmend studying the link above and also this page. In my experience, client vs. server side stuff in EE is the most difficult thing to keep tabs on.

Jesse Anderson
  • 4,507
  • 26
  • 36