Coverage for enpt_enmapboxapp/enpt_enmapboxapp.py: 70%

77 statements  

« prev     ^ index     » next       coverage.py v7.4.1, created at 2024-03-05 19:40 +0000

1# -*- coding: utf-8 -*- 

2 

3# enpt_enmapboxapp, A QGIS EnMAPBox plugin providing a GUI for the EnMAP processing tools (EnPT) 

4# 

5# Copyright (C) 2018-2024 Daniel Scheffler (GFZ Potsdam, daniel.scheffler@gfz-potsdam.de) 

6# 

7# This software was developed within the context of the EnMAP project supported 

8# by the DLR Space Administration with funds of the German Federal Ministry of 

9# Economic Affairs and Energy (on the basis of a decision by the German Bundestag: 

10# 50 EE 1529) and contributions from DLR, GFZ and OHB System AG. 

11# 

12# This program is free software: you can redistribute it and/or modify it under 

13# the terms of the GNU Lesser General Public License as published by the Free 

14# Software Foundation, either version 3 of the License, or (at your option) any 

15# later version. 

16# 

17# This program is distributed in the hope that it will be useful, but WITHOUT 

18# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 

19# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 

20# details. 

21# 

22# You should have received a copy of the GNU Lesser General Public License along 

23# with this program. If not, see <https://www.gnu.org/licenses/>. 

24 

25"""This module provides a QGIS EnMAPBox GUI for the EnMAP processing tools (EnPT).""" 

26import os 

27import traceback 

28import sys 

29from importlib.util import find_spec 

30from importlib.metadata import version as get_version 

31 

32from qgis.PyQt.QtGui import QIcon 

33from qgis.PyQt.QtWidgets import QMenu, QAction, QMessageBox 

34from enmapbox.gui.applications import EnMAPBoxApplication as _EnMAPBoxApplication 

35from qgis.core import QgsApplication, Qgis 

36 

37from .version import __version__ 

38from .enpt_algorithm import EnPTAlgorithm 

39from .enpt_external_algorithm import ExternalEnPTAlgorithm 

40 

41VERSION = __version__ 

42LICENSE = 'GNU GPL-3' 

43APP_DIR = os.path.dirname(__file__) 

44APP_NAME = 'EnPT EnMAPBox App' 

45 

46 

47class EnPTEnMAPBoxApp(_EnMAPBoxApplication): 

48 """The EnPT GUI class.""" 

49 

50 def __init__(self, enmapBox, parent=None): 

51 super(EnPTEnMAPBoxApp, self).__init__(enmapBox, parent=parent) 

52 

53 self.name = APP_NAME 

54 self.version = VERSION 

55 self.licence = LICENSE 

56 self.enpt_is_internal = self.is_enpt_internally_installed() 

57 self.ALG = EnPTAlgorithm if self.enpt_is_internal else ExternalEnPTAlgorithm 

58 

59 @staticmethod 

60 def is_enpt_internally_installed(): 

61 if not find_spec('enpt'): 

62 return False 

63 else: 

64 try: 

65 # import enpt to avoid that it cannot be found in sys.modules due to lazy importing 

66 import enpt # noqa 

67 

68 # check if it is in sys.modules which is not the case if the test is executed from within the root 

69 # directory of a local EnPT repository (find_spec returns True then) which is not installed 

70 if 'enpt' in sys.modules: 

71 return True 

72 else: 

73 return False 

74 

75 except (ModuleNotFoundError, ImportError): 

76 return False 

77 

78 def icon(self): 

79 """Return the QIcon of EnPTEnMAPBoxApp. 

80 

81 :return: QIcon() 

82 """ 

83 return QIcon(os.path.join(APP_DIR, 'icon.png')) 

84 

85 def menu(self, appMenu): 

86 """Return a QMenu that will be added to the parent `appMenu`. 

87 

88 :param appMenu: 

89 :return: QMenu 

90 """ 

91 assert isinstance(appMenu, QMenu) 

92 """ 

93 Specify menu, submenus and actions that become accessible from the EnMAP-Box GUI 

94 :return: the QMenu or QAction to be added to the "Applications" menu. 

95 """ 

96 

97 # this way you can add your QMenu/QAction to another menu entry, e.g. 'Tools' 

98 # appMenu = self.enmapbox.menu('Tools') 

99 

100 menu_entry = 'EnPT (EnMAP Processing Tool)' 

101 _alphanumeric_menu = False 

102 try: 

103 # alphanumeric menu ordering was implemented in EnMAP-Box around 3.10.1 

104 menu = self.utilsAddMenuInAlphanumericOrder(appMenu, menu_entry) 

105 _alphanumeric_menu = True 

106 except AttributeError: 

107 menu = appMenu.addMenu(menu_entry) 

108 menu.setIcon(self.icon()) 

109 

110 # add a QAction that starts a process of your application. 

111 # In this case it will open your GUI. 

112 a = menu.addAction('About EnPT') 

113 a.triggered.connect(self.showAboutDialog) 

114 a = menu.addAction('Start EnPT GUI') 

115 assert isinstance(a, QAction) 

116 a.triggered.connect(self.startGUI) 

117 

118 if not _alphanumeric_menu: 

119 appMenu.addMenu(menu) 

120 

121 return menu 

122 

123 def showAboutDialog(self): 

124 if self.enpt_is_internal: 

125 _enpt_version_str = get_version('enpt') 

126 else: 

127 _enpt_version_str = 'NA (not installed in QGIS environment)' 

128 QMessageBox.information( 

129 None, self.name, 

130 f'EnPT (the EnMAP processing tool) is an automated pre-processing pipeline for the new EnMAP ' 

131 f'hyperspectral satellite data. It provides free and open-source features to transform EnMAP Level-1B data ' 

132 f'to Level-2A. The code has been developed at the German Research Centre for Geosciences Potsdam (GFZ) as ' 

133 f'an alternative to the processing chain of the EnMAP Ground Segment.\n' 

134 f'\n' 

135 f'GUI version: {self.version}\n' 

136 f'EnPT backend version: {_enpt_version_str}' 

137 ) 

138 

139 def processingAlgorithms(self): 

140 """Return the QGIS Processing Framework GeoAlgorithms specified by your application. 

141 

142 :return: [list-of-GeoAlgorithms] 

143 """ 

144 return [self.ALG()] 

145 

146 def startGUI(self): 

147 """Open the GUI.""" 

148 try: 

149 from processing.gui.AlgorithmDialog import AlgorithmDialog 

150 

151 alg = QgsApplication.processingRegistry().algorithmById('enmapbox:EnPTAlgorithm') 

152 assert isinstance(alg, self.ALG) 

153 dlg = AlgorithmDialog(alg.create(), in_place=False, parent=self.enmapbox.ui) 

154 dlg.show() 

155 

156 return dlg 

157 

158 except Exception as ex: 

159 msg = str(ex) 

160 msg += '\n' + str(traceback.format_exc()) 

161 self.enmapbox.messageBar().pushMessage(APP_NAME, 'Error', msg, level=Qgis.Critical, duration=10) 

162 

163 return None