Example #1
0
    def otbPath():
        folder = ProcessingConfig.getSetting(OTBUtils.OTB_FOLDER)
        if folder is None:
            folder = ''

            # Try to configure the path automatically
            if isMac():
                testfolder = os.path.join(str(QgsApplication.prefixPath()),
                        'bin')
                if os.path.exists(os.path.join(testfolder, 'otbcli')):
                    folder = testfolder
                else:
                    testfolder = '/usr/local/bin'
                    if os.path.exists(os.path.join(testfolder, 'otbcli')):
                        folder = testfolder
            elif isWindows():
                testfolder = os.path.dirname(str(QgsApplication.prefixPath()))
                testfolder = os.path.dirname(testfolder)
                testfolder = os.path.join(testfolder, 'bin')
                path = os.path.join(testfolder, 'otbcli.bat')
                if os.path.exists(path):
                    folder = testfolder
            else:
                testfolder = '/usr/bin'
                if os.path.exists(os.path.join(testfolder, 'otbcli')):
                    folder = testfolder
        return folder
Example #2
0
    def grassPath():
        if not isWindows() and not isMac():
            return ""

        folder = ProcessingConfig.getSetting(Grass7Utils.GRASS_FOLDER) or ""
        if not os.path.exists(folder):
            folder = None
        if folder is None:
            if isWindows():
                if "OSGEO4W_ROOT" in os.environ:
                    testfolder = os.path.join(str(os.environ["OSGEO4W_ROOT"]), "apps")
                else:
                    testfolder = str(QgsApplication.prefixPath())
                testfolder = os.path.join(testfolder, "grass")
                if os.path.isdir(testfolder):
                    for subfolder in os.listdir(testfolder):
                        if subfolder.startswith("grass-7"):
                            folder = os.path.join(testfolder, subfolder)
                            break
            else:
                folder = os.path.join(str(QgsApplication.prefixPath()), "grass7")
                if not os.path.isdir(folder):
                    folder = "/Applications/GRASS-7.0.app/Contents/MacOS"

        return folder or ""
Example #3
0
    def grassPath():
        if not isWindows() and not isMac():
            return ''

        folder = ProcessingConfig.getSetting(GrassUtils.GRASS_FOLDER) or ''
        if not os.path.exists(folder):
            folder = None
        if folder is None:
            if isWindows():
                if "OSGEO4W_ROOT" in os.environ:
                    testfolder = os.path.join(str(os.environ['OSGEO4W_ROOT']), "apps")
                else:
                    testfolder = str(QgsApplication.prefixPath())
                testfolder = os.path.join(testfolder, 'grass')
                if os.path.isdir(testfolder):
                    for subfolder in os.listdir(testfolder):
                        if subfolder.startswith('grass-6'):
                            folder = os.path.join(testfolder, subfolder)
                            break
            else:
                folder = os.path.join(QgsApplication.prefixPath(), 'grass')
                if not os.path.isdir(folder):
                    folder = '/Applications/GRASS-6.4.app/Contents/MacOS'

        if folder:
            ProcessingConfig.setSettingValue(GrassUtils.GRASS_FOLDER, folder)
        return folder or ''
Example #4
0
 def otbPath():
     folder = ProcessingConfig.getSetting(OTBUtils.OTB_FOLDER)
     if folder == None:
         folder = ""
         #try to configure the path automatically
         if ProcessingUtils.isMac():
             testfolder = os.path.join(str(QgsApplication.prefixPath()), "bin")
             if os.path.exists(os.path.join(testfolder, "otbcli")):
                 folder = testfolder
             else:
                 testfolder = "/usr/local/bin"
                 if os.path.exists(os.path.join(testfolder, "otbcli")):
                     folder = testfolder
         elif ProcessingUtils.isWindows():
             testfolder = os.path.dirname(str(QgsApplication.prefixPath()))
             testfolder = os.path.dirname(testfolder)
             testfolder = os.path.join(testfolder,  "bin")
             path = os.path.join(testfolder, "otbcli.bat")
             if os.path.exists(path):
                 folder = testfolder
         else:
             testfolder = "/usr/bin"
             if os.path.exists(os.path.join(testfolder, "otbcli")):
                 folder = testfolder
     return folder
Example #5
0
    def grassPath():
        if not isWindows() and not isMac():
            return ''

        folder = ProcessingConfig.getSetting(Grass7Utils.GRASS_FOLDER) or ''
        if not os.path.exists(folder):
            folder = None
        if folder is None:
            if isWindows():
                if "OSGEO4W_ROOT" in os.environ:
                    testfolder = os.path.join(unicode(os.environ['OSGEO4W_ROOT']), "apps")
                else:
                    testfolder = unicode(QgsApplication.prefixPath())
                testfolder = os.path.join(testfolder, 'grass')
                if os.path.isdir(testfolder):
                    for subfolder in os.listdir(testfolder):
                        if subfolder.startswith('grass-7'):
                            folder = os.path.join(testfolder, subfolder)
                            fn = os.path.join(folder, "etc", "VERSIONNUMBER")
                            if not os.path.isfile(fn):
                                continue

                            f = open(fn, "r")
                            Grass7Utils.version = f.read().split(' ')[0]
                            f.close()

                            major, minor, patch = Grass7Utils.version.split('.')
                            Grass7Utils.command = "grass{}{}".format(major, minor)
                            break
            else:
                folder = os.path.join(unicode(QgsApplication.prefixPath()), 'grass7')
                if not os.path.isdir(folder):
                    folder = '/Applications/GRASS-7.0.app/Contents/MacOS'

        return folder or ''
Example #6
0
 def otbLibPath():
     folder = ProcessingConfig.getSetting(OTBUtils.OTB_LIB_FOLDER)
     if folder is None:
         folder = ''
         # Try to configure the path automatically
         if isMac():
             testfolder = os.path.join(str(QgsApplication.prefixPath()),
                     'lib/otb/applications')
             if os.path.exists(testfolder):
                 folder = testfolder
             else:
                 testfolder = '/usr/local/lib/otb/applications'
                 if os.path.exists(testfolder):
                     folder = testfolder
         elif isWindows():
             testfolder = os.path.dirname(str(QgsApplication.prefixPath()))
             testfolder = os.path.join(testfolder, 'orfeotoolbox')
             testfolder = os.path.join(testfolder, 'applications')
             if os.path.exists(testfolder):
                 folder = testfolder
         else:
             testfolder = '/usr/lib/otb/applications'
             if os.path.exists(testfolder):
                 folder = testfolder
     return folder
