OTBUtils.py 4.44 KB
Newer Older
volaya's avatar
volaya committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# -*- coding: utf-8 -*-

"""
***************************************************************************
    OTBUtils.py
    ---------------------
    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
***************************************************************************
*                                                                         *
*   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
35

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

41
42
43
44
def cliPath():
    cli_ext = '.bat' if os.name == 'nt' else ''
    return os.path.normpath(os.path.join(QgsApplication.qgisSettingsDirPath(),
                                             'processing', 'qgis_otb_cli' + cli_ext))
45
def version():
46
47
48
49
50
51
52
    return ProcessingConfig.getSetting(OTBSettings.VERSION) or '0.0.0'

def loggerLevel():
    return ProcessingConfig.getSetting(OTBSettings.LOGGER_LEVEL) or 'INFO'

def maxRAMHint():
    return ProcessingConfig.getSetting(OTBSettings.MAX_RAM_HINT) or ''
53
54

def otbFolder():
55
56
    if ProcessingConfig.getSetting(OTBSettings.FOLDER):
        return os.path.normpath(os.sep.join(re.split(r'\\|/', ProcessingConfig.getSetting(OTBSettings.FOLDER))))
57
58
    else:
        return None
59
60

def appFolder():
61
    app_folder = ProcessingConfig.getSetting(OTBSettings.APP_FOLDER)
62
63
    if app_folder:
      return os.pathsep.join(app_folder.split(';'))
64
    else:
65
      return None
volaya's avatar
volaya committed
66

Rashad Kanavath's avatar
Rashad Kanavath committed
67
def srtmFolder():
68
    return ProcessingConfig.getSetting(OTBSettings.SRTM_FOLDER) or ''
volaya's avatar
volaya committed
69

Rashad Kanavath's avatar
Rashad Kanavath committed
70
def geoidFile():
71
    return ProcessingConfig.getSetting(OTBSettings.GEOID_FILE) or ''
volaya's avatar
volaya committed
72
73
74

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

104
105
106
107
108
109
110
111
112
113
114
115
        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
116
117
118
119
120

def tr(string, context=''):
    if context == '':
        context = 'OTBUtils'
    return QCoreApplication.translate(context, string)