Skip to content
Permalink
Browse files

[BACKPORT] Enable path to pymods in GdalTools settings dialog and aut…

…omatically set default OSX environment (fix #3170)
  • Loading branch information
brushtyler committed Dec 9, 2011
1 parent 81346a4 commit cd7984e128645851606b862309414930ea74da5c
@@ -22,7 +22,7 @@ def name():
def description():
return "Integrate gdal tools into qgis"
def version():
return "Version 1.2.28"
return "Version 1.2.29"
def qgisMinimumVersion():
return "1.0"
def icon():
@@ -16,6 +16,7 @@
from osgeo.gdalconst import *
from osgeo import ogr

import os
# to know the os
import platform

@@ -47,15 +48,25 @@ def setLastUsedDir(filePath):
settings.setValue( "/GdalTools/lastUsedDir", QVariant(dirPath) )

# Retrieves GDAL binaries location
def getGdalPath():
def getGdalBinPath():
settings = QSettings()
return settings.value( "/GdalTools/gdalPath", QVariant( "" ) ).toString()

# Stores GDAL binaries location
def setGdalPath( path ):
def setGdalBinPath( path ):
settings = QSettings()
settings.setValue( "/GdalTools/gdalPath", QVariant( path ) )

# Retrieves GDAL python modules location
def getGdalPymodPath():
settings = QSettings()
return settings.value( "/GdalTools/gdalPymodPath", QVariant( "" ) ).toString()

# Stores GDAL python modules location
def setGdalPymodPath( path ):
settings = QSettings()
settings.setValue( "/GdalTools/gdalPymodPath", QVariant( path ) )

# Retrieves GDAL help files location
def getHelpPath():
settings = QSettings()
@@ -174,7 +185,6 @@ def getRasterFiles(path, recursive=False):
rasters << path + "/" + f

if recursive:
import os
for myRoot, myDirs, myFiles in os.walk( unicode(path) ):
for dir in myDirs:
workDir = QDir( myRoot + "/" + dir )
@@ -762,7 +772,73 @@ def __str__(self):
return ".".join(self.vers)


# setup the default MacOs path
#if platform.system() == "Darwin" and getGdalPath().isEmpty():
# setGdalPath( u"/Library/Frameworks/GDAL.framework/Versions/%s/Programs" % str(GdalConfig.version())[:3] )
def setProcessEnvironment(process):
envvar_list = {
"PATH" : getGdalBinPath(),
"PYTHONPATH" : getGdalPymodPath()
}

sep = os.pathsep

for name, val in envvar_list.iteritems():
if val == None or val == "":
continue

envval = os.getenv(name)
if envval == None or envval == "":
envval = str(val)
elif not QString( envval ).split( sep ).contains( val, Qt.CaseInsensitive ):
envval += "%s%s" % (sep, str(val))
else:
envval = None

if envval != None:
os.putenv( name, envval )

if False: # not needed because os.putenv() has already updated the environment for new child processes
env = QProcess.systemEnvironment()
if env.contains( QRegExp( "^%s=(.*)" % name, Qt.CaseInsensitive ) ):
env.replaceInStrings( QRegExp( "^%s=(.*)" % name, Qt.CaseInsensitive ), "%s=\\1%s%s" % (name, sep, gdalPath) )
else:
env << "%s=%s" % (name, val)
process.setEnvironment( env )


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_bin_path = u"/Library/Frameworks/GDAL.framework/Versions/%s/Programs" % str(GdalConfig.version())[:3]

if os.path.exists( qgis_standalone_gdal_path ): # qgis standalone
# GDAL executables are in the QGis bin folder
if getGdalBinPath().isEmpty():
setGdalBinPath( qgis_bin )
# GDAL pymods are in the QGis python folder
if getGdalPymodPath().isEmpty():
setGdalPymodPath( qgis_python )

elif os.path.exists( gdal_bin_path ):
# GDAL executables are in the GDAL framework Programs folder
if getGdalBinPath().isEmpty():
setGdalBinPath( gdal_bin_path )


# setup the MacOSX path to both GDAL executables and python modules
if platform.system() == "Darwin":
setMacOSXDefaultEnvironment()

@@ -22,21 +22,7 @@ def __init__(self, parent, iface, pluginBase, pluginName, pluginCommand):
self.iface = iface

self.process = QProcess(self)
gdalPath = Utils.getGdalPath()
if not gdalPath.isEmpty():
sep = ";" if platform.system() == "Windows" else ":"
env = self.process.environment()
if env.isEmpty():
# process.enviroment() is probably not supported (MacOS?),
# use os.putenv() instead
path = os.getenv("PATH")
if path != "":
path += sep
path += gdalPath
os.putenv( "PATH", path )
else:
env.replaceInStrings( QRegExp( "^PATH=(.*)", Qt.CaseInsensitive ), "PATH=\\1%s%s" % (sep, gdalPath) )
self.process.setEnvironment( env )
Utils.setProcessEnvironment(self.process)
self.connect(self.process, SIGNAL("error(QProcess::ProcessError)"), self.processError)
self.connect(self.process, SIGNAL("finished(int, QProcess::ExitStatus)"), self.processFinished)

@@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>368</width>
<height>337</height>
<width>371</width>
<height>341</height>
</rect>
</property>
<property name="windowTitle">
@@ -19,7 +19,7 @@
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Path to the GDAL binaries</string>
<string>Path to the GDAL executables</string>
</property>
</widget>
</item>
@@ -52,6 +52,44 @@
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_8">
<item>
<widget class="QLabel" name="label_4">
<property name="text">
<string>Path to the GDAL python modules</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="pymod_tooltip_label">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string notr="true">##tooltip icon##</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
<widget class="QLineEdit" name="leGdalPymodPath"/>
</item>
<item>
<widget class="QPushButton" name="btnSetPymodPath">
<property name="text">
<string>Browse</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_7">
<item>
@@ -152,37 +190,6 @@
</item>
</layout>
</item>
<item>
<widget class="QLabel" name="label_4">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>GDAL pymod path</string>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
<widget class="QLineEdit" name="leGdalPythonPath">
<property name="enabled">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnSetPythonPath">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Browse</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
@@ -18,16 +18,25 @@ def __init__( self, iface ):
self.setupUi( self )

# binaries
self.leGdalBinPath.setText( Utils.getGdalPath() )
self.leGdalBinPath.setText( Utils.getGdalBinPath() )
QObject.connect( self.btnSetBinPath, SIGNAL( "clicked()" ), self.setBinPath )
self.bin_tooltip_label.setPixmap( QPixmap(':/icons/tooltip.png') )
self.bin_tooltip_label.setToolTip( self.tr( \
u"""A list of colon-separated (Linux and MacOS) or
semicolon-separated (Windows) paths to executables.
semicolon-separated (Windows) paths to both binaries
and python executables.
MacOS users usually need to set it to something like
/Library/Frameworks/GDAL.framework/Versions/1.8/Programs""") )

# python modules
self.leGdalPymodPath.setText( Utils.getGdalPymodPath() )
QObject.connect( self.btnSetPymodPath, SIGNAL( "clicked()" ), self.setPymodPath )
self.pymod_tooltip_label.setPixmap( QPixmap(':/icons/tooltip.png') )
self.pymod_tooltip_label.setToolTip( self.tr( \
u"""A list of colon-separated (Linux and MacOS) or
semicolon-separated (Windows) paths to python modules.""") )

# help
self.leGdalHelpPath.setText( Utils.getHelpPath() )
QObject.connect( self.btnSetHelpPath, SIGNAL( "clicked()" ), self.setHelpPath )
@@ -44,6 +53,13 @@ def setBinPath( self ):

self.leGdalBinPath.setText( inputDir )

def setPymodPath( self ):
inputDir = Utils.FileDialog.getExistingDirectory( self, self.tr( "Select directory with GDAL python modules" ) )
if inputDir.isEmpty():
return

self.leGdalPymodPath.setText( inputDir )

def setHelpPath( self ):
inputDir = Utils.FileDialog.getExistingDirectory( self, self.tr( "Select directory with the GDAL documentation" ) )
if inputDir.isEmpty():
@@ -53,6 +69,8 @@ def setHelpPath( self ):


def accept( self ):
Utils.setGdalPath( self.leGdalBinPath.text() )
Utils.setGdalBinPath( self.leGdalBinPath.text() )
Utils.setGdalPymodPath( self.leGdalPymodPath.text() )
Utils.setHelpPath( self.leGdalHelpPath.text() )
QDialog.accept( self )

0 comments on commit cd7984e

Please sign in to comment.
You can’t perform that action at this time.