Skip to content

Commit

Permalink
Merge branch 'master' into anywidget
Browse files Browse the repository at this point in the history
  • Loading branch information
naschmitz authored Nov 6, 2024
2 parents 921edea + f0c1950 commit eafaa4c
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 68 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ repos:
]

- repo: https://github.com/kynan/nbstripout
rev: 0.7.1
rev: 0.8.0
hooks:
- id: nbstripout
2 changes: 1 addition & 1 deletion geemap/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

__author__ = """Qiusheng Wu"""
__email__ = "[email protected]"
__version__ = "0.35.0"
__version__ = "0.35.1"

import os

Expand Down
7 changes: 6 additions & 1 deletion geemap/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -12601,6 +12601,7 @@ def download_ee_image_tiles_parallel(
column=None,
job_args={"n_jobs": -1},
ee_init=True,
project_id=None,
**kwargs,
):
"""Download an Earth Engine Image as small tiles based on ee.FeatureCollection. Images larger than the `Earth Engine size limit are split and downloaded as
Expand Down Expand Up @@ -12637,6 +12638,7 @@ def download_ee_image_tiles_parallel(
column (str, optional): The column name in the feature collection to use as the filename. Defaults to None.
job_args (dict, optional): The arguments to pass to joblib.Parallel. Defaults to {"n_jobs": -1}.
ee_init (bool, optional): Whether to initialize Earth Engine. Defaults to True.
project_id (str, optional): The Earth Engine project ID. Defaults to None.

"""
import joblib
Expand Down Expand Up @@ -12668,7 +12670,10 @@ def download_ee_image_tiles_parallel(

def download_data(index):
if ee_init:
ee_initialize(opt_url="https://earthengine-highvolume.googleapis.com")
ee_initialize(
opt_url="https://earthengine-highvolume.googleapis.com",
project=project_id,
)
region = ee.Feature(collection.get(index)).geometry()
filename = os.path.join(
out_dir, "{}{}.tif".format(prefix, names[index].replace("/", "_"))
Expand Down
87 changes: 24 additions & 63 deletions geemap/coreutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ def get_env_var(key: str) -> Optional[str]:
def ee_initialize(
token_name: str = "EARTHENGINE_TOKEN",
auth_mode: Optional[str] = None,
service_account: bool = False,
auth_args: Optional[Dict[str, Any]] = None,
user_agent_prefix: str = "geemap",
project: Optional[str] = None,
Expand All @@ -58,8 +57,6 @@ def ee_initialize(
notebook, localhost, or gcloud.
See https://developers.google.com/earth-engine/guides/auth for more
details. Defaults to None.
service_account (bool, optional): If True, use a service account.
Defaults to False.
auth_args (dict, optional): Additional authentication parameters for
aa.Authenticate(). Defaults to {}.
user_agent_prefix (str, optional): If set, the prefix (version-less)
Expand All @@ -70,17 +67,33 @@ def ee_initialize(
For example, opt_url='https://earthengine-highvolume.googleapis.com'
to use the Earth Engine High-Volume platform. Defaults to {}.
"""
import httplib2
import google.oauth2.credentials
from .__init__ import __version__

if auth_args is None:
auth_args = {}

user_agent = f"{user_agent_prefix}/{__version__}"
ee.data.setUserAgent(user_agent)

if "http_transport" not in kwargs:
kwargs["http_transport"] = httplib2.Http()
if ee.data._credentials is not None:
return

ee_token = get_env_var(token_name)
if ee_token is not None:

stored = json.loads(ee_token)
credentials = google.oauth2.credentials.Credentials(
None,
token_uri="https://oauth2.googleapis.com/token",
client_id=stored["client_id"],
client_secret=stored["client_secret"],
refresh_token=stored["refresh_token"],
quota_project_id=stored["project"],
)

ee.Initialize(credentials=credentials, **kwargs)
return

if auth_args is None:
auth_args = {}

if project is None:
kwargs["project"] = get_env_var("EE_PROJECT_ID")
Expand All @@ -97,60 +110,8 @@ def ee_initialize(

auth_args["auth_mode"] = auth_mode

if ee.data._credentials is None:
ee_token = get_env_var(token_name)
if service_account:
try:
credential_file_path = os.path.expanduser(
"~/.config/earthengine/private-key.json"
)

if os.path.exists(credential_file_path):
with open(credential_file_path) as f:
token_dict = json.load(f)
else:
token_name = "EARTHENGINE_TOKEN"
ee_token = os.environ.get(token_name)
token_dict = json.loads(ee_token, strict=False)
service_account = token_dict["client_email"]
private_key = token_dict["private_key"]

credentials = ee.ServiceAccountCredentials(
service_account, key_data=private_key
)
ee.Initialize(credentials, **kwargs)

except Exception as e:
raise Exception(e)

else:
try:
if ee_token is not None:
credential_file_path = os.path.expanduser(
"~/.config/earthengine/credentials"
)
if not os.path.exists(credential_file_path):
os.makedirs(
os.path.dirname(credential_file_path), exist_ok=True
)
if ee_token.startswith("{") and ee_token.endswith(
"}"
): # deals with token generated by new auth method (earthengine-api>=0.1.304).
token_dict = json.loads(ee_token)
with open(credential_file_path, "w") as f:
f.write(json.dumps(token_dict))
else:
credential = (
'{"refresh_token":"%s"}' % ee_token
) # deals with token generated by old auth method.
with open(credential_file_path, "w") as f:
f.write(credential)

ee.Initialize(**kwargs)

except Exception:
ee.Authenticate(**auth_args)
ee.Initialize(**kwargs)
ee.Authenticate(**auth_args)
ee.Initialize(**kwargs)


def get_info(
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"

[project]
name = "geemap"
version = "0.35.0"
version = "0.35.1"
description = "A Python package for interactive mapping using Google Earth Engine and ipyleaflet"
readme = "README.md"
requires-python = ">=3.9"
Expand Down Expand Up @@ -150,7 +150,7 @@ exclude = ["docs*"]
universal = true

[tool.bumpversion]
current_version = "0.35.0"
current_version = "0.35.1"
commit = true
tag = true

Expand Down

0 comments on commit eafaa4c

Please sign in to comment.