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

Refactoring the method to import EE lib and auth process, fixing bugs for the new GEE python API #142

Merged
merged 8 commits into from
Sep 30, 2024
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,29 @@ Check [User Guide](https://gee-community.github.io/qgis-earthengine-plugin/) to

![Add Sentinel-2 image](https://raw.githubusercontent.com/gee-community/qgis-earthengine-plugin/master/media/add_map_layer.png)

### Run first

Run the Google Earth Engine authentication flow in the QGIS Python console:

```python
import ee
ee.Authenticate()
```

### How to use with a simple example

```python
import ee
from ee_plugin import Map
ee.Initialize()

# Add Earth Engine dataset
image = ee.Image('USGS/SRTMGL1_003')
vis_params = {'min': 0, 'max': 4000, 'palette': ['006633', 'E5FFCC', '662A00', 'D8D8D8', 'F5F5F5']}
Map.addLayer(image, vis_params, 'DEM')
Map.setCenter(-121.753, 46.855, 9)
```

### Troubleshooting

#### How to reset your authentication settings?
Expand Down
36 changes: 3 additions & 33 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import platform
import site
import pkg_resources
import builtins


def pre_init_plugin():
Expand All @@ -23,36 +22,6 @@ def pre_init_plugin():
pkg_resources.working_set.add_entry(extra_libs_path)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add ee.Initialize() here, to initialize EE libraries after plugin is initialized, or maybe in the classFactory(), also calling ee_auth() there?

This won't force users to change their scripts and in the meantime a Cloud Project selection can be implemented before the hard deadline.



def import_ee():
"""This is a wrapper of the Google Earth engine library for the
purpose of initializing or starting ee authentication when the
user or the plugin import ee library.
"""
# we can now import the libraries
# Work around bug https://github.com/google/earthengine-api/issues/181
import httplib2

from ee_plugin.ee_auth import authenticate
XavierCLL marked this conversation as resolved.
Show resolved Hide resolved

def __wrapping_ee_import__(name, *args, **kwargs):
_module_ = __builtin_import__(name, *args, **kwargs)
if name == "ee":
if not _module_.data._credentials:
try:
_module_.Initialize(http_transport=httplib2.Http())
except _module_.ee_exception.EEException:
if authenticate(ee=_module_):
# retry initialization once the user logs in
_module_.Initialize(http_transport=httplib2.Http())
else:
print("\nGoogle Earth Engine authorization failed!\n")

return _module_

__builtin_import__ = builtins.__import__
builtins.__import__ = __wrapping_ee_import__


# noinspection PyPep8Naming
def classFactory(iface): # pylint: disable=invalid-name
"""Instantiates Google Earth Engine Plugin.
Expand All @@ -64,8 +33,9 @@ def classFactory(iface): # pylint: disable=invalid-name
# load extra python dependencies
pre_init_plugin()

# wrap the ee library import
import_ee()
# Initialize the Earth Engine Python API
import ee
ee.Initialize()

# start
from .ee_plugin import GoogleEarthEnginePlugin
Expand Down
95 changes: 0 additions & 95 deletions ee_auth.py

This file was deleted.

5 changes: 5 additions & 0 deletions ee_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ def updateLayers(self):
print('\nWARNING:\n Map layer saved with older version of EE plugin is detected, backward-compatibility for versions before 0.0.3 is not supported due to changes in EE library, please re-create EE layer by re-running the Python script\n')
return

# needed to load EE objects saved in QGIS projects
# TODO: it does not work for init EE in a Google Cloud Project, see #150
if not ee.data.is_initialized():
ee.Initialize()

ee_object = ee.deserializer.fromJSON(ee_object)

if ee_object_vis is not None:
Expand Down
Loading