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

fixing typo and features update #561

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
92 changes: 37 additions & 55 deletions geemap/common.py
Expand Up @@ -204,13 +204,13 @@ def check_install(package):
__import__(package)
# print('{} is already installed.'.format(package))
except ImportError:
print("{} is not installed. Installing ...".format(package))
print(f"{package} is not installed. Installing ...")
try:
subprocess.check_call(["python", "-m", "pip", "install", package])
except Exception as e:
print("Failed to install {}".format(package))
print(f"Failed to install {package}")
print(e)
print("{} has been installed successfully.".format(package))
print(f"{package} has been installed successfully.")


def update_package():
Expand Down Expand Up @@ -290,11 +290,11 @@ def install_from_github(url):
pkg_name = os.path.basename(url)
work_dir = os.getcwd()
os.chdir(pkg_dir)
print("Installing {}...".format(pkg_name))
print(f"Installing {pkg_name}...")
cmd = "pip install ."
os.system(cmd)
os.chdir(work_dir)
print("{} has been installed successfully.".format(pkg_name))
print(f"{pkg_name} has been installed successfully.")
# print("\nPlease comment out 'install_from_github()' and restart the kernel to take effect:\nJupyter menu -> Kernel -> Restart & Clear Output")

except Exception as e:
Expand All @@ -316,11 +316,7 @@ def check_git_install():
return True
else:
url = "https://git-scm.com/downloads"
print(
"Git is not installed. Please download Git from {} and install it.".format(
url
)
)
print(f"Git is not installed. Please download Git from {url} and install it.")
webbrowser.open_new_tab(url)
return False

Expand Down Expand Up @@ -383,7 +379,7 @@ def clone_google_repo(url, out_dir=None):

if check_git_install():

cmd = 'git clone "{}" "{}"'.format(url, out_dir)
cmd = f'git clone "{url}" "{out_dir}"'
os.popen(cmd).read()