Example #7
0
    def runGdal(commands, progress=None):
        if progress is None:
            progress = SilentProgress()
        envval = os.getenv("PATH")
        # We need to give some extra hints to get things picked up on OS X
        isDarwin = False
        try:
            isDarwin = platform.system() == "Darwin"
        except IOError:  # https://travis-ci.org/m-kuhn/QGIS#L1493-L1526
            pass
        if isDarwin and os.path.isfile(os.path.join(QgsApplication.prefixPath(), "bin", "gdalinfo")):
            # Looks like there's a bundled gdal. Let's use it.
            os.environ["PATH"] = "{}{}{}".format(os.path.join(QgsApplication.prefixPath(), "bin"), os.pathsep, envval)
            os.environ["DYLD_LIBRARY_PATH"] = os.path.join(QgsApplication.prefixPath(), "lib")
        else:
            # Other platforms should use default gdal finder codepath
            settings = QSettings()
            path = settings.value("/GdalTools/gdalPath", "")
            if not path.lower() in envval.lower().split(os.pathsep):
                envval += "{}{}".format(os.pathsep, path)
                os.putenv("PATH", envval)

        fused_command = " ".join([str(c) for c in commands])
        ProcessingLog.addToLog(ProcessingLog.LOG_INFO, fused_command)
        progress.setInfo("GDAL command:")
        progress.setCommand(fused_command)
        progress.setInfo("GDAL command output:")
        success = False
        retry_count = 0
        while success == False:
            loglines = []
            loglines.append("GDAL execution console output")
            try:
                with subprocess.Popen(
                    fused_command,
                    shell=True,
                    stdout=subprocess.PIPE,
                    stdin=subprocess.DEVNULL,
                    stderr=subprocess.STDOUT,
                    universal_newlines=True,
                ) as proc:
                    for line in proc.stdout:
                        progress.setConsoleInfo(line)
                        loglines.append(line)
                    success = True
            except IOError as e:
                if retry_count < 5:
                    retry_count += 1
                else:
                    raise IOError(
                        e.message
                        + u"\nTried 5 times without success. Last iteration stopped after reading {} line(s).\nLast line(s):\n{}".format(
                            len(loglines), u"\n".join(loglines[-10:])
                        )
                    )

        ProcessingLog.addToLog(ProcessingLog.LOG_INFO, loglines)
        GdalUtils.consoleOutput = loglines
Example #8
0
    def runGdal(commands, feedback=None):
        if feedback is None:
            feedback = QgsProcessingFeedback()
        envval = os.getenv('PATH')
        # We need to give some extra hints to get things picked up on OS X
        isDarwin = False
        try:
            isDarwin = platform.system() == 'Darwin'
        except IOError:  # https://travis-ci.org/m-kuhn/QGIS#L1493-L1526
            pass
        if isDarwin and os.path.isfile(os.path.join(QgsApplication.prefixPath(), "bin", "gdalinfo")):
            # Looks like there's a bundled gdal. Let's use it.
            os.environ['PATH'] = "{}{}{}".format(os.path.join(QgsApplication.prefixPath(), "bin"), os.pathsep, envval)
            os.environ['DYLD_LIBRARY_PATH'] = os.path.join(QgsApplication.prefixPath(), "lib")
        else:
            # Other platforms should use default gdal finder codepath
            settings = QgsSettings()
            path = settings.value('/GdalTools/gdalPath', '')
            if not path.lower() in envval.lower().split(os.pathsep):
                envval += '{}{}'.format(os.pathsep, path)
                os.putenv('PATH', envval)

        fused_command = ' '.join([str(c) for c in commands])
        QgsMessageLog.logMessage(fused_command, 'Processing', Qgis.Info)
        feedback.pushInfo('GDAL command:')
        feedback.pushCommandInfo(fused_command)
        feedback.pushInfo('GDAL command output:')
        success = False
        retry_count = 0
        while not success:
            loglines = []
            loglines.append('GDAL execution console output')
            try:
                with subprocess.Popen(
                    fused_command,
                    shell=True,
                    stdout=subprocess.PIPE,
                    stdin=subprocess.DEVNULL,
                    stderr=subprocess.STDOUT,
                    universal_newlines=True,
                ) as proc:
                    for line in proc.stdout:
                        feedback.pushConsoleInfo(line)
                        loglines.append(line)
                    success = True
            except IOError as e:
                if retry_count < 5:
                    retry_count += 1
                else:
                    raise IOError(
                        str(e) + u'\nTried 5 times without success. Last iteration stopped after reading {} line(s).\nLast line(s):\n{}'.format(
                            len(loglines), u'\n'.join(loglines[-10:])))

            QgsMessageLog.logMessage('\n'.join(loglines), 'Processing', Qgis.Info)
            GdalUtils.consoleOutput = loglines
Example #9
0
    def grassPath():
        """
        Find GRASS path on the operating system.
        Sets global variable Grass7Utils.path
        """
        if Grass7Utils.path is not None:
            return Grass7Utils.path

        if not isWindows() and not isMac():
            return ''

        if isMac():
            folder = ProcessingConfig.getSetting(Grass7Utils.GRASS_FOLDER) or ''
            if not os.path.exists(folder):
                folder = None
        else:
            folder = None

        if folder is None:
            # Under MS-Windows, we use OSGEO4W or QGIS Path for folder
            if isWindows():
                if "OSGEO4W_ROOT" in os.environ:
                    testfolder = os.path.join(str(os.environ['OSGEO4W_ROOT']), "apps")
                else:
                    testfolder = str(QgsApplication.prefixPath())
                testfolder = os.path.join(testfolder, 'grass')
                if os.path.isdir(testfolder):
                    grassfolders = sorted([f for f in os.listdir(testfolder) if f.startswith("grass-7.") and os.path.isdir(os.path.join(testfolder, f))], reverse=True, key=lambda x: [int(v) for v in x[len("grass-"):].split('.')])
                    if grassfolders:
                        folder = os.path.join(testfolder, grassfolders[0])
            elif isMac():
                # For MacOSX, we scan some well-known directories
                # Start with QGIS bundle
                for version in ['', '7', '70', '71', '72', '74']:
                    testfolder = os.path.join(str(QgsApplication.prefixPath()),
                                              'grass{}'.format(version))
                    if os.path.isdir(testfolder):
                        folder = testfolder
                        break
                    # If nothing found, try standalone GRASS installation
                    if folder is None:
                        for version in ['0', '1', '2', '4']:
                            testfolder = '/Applications/GRASS-7.{}.app/Contents/MacOS'.format(version)
                            if os.path.isdir(testfolder):
                                folder = testfolder
                                break

        if folder is not None:
            Grass7Utils.path = folder

        return folder or ''
Example #10
0
def findSagaFolder():
    folder = None
    if isMac():
        testfolder = os.path.join(QgsApplication.prefixPath(), 'bin')
        if os.path.exists(os.path.join(testfolder, 'saga_cmd')):
            folder = testfolder
        else:
            testfolder = '/usr/local/bin'
            if os.path.exists(os.path.join(testfolder, 'saga_cmd')):
                folder = testfolder
    elif isWindows():
        testfolder = os.path.join(os.path.dirname(QgsApplication.prefixPath()), 'saga')
        if os.path.exists(os.path.join(testfolder, 'saga_cmd.exe')):
            folder = testfolder
    return folder
