OtbUtils.py 4.49 KB
Newer Older
volaya's avatar
volaya committed
1
2
3
4
# -*- coding: utf-8 -*-

"""
***************************************************************************
5
6
    OtbUtils.py
    -----------
volaya's avatar
volaya committed
7
8
9
10
11
12
13
14
    Date                 : August 2012
    Copyright            : (C) 2012 by Victor Olaya
                           (C) 2013 by CS Systemes d'information (CS SI)
    Email                : volayaf at gmail dot com
                           otb at c-s dot fr (CS SI)
    Contributors         : Victor Olaya
                           Julien Malik, Oscar Picas  (CS SI) - add functions to manage xml tree
                           Alexia Mondot (CS SI) - add a trick for OTBApplication SplitImages
15
                           Rashad Kanavath (CS SI) - re-integration of provider to QGIS
volaya's avatar
volaya committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
***************************************************************************
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License as published by  *
*   the Free Software Foundation; either version 2 of the License, or     *
*   (at your option) any later version.                                   *
*                                                                         *
***************************************************************************
"""
from builtins import str

__author__ = 'Victor Olaya'
__date__ = 'August 2012'
__copyright__ = '(C) 2012, Victor Olaya'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

import os
import re
import subprocess
Rashad Kanavath's avatar
Rashad Kanavath committed
36

volaya's avatar
volaya committed
37
from processing.core.ProcessingConfig import ProcessingConfig
38
from qgis.core import (Qgis, QgsApplication, QgsMessageLog)
39
from qgis.PyQt.QtCore import QCoreApplication
40
from otb.OtbSettings import OtbSettings
41

42

43
44
45
def cliPath():
    cli_ext = '.bat' if os.name == 'nt' else ''
    return os.path.normpath(os.path.join(QgsApplication.qgisSettingsDirPath(),
46
47
48
                                         'processing', 'qgis_otb_cli' + cli_ext))


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
63
    if ProcessingConfig.getSetting(OtbSettings.FOLDER):
        return os.path.normpath(os.sep.join(re.split(r'\\|/', ProcessingConfig.getSetting(OtbSettings.FOLDER))))
64
65
    else:
        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
74
        return None

volaya's avatar
volaya committed
75

Rashad Kanavath's avatar
Rashad Kanavath committed
76
def srtmFolder():
77
    return ProcessingConfig.getSetting(OtbSettings.SRTM_FOLDER) or ''
volaya's avatar
volaya committed
78

79

Rashad Kanavath's avatar
Rashad Kanavath committed
80
def geoidFile():
81
    return ProcessingConfig.getSetting(OtbSettings.GEOID_FILE) or ''
volaya's avatar
volaya committed
82

Rashad Kanavath's avatar
fix #25    
Rashad Kanavath committed
83

84
85
def executeOtb(command, feedback, addToLog=True):
    loglines = []
86
    with subprocess.Popen(
87
            [command],
88
89
90
91
92
            shell=True,
            stdout=subprocess.PIPE,
            stdin=subprocess.DEVNULL,
            stderr=subprocess.STDOUT,
            universal_newlines=True
93
    ) as proc:
94
95
        try:
            for line in iter(proc.stdout.readline, ''):
96
                line = line.strip()
97
98
99
                #'* ]' and '  ]' says its some progress update
                #print('line[-3:]',line[-3:])
                if line[-3:] == "* ]" or line[-3:] == "  ]":
100
101
                    part = line.split(':')[1]
                    percent = part.split('%')[0]
102
                    try:
103
104
105
                        if int(percent) >= 100:
                            loglines.append(line)
                        feedback.setProgress(int(percent))
106
107
108
                    except:
                        pass
                else:
109
                    loglines.append(line)
110
111
        except BaseException as e:
            loglines.append(str(e))
112
            pass
volaya's avatar
volaya committed
113

114
115
116
117
118
119
120
121
122
123
124
125
        for logline in loglines:
            if feedback is None:
                QgsMessageLog.logMessage(logline, 'Processing', Qgis.Info)
            else:
                feedback.pushConsoleInfo(logline)

        # for logline in loglines:
        #     if 'INFO' in logline or 'FATAL' in logline:
        #         if feedback is None:
        #             QgsMessageLog.logMessage(logline, 'Processing', Qgis.Info)
        #         else:
        #             feedback.pushConsoleInfo(logline)
volaya's avatar
volaya committed
126

127

volaya's avatar
volaya committed
128
129
def tr(string, context=''):
    if context == '':
130
        context = 'OtbUtils'
volaya's avatar
volaya committed
131
    return QCoreApplication.translate(context, string)