"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Automatic conversion from Earth Engine JavaScripts to Python scripts\n",
"\n",
"### Install Earth Engine API and geemap\n",
"Install the [Earth Engine Python API](https://developers.google.com/earth-engine/python_install) and [geemap](https://github.com/giswqs/geemap). The **geemap** Python package is built upon the [ipyleaflet](https://github.com/jupyter-widgets/ipyleaflet) and [folium](https://github.com/python-visualization/folium) packages and implements several methods for interacting with Earth Engine data layers, such as `Map.addLayer()`, `Map.setCenter()`, and `Map.centerObject()`.\n",
"The following script checks if the geemap package has been installed. If not, it will install geemap, which automatically installs its [dependencies](https://github.com/giswqs/geemap#dependencies), including earthengine-api, folium, and ipyleaflet.\n",
"\n",
"**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](https://blog.jupyter.org/interactive-gis-in-jupyter-with-ipyleaflet-52f9657fa7a)). Note that [Google Colab](https://colab.research.google.com/) currently does not support ipyleaflet ([source](https://github.com/googlecolab/colabtools/issues/60#issuecomment-596225619)). Therefore, if you are using geemap with Google Colab, you should use [`import geemap.eefolium`](https://github.com/giswqs/geemap/blob/master/geemap/eefolium.py). If you are using geemap with [binder](https://mybinder.org/) or a local Jupyter notebook server, you can use [`import geemap`](https://github.com/giswqs/geemap/blob/master/geemap/geemap.py), which provides more functionalities for capturing user input (e.g., mouse-clicking and moving)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import subprocess\n",
"\n",
"try:\n",
" import geemap\n",
"except ImportError:\n",
" print('geemap package not installed. Installing ...')\n",
" subprocess.check_call([\"python\", '-m', 'pip', 'install', 'geemap'])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"geemap.show_youtube('RpIaalFk4H8')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Convert Earth Eninge JavaScripts to Python scripts"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"from geemap.conversion import *\n",
"\n",
"# Create a temporary working directory\n",
"work_dir = os.path.join(os.path.expanduser('~'), 'geemap')\n",
"# Get Earth Engine JavaScript examples. There are five examples in the geemap package folder. \n",
"# Change js_dir to your own folder containing your Earth Engine JavaScripts, \n",
"# such as js_dir = '/path/to/your/js/folder'\n",
"js_dir = get_js_examples(out_dir=work_dir) \n",
"\n",
"# Convert all Earth Engine JavaScripts in a folder recursively to Python scripts.\n",
"js_to_python_dir(in_dir=js_dir, out_dir=js_dir, use_qgis=True)\n",
"print(\"Python scripts saved at: {}\".format(js_dir))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Convert Earth Engine Python scripts to Jupyter Notebooks"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Convert all Earth Engine Python scripts in a folder recursively to Jupyter notebooks.\n",
"nb_template = get_nb_template() # Get the notebook template from the package folder.\n",
"py_to_ipynb_dir(js_dir, nb_template)\n",
"\n",
"# Execute all Jupyter notebooks in a folder recursively and save the output cells.\n",
"execute_notebook_dir(in_dir=js_dir)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.2"
}
},
"nbformat": 4,
"nbformat_minor": 4
}