Example #11
0
    def __init__(self, iface):
        Plugin.__init__(self, iface, "PointsInPolygons")

        userPluginPath = QtCore.QFileInfo(QgsApplication.qgisUserDbFilePath()).path() + '/python/plugins/points_in_polygons'
        systemPluginPath = QgsApplication.prefixPath() + '/python/plugins/points_in_polygons'

        overrideLocale = QtCore.QSettings().value('locale/overrideFlag', False, type=bool)
        if not overrideLocale:
            localeFullName = QtCore.QLocale.system().name()[:2]
        else:
            localeFullName = QtCore.QSettings().value("locale/userLocale", "")

        if QtCore.QFileInfo(userPluginPath).exists():
            translationPath = userPluginPath + '/i18n/pointsinpolygons_' + localeFullName + '.qm'
            self.pluginPath = userPluginPath
        else:
            translationPath = systemPluginPath + '/i18n/pointsinpolygons_' + localeFullName + '.qm'
            self.pluginPath = systemPluginPath

        self.localePath = translationPath
        if QtCore.QFileInfo(self.localePath).exists():
            self.translator = QtCore.QTranslator()
            self.translator.load(self.localePath)
            QgsApplication.installTranslator(self.translator)

        self.pointLayerName = ""
        self.polygonLayerName = ""
        self.fieldName = ""
def getTranslate(namePlugin, nameDir=None):
    if nameDir is None:
      nameDir = namePlugin

    pluginPath = os.path.join('python', 'plugins', nameDir)

    userPath = QFileInfo(QgsApplication.qgisUserDatabaseFilePath()).path()
    userPluginPath = os.path.join(userPath, pluginPath)
    
    systemPath = QgsApplication.prefixPath()
    systemPluginPath = os.path.join(systemPath, pluginPath)

    overrideLocale = QSettings().value('locale/overrideFlag', False, type=bool)
    localeFullName = QLocale.system().name() if not overrideLocale else QSettings().value('locale/userLocale', '')

    qmPathFile = os.path.join('i18n', '{0}_{1}.qm'.format(namePlugin, localeFullName))
    pp = userPluginPath if QFileInfo(userPluginPath).exists() else systemPluginPath
    translationFile = os.path.join(pp, qmPathFile)

    if QFileInfo(translationFile).exists():
        translator = QTranslator()
        translator.load(translationFile)
        QCoreApplication.installTranslator(translator)
        QgsApplication.messageLog().logMessage(('Installed translation file {}'.format(translationFile)), 'Midvatten',
                                               level=Qgis.Info)
        return translator
    else:
        QgsApplication.messageLog().logMessage(
            ("translationFile {} didn't exist, no translation file installed!".format(translationFile)), 'Midvatten',
                                               level=Qgis.Info)
Example #13
0
 def grassWinShell():
     folder = ProcessingConfig.getSetting(GrassUtils.GRASS_WIN_SHELL) or ''
     if not os.path.exists(folder):
         folder = None
     if folder is None:
         folder = os.path.dirname(str(QgsApplication.prefixPath()))
         folder = os.path.join(folder, 'msys')
     return folder
Example #14
0
    def grassBin():
        """
        Find GRASS binary path on the operating system.
        Sets global variable Grass7Utils.command
        """
        cmdList = ["grass74", "grass72", "grass71", "grass70", "grass",
                   "grass74.sh", "grass72.sh", "grass71.sh", "grass70.sh", "grass.sh"]

        def searchFolder(folder):
            """
            Inline function to search for grass binaries into a folder
            with os.walk
            """
            command = None
            if os.path.exists(folder):
                for root, dirs, files in os.walk(folder):
                    for cmd in cmdList:
                        if cmd in files:
                            command = os.path.join(root, cmd)
                            break
            return command

        if Grass7Utils.command:
            return Grass7Utils.command

        path = Grass7Utils.grassPath()
        command = None

        # For MS-Windows there is a difference between GRASS Path and GRASS binary
        if isWindows():
            # If nothing found, use OSGEO4W or QgsPrefix:
            if "OSGEO4W_ROOT" in os.environ:
                testFolder = str(os.environ['OSGEO4W_ROOT'])
            else:
                testFolder = str(QgsApplication.prefixPath())
            testFolder = os.path.join(testFolder, 'bin')
            command = searchFolder(testFolder)
        elif isMac():
            # Search in grassPath
            command = searchFolder(path)

        # Under GNU/Linux or if everything has failed, use shutil
        if not command:
            for cmd in cmdList:
                testBin = shutil.which(cmd)
                if testBin:
                    command = os.path.abspath(testBin)
                    break

        if command:
            Grass7Utils.command = command
            if path is '':
                Grass7Utils.path = os.path.dirname(command)

        return command
Example #15
0
def setMacOSXDefaultEnvironment():
    # fix bug #3170: many GDAL Tools don't work in OS X standalone

    if platform.system() != "Darwin":
        return

    # QgsApplication.prefixPath() contains the path to qgis executable (i.e. .../Qgis.app/MacOS)
    # get the path to Qgis application folder
    qgis_app = u"%s/.." % QgsApplication.prefixPath()
    qgis_app = QDir(qgis_app).absolutePath()

    qgis_bin = u"%s/bin" % QgsApplication.prefixPath()  # path to QGis bin folder
    qgis_python = u"%s/Resources/python" % qgis_app  # path to QGis python folder

    # path to the GDAL framework within the Qgis application folder (QGis standalone only)
    qgis_standalone_gdal_path = u"%s/Frameworks/GDAL.framework" % qgis_app

    # path to the GDAL framework when installed as external framework
    gdal_versionsplit = unicode(GdalConfig.version()).split(".")
    gdal_base_path = u"/Library/Frameworks/GDAL.framework/Versions/%s.%s" % (gdal_versionsplit[0], gdal_versionsplit[1])

    if os.path.exists(qgis_standalone_gdal_path):  # qgis standalone
        # GDAL executables are in the QGis bin folder
        if getGdalBinPath() == "":
            setGdalBinPath(qgis_bin)
        # GDAL pymods are in the QGis python folder
        if getGdalPymodPath() == "":
            setGdalPymodPath(qgis_python)
        # GDAL help is in the framework folder
        if getHelpPath() == "":
            setHelpPath(u"%s/Resources/doc" % qgis_standalone_gdal_path)

    elif os.path.exists(gdal_base_path):
        # all GDAL parts are in the GDAL framework folder
        if getGdalBinPath() == "":
            setGdalBinPath(u"%s/Programs" % gdal_base_path)
        if getGdalPymodPath() == "":
            setGdalPymodPath(
                u"%s/Python/%s.%s/site-packages" % (gdal_base_path, sys.version_info[0], sys.version_info[1])
            )
        if getHelpPath() == "":
            setHelpPath(u"%s/Resources/doc" % gdal_base_path)
