__init__.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # -*- coding: utf-8 -*-
  2. import os
  3. import platform
  4. import site
  5. import pkg_resources
  6. import builtins
  7. def pre_init_plugin():
  8. if platform.system() == "Windows":
  9. extlib_path = "extlibs_windows"
  10. if platform.system() == "Darwin":
  11. extlib_path = "extlibs_darwin"
  12. if platform.system() == "Linux":
  13. extlib_path = "extlibs_linux"
  14. extra_libs_path = os.path.abspath(
  15. os.path.join(os.path.dirname(__file__), extlib_path)
  16. )
  17. # add to python path
  18. site.addsitedir(extra_libs_path)
  19. # pkg_resources doesn't listen to changes on sys.path.
  20. pkg_resources.working_set.add_entry(extra_libs_path)
  21. def import_ee():
  22. """This is a wrapper of the Google Earth engine library for the
  23. purpose of initializing or starting ee authentication when the
  24. user or the plugin import ee library.
  25. """
  26. # we can now import the libraries
  27. # Work around bug https://github.com/google/earthengine-api/issues/181
  28. import httplib2
  29. from ee_plugin.ee_auth import authenticate
  30. def __wrapping_ee_import__(name, *args, **kwargs):
  31. _module_ = __builtin_import__(name, *args, **kwargs)
  32. if name == "ee":
  33. if not _module_.data._credentials:
  34. try:
  35. _module_.Initialize(http_transport=httplib2.Http())
  36. except _module_.ee_exception.EEException:
  37. if authenticate(ee=_module_):
  38. # retry initialization once the user logs in
  39. _module_.Initialize(http_transport=httplib2.Http())
  40. else:
  41. print("\nGoogle Earth Engine authorization failed!\n")
  42. return _module_
  43. __builtin_import__ = builtins.__import__
  44. builtins.__import__ = __wrapping_ee_import__
  45. # noinspection PyPep8Naming
  46. def classFactory(iface): # pylint: disable=invalid-name
  47. """Instantiates Google Earth Engine Plugin.
  48. :param iface: A QGIS interface instance.
  49. :type iface: QgsInterface
  50. """
  51. # load extra python dependencies
  52. pre_init_plugin()
  53. # wrap the ee library import
  54. import_ee()
  55. # start
  56. from .ee_plugin import GoogleEarthEnginePlugin
  57. return GoogleEarthEnginePlugin(iface)