Earth Engine Python API

1.7.22 · active · verified Wed Apr 15

The Earth Engine Python API provides client libraries for interacting with Google Earth Engine, a planetary-scale cloud-based platform for geospatial analysis. It offers access to a multi-petabyte catalog of satellite imagery and other geospatial datasets, enabling users to perform complex analyses like detecting changes, mapping trends, and quantifying differences on the Earth's surface. The API is in active development, with frequent updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to authenticate, initialize the Earth Engine API, and perform a basic operation (loading an image collection and getting its size). Users must replace `your-earthengine-project-id` with their actual Google Cloud Project ID, which needs to have the Earth Engine API enabled and registered for use.

import ee
import os

# IMPORTANT: Replace 'your-earthengine-project-id' with your actual Google Cloud Project ID.
# This project must have the Earth Engine API enabled.
# If running in Colab or a local environment with gcloud SDK, ee.Authenticate() will open a browser for login.
# For service account authentication, set GOOGLE_APPLICATION_CREDENTIALS env var.

project_id = os.environ.get('EE_PROJECT', 'your-earthengine-project-id')

try:
    ee.Authenticate()
    ee.Initialize(project=project_id, opt_url='https://earthengine-api.googleapis.com')
    print('Earth Engine initialized successfully.')
    
    # Example: Load a Landsat 8 image collection and print its size.
    collection = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')\
                     .filterDate('2020-01-01', '2020-01-31')
    
    # All Earth Engine operations are server-side. To get a client-side value,
    # use .getInfo(). Be mindful of data size when using getInfo().
    print(f'Collection size: {collection.size().getInfo()}')
    
except ee.EEException as e:
    print(f'Earth Engine initialization failed: {e}')
    print('Please ensure you have registered for Earth Engine access, enabled the API for your Google Cloud Project, and authenticated.')
    print('Refer to https://developers.google.com/earth-engine/guides/getstarted for setup instructions.')
except Exception as e:
    print(f'An unexpected error occurred: {e}')

view raw JSON →