The Google Earth Engine (GEE) Python API facilitates interacting with Earth Engine servers using the Python programming language. It is available as a Python package that can be installed locally or within the cloud and accessed from a command-line interpreter or within a Jupyter notebook.
Set up Python API for GEE and continue following
The Python API provides a programmatic and flexible interface to Earth Engine. It allows for automating batch processing tasks, piping Earth Engine processed data to Python packages for post-processing and leveraging the power of the command line.
The Python API package is called ee
. It must also be initialized for every new session and script.
# import Google earth engine module import ee #Authenticate the Google earth engine with google account ee.Initialize()
To render GEE Tile Layers in python API, there is a python library called Folium which helps create the interactive map based on Leaflet JS. Install Folium as PIP
pip install folium
Now import Folium and set the satellite image to get an interactive map with GEE WMS
#import module import folium #Define year startyear = 2019; endyear = 2019; #Create the Date object startdate = ee.Date.fromYMD(startyear,1,1); enddate = ee.Date.fromYMD(endyear,12,1); # Define a method for displaying Earth Engine image tiles on a folium map. def add_ee_layer(self, ee_object, vis_params, name): try: # display ee.Image() if isinstance(ee_object, ee.image.Image): map_id_dict = ee.Image(ee_object).getMapId(vis_params) folium.raster_layers.TileLayer( tiles = map_id_dict['tile_fetcher'].url_format, attr = 'Google Earth Engine', name = name, overlay = True, control = True ).add_to(self) except: print("Could not display {}".format(name)) # Add EE drawing method to folium. folium.Map.add_ee_layer = add_ee_layer #modis Data for NDVI dataset = ee.ImageCollection('MODIS/006/MOD13Q1').filter(ee.Filter.date(startdate,enddate)).first() modisndvi = dataset.select('NDVI') # Set visualization parameters. visParams = { 'min': 0.0, 'max': 8000.0, 'palette': [ 'FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163', '99B718', '74A901', '66A000', '529400', '3E8601', '207401', '056201', '004C00', '023B01', '012E01', '011D01', '011301' ],} # Create a folium map object. my_map = folium.Map(location=[28.5973518, 83.54495724], zoom_start=6, height=500) # Add the data to the map object. my_map.add_ee_layer(modisndvi, visParams, 'Modis NDVI') # Display the map. display(my_map)
In the above code, Modis NDVI is generated for the year 2019 and displayed in the interactive map with the folium module. To generate HTML output of the above result use:
my_map.save('index.html')