Introduction to the Google Earth Engine Python API

Published on Apr 13, 2020 | Bikesh Bade | 2549 Views

Google Earth Engine is available via a web-based JavaScript Application Program Interface (API) called the Code Editor. This platform is where users can write and execute scripts to share and repeat geospatial analysis and processing workflows. The Code Editor offers access to the full power of the Earth Engine. 

 

In addition to the web-based IDE Google Earth Engine also provides a Python API that can be used on your local machine without the need to utilize a browser, although the capabilities of this API are reduced compared to the Code Editor/IDE. This tutorial will go over how to set up the API on your local machine as well as some basic Python scripts utilizing the API. It is important to note that the Python API does not support any kind of visual output. 

 

Access to Google Earth Engine is currently only available to registered users. The API is in active development, and users should expect the API to change. When (not if) API changes occur, applications that use the API will likely need to be updated.

 

Step 1:

To set up Python, first, you have to install python on your local machine and pip as well for installing packages (if you are using python higher than 2.7 pip is default installed)

 

  1. Download Python
  2. Download pip

 

 

Step 2:

Install the python module google API python client.

 

pip install google-api-python-client

 

 

Step 3:

Install the proper crypto libraries for the code security

 

pip install pyCrypto

 

 

Step 4:

Install the Earth Engine Python library

 

 pip install earthengine-api

 

 

Step 5:

Run the below command from a command-line to initialize the API and verify your account. (Mostly remember you you have registered in google earth engine with Gmail). It's a one-time setup.

 

 earthengine authenticate

 

This will open your default web-browser (ensure that you’re currently logged into your Google account) and provide you with a unique key to verify your account. Copy and paste the key into the terminal when prompted for the key.

 

 

Step 6:

Run python so that you’re utilizing the Python Command Line Interface (CLI) and run the following commands to ensure that the Earth Engine Python API is properly installed:

 


#import module 
 >>> import ee

#intialize the google earth api
 >>> ee.Initialize()

#create image object
 >>> image = ee.Image('srtm90_v4')

#get the information on the image
 >>> print(image.getInfo())

 

If you see metadata printed to the terminal and there are no errors then the Python API for Earth Engine is properly installed and you are ready to use it.

 

Examples

 

Running below script will print the path used to download Landsat image collection from Landsat 8

 

#import module 
import ee

#intialize the google earth api
ee.Initialize()

geometry = ee.Geometry.Rectangle([80.058, 26.347, 82.201, 28.447]) 

region = geometry.toGeoJSONString()#region must in JSON format


# Get a download URL for an image.
imageCollection = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')
imageCollection  = imageCollection .mosaic()
url = imageCollection .getDownloadUrl({
                    'scale': 30,
                    'crs': 'EPSG:4326',
                    'region': region
                })
print(url)

Responses

Attia

Thank you! I would love to get some more tutorials on the Python API for GEE. More specific - how to load JSON file, how to set region for ROI using JSON file, how to calculate NDVI etc. Thank you!

  • Apr 22, 2020 |

Amrit Pokhrel

I went to a error while trying to run ee.Initialize() in python. I am using Pop!_os and Anacando for the python. Any suggestions??

  • May 14, 2020 |

Adrián Rodríguez

When I ran the example code the following error is generated: The total size of the request (1425487767 bytes) must be less than or equal to 33554432 bytes. I had to reduce the geometry to 3 decimal places for it to work: [80, 26, 80.32503, 26.32503]. Is there a solution to this problem without reducing the geometry?

  • Aug 16, 2020 |

Leave your comment