Skip to content

Commit

Permalink
Merge pull request #91 from gee-community/fix_init_ee_auth
Browse files Browse the repository at this point in the history
A new way to initializing and authentication to EE lib
  • Loading branch information
XavierCLL authored Feb 20, 2021
2 parents 631807c + ad1a4fa commit dfb7cba
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 27 deletions.
9 changes: 2 additions & 7 deletions Map.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@
from qgis.core import QgsCoordinateReferenceSystem, QgsCoordinateTransform, QgsProject, QgsPointXY, QgsRectangle
from qgis.utils import iface

import ee_plugin.utils
import ee_plugin.ee_auth

# init the Google Earth Engine user authorization system
ee_plugin.ee_auth.init()


def addLayer(eeObject, visParams=None, name=None, shown=True, opacity=1.0):
"""
Expand All @@ -25,8 +19,9 @@ def addLayer(eeObject, visParams=None, name=None, shown=True, opacity=1.0):
>>> from ee_plugin import Map
>>> Map.addLayer(.....)
"""
from ee_plugin.utils import add_or_update_ee_layer

ee_plugin.utils.add_or_update_ee_layer(eeObject, visParams, name, shown, opacity)
add_or_update_ee_layer(eeObject, visParams, name, shown, opacity)


def centerObject(feature, zoom=None):
Expand Down
29 changes: 29 additions & 0 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import platform
import site
import pkg_resources
import builtins


def pre_init_plugin():
Expand All @@ -20,6 +21,31 @@ def pre_init_plugin():
pkg_resources.working_set.add_entry(extra_libs_path)


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.
"""
from ee_plugin.ee_auth import authenticate

def __wrapping_ee_import__(name, *args, **kwargs):
_module_ = __builtin_import__(name, *args, **kwargs)
if name == 'ee':
if not _module_.data._credentials:
try:
_module_.Initialize()
except _module_.ee_exception.EEException:
if authenticate(ee=_module_):
_module_.Initialize() # retry initialization once the user logs in
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 @@ -31,6 +57,9 @@ def classFactory(iface): # pylint: disable=invalid-name
# load extra python dependencies
pre_init_plugin()

# wrap the ee library import
import_ee()

# start
from .ee_plugin import GoogleEarthEnginePlugin
return GoogleEarthEnginePlugin(iface)
16 changes: 4 additions & 12 deletions ee_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,13 @@
import webbrowser
from qgis.PyQt.QtWidgets import QInputDialog

import ee
import logging

# fix the warnings/errors messages from 'file_cache is unavailable when using oauth2client'
# https://github.com/googleapis/google-api-python-client/issues/299
logging.getLogger('googleapiclient.discovery_cache').setLevel(logging.ERROR)


def init():
try:
ee.Initialize()
except ee.ee_exception.EEException:
if authenticate():
ee.Initialize() # retry initialization once the user logs in
else:
print('\nGoogle Earth Engine authorization failed!\n')


def tiny_url(url):
try:
apiurl = "http://tinyurl.com/api-create.php?url="
Expand All @@ -35,7 +24,10 @@ def tiny_url(url):
return False, url


def authenticate():
def authenticate(ee=None):
if ee is None:
import ee

# PKCE. Generates a challenge that the server will use to ensure that the
# auth_code only works with our verifier. https://tools.ietf.org/html/rfc7636
code_verifier = ee.oauth._base64param(os.urandom(32))
Expand Down
8 changes: 4 additions & 4 deletions ee_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import configparser
import requests

from ee_plugin.icons import resources
import webbrowser
from builtins import object
import os.path
Expand All @@ -18,9 +17,8 @@
from qgis.PyQt.QtGui import QIcon
from qgis.core import QgsProject

import ee
from ee_plugin import utils
from ee_plugin import provider
from ee_plugin.icons import resources

# read the plugin version from metadata
cfg = configparser.ConfigParser()
Expand Down Expand Up @@ -124,6 +122,8 @@ def unload(self):
self.menu_name_plugin, self.dockable_action)

def updateLayers(self):
import ee
from ee_plugin.utils import add_or_update_ee_layer
layers = QgsProject.instance().mapLayers().values()

for l in filter(lambda layer: layer.customProperty('ee-layer'), layers):
Expand All @@ -141,4 +141,4 @@ def updateLayers(self):
shown = QgsProject.instance().layerTreeRoot().findLayer(l.id()).itemVisibilityChecked()
opacity = l.renderer().opacity()

utils.add_or_update_ee_layer(ee_object, ee_object_vis, name, shown, opacity)
add_or_update_ee_layer(ee_object, ee_object_vis, name, shown, opacity)
6 changes: 2 additions & 4 deletions provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@
QgsVectorDataProvider, QgsDataProvider
)

import ee

from ee_plugin import utils

BAND_TYPES = {
'int8': Qgis.Int16,
'int16': Qgis.Int16,
Expand Down Expand Up @@ -109,7 +105,9 @@ def sourceDataType(self, band_no):

def identify(self, point, format, boundingBox=None, width=None, height=None, dpi=None):
# TODO: speed-up, extend this to maintain cache of visible image, update cache on-the-fly when needed
import ee
from ee_plugin import Map
from ee_plugin import utils

point = utils.geom_to_geo(point)
point_ee = ee.Geometry.Point([point.x(), point.y()])
Expand Down

0 comments on commit dfb7cba

Please sign in to comment.