Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve js-py auto conversion #1836

Merged
merged 1 commit into from
Nov 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 30 additions & 9 deletions geemap/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ def js_to_python(
github_repo=None,
show_map=True,
import_geemap=False,
Map="m",
):
"""Converts an Earth Engine JavaScript to Python script.

Expand All @@ -289,6 +290,7 @@ def js_to_python(
github_repo (str, optional): GitHub repo url. Defaults to None.
show_map (bool, optional): Whether to add "Map" to the output script. Defaults to True.
import_geemap (bool, optional): Whether to add "import geemap" to the output script. Defaults to False.
Map (str, optional): The name of the map variable. Defaults to "m".

Returns:
list: Python script
Expand All @@ -315,7 +317,7 @@ def js_to_python(
if use_qgis:
import_str = "from ee_plugin import Map\n"
if import_geemap:
import_str = "import geemap\n\nMap = geemap.Map()\n"
import_str = f"import geemap\n\n{Map} = geemap.Map()\n"

github_url = ""
if github_repo is not None:
Expand Down Expand Up @@ -449,6 +451,7 @@ def js_to_python(
line = line.replace("Math.PI", "math.pi")
line = line.replace("Math.", "math.")
line = line.replace("= new", "=")
line = line.replace("Map.", f"{Map}.")
line = line.rstrip()

if ".style(" in line and ".style(**" not in line:
Expand Down Expand Up @@ -515,6 +518,7 @@ def js_snippet_to_py(
import_ee=True,
import_geemap=False,
show_map=True,
Map="m",
):
"""Converts an Earth Engine JavaScript snippet wrapped in triple quotes to Python directly on a Jupyter notebook.

Expand All @@ -524,6 +528,7 @@ def js_snippet_to_py(
import_ee (bool, optional): Whether to import ee. Defaults to True.
import_geemap (bool, optional): Whether to import geemap. Defaults to False.
show_map (bool, optional): Whether to show the map. Defaults to True.
Map (str, optional): The name of the map variable. Defaults to "m".

Returns:
list: A list of Python script.
Expand All @@ -541,14 +546,15 @@ def js_snippet_to_py(
use_qgis=False,
show_map=show_map,
import_geemap=import_geemap,
Map=Map,
)

out_lines = []
if import_ee:
out_lines.append("import ee\n")
if import_geemap:
out_lines.append("import geemap\n\n")
out_lines.append("Map = geemap.Map()\n")
out_lines.append(f"{Map} = geemap.Map()\n")

with open(out_py, encoding="utf-8") as f:
lines = f.readlines()
Expand Down Expand Up @@ -582,7 +588,7 @@ def js_snippet_to_py(


def js_to_python_dir(
in_dir, out_dir=None, use_qgis=True, github_repo=None, import_geemap=False
in_dir, out_dir=None, use_qgis=True, github_repo=None, import_geemap=False, Map="m"
):
"""Converts all Earth Engine JavaScripts in a folder recursively to Python scripts.

Expand All @@ -592,6 +598,7 @@ def js_to_python_dir(
use_qgis (bool, optional): Whether to add "from ee_plugin import Map \n" to the output script. Defaults to True.
github_repo (str, optional): GitHub repo url. Defaults to None.
import_geemap (bool, optional): Whether to add "import geemap" to the output script. Defaults to False.
Map (str, optional): The name of the map variable. Defaults to "m".
"""
print("Converting Earth Engine JavaScripts to Python scripts...\n")
in_dir = os.path.abspath(in_dir)
Expand All @@ -613,7 +620,12 @@ def js_to_python_dir(
out_file = os.path.splitext(in_file)[0] + "_geemap.py"
out_file = out_file.replace(in_dir, out_dir)
js_to_python(
in_file, out_file, use_qgis, github_repo, import_geemap=import_geemap
in_file,
out_file,
use_qgis,
github_repo,
import_geemap=import_geemap,
Map=Map,
)
# print("Output Python script folder: {}".format(out_dir))

Expand All @@ -631,11 +643,12 @@ def js_to_python_dir(
# return line


def remove_qgis_import(in_file):
def remove_qgis_import(in_file, Map="m"):
"""Removes 'from ee_plugin import Map' from an Earth Engine Python script.

Args:
in_file (str): Input file path of the Python script.
Map (str, optional): The name of the map variable. Defaults to "m".

Returns:
list: List of lines 'from ee_plugin import Map' removed.
Expand All @@ -655,7 +668,7 @@ def remove_qgis_import(in_file):
return lines[start_index + i :]
else:
i = i + 1
elif "Map = geemap.Map()" in line:
elif f"{Map} = geemap.Map()" in line:
return lines[index + 1 :]


Expand Down Expand Up @@ -776,6 +789,7 @@ def py_to_ipynb(
out_file=None,
github_username=None,
github_repo=None,
Map="m",
):
"""Converts Earth Engine Python script to Jupyter notebook.

Expand All @@ -785,6 +799,7 @@ def py_to_ipynb(
out_file (str, optional)): Output Jupyter notebook.
github_username (str, optional): GitHub username. Defaults to None.
github_repo (str, optional): GitHub repo name. Defaults to None.
Map (str, optional): The name of the map variable. Defaults to "m".
"""
in_file = os.path.abspath(in_file)

Expand All @@ -802,7 +817,7 @@ def py_to_ipynb(
if out_dir == os.path.dirname(in_file):
out_py_file = os.path.splitext(out_file)[0] + "_tmp.py"

content = remove_qgis_import(in_file)
content = remove_qgis_import(in_file, Map=Map)
if content[-1].strip() == "Map":
content = content[:-1]
header = template_header(template_file)
Expand Down Expand Up @@ -855,7 +870,12 @@ def py_to_ipynb(


def py_to_ipynb_dir(
in_dir, template_file=None, out_dir=None, github_username=None, github_repo=None
in_dir,
template_file=None,
out_dir=None,
github_username=None,
github_repo=None,
Map="m",
):
"""Converts Earth Engine Python scripts in a folder recursively to Jupyter notebooks.

Expand All @@ -865,6 +885,7 @@ def py_to_ipynb_dir(
template_file (str): Input jupyter notebook template file.
github_username (str, optional): GitHub username. Defaults to None.
github_repo (str, optional): GitHub repo name. Defaults to None.
Map (str, optional): The name of the map variable. Defaults to "m".
"""
print("Converting Earth Engine Python scripts to Jupyter notebooks ...\n")

Expand Down Expand Up @@ -894,7 +915,7 @@ def py_to_ipynb_dir(
.replace(".py", ".ipynb")
)
print(f"Processing {index + 1}/{len(files)}: {in_file}")
py_to_ipynb(in_file, template_file, out_file, github_username, github_repo)
py_to_ipynb(in_file, template_file, out_file, github_username, github_repo, Map)


def execute_notebook(in_file):
Expand Down