pktools  2.6.3
Processing Kernel for geospatial data
pktoolsUtils.py
1 # -*- coding: utf-8 -*-
2 
3 """
4 ***************************************************************************
5  pktoolsUtils.py
6  ---------------------
7  Date : April 2015
8  Copyright : (C) 2015 by Pieter Kempeneers
9  Email : kempenep at gmail dot com
10 ***************************************************************************
11 * *
12 * This program is free software; you can redistribute it and/or modify *
13 * it under the terms of the GNU General Public License as published by *
14 * the Free Software Foundation; either version 2 of the License, or *
15 * (at your option) any later version. *
16 * *
17 ***************************************************************************
18 """
19 
20 __author__ = 'Pieter Kempeneers'
21 __date__ = 'April 2015'
22 __copyright__ = '(C) 2015, Pieter Kempeneers'
23 # This will get replaced with a git SHA1 when you do a git archive
24 __revision__ = '$Format:%H$'
25 
26 from PyQt4.QtCore import *
27 from PyQt4.QtGui import *
28 import os
29 import subprocess
30 from processing.core.ProcessingLog import ProcessingLog
31 from processing.core.ProcessingConfig import ProcessingConfig
32 
33 class pktoolsUtils():
34 
35  PKTOOLS_FOLDER = "PKTOOLS_FOLDER"
36 
37  @staticmethod
38  def pktoolsPath():
39  folder = ProcessingConfig.getSetting(pktoolsUtils.PKTOOLS_FOLDER)
40  if folder == None:
41  folder = unicode('/usr/local/bin')
42  return os.path.abspath(folder)
43 
44  @staticmethod
45  def runpktools(commands, progress):
46  settings = QSettings()#from gdal
47  loglines = []
48  loglines.append("pktools execution console output")
49  loglines.append(commands)
50  progress.setInfo('pktools command:')
51  commandline = " ".join(commands)
52  progress.setCommand(commandline)
53  proc = subprocess.Popen(
54  commandline,
55  shell=True,
56  stdout=subprocess.PIPE,
57  stdin=open(os.devnull),
58  stderr=subprocess.STDOUT,
59  universal_newlines=True,
60  ).stdout
61  progress.setInfo('pktools command output:')
62 
63  #original
64  #proc = subprocess.Popen(commandline, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE,stderr=subprocess.STDOUT, universal_newlines=True).stdout
65  #1
66  #proc = subprocess.Popen(commandline, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE,stderr=subprocess.PIPE, universal_newlines=True).stdout
67  #from gdal
68 
69  for line in iter(proc.readline, ""):
70  progress.setConsoleInfo(line)
71  loglines.append(line)
72  ProcessingLog.addToLog(ProcessingLog.LOG_INFO, loglines)
73 # except Exception, e:
74 # ProcessingLog.addToLog(ProcessingLog.LOG_ERROR,
75 # self.tr('Error in pktools algorithm: %s\n%s' % (descriptionFile, str(e))))
76 
77 
78  ProcessingLog.addToLog(ProcessingLog.LOG_INFO, commandline)
79  pktoolsUtils.consoleOutput = loglines
80 
81 # @staticmethod
82 # def getConsoleOutput():
83 # return pktoolsUtils.consoleOutput