08 ee js to ipynb
Uncomment the following line to install geemap if needed.
# !pip install geemap
Automatic conversion from Earth Engine JavaScripts to Python scripts¶
Install Earth Engine API and geemap¶
Install the Earth Engine Python API and geemap. The geemap Python package is built upon the ipyleaflet and folium packages and implements several methods for interacting with Earth Engine data layers, such as Map.addLayer()
, Map.setCenter()
, and Map.centerObject()
.
The following script checks if the geemap package has been installed. If not, it will install geemap, which automatically installs its dependencies, including earthengine-api, folium, and ipyleaflet.
Important note: A key difference between folium and ipyleaflet is that ipyleaflet is built upon ipywidgets and allows bidirectional communication between the front-end and the backend enabling the use of the map to capture user input, while folium is meant for displaying static data only (source). Note that Google Colab currently does not support ipyleaflet (source). Therefore, if you are using geemap with Google Colab, you should use import geemap.foliumap
. If you are using geemap with binder or a local Jupyter notebook server, you can use import geemap
, which provides more functionalities for capturing user input (e.g., mouse-clicking and moving).
import geemap
geemap.show_youtube('RpIaalFk4H8')
Convert Earth Eninge JavaScripts to Python scripts¶
import os
from geemap.conversion import *
# Create a temporary working directory
work_dir = os.path.join(os.path.expanduser('~'), 'geemap')
# Get Earth Engine JavaScript examples. There are five examples in the geemap package folder.
# Change js_dir to your own folder containing your Earth Engine JavaScripts,
# such as js_dir = '/path/to/your/js/folder'
js_dir = get_js_examples(out_dir=work_dir)
# Convert all Earth Engine JavaScripts in a folder recursively to Python scripts.
js_to_python_dir(in_dir=js_dir, out_dir=js_dir, use_qgis=True)
print("Python scripts saved at: {}".format(js_dir))
Converting Earth Engine JavaScripts to Python scripts... Processing 1/5: /home/runner/geemap/ModisQaBands.js Processing 2/5: /home/runner/geemap/QualityMosaic.js Processing 3/5: /home/runner/geemap/NormalizedDifference.js Processing 4/5: /home/runner/geemap/ClippedComposite.js Processing 5/5: /home/runner/geemap/FromName.js Python scripts saved at: /home/runner/geemap
Convert Earth Engine Python scripts to Jupyter Notebooks¶
# Convert all Earth Engine Python scripts in a folder recursively to Jupyter notebooks.
nb_template = get_nb_template() # Get the notebook template from the package folder.
py_to_ipynb_dir(js_dir, nb_template)
# Execute all Jupyter notebooks in a folder recursively and save the output cells.
execute_notebook_dir(in_dir=js_dir)
Converting Earth Engine Python scripts to Jupyter notebooks ... Processing 1/5: /home/runner/geemap/ModisQaBands_qgis.py Processing 2/5: /home/runner/geemap/NormalizedDifference_qgis.py Processing 3/5: /home/runner/geemap/FromName_qgis.py Processing 4/5: /home/runner/geemap/ClippedComposite_qgis.py Processing 5/5: /home/runner/geemap/QualityMosaic_qgis.py Executing Earth Engine Jupyter notebooks ... Processing 1/5: /home/runner/geemap/ModisQaBands.ipynb ...
[NbConvertApp] Converting notebook /home/runner/geemap/ModisQaBands.ipynb to notebook [NbConvertApp] Writing 45001 bytes to /home/runner/geemap/ModisQaBands.ipynb
Processing 2/5: /home/runner/geemap/QualityMosaic.ipynb ...
[NbConvertApp] Converting notebook /home/runner/geemap/QualityMosaic.ipynb to notebook [NbConvertApp] Writing 44905 bytes to /home/runner/geemap/QualityMosaic.ipynb
Processing 3/5: /home/runner/geemap/FromName.ipynb ...
[NbConvertApp] Converting notebook /home/runner/geemap/FromName.ipynb to notebook [NbConvertApp] Writing 42364 bytes to /home/runner/geemap/FromName.ipynb
Processing 4/5: /home/runner/geemap/ClippedComposite.ipynb ...
[NbConvertApp] Converting notebook /home/runner/geemap/ClippedComposite.ipynb to notebook [NbConvertApp] Writing 43115 bytes to /home/runner/geemap/ClippedComposite.ipynb
Processing 5/5: /home/runner/geemap/NormalizedDifference.ipynb ...
[NbConvertApp] Converting notebook /home/runner/geemap/NormalizedDifference.ipynb to notebook
[NbConvertApp] Writing 44316 bytes to /home/runner/geemap/NormalizedDifference.ipynb