Expand Down Expand Up @@ -699,7 +695,7 @@ def download_from_url(url, out_file_name=None, out_dir=".", unzip=True, verbose=
out_file_path = os.path.join(os.path.abspath(out_dir), out_file_name)

if verbose:
print("Downloading {} ...".format(url))
print(f"Downloading {url} ...")

try:
urllib.request.urlretrieve(url, out_file_path)
Expand All @@ -712,7 +708,7 @@ def download_from_url(url, out_file_name=None, out_dir=".", unzip=True, verbose=
# if it is a zip file
if ".zip" in out_file_name:
if verbose:
print("Unzipping {} ...".format(out_file_name))
print(f"Unzipping {out_file_name} ...")
with zipfile.ZipFile(out_file_path, "r") as zip_ref:
zip_ref.extractall(out_dir)
final_path = os.path.join(
Expand All @@ -722,15 +718,15 @@ def download_from_url(url, out_file_name=None, out_dir=".", unzip=True, verbose=
# if it is a tar file
if ".tar" in out_file_name:
if verbose:
print("Unzipping {} ...".format(out_file_name))
print(f"Unzipping {out_file_name} ...")
with tarfile.open(out_file_path, "r") as tar_ref:
tar_ref.extractall(out_dir)
final_path = os.path.join(
os.path.abspath(out_dir), out_file_name.replace(".tart", "")
os.path.abspath(out_dir), out_file_name.replace(".tar", "")
)

if verbose:
print("Data downloaded to: {}".format(final_path))
print(f"Data downloaded to: {final_path}")

return

Expand All @@ -750,7 +746,7 @@ def download_from_gdrive(gfile_url, file_name, out_dir=".", unzip=True, verbose=

file_id = gfile_url.split("/")[5]
if verbose:
print("Google Drive file id: {}".format(file_id))
print(f"Google Drive file id: {file_id}")

dest_path = os.path.join(out_dir, file_name)
gdd.download_file_from_google_drive(file_id, dest_path, True, unzip)
Expand Down Expand Up @@ -1327,7 +1323,7 @@ def ee_export_vector(ee_object, filename, selectors=None, verbose=True, keep_zip
filetype=filetype, selectors=selectors, filename=name
)
if verbose:
print("Downloading data from {}\nPlease wait ...".format(url))
print(f"Downloading data from {url}\nPlease wait ...")
r = requests.get(url, stream=True)

if r.status_code != 200:
Expand All @@ -1338,7 +1334,7 @@ def ee_export_vector(ee_object, filename, selectors=None, verbose=True, keep_zip
url = new_ee_object.getDownloadURL(
filetype=filetype, selectors=selectors, filename=name
)
print("Downloading data from {}\nPlease wait ...".format(url))
print(f"Downloading data from {url}\nPlease wait ...")
r = requests.get(url, stream=True)
except Exception as e:
print(e)
Expand All @@ -1359,7 +1355,7 @@ def ee_export_vector(ee_object, filename, selectors=None, verbose=True, keep_zip
os.remove(filename)
filename = filename.replace(".zip", ".shp")
if verbose:
print("Data downloaded to {}".format(filename))
print(f"Data downloaded to {filename}")
except Exception as e:
raise ValueError(e)

Expand Down Expand Up @@ -1400,7 +1396,7 @@ def ee_export_vector_to_drive(
# remove .geo coordinate field
ee_object = ee_object.select([".*"], None, False)

print("Exporting {}...".format(description))
print(f"Exporting {description}...")
task = ee.batch.Export.table.toDrive(ee_object, description, **task_config)
task.start()

Expand Down Expand Up @@ -1467,7 +1463,7 @@ def ee_export_geojson(ee_object, filename=None, selectors=None):
url = new_ee_object.getDownloadURL(
filetype=filetype, selectors=selectors, filename=name
)
print("Downloading data from {}\nPlease wait ...".format(url))
print(f"Downloading data from {url}\nPlease wait ...")
r = requests.get(url, stream=True)
except Exception as e:
print(e)
Expand Down Expand Up @@ -1606,7 +1602,7 @@ def ee_export_image(
params["crs"] = crs

url = ee_object.getDownloadURL(params)
print("Downloading data from {}\nPlease wait ...".format(url))
print(f"Downloading data from {url}\nPlease wait ...")
r = requests.get(url, stream=True)

if r.status_code != 200:
Expand All @@ -1628,9 +1624,9 @@ def ee_export_image(
os.remove(filename_zip)

if file_per_band:
print("Data downloaded to {}".format(os.path.dirname(filename)))
print(f"Data downloaded to {os.path.dirname(filename)}")
else:
print("Data downloaded to {}".format(filename))
print(f"Data downloaded to {filename}")
except Exception as e:
print(e)

Expand Down Expand Up @@ -1659,13 +1655,13 @@ def ee_export_image_collection(
try:

count = int(ee_object.size().getInfo())
print("Total number of images: {}\n".format(count))
print(f"Total number of images: {count}\n")

for i in range(0, count):
image = ee.Image(ee_object.toList(count).get(i))
name = image.get("system:index").getInfo() + ".tif"
filename = os.path.join(os.path.abspath(out_dir), name)
print("Exporting {}/{}: {}".format(i + 1, count, name))
print(f"Exporting {i + 1}/{count}: {name}")
ee_export_image(
image,
filename=filename,
Expand Down Expand Up @@ -1729,7 +1725,7 @@ def ee_export_image_to_drive(
task = ee.batch.Export.image(ee_object, description, params)
task.start()

print("Exporting {} ...".format(description))
print(f"Exporting {description} ...")

except Exception as e:
print(e)
Expand Down Expand Up @@ -1767,7 +1763,7 @@ def ee_export_image_collection_to_drive(

try:
count = int(ee_object.size().getInfo())
print("Total number of images: {}\n".format(count))
print(f"Total number of images: {count}\n")

if (descriptions is not None) and (len(descriptions) != count):
print("The number of descriptions is not equal to the number of images.")
Expand Down Expand Up @@ -1877,7 +1873,7 @@ def get_image_collection_thumbnails(
try:
count = int(ee_object.size().getInfo())
if verbose:
print("Total number of images: {}\n".format(count))
print(f"Total number of images: {count}\n")

if (names is not None) and (len(names) != count):
print("The number of names is not equal to the number of images.")
Expand Down Expand Up @@ -2149,7 +2145,7 @@ def download_ee_video(collection, video_args, out_gif):
print("Generating URL...")
url = collection.getVideoThumbURL(video_args)

print("Downloading GIF image from {}\nPlease wait ...".format(url))
print(f"Downloading GIF image from {url}\nPlease wait ...")
r = requests.get(url, stream=True)

if r.status_code != 200:
Expand All @@ -2159,7 +2155,7 @@ def download_ee_video(collection, video_args, out_gif):
with open(out_gif, "wb") as fd:
for chunk in r.iter_content(chunk_size=1024):
fd.write(chunk)
print("The GIF image has been saved to: {}".format(out_gif))
print(f"The GIF image has been saved to: {out_gif}")
except Exception as e:
print(e)

Expand Down Expand Up @@ -3022,9 +3018,7 @@ def add_text_to_gif(
pass
else:
print(
"xy is out of bounds. x must be within [0, {}], and y must be within [0, {}]".format(
W, H
)
f"xy is out of bounds. x must be within [0, {W}], and y must be within [0, {H}]"
)
return
elif all(isinstance(item, str) for item in xy) and (len(xy) == 2):
Expand Down Expand Up @@ -3056,9 +3050,7 @@ def add_text_to_gif(
text = [text_sequence] * count
elif isinstance(text_sequence, list) and len(text_sequence) != count:
print(
"The length of the text sequence must be equal to the number ({}) of frames in the gif.".format(
count
)
f"The length of the text sequence must be equal to the number ({count}) of frames in the gif."
)
return
else:
Expand Down Expand Up @@ -3748,7 +3740,7 @@ def search_ee_data(keywords):
list: Returns a lit of assets.
"""
try:
cmd = 'geeadd search --keywords "{}"'.format(str(keywords))
cmd = f'geeadd search --keywords "{str(keywords)}"'
output = os.popen(cmd).read()
start_index = output.index("[")
assets = eval(output[start_index:])
Expand Down Expand Up @@ -4488,7 +4480,7 @@ def build_repo_tree(out_dir=None, name="gee_repos"):
os.makedirs(repo_dir)

URLs = {
# 'Owner': 'https://earthengine.googlesource.com/{}/default'.format(ee_user_id()),
# 'Owner': 'https://earthengine.googlesource.com/{ee_user_id()}/default',
"Writer": "",
"Reader": "https://github.com/giswqs/geemap",
"Examples": "https://github.com/giswqs/earthengine-py-examples",
Expand All @@ -4497,9 +4489,7 @@ def build_repo_tree(out_dir=None, name="gee_repos"):

user_id = ee_user_id()
if user_id is not None:
URLs["Owner"] = "https://earthengine.googlesource.com/{}/default".format(
ee_user_id()
)
URLs["Owner"] = f"https://earthengine.googlesource.com/{ee_user_id()}/default"

path_widget = widgets.Text(placeholder="Enter the link to a Git repository here...")
path_widget.layout.width = "475px"
Expand Down Expand Up @@ -4958,16 +4948,12 @@ def load_GeoTIFF(URL):

if not uri.startswith("gs://"):
raise Exception(
'Invalid GCS URL: {}. Expected something of the form "gs://bucket/path/to/object.tif".'.format(
uri
)
f'Invalid GCS URL: {uri}. Expected something of the form "gs://bucket/path/to/object.tif".'
)

if not uri.lower().endswith(".tif"):
raise Exception(
'Invalid GCS URL: {}. Expected something of the form "gs://bucket/path/to/object.tif".'.format(
uri
)
f'Invalid GCS URL: {uri}. Expected something of the form "gs://bucket/path/to/object.tif".'
)

cloud_image = ee.Image.loadGeoTIFF(uri)
Expand Down Expand Up @@ -5001,16 +4987,12 @@ def load_GeoTIFFs(URLs):

if not uri.startswith("gs://"):
raise Exception(
'Invalid GCS URL: {}. Expected something of the form "gs://bucket/path/to/object.tif".'.format(
uri
)
f'Invalid GCS URL: {uri}. Expected something of the form "gs://bucket/path/to/object.tif".'
)

if not uri.lower().endswith(".tif"):
raise Exception(
'Invalid GCS URL: {}. Expected something of the form "gs://bucket/path/to/object.tif".'.format(
uri
)
f'Invalid GCS URL: {uri}. Expected something of the form "gs://bucket/path/to/object.tif".'
)

URIs.append(uri)
Expand Down
16 changes: 8 additions & 8 deletions geemap/conversion.py
Expand Up @@ -575,7 +575,7 @@ def js_to_python_dir(in_dir, out_dir=None, use_qgis=True, github_repo=None):
files = list(Path(in_dir).rglob("*.js"))

for index, in_file in enumerate(files):
print("Processing {}/{}: {}".format(index + 1, len(files), in_file))
print(f"Processing {index + 1}/{len(files)}: {in_file}")
out_file = os.path.splitext(in_file)[0] + "_qgis.py"
out_file = out_file.replace(in_dir, out_dir)
js_to_python(in_file, out_file, use_qgis, github_repo)
Expand Down Expand Up @@ -676,7 +676,7 @@ def get_nb_template(download_latest=False, out_file=None):

if download_latest:
template_url = "https://raw.githubusercontent.com/giswqs/geemap/master/examples/template/template.py"
print("Downloading the latest notebook template from {}".format(template_url))
print(f"Downloading the latest notebook template from {template_url}")
urllib.request.urlretrieve(template_url, out_file)
elif out_file is not None:
shutil.copyfile(template_file, out_file)
Expand Down Expand Up @@ -845,7 +845,7 @@ def py_to_ipynb_dir(
.replace("_qgis", "")
.replace(".py", ".ipynb")
)
print("Processing {}/{}: {}".format(index + 1, len(files), in_file))
print(f"Processing {index + 1}/{len(files)}: {in_file}")
py_to_ipynb(in_file, template_file, out_file, github_username, github_repo)


Expand Down Expand Up @@ -875,7 +875,7 @@ def execute_notebook_dir(in_dir):
if files is not None:
for index, file in enumerate(files):
in_file = str(file)
print("Processing {}/{}: {} ...".format(index + 1, count, file))
print(f"Processing {index + 1}/{count}: {file} ...")
execute_notebook(in_file)


Expand Down Expand Up @@ -950,7 +950,7 @@ def update_nb_header_dir(in_dir, github_username=None, github_repo=None):
if files is not None:
for index, file in enumerate(files):
in_file = str(file)
print("Processing {}/{}: {} ...".format(index + 1, count, file))
print(f"Processing {index + 1}/{count}: {file} ...")
update_nb_header(in_file, github_username, github_repo)


Expand Down Expand Up @@ -1013,7 +1013,7 @@ def download_gee_app(url, out_file=None):
items[3] = "javascript"
items[4] = items[4] + "-modules.json"
json_url = "/".join(items)
print("The json url: {}".format(json_url))
print(f"The json url: {json_url}")

if out_file is not None:
out_file_path = out_file
Expand Down Expand Up @@ -1045,7 +1045,7 @@ def download_gee_app(url, out_file=None):
item = item.replace("\\r", "")
f1.write(item + "\n")
os.remove(json_path)
print("The JavaScript is saved at: {}".format(out_file_path))
print(f"The JavaScript is saved at: {out_file_path}")


# # Download file shared via Google Drive
Expand Down Expand Up @@ -1084,7 +1084,7 @@ def download_gee_app(url, out_file=None):

# 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))
print(f"Python scripts saved at: {js_dir}")

# Convert all Earth Engine Python scripts in a folder recursively to Jupyter notebooks.
# Get the notebook template from the package folder.
Expand Down