2

I just signed up for Google Earth Engine and installed the Python API. I am currently facing an issue where outputs from the various API functions do not work as intended, they produce references to the objects in memory instead of producing the outputs mentioned in the documentation. An example of calculating the area of a Polygon is shown below where the area is not outputted.

import ee
ee.Initialize()

polygon = ee.Geometry.Polygon(
    [[[-122.092,37.424],
    [-122.086,37.418],
    [-122.079,37.425],
    [-122.085,37.423]]])

polygonArea = polygon.area(maxError=1)
polygonArea
Out[3]: <ee.ee_number.Number at Ox1d4d8250130>
0
1

The objects are working as intended, they are references to objects in the Google Earth Engine servers.

Read up on the difference between server and client side objects. The doc is javascript based, but applies to the python API as well. You can use getInfo() to convert to a client side object, but do so only if you really need to.

Warning: Use getInfo() with caution!

You shouldn't use getInfo() unless you absolutely need to. If you call getInfo() in your code, Earth Engine will open the container and tell you what's inside, but it will block the rest of your code until that's done. (You can optionally provide a callback function to avoid that, but the better option is to do everything on the server, if possible.)

import ee
ee.Authenticate()
ee.Initialize()

serverside_number = ee.Number(10.3)
clientside_number = serverside_number.getInfo()

print(type(serverside_number))
print(type(clientside_number))
<class 'ee.ee_number.Number'>
<class 'float'>
polygon = ee.Geometry.Polygon(
    [[[-122.092,37.424],
    [-122.086,37.418],
    [-122.079,37.425],
    [-122.085,37.423]]])

polygonArea = polygon.area(maxError=1).getInfo()
print(type(polygonArea))
print(polygonArea)
<class 'float'>
314214.1383491078

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.