|
40 | 40 | from processing.algs.otb.OtbSettings import OtbSettings
|
41 | 41 |
|
42 | 42 |
|
43 |
| -def cliPath(): |
44 |
| - cli_ext = '.bat' if os.name == 'nt' else '' |
45 |
| - return os.path.normpath(os.path.join(QgsApplication.qgisSettingsDirPath(), |
46 |
| - 'processing', 'qgis_otb_cli' + cli_ext)) |
| 43 | +class OtbUtils: |
| 44 | + |
| 45 | + @staticmethod |
| 46 | + def version(): |
| 47 | + return ProcessingConfig.getSetting(OtbSettings.VERSION) or '0.0.0' |
| 48 | + |
| 49 | + @staticmethod |
| 50 | + def loggerLevel(): |
| 51 | + return ProcessingConfig.getSetting(OtbSettings.LOGGER_LEVEL) or 'INFO' |
| 52 | + |
| 53 | + @staticmethod |
| 54 | + def maxRAMHint(): |
| 55 | + return ProcessingConfig.getSetting(OtbSettings.MAX_RAM_HINT) or '' |
| 56 | + |
| 57 | + @staticmethod |
| 58 | + def otbFolder(): |
| 59 | + if ProcessingConfig.getSetting(OtbSettings.FOLDER): |
| 60 | + return os.path.normpath(os.sep.join(re.split(r'\\|/', ProcessingConfig.getSetting(OtbSettings.FOLDER)))) |
| 61 | + else: |
| 62 | + return None |
| 63 | + |
| 64 | + @staticmethod |
| 65 | + def appFolder(): |
| 66 | + app_folder = ProcessingConfig.getSetting(OtbSettings.APP_FOLDER) |
| 67 | + if app_folder: |
| 68 | + return os.pathsep.join(app_folder.split(';')) |
| 69 | + else: |
| 70 | + return None |
| 71 | + |
| 72 | + @staticmethod |
| 73 | + def srtmFolder(): |
| 74 | + return ProcessingConfig.getSetting(OtbSettings.SRTM_FOLDER) or '' |
| 75 | + |
| 76 | + @staticmethod |
| 77 | + def geoidFile(): |
| 78 | + return ProcessingConfig.getSetting(OtbSettings.GEOID_FILE) or '' |
| 79 | + |
| 80 | + @staticmethod |
| 81 | + def getExecutableInPath(path, exe): |
| 82 | + ext = '.exe' if os.name == 'nt' else '' |
| 83 | + return os.path.join(path, 'bin', exe + ext) |
| 84 | + |
| 85 | + @staticmethod |
| 86 | + def getAuxiliaryDataDirectories(): |
| 87 | + gdal_data_dir = None |
| 88 | + gtiff_csv_dir = None |
| 89 | + otb_folder = OtbUtils.otbFolder() |
| 90 | + if os.name == 'nt': |
| 91 | + gdal_data_dir = os.path.join(otb_folder, 'share', 'data') |
| 92 | + gtiff_csv_dir = os.path.join(otb_folder, 'share', 'epsg_csv') |
| 93 | + else: |
| 94 | + env_profile = os.path.join(otb_folder, 'otbenv.profile') |
| 95 | + try: |
| 96 | + if os.path.exists(env_profile): |
| 97 | + with open(env_profile) as f: |
| 98 | + lines = f.readlines() |
| 99 | + lines = [x.strip() for x in lines] |
| 100 | + for line in lines: |
| 101 | + if not line or line.startswith('#'): |
| 102 | + continue |
| 103 | + if 'GDAL_DATA=' in line: |
| 104 | + gdal_data_dir = line.split("GDAL_DATA=")[1] |
| 105 | + if 'GEOTIFF_CSV='in line: |
| 106 | + gtiff_csv_dir = line.split("GEOTIFF_CSV=")[1] |
| 107 | + except BaseException as exc: |
| 108 | + errmsg = "Cannot find gdal and geotiff data directory." + str(exc) |
| 109 | + QgsMessageLog.logMessage(errmsg, OtbUtils.tr('Processing'), Qgis.Info) |
| 110 | + pass |
| 111 | + |
| 112 | + return gdal_data_dir, gtiff_csv_dir |
| 113 | + |
| 114 | + @staticmethod |
| 115 | + def executeOtb(commands, feedback, addToLog=True): |
| 116 | + otb_env = { |
| 117 | + 'LC_NUMERIC': 'C', |
| 118 | + 'GDAL_DRIVER_PATH': 'disable' |
| 119 | + } |
| 120 | + gdal_data_dir, gtiff_csv_dir = OtbUtils.getAuxiliaryDataDirectories() |
| 121 | + if gdal_data_dir and os.path.exists(gdal_data_dir): |
| 122 | + otb_env['GDAL_DATA'] = gdal_data_dir |
| 123 | + if gtiff_csv_dir and os.path.exists(gtiff_csv_dir): |
| 124 | + otb_env['GEOTIFF_CSV'] = gtiff_csv_dir |
| 125 | + |
| 126 | + otb_env['OTB_LOGGER_LEVEL'] = OtbUtils.loggerLevel() |
| 127 | + max_ram_hint = OtbUtils.maxRAMHint() |
| 128 | + if max_ram_hint and int(max_ram_hint) > 256: |
| 129 | + otb_env['OTB_MAX_RAM_HINT'] = max_ram_hint |
| 130 | + |
| 131 | + kw = {} |
| 132 | + kw['env'] = otb_env |
| 133 | + if os.name == 'nt' and sys.version_info >= (3, 6): |
| 134 | + kw['encoding'] = "cp{}".format(OtbUtils.getWindowsCodePage()) |
| 135 | + |
| 136 | + QgsMessageLog.logMessage("{}".format(kw), OtbUtils.tr('Processing'), Qgis.Info) |
| 137 | + QgsMessageLog.logMessage("cmd={}".format(commands), OtbUtils.tr('Processing'), Qgis.Info) |
| 138 | + with subprocess.Popen( |
| 139 | + commands, |
| 140 | + shell=True, |
| 141 | + stdout=subprocess.PIPE, |
| 142 | + stdin=subprocess.DEVNULL, |
| 143 | + stderr=subprocess.STDOUT, |
| 144 | + universal_newlines=True, |
| 145 | + **kw |
| 146 | + ) as proc: |
47 | 147 |
|
48 |
| - |
49 |
| -def version(): |
50 |
| - return ProcessingConfig.getSetting(OtbSettings.VERSION) or '0.0.0' |
51 |
| - |
52 |
| - |
53 |
| -def loggerLevel(): |
54 |
| - return ProcessingConfig.getSetting(OtbSettings.LOGGER_LEVEL) or 'INFO' |
55 |
| - |
56 |
| - |
57 |
| -def maxRAMHint(): |
58 |
| - return ProcessingConfig.getSetting(OtbSettings.MAX_RAM_HINT) or '' |
59 |
| - |
60 |
| - |
61 |
| -def otbFolder(): |
62 |
| - if ProcessingConfig.getSetting(OtbSettings.FOLDER): |
63 |
| - return os.path.normpath(os.sep.join(re.split(r'\\|/', ProcessingConfig.getSetting(OtbSettings.FOLDER)))) |
64 |
| - else: |
65 |
| - return None |
66 |
| - |
67 |
| - |
68 |
| -def appFolder(): |
69 |
| - app_folder = ProcessingConfig.getSetting(OtbSettings.APP_FOLDER) |
70 |
| - if app_folder: |
71 |
| - return os.pathsep.join(app_folder.split(';')) |
72 |
| - else: |
73 |
| - return None |
74 |
| - |
75 |
| - |
76 |
| -def srtmFolder(): |
77 |
| - return ProcessingConfig.getSetting(OtbSettings.SRTM_FOLDER) or '' |
78 |
| - |
79 |
| - |
80 |
| -def geoidFile(): |
81 |
| - return ProcessingConfig.getSetting(OtbSettings.GEOID_FILE) or '' |
82 |
| - |
83 |
| - |
84 |
| -def executeOtb(command, feedback, addToLog=True): |
85 |
| - loglines = [] |
86 |
| - with subprocess.Popen( |
87 |
| - [command], |
88 |
| - shell=True, |
89 |
| - stdout=subprocess.PIPE, |
90 |
| - stdin=subprocess.DEVNULL, |
91 |
| - stderr=subprocess.STDOUT, |
92 |
| - universal_newlines=True |
93 |
| - ) as proc: |
94 |
| - try: |
95 | 148 | for line in iter(proc.stdout.readline, ''):
|
96 | 149 | line = line.strip()
|
97 | 150 | #'* ]' and ' ]' says its some progress update
|
98 |
| - #print('line[-3:]',line[-3:]) |
99 |
| - if line[-3:] == "* ]" or line[-3:] == " ]": |
| 151 | + if '% [' in line: |
100 | 152 | part = line.split(':')[1]
|
101 | 153 | percent = part.split('%')[0]
|
102 | 154 | try:
|
103 | 155 | if int(percent) >= 100:
|
104 |
| - loglines.append(line) |
| 156 | + feedback.pushConsoleInfo(line) |
105 | 157 | feedback.setProgress(int(percent))
|
106 | 158 | except:
|
107 | 159 | pass
|
108 | 160 | else:
|
109 |
| - loglines.append(line) |
110 |
| - except BaseException as e: |
111 |
| - loglines.append(str(e)) |
112 |
| - pass |
113 |
| - |
114 |
| - for logline in loglines: |
115 |
| - if feedback is None: |
116 |
| - QgsMessageLog.logMessage(logline, 'Processing', Qgis.Info) |
117 |
| - else: |
118 |
| - feedback.pushConsoleInfo(logline) |
119 |
| - |
120 |
| - # for logline in loglines: |
121 |
| - # if 'INFO' in logline or 'FATAL' in logline: |
122 |
| - # if feedback is None: |
123 |
| - # QgsMessageLog.logMessage(logline, 'Processing', Qgis.Info) |
124 |
| - # else: |
125 |
| - # feedback.pushConsoleInfo(logline) |
126 |
| - |
127 |
| - |
128 |
| -def tr(string, context=''): |
129 |
| - if context == '': |
130 |
| - context = 'OtbUtils' |
131 |
| - return QCoreApplication.translate(context, string) |
| 161 | + |
| 162 | + if feedback is None: |
| 163 | + QgsMessageLog.logMessage(line, OtbUtils.tr('Processing'), Qgis.Info) |
| 164 | + else: |
| 165 | + if any([l in line for l in ['(WARNING)', '(FATAL)', 'ERROR']]): |
| 166 | + feedback.reportError(line) |
| 167 | + else: |
| 168 | + feedback.pushConsoleInfo(line.strip()) |
| 169 | + |
| 170 | + @staticmethod |
| 171 | + def getWindowsCodePage(): |
| 172 | + """ |
| 173 | + Determines MS-Windows CMD.exe shell codepage. |
| 174 | + Used into GRASS exec script under MS-Windows. |
| 175 | + """ |
| 176 | + from ctypes import cdll |
| 177 | + return str(cdll.kernel32.GetACP()) |
| 178 | + |
| 179 | + @staticmethod |
| 180 | + def tr(string, context=''): |
| 181 | + if context == '': |
| 182 | + context = 'OtbUtils' |
| 183 | + return QCoreApplication.translate(context, string) |
0 commit comments