Using Google Earth Engine in a Jupyter Notebook#
In this lesson, we will learn how to set up and use the Google Earth Engine (GEE) Python API in a Jupyter notebook. By the end of this lesson, you will be able to install the necessary packages, authenticate your account, write your first code, and access documentation to explore more advanced features.
Key Topics#
Setup and Installation
Authentication
Writing Your First Code
Accessing Documentation and Resources
Example: Visualizing a Satellite Image
Setup and Installation#
Before we can start using Google Earth Engine in a Jupyter notebook, we need to install the Earth Engine Python API.
Step 1: Install the Earth Engine Python API#
Run the following command to install the earthengine-api
package:
!pip install earthengine-api
Step 2: Install Additional Libraries#
We will also need the folium library to visualize maps. Run the following command to install folium:
!pip install folium
Step 3. Authentication#
Authentication#
To use Google Earth Engine, you need to authenticate your account. This involves linking your Google account to Earth Engine.
Authenticate Your Account#
Run the following code to authenticate your account:
import ee
ee.Authenticate()
Follow the instructions to open the provided URL in your browser, log in with your Google account, and paste the authentication code back into the notebook.
Step 4. Initialize the Earth Engine API#
After authenticating, initialize the Earth Engine API:
ee.Initialize()
Writing Your First Code#
Now that we have set up and authenticated the Earth Engine API, let’s write our first code to load and display a satellite image.
# Load a Landsat image
image = ee.Image('LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140318')
# Define visualization parameters
vis_params = {
'bands': ['B4', 'B3', 'B2'],
'max': 0.3
}
import folium
# Center the map on the image
map_center = image.geometry().centroid().getInfo()['coordinates'][::-1]
# Create a folium map
mymap = folium.Map(location=map_center, zoom_start=10)
mymap.add_ee_layer(image, vis_params, 'Landsat image')
# Display the map
mymap
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[1], line 2
1 # Load a Landsat image
----> 2 image = ee.Image('LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140318')
4 # Define visualization parameters
5 vis_params = {
6 'bands': ['B4', 'B3', 'B2'],
7 'max': 0.3
8 }
NameError: name 'ee' is not defined
In this example, we:
Loaded a Landsat image using the Earth Engine API. Defined visualization parameters to specify which bands to display and their maximum values. Created a Folium map centered on the image’s location. Added the image layer to the map and displayed it.
Accessing Documentation and Resources#
Google Earth Engine provides extensive documentation and resources to help you learn and explore its capabilities.
Official Documentation#
Earth Engine API Documentation: Google Earth Engine API
Python API Quickstart: Python API Quickstart
Tutorials and Examples#
Earth Engine Guide: Earth Engine Guide
Code Editor Examples: Code Editor Examples
Community and Support#
Earth Engine Developers Group: Google Groups
Stack Overflow: Use the tag
google-earth-engine
to ask questions and find answers.
By exploring these resources, you can deepen your understanding of Google Earth Engine and discover new ways to apply it to your projects.