Example #16
0
def findOtbLibPath():
    folder = ""
    #try to configure the path automatically
    if isMac():
        testfolder = os.path.join(str(QgsApplication.prefixPath()), "lib/otb/applications")
        if os.path.exists(testfolder):
            folder = testfolder
        else:
            testfolder = "/usr/local/lib/otb/applications"
            if os.path.exists(testfolder):
                folder = testfolder
    elif isWindows():
        testfolder = os.path.join(os.path.dirname(QgsApplication.prefixPath()), "orfeotoolbox", "applications")
        if os.path.exists(testfolder):
            folder = testfolder
    else:
        testfolder = "/usr/lib/otb/applications"
        if os.path.exists(testfolder):
            folder = testfolder
    return folder
Example #17
0
    def grassPath():
        if not SextanteUtils.isWindows() and not SextanteUtils.isMac():
            return ""

        folder = SextanteConfig.getSetting(GrassUtils.GRASS_FOLDER)
        if folder == None:
            if SextanteUtils.isWindows():                    
                testfolder = os.path.dirname(str(QgsApplication.prefixPath()))                                
                testfolder = os.path.join(testfolder,  "grass")
                if os.path.isdir(testfolder):                                                                
                    for subfolder in os.listdir(testfolder):
                        if subfolder.startswith("grass"):
                            folder = os.path.join(testfolder, subfolder)
                            break
            else:
                folder = os.path.join(str(QgsApplication.prefixPath()), "grass")
                if not os.path.isdir(folder):
                    folder = "/Applications/GRASS-6.4.app/Contents/MacOS"

        return folder
Example #18
0
def findOtbPath():
    folder = ""
    # try to configure the path automatically
    if isMac():
        testfolder = os.path.join(str(QgsApplication.prefixPath()), "bin")
        if os.path.exists(os.path.join(testfolder, "otbcli")):
            folder = testfolder
        else:
            testfolder = "/usr/local/bin"
            if os.path.exists(os.path.join(testfolder, "otbcli")):
                folder = testfolder
    elif isWindows():
        testfolder = os.path.join(os.path.dirname(QgsApplication.prefixPath()), os.pardir, "bin")
        if os.path.exists(os.path.join(testfolder, "otbcli.bat")):
            folder = testfolder
    else:
        testfolder = "/usr/bin"
        if os.path.exists(os.path.join(testfolder, "otbcli")):
            folder = testfolder
    return folder
Example #19
0
    def grassPath():
        if not isWindows() and not isMac():
            return ''

        folder = ProcessingConfig.getSetting(GrassUtils.GRASS_FOLDER)
        if folder is None:
            if isWindows():
                testfolder = os.path.dirname(QgsApplication.prefixPath())
                testfolder = os.path.join(testfolder, 'grass')
                if os.path.isdir(testfolder):
                    for subfolder in os.listdir(testfolder):
                        if subfolder.startswith('grass'):
                            folder = os.path.join(testfolder, subfolder)
                            break
            else:
                folder = os.path.join(QgsApplication.prefixPath(), 'grass')
                if not os.path.isdir(folder):
                    folder = '/Applications/GRASS-6.4.app/Contents/MacOS'

        return folder or ''
Example #20
0
    def runGdal(commands, progress):
        envval = unicode(os.getenv('PATH'))
        # We need to give some extra hints to get things picked up on OS X
        if platform.system() == 'Darwin':
            if os.path.isfile(os.path.join(QgsApplication.prefixPath(), "bin", "gdalinfo")):
                # Looks like there's a bundled gdal. Let's use it.
                os.environ['PATH'] = "%s%s%s" % (os.path.join(QgsApplication.prefixPath(), "bin"), os.pathsep, envval)
                os.environ['DYLD_LIBRARY_PATH'] = os.path.join(QgsApplication.prefixPath(), "lib")
            else:
                # Nothing internal. Let's see if we can find it elsewhere.
                settings = QSettings()
                path = unicode(settings.value('/GdalTools/gdalPath', ''))
                envval += '%s%s' % (os.pathsep, path)
                os.putenv('PATH', envval)
        else:
            # Other platforms should use default gdal finder codepath
            settings = QSettings()
            path = unicode(settings.value('/GdalTools/gdalPath', ''))
            if not path.lower() in envval.lower().split(os.pathsep):
                envval += '%s%s' % (os.pathsep, path)
                os.putenv('PATH', envval)

        loglines = []
        loglines.append('GDAL execution console output')
        fused_command = ''.join(['%s ' % c for c in commands])
        progress.setInfo('GDAL command:')
        progress.setCommand(fused_command)
        proc = subprocess.Popen(
            fused_command,
            shell=True,
            stdout=subprocess.PIPE,
            stdin=open(os.devnull),
            stderr=subprocess.STDOUT,
            universal_newlines=True,
        ).stdout
        progress.setInfo('GDAL command output:')
        for line in iter(proc.readline, ''):
            progress.setConsoleInfo(line)
            loglines.append(line)
        ProcessingLog.addToLog(ProcessingLog.LOG_INFO, loglines)
        GdalUtils.consoleOutput = loglines
Example #21
0
    def runGdal(commands, progress=None):
        if progress is None:
            progress = SilentProgress()
        envval = os.getenv('PATH')
        # We need to give some extra hints to get things picked up on OS X
        isDarwin = False
        try:
            isDarwin = platform.system() == 'Darwin'
        except IOError: # https://travis-ci.org/m-kuhn/QGIS#L1493-L1526
            pass
        if isDarwin and os.path.isfile(os.path.join(QgsApplication.prefixPath(), "bin", "gdalinfo")):
            # Looks like there's a bundled gdal. Let's use it.
            os.environ['PATH'] = "{}{}{}".format(os.path.join(QgsApplication.prefixPath(), "bin"), os.pathsep, envval)
            os.environ['DYLD_LIBRARY_PATH'] = os.path.join(QgsApplication.prefixPath(), "lib")
        else:
            # Other platforms should use default gdal finder codepath
            settings = QSettings()
            path = settings.value('/GdalTools/gdalPath', '')
            if not path.lower() in envval.lower().split(os.pathsep):
                envval += '{}{}'.format(os.pathsep, path)
                os.putenv('PATH', envval)

        loglines = []
        loglines.append('GDAL execution console output')
        fused_command = ''.join(['%s ' % c for c in commands])
        progress.setInfo('GDAL command:')
        progress.setCommand(fused_command)
        proc = subprocess.Popen(
            fused_command,
            shell=True,
            stdout=subprocess.PIPE,
            stdin=open(os.devnull),
            stderr=subprocess.STDOUT,
            universal_newlines=True,
        ).stdout
        progress.setInfo('GDAL command output:')
        for line in iter(proc.readline, ''):
            progress.setConsoleInfo(line)
            loglines.append(line)
        ProcessingLog.addToLog(ProcessingLog.LOG_INFO, loglines)
        GdalUtils.consoleOutput = loglines
def get_environment():
    # setup the MacOSX path to both GDAL executables and python modules
    if system() == 'Darwin':
        GdalUtils.setMacOSXDefaultEnvironment()

    environment = GdalUtils.setProcessEnvironment()

    if system() == 'Windows':
        # The gdal-processing executables are located at <QGIS Installation folder>\bin
        # The gcs.csv-file (required as a GDAL_DATA env-var) is stored in <QGIS Installation folder>\share\gdal
        # The projection files (required as a PROJ_LIB env-var) are stored in <QGIS Installation folder>\share\proj
        # prefixPath() returns <QGIS Installation folder>\apps\qgis
        gdal_location = path.join(QgsApplication.prefixPath(), '..', '..', 'bin')
        gdal_data = path.join(QgsApplication.prefixPath(), '..', '..', 'share', 'gdal')
        gdal_python_tools = path.join(QgsApplication.prefixPath(), 'python', 'plugins', 'GdalTools')
        gdal_projection = path.join(QgsApplication.prefixPath(), '..', '..', 'share', 'proj')
        environment['PATH'] = str(path.abspath(gdal_location))
        environment['PYTHONPATH'] = str(path.abspath(gdal_python_tools))
        environment['GDAL_DATA'] = str(path.abspath(gdal_data))
        environment['PROJ_LIB'] = str(path.abspath(gdal_projection))

    return environment
Example #23
0
    def taudemPath():
        folder = ProcessingConfig.getSetting(TauDEMUtils.TAUDEM_FOLDER)
        if folder is None:
            folder = ''

        if isMac():
            testfolder = os.path.join(QgsApplication.prefixPath(), 'bin')
            if os.path.exists(os.path.join(testfolder, 'pitremove')):
                folder = testfolder
            else:
                testfolder = '/usr/local/bin'
                if os.path.exists(os.path.join(testfolder, 'pitremove')):
                    folder = testfolder
        return folder
Example #24
0
    def mpiexecPath():
        folder = ProcessingConfig.getSetting(TauDEMUtils.MPIEXEC_FOLDER)
        if folder is None:
            folder = ''

        if isMac():
            testfolder = os.path.join(QgsApplication.prefixPath(), 'bin')
            if os.path.exists(os.path.join(testfolder, 'mpiexec')):
                folder = testfolder
            else:
                testfolder = '/usr/local/bin'
                if os.path.exists(os.path.join(testfolder, 'mpiexec')):
                    folder = testfolder
        return folder
Example #25
0
    def mpiexecPath():
        folder = SextanteConfig.getSetting(TauDEMUtils.MPIEXEC_FOLDER)
        if folder == None:
            folder = ""

        if SextanteUtils.isMac():
            testfolder = os.path.join(str(QgsApplication.prefixPath()), "bin")
            if os.path.exists(os.path.join(testfolder, "mpiexec")):
                folder = testfolder
            else:
                testfolder = "/usr/local/bin"
                if os.path.exists(os.path.join(testfolder, "mpiexec")):
                    folder = testfolder
        return folder
Example #26
0
    def grassPath():
        if not isWindows() and not isMac():
            return ''

        folder = ProcessingConfig.getSetting(Grass7Utils.GRASS_FOLDER)
        if folder is None:
            if isWindows():
                if "OSGEO4W_ROOT" in os.environ:
                    testfolder = os.path.join(unicode(os.environ['OSGEO4W_ROOT']), "apps")
                else:
                    testfolder = unicode(QgsApplication.prefixPath())
                testfolder = os.path.join(testfolder, 'grass')
                if os.path.isdir(testfolder):
                    for subfolder in os.listdir(testfolder):
                        if subfolder.startswith('grass-7'):
                            folder = os.path.join(testfolder, subfolder)
                            break
            else:
                folder = os.path.join(unicode(QgsApplication.prefixPath()), 'grass7')
                if not os.path.isdir(folder):
                    folder = '/Applications/GRASS-7.0.app/Contents/MacOS'

        return folder or ''
Example #27
0
def findSagaFolder():
    folder = None
    if isMac():
        testfolder = os.path.join(QgsApplication.prefixPath(), 'bin')
        if os.path.exists(os.path.join(testfolder, 'saga_cmd')):
            folder = testfolder
        else:
            testfolder = '/usr/local/bin'
            if os.path.exists(os.path.join(testfolder, 'saga_cmd')):
                folder = testfolder
    elif isWindows():
        folders = []
        folders.append(os.path.join(os.path.dirname(QgsApplication.prefixPath()), 'saga'))
        if "OSGEO4W_ROOT" in os.environ:
            folders.append(os.path.join(str(os.environ['OSGEO4W_ROOT']), "apps", "saga-ltr"))
            folders.append(os.path.join(str(os.environ['OSGEO4W_ROOT']), "apps", "saga"))

        for testfolder in folders:
            if os.path.exists(os.path.join(testfolder, 'saga_cmd.exe')):
                folder = testfolder
                break

    return folder
Example #28
0
    def taudemPath():
        folder = SextanteConfig.getSetting(TauDEMUtils.TAUDEM_FOLDER)
        if folder == None:
            folder = ""

        if SextanteUtils.isMac():
            testfolder = os.path.join(str(QgsApplication.prefixPath()), "bin")
            if os.path.exists(os.path.join(testfolder, "slopearea")):
                folder = testfolder
            else:
                testfolder = "/usr/local/bin"
                if os.path.exists(os.path.join(testfolder, "slopearea")):
                    folder = testfolder
        return folder
Example #29
0
 def otbLibPath():
     folder = ProcessingConfig.getSetting(OTBUtils.OTB_LIB_FOLDER)
     if folder == None:
         folder =""
         #try to configure the path automatically
         if ProcessingUtils.isMac():
             testfolder = os.path.join(str(QgsApplication.prefixPath()), "lib/otb/applications")
             if os.path.exists(testfolder):
                 folder = testfolder
             else:
                 testfolder = "/usr/local/lib/otb/applications"
                 if os.path.exists(testfolder):
                     folder = testfolder
         elif ProcessingUtils.isWindows():
             testfolder = os.path.dirname(str(QgsApplication.prefixPath()))
             testfolder = os.path.join(testfolder,  "orfeotoolbox")
             testfolder = os.path.join(testfolder,  "applications")
             if os.path.exists(testfolder):
                 folder = testfolder
         else:
             testfolder = "/usr/lib/otb/applications"
             if os.path.exists(testfolder):
                 folder = testfolder
     return folder
Example #30
0
    def otbLibPath():
        folder = SextanteConfig.getSetting(OTBUtils.OTB_LIB_FOLDER)
        if folder == None:
            folder =""

            if SextanteUtils.isMac():
                testfolder = os.path.join(str(QgsApplication.prefixPath()), "lib/otb/applications")
                if os.path.exists(testfolder):
                    folder = testfolder
                else:
                    testfolder = "/usr/local/lib/otb/applications"
                    if os.path.exists(testfolder):
                        folder = testfolder
            else:
                testfolder = "/usr/lib/otb/applications"
                if os.path.exists(testfolder):
                    folder = testfolder
        return folder
Example #31
0
# -*- coding: utf-8 -*-
# @Author  : llc
# @Time    : 2021/4/3 10:52
import os
import sys

from qgis.core import QgsApplication, QgsProcessingAlgorithm

# 提供qgis安装位置的路径(windows默认:C:\Program Files\QGIS 3.x\apps\qgis-ltr)
QgsApplication.setPrefixPath("/path/to/qgis/installation", True)
# 创建对QgsApplication的引用,第二个参数设置为False将禁用GUI
qgs = QgsApplication([], False)
# 加载提供者
qgs.initQgis()
prefix = QgsApplication.prefixPath()
print(prefix)

# 在这里编写代码,加载一些图层,使用处理算法等
sys.path.append(os.path.join(prefix, "python", "plugins"))
from plugins.processing.core.Processing import Processing
from plugins import processing

Processing.initialize()

for alg in QgsApplication.processingRegistry().algorithms():
    alg: QgsProcessingAlgorithm
    print(alg.group(), alg.provider().name(), alg.name())

result = processing.run('gdal:gdalinfo',
                        {
                            'EXTRA': '',
Example #32
0
 def __init__(self, iface):
     self.iface = iface
     userPluginPath = os.path.dirname(__file__)
     systemPluginPath = QgsApplication.prefixPath() + "/python/plugins/HFF"
Example #33
0
    def grassBin():
        """
        Find GRASS binary path on the operating system.
        Sets global variable Grass7Utils.command
        """
        def searchFolder(folder):
            """
            Inline function to search for grass binaries into a folder
            with os.walk
            """
            if os.path.exists(folder):
                for root, dirs, files in os.walk(folder):
                    for cmd in cmdList:
                        if cmd in files:
                            return os.path.join(root, cmd)
            return None

        if Grass7Utils.command:
            return Grass7Utils.command

        path = Grass7Utils.grassPath()
        command = None

        vn = os.path.join(path, "etc", "VERSIONNUMBER")
        if os.path.isfile(vn):
            with open(vn, "r") as f:
                major, minor, patch = f.readlines()[0].split(' ')[0].split('.')
                if patch != 'svn':
                    patch = ''
                cmdList = [
                    "grass{}{}{}".format(major, minor, patch), "grass",
                    "grass{}{}{}.sh".format(major, minor, patch), "grass.sh"
                ]
        else:
            cmdList = [
                "grass76", "grass74", "grass72", "grass70", "grass",
                "grass76.sh", "grass74.sh", "grass72.sh", "grass70.sh",
                "grass.sh"
            ]

        # For MS-Windows there is a difference between GRASS Path and GRASS binary
        if isWindows():
            # If nothing found, use OSGEO4W or QgsPrefix:
            if "OSGEO4W_ROOT" in os.environ:
                testFolder = str(os.environ['OSGEO4W_ROOT'])
            else:
                testFolder = str(QgsApplication.prefixPath())
            testFolder = os.path.join(testFolder, 'bin')
            command = searchFolder(testFolder)
        elif isMac():
            # Search in grassPath
            command = searchFolder(path)

        # If everything has failed, use shutil
        if not command:
            for cmd in cmdList:
                testBin = shutil.which(cmd)
                if testBin:
                    command = os.path.abspath(testBin)
                    break

        if command:
            Grass7Utils.command = command
            if path is '':
                Grass7Utils.path = os.path.dirname(command)

        return command
def check_python_dependencies(plugin_path, iface):
    """ Check for extra python modules which the plugin requires.

    If they are not installed, a windows batch file will be created on the users desktop.
    Run this as administrator to install relevant packages

    Args:
         iface (): The QGIs gui interface required to add messagebar too.
         plugin_path (): The path to the users plugin directory.

    Returns (bool): Passed Check or Not.
    """

    try:
        # comes from metadata.txt
        from qgis.utils import pluginMetadata
        meta_version = '{} {} '.format(pluginMetadata('pat', 'version'),
                                       pluginMetadata('pat', 'update_date'))

        # # get the list of wheels matching the gdal version
        # if not os.environ.get('GDAL_VERSION', None):
        #     gdal_ver = osgeo.gdal.__version__
        #     LOGGER.warning(
        #         'Environment Variable GDAL_VERSION does not exist. Setting to {}'.format(gdal_ver))
        #     os.environ['GDAL_VERSION'] = gdal_ver

        # the name of the install file.
        title = 'Install_PAT3_Extras'
        if platform.system() == 'Windows':
            user_path = os.path.join(os.path.expanduser('~'))
            shortcutPath = os.path.join(user_path, 'Desktop',
                                        title.replace('_', ' ') + '.lnk')
            osgeo_path = os.path.abspath(
                win32api.GetLongPathName(os.environ['OSGEO4W_ROOT']))

            if os.path.exists(shortcutPath):
                # Copy logs into PAT plugin folder
                pass
        else:
            osgeo_path = os.path.abspath(os.environ['OSGEO4W_ROOT'])

        qgis_prefix_path = os.path.abspath(QgsApplication.prefixPath())

        packCheck = {}
        pip_packs = []
        osgeo_packs = []

        # Check for the listed modules.
        for argCheck in ['fiona', 'geopandas', 'rasterio']:
            packCheck[argCheck] = check_package(argCheck)
            if packCheck[argCheck]['Action'] == 'Install':
                osgeo_packs += [argCheck]

        packCheck['pyprecag'] = check_package('pyprecag')
        cur_pyprecag_ver = check_pip_for_update('pyprecag')
        if cur_pyprecag_ver is not None:
            if parse_version(packCheck['pyprecag']['Version']) < parse_version(
                    cur_pyprecag_ver):
                packCheck['pyprecag']['Action'] = 'Upgrade'

        if packCheck['fiona']['Action'] == 'Install':
            message = ''

            if 'ltr' in os.path.basename(QgsApplication.prefixPath()).lower(
            ) and Qgis.QGIS_VERSION_INT < 31011:
                message = 'PAT is no longer supported by QGIS LTR {}\nPlease upgrade to the current QGIS release.'.format(
                    Qgis.QGIS_VERSION)

            elif Qgis.QGIS_VERSION_INT < 31600:
                message = 'PAT is no longer supported by QGIS {}\nPlease upgrade to the current QGIS release.'.format(
                    Qgis.QGIS_VERSION)

            if message != '':
                iface.messageBar().pushMessage(message,
                                               level=Qgis.Critical,
                                               duration=30)
                LOGGER.info(message)
                QMessageBox.critical(None, 'QGIS Upgrade required', message)
                return (message)

        # Install via a tar wheel file prior to publishing via pip to test pyprecag bug fixes
        # otherwise just use a standard pip install.
        local_wheel = glob.glob1(os.path.join(plugin_path, 'install_files'),
                                 "pyprecag*")
        if len(local_wheel) == 1 and 'installed' not in local_wheel[0]:
            packCheck['pyprecag']['Action'] = 'Upgrade'
            pip_packs += [local_wheel[0]]
        elif packCheck['pyprecag']['Action'] in ['Install', 'Upgrade']:
            pip_packs += ['{}'.format('pyprecag')]

        failDependencyCheck = [
            key for key, val in packCheck.items()
            if val['Action'] in ['Install', 'Upgrade']
        ]

        # create a dictionary to use with the template file.
        d = {
            'dependency_log':
            os.path.join(
                plugin_path, 'install_files',
                'dependency_{}.log'.format(date.today().strftime("%Y-%m-%d"))),
            'QGIS_PATH':
            osgeo_path,
            'QGIS_VERSION':
            Qgis.QGIS_VERSION,
            'osgeo_message':
            'Installing {}'.format(', '.join(osgeo_packs)),
            'osgeo_packs':
            '' if len(osgeo_packs) == 0 else '-P python3-' +
            ' -P python3-'.join(osgeo_packs),
            'pip_func':
            'install',
            'pip_packs':
            '' if len(pip_packs) == 0 else ' '.join(pip_packs),
            'py_version':
            struct.calcsize("P") * 8
        }  # this will return 64 or 32

        # 'osgeo_uninst': ' -x python3-'.join(['fiona', 'geopandas', 'rasterio'])
        temp_file = os.path.join(plugin_path, 'util',
                                 'Install_PAT3_Extras.template')
        install_file = os.path.join(
            plugin_path, 'install_files',
            '{}_4_qgis{}.bat'.format(title,
                                     str(Qgis.QGIS_VERSION_INT)[:-2]))
        uninstall_file = os.path.join(
            plugin_path, 'install_files',
            'Un{}_4_qgis{}.bat'.format(title,
                                       str(Qgis.QGIS_VERSION_INT)[:-2]))
        python_version = struct.calcsize("P") * 8  # this will return 64 or 32

        if not os.path.exists(os.path.dirname(install_file)):
            os.mkdir(os.path.dirname(install_file))

        if len(failDependencyCheck) > 0:

            create_file_from_template(temp_file, d, install_file)

            # Create a shortcut on desktop with admin privileges.
            if platform.system() == 'Windows':
                try:
                    aReg = ConnectRegistry(None, HKEY_CURRENT_USER)
                    aKey = OpenKey(
                        aReg,
                        r"Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders"
                    )
                    aVal = os.path.normpath(QueryValueEx(aKey, "Desktop")[0])
                    LOGGER.info(
                        'Desktop path from registry is {}'.format(aVal))
                except:
                    pass

                LOGGER.critical(
                    "Failed Dependency Check. Please run the shortcut {} "
                    "or the following bat file as administrator {}".format(
                        shortcutPath, install_file))

                create_link(shortcutPath, install_file,
                            "Install setup for QGIS PAT Plugin", user_path,
                            True)

            message = 'Installation or updates are required for {}.\n\nPlease quit QGIS and run {} ' \
                      'located on your desktop.'.format(', '.join(failDependencyCheck), title)
            iface.messageBar().pushMessage("ERROR Failed Dependency Check",
                                           message,
                                           level=Qgis.Critical,
                                           duration=0)
            QMessageBox.critical(None, 'Failed Dependency Check', message)
            return (message)
        else:

            settings_version = read_setting(PLUGIN_NAME + "/PAT_VERSION")

            if settings_version is None:
                LOGGER.info('Successfully installed and setup PAT {}'.format(
                    meta_version))
            else:
                if parse_version(meta_version) > parse_version(
                        settings_version):
                    LOGGER.info(
                        'Successfully upgraded and setup PAT from {} to {})'.
                        format(settings_version, meta_version))

                elif parse_version(meta_version) < parse_version(
                        settings_version):
                    LOGGER.info(
                        'Successfully downgraded and setup PAT from {} to {})'.
                        format(settings_version, meta_version))

            write_setting(PLUGIN_NAME + '/PAT_VERSION', meta_version)

            if os.path.exists(shortcutPath):
                os.remove(shortcutPath)

            if len(osgeo_packs) == 0:
                osgeo_packs = ['geopandas', 'rasterio']

            if len(pip_packs) == 0:
                pip_packs = ["pyprecag"]

            d.update({
                'osgeo_message':
                'Installing {}'.format(', '.join(osgeo_packs)),
                'osgeo_packs':
                '' if len(osgeo_packs) == 0 else '-P python3-' +
                ' -P python3-'.join(osgeo_packs),
                'pip_packs':
                '' if len(pip_packs) == 0 else ' '.join(pip_packs),
            })

            # Create master un&install files
            create_file_from_template(temp_file, d, install_file)

            d.update({
                'osgeo_message':
                'Uninstalling {}'.format(', '.join(osgeo_packs)),
                'osgeo_packs':
                '-o -x python3-' + ' -x python3-'.join(osgeo_packs),
                'pip_func':
                'uninstall'
            })

            create_file_from_template(temp_file, d, uninstall_file)

    except Exception as err:
        exc_type, exc_value, exc_traceback = sys.exc_info()
        mess = str(traceback.format_exc())

        setup = get_plugin_state()
        LOGGER.info(setup + '\n\n')
        LOGGER.error('\n' + mess)

        message = (
            'An error occurred during setup, please report this error to [email protected] '
            'and attach the following files \n\t {} \n\t{}'.format(
                get_logger_file(), install_file))

        QMessageBox.critical(None, 'Failed setup', message)

        sys.exit(message + '\n\n' + mess)

    return failDependencyCheck
Example #35
0
    def grassPath():
        """
        Find GRASS path on the operating system.
        Sets global variable Grass7Utils.path
        """
        if Grass7Utils.path is not None:
            return Grass7Utils.path

        if not isWindows() and not isMac():
            return ''

        if isMac():
            folder = ProcessingConfig.getSetting(
                Grass7Utils.GRASS_FOLDER) or ''
            if not os.path.exists(folder):
                folder = None
        else:
            folder = None

        if folder is None:
            # Under MS-Windows, we use OSGEO4W or QGIS Path for folder
            if isWindows():
                if "OSGEO4W_ROOT" in os.environ:
                    testfolder = os.path.join(str(os.environ['OSGEO4W_ROOT']),
                                              "apps")
                else:
                    testfolder = str(QgsApplication.prefixPath())
                testfolder = os.path.join(testfolder, 'grass')
                if os.path.isdir(testfolder):
                    grassfolders = sorted(
                        [
                            f for f in os.listdir(testfolder)
                            if f.startswith("grass-7.")
                            and os.path.isdir(os.path.join(testfolder, f))
                        ],
                        reverse=True,
                        key=lambda x:
                        [int(v) for v in x[len("grass-"):].split('.')])
                    if grassfolders:
                        folder = os.path.join(testfolder, grassfolders[0])
            elif isMac():
                # For MacOSX, we scan some well-known directories
                # Start with QGIS bundle
                for version in ['', '7', '70', '71', '72', '74']:
                    testfolder = os.path.join(str(QgsApplication.prefixPath()),
                                              'grass{}'.format(version))
                    if os.path.isdir(testfolder):
                        folder = testfolder
                        break
                    # If nothing found, try standalone GRASS installation
                    if folder is None:
                        for version in ['0', '1', '2', '4']:
                            testfolder = '/Applications/GRASS-7.{}.app/Contents/MacOS'.format(
                                version)
                            if os.path.isdir(testfolder):
                                folder = testfolder
                                break

        if folder is not None:
            Grass7Utils.path = folder

        return folder or ''
Example #36
0
    if not valid:
        print('Could not even validate the vector file... ')
        print('Printing environment\n')
        for k, v in os.environ.items():
            print('{k}: {v}'.format(k=k, v=v))
        raise Exception("QGIS Python API not functional")
    else:
        nfeat = vl.featureCount()
        print('Feature count?: {nfeat}'.format(nfeat=nfeat))
        assert nfeat == 1


if __name__ == '__main__':
    # Initialize QGIS API -- we shouldn't have to fuss with paths
    app = QgsApplication([], False)
    app.initQgis()
    print('Ran `app.initQgis`')

    try:
        test(fname)
        testPROJ()

    except Exception as e:
        print(e)
        print('QGIS prefixPath(): "{0}"'.format(app.prefixPath()))
        sys.exit(1)

    # Shut down
    app.exitQgis()
    print('Ran `app.exitQgis()`')
Example #37
0
    def runGdal(commands, feedback=None):
        if feedback is None:
            feedback = QgsProcessingFeedback()
        envval = os.getenv('PATH')
        # We need to give some extra hints to get things picked up on OS X
        isDarwin = False
        try:
            isDarwin = platform.system() == 'Darwin'
        except IOError:  # https://travis-ci.org/m-kuhn/QGIS#L1493-L1526
            pass
        if isDarwin and os.path.isfile(
                os.path.join(QgsApplication.prefixPath(), "bin", "gdalinfo")):
            # Looks like there's a bundled gdal. Let's use it.
            os.environ['PATH'] = "{}{}{}".format(
                os.path.join(QgsApplication.prefixPath(), "bin"), os.pathsep,
                envval)
            os.environ['DYLD_LIBRARY_PATH'] = os.path.join(
                QgsApplication.prefixPath(), "lib")
        else:
            # Other platforms should use default gdal finder codepath
            settings = QgsSettings()
            path = settings.value('/GdalTools/gdalPath', '')
            if not path.lower() in envval.lower().split(os.pathsep):
                envval += '{}{}'.format(os.pathsep, path)
                os.putenv('PATH', envval)

        fused_command = ' '.join([str(c) for c in commands])
        QgsMessageLog.logMessage(fused_command, 'Processing',
                                 QgsMessageLog.INFO)
        feedback.pushInfo('GDAL command:')
        feedback.pushCommandInfo(fused_command)
        feedback.pushInfo('GDAL command output:')
        success = False
        retry_count = 0
        while not success:
            loglines = []
            loglines.append('GDAL execution console output')
            try:
                with subprocess.Popen(
                        fused_command,
                        shell=True,
                        stdout=subprocess.PIPE,
                        stdin=subprocess.DEVNULL,
                        stderr=subprocess.STDOUT,
                        universal_newlines=True,
                ) as proc:
                    for line in proc.stdout:
                        feedback.pushConsoleInfo(line)
                        loglines.append(line)
                    success = True
            except IOError as e:
                if retry_count < 5:
                    retry_count += 1
                else:
                    raise IOError(
                        str(e) +
                        u'\nTried 5 times without success. Last iteration stopped after reading {} line(s).\nLast line(s):\n{}'
                        .format(len(loglines), u'\n'.join(loglines[-10:])))

            QgsMessageLog.logMessage('\n'.join(loglines), 'Processing',
                                     QgsMessageLog.INFO)
            GdalUtils.consoleOutput = loglines
Example #38
0
 def grassWinShell():
     folder = ProcessingConfig.getSetting(GrassUtils.GRASS_WIN_SHELL)
     if folder is None:
         folder = os.path.dirname(str(QgsApplication.prefixPath()))
         folder = os.path.join(folder, 'msys')
     return folder
Example #39
0
    def run(self):
        QgsMessageLog.logMessage(f"GeoRectifyTask.run, process: %s" %
                                 self.name,
                                 tag="OAW",
                                 level=Qgis.Info)
        try:
            self.set_status('running', 'started')

            input_tif = os.path.join(self.options["staging_folder"],
                                     self.name + ".tif")
            scripts_folder = os.path.join(QgsApplication.prefixPath(), "..",
                                          "Python37/Scripts")
            geo_rectify = GeoRectifyFactory.create(
                input=input_tif,
                qgis_scripts=scripts_folder,
                min_points=self.options["min_points"],
                gdal_threads=self.options["gdal_threads"])
            geo_rectify.on_progress += self.on_progress
            geo_rectify.process()

            auth_id = self.options["remote_authid"]
            auth_manager = QgsApplication.authManager()
            auth_cfg = QgsAuthMethodConfig()
            auth_manager.loadAuthenticationConfig(auth_id, auth_cfg, True)
            if auth_cfg.id():
                username = auth_cfg.config('username', '')
                password = auth_cfg.config('password', '')
                uri = auth_cfg.uri()
                # call FTP task
                QgsMessageLog.logMessage(f"GeoRectifyTask.run, URI: %s" %
                                         str(uri),
                                         tag="OAW",
                                         level=Qgis.Info)
                QgsMessageLog.logMessage(f"GeoRectifyTask.run, username: %s" %
                                         str(username),
                                         tag="OAW",
                                         level=Qgis.Info)
                QgsMessageLog.logMessage(f"GeoRectifyTask.run, password: %s" %
                                         "***********",
                                         tag="OAW",
                                         level=Qgis.Info)
                # upload file via SFTP
                output_tif = input_tif.replace(".tif", "_grf_fin.tif")
                remote_folder = self.options[
                    "remote_folder"] if "remote_folder" in self.options else "public"
                cnopts = pysftp.CnOpts()
                cnopts.hostkeys = None
                with pysftp.Connection(uri,
                                       username=username,
                                       password=password,
                                       cnopts=cnopts) as sftp:
                    with sftp.cd(remote_folder):
                        sftp.put(output_tif, remotepath=self.name + ".tif")

                # Remove intermediate file (if requested)
                if self.options["remove_file_after"] == Qt.Checked:
                    os.remove(output_tif)
                    QgsMessageLog.logMessage(
                        f"GeoRectifyTask.run, removing intermediate file: %s" %
                        output_tif,
                        tag="OAW",
                        level=Qgis.Info)
            else:
                raise Exception(
                    "Failed to extract information from the QGIS authentication manager using authid: %s"
                    % auth_id)
            self.set_status('completed', 'done')
        except Exception as e:
            self.exception = e
            self.set_status('failed', str(e))
            QgsMessageLog.logMessage(f"GeoRectifyTask.run, exception: %s" %
                                     str(e),
                                     tag="OAW",
                                     level=Qgis.Warning)
        self.handlers["on_completed"](self)
        QgsMessageLog.logMessage(f"GeoRectifyTask.run, result: %s" %
                                 self.status,
                                 tag="OAW",
                                 level=Qgis.Info)
        return self.status == 'completed'