Skip to content

Commit cd7984e

Browse files
committedDec 9, 2011
[BACKPORT] Enable path to pymods in GdalTools settings dialog and automatically set default OSX environment (fix #3170)
1 parent 81346a4 commit cd7984e

File tree

5 files changed

+146
-59
lines changed

5 files changed

+146
-59
lines changed
 

‎python/plugins/GdalTools/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def name():
2222
def description():
2323
return "Integrate gdal tools into qgis"
2424
def version():
25-
return "Version 1.2.28"
25+
return "Version 1.2.29"
2626
def qgisMinimumVersion():
2727
return "1.0"
2828
def icon():

‎python/plugins/GdalTools/tools/GdalTools_utils.py

Lines changed: 82 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from osgeo.gdalconst import *
1717
from osgeo import ogr
1818

19+
import os
1920
# to know the os
2021
import platform
2122

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

4950
# Retrieves GDAL binaries location
50-
def getGdalPath():
51+
def getGdalBinPath():
5152
settings = QSettings()
5253
return settings.value( "/GdalTools/gdalPath", QVariant( "" ) ).toString()
5354

5455
# Stores GDAL binaries location
55-
def setGdalPath( path ):
56+
def setGdalBinPath( path ):
5657
settings = QSettings()
5758
settings.setValue( "/GdalTools/gdalPath", QVariant( path ) )
5859

60+
# Retrieves GDAL python modules location
61+
def getGdalPymodPath():
62+
settings = QSettings()
63+
return settings.value( "/GdalTools/gdalPymodPath", QVariant( "" ) ).toString()
64+
65+
# Stores GDAL python modules location
66+
def setGdalPymodPath( path ):
67+
settings = QSettings()
68+
settings.setValue( "/GdalTools/gdalPymodPath", QVariant( path ) )
69+
5970
# Retrieves GDAL help files location
6071
def getHelpPath():
6172
settings = QSettings()
@@ -174,7 +185,6 @@ def getRasterFiles(path, recursive=False):
174185
rasters << path + "/" + f
175186

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

764774

765-
# setup the default MacOs path
766-
#if platform.system() == "Darwin" and getGdalPath().isEmpty():
767-
# setGdalPath( u"/Library/Frameworks/GDAL.framework/Versions/%s/Programs" % str(GdalConfig.version())[:3] )
775+
def setProcessEnvironment(process):
776+
envvar_list = {
777+
"PATH" : getGdalBinPath(),
778+
"PYTHONPATH" : getGdalPymodPath()
779+
}
780+
781+
sep = os.pathsep
782+
783+
for name, val in envvar_list.iteritems():
784+
if val == None or val == "":
785+
continue
786+
787+
envval = os.getenv(name)
788+
if envval == None or envval == "":
789+
envval = str(val)
790+
elif not QString( envval ).split( sep ).contains( val, Qt.CaseInsensitive ):
791+
envval += "%s%s" % (sep, str(val))
792+
else:
793+
envval = None
794+
795+
if envval != None:
796+
os.putenv( name, envval )
797+
798+
if False: # not needed because os.putenv() has already updated the environment for new child processes
799+
env = QProcess.systemEnvironment()
800+
if env.contains( QRegExp( "^%s=(.*)" % name, Qt.CaseInsensitive ) ):
801+
env.replaceInStrings( QRegExp( "^%s=(.*)" % name, Qt.CaseInsensitive ), "%s=\\1%s%s" % (name, sep, gdalPath) )
802+
else:
803+
env << "%s=%s" % (name, val)
804+
process.setEnvironment( env )
805+
806+
807+
def setMacOSXDefaultEnvironment():
808+
# fix bug #3170: many GDAL Tools don't work in OS X standalone
809+
810+
if platform.system() != "Darwin":
811+
return
812+
813+
# QgsApplication.prefixPath() contains the path to qgis executable (i.e. .../Qgis.app/MacOS)
814+
# get the path to Qgis application folder
815+
qgis_app = u"%s/.." % QgsApplication.prefixPath()
816+
qgis_app = QDir( qgis_app ).absolutePath()
817+
818+
qgis_bin = u"%s/bin" % QgsApplication.prefixPath() # path to QGis bin folder
819+
qgis_python = u"%s/Resources/python" % qgis_app # path to QGis python folder
820+
821+
# path to the GDAL framework within the Qgis application folder (QGis standalone only)
822+
qgis_standalone_gdal_path = u"%s/Frameworks/GDAL.framework" % qgis_app
823+
824+
# path to the GDAL framework when installed as external framework
825+
gdal_bin_path = u"/Library/Frameworks/GDAL.framework/Versions/%s/Programs" % str(GdalConfig.version())[:3]
826+
827+
if os.path.exists( qgis_standalone_gdal_path ): # qgis standalone
828+
# GDAL executables are in the QGis bin folder
829+
if getGdalBinPath().isEmpty():
830+
setGdalBinPath( qgis_bin )
831+
# GDAL pymods are in the QGis python folder
832+
if getGdalPymodPath().isEmpty():
833+
setGdalPymodPath( qgis_python )
834+
835+
elif os.path.exists( gdal_bin_path ):
836+
# GDAL executables are in the GDAL framework Programs folder
837+
if getGdalBinPath().isEmpty():
838+
setGdalBinPath( gdal_bin_path )
839+
840+
841+
# setup the MacOSX path to both GDAL executables and python modules
842+
if platform.system() == "Darwin":
843+
setMacOSXDefaultEnvironment()
768844

‎python/plugins/GdalTools/tools/dialogBase.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,7 @@ def __init__(self, parent, iface, pluginBase, pluginName, pluginCommand):
2222
self.iface = iface
2323

2424
self.process = QProcess(self)
25-
gdalPath = Utils.getGdalPath()
26-
if not gdalPath.isEmpty():
27-
sep = ";" if platform.system() == "Windows" else ":"
28-
env = self.process.environment()
29-
if env.isEmpty():
30-
# process.enviroment() is probably not supported (MacOS?),
31-
# use os.putenv() instead
32-
path = os.getenv("PATH")
33-
if path != "":
34-
path += sep
35-
path += gdalPath
36-
os.putenv( "PATH", path )
37-
else:
38-
env.replaceInStrings( QRegExp( "^PATH=(.*)", Qt.CaseInsensitive ), "PATH=\\1%s%s" % (sep, gdalPath) )
39-
self.process.setEnvironment( env )
25+
Utils.setProcessEnvironment(self.process)
4026
self.connect(self.process, SIGNAL("error(QProcess::ProcessError)"), self.processError)
4127
self.connect(self.process, SIGNAL("finished(int, QProcess::ExitStatus)"), self.processFinished)
4228

‎python/plugins/GdalTools/tools/dialogSettings.ui

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
<rect>
77
<x>0</x>
88
<y>0</y>
9-
<width>368</width>
10-
<height>337</height>
9+
<width>371</width>
10+
<height>341</height>
1111
</rect>
1212
</property>
1313
<property name="windowTitle">
@@ -19,7 +19,7 @@
1919
<item>
2020
<widget class="QLabel" name="label">
2121
<property name="text">
22-
<string>Path to the GDAL binaries</string>
22+
<string>Path to the GDAL executables</string>
2323
</property>
2424
</widget>
2525
</item>
@@ -52,6 +52,44 @@
5252
</item>
5353
</layout>
5454
</item>
55+
<item>
56+
<layout class="QHBoxLayout" name="horizontalLayout_8">
57+
<item>
58+
<widget class="QLabel" name="label_4">
59+
<property name="text">
60+
<string>Path to the GDAL python modules</string>
61+
</property>
62+
</widget>
63+
</item>
64+
<item>
65+
<widget class="QLabel" name="pymod_tooltip_label">
66+
<property name="sizePolicy">
67+
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
68+
<horstretch>0</horstretch>
69+
<verstretch>0</verstretch>
70+
</sizepolicy>
71+
</property>
72+
<property name="text">
73+
<string notr="true">##tooltip icon##</string>
74+
</property>
75+
</widget>
76+
</item>
77+
</layout>
78+
</item>
79+
<item>
80+
<layout class="QHBoxLayout" name="horizontalLayout_4">
81+
<item>
82+
<widget class="QLineEdit" name="leGdalPymodPath"/>
83+
</item>
84+
<item>
85+
<widget class="QPushButton" name="btnSetPymodPath">
86+
<property name="text">
87+
<string>Browse</string>
88+
</property>
89+
</widget>
90+
</item>
91+
</layout>
92+
</item>
5593
<item>
5694
<layout class="QHBoxLayout" name="horizontalLayout_7">
5795
<item>
@@ -152,37 +190,6 @@
152190
</item>
153191
</layout>
154192
</item>
155-
<item>
156-
<widget class="QLabel" name="label_4">
157-
<property name="enabled">
158-
<bool>false</bool>
159-
</property>
160-
<property name="text">
161-
<string>GDAL pymod path</string>
162-
</property>
163-
</widget>
164-
</item>
165-
<item>
166-
<layout class="QHBoxLayout" name="horizontalLayout_4">
167-
<item>
168-
<widget class="QLineEdit" name="leGdalPythonPath">
169-
<property name="enabled">
170-
<bool>false</bool>
171-
</property>
172-
</widget>
173-
</item>
174-
<item>
175-
<widget class="QPushButton" name="btnSetPythonPath">
176-
<property name="enabled">
177-
<bool>false</bool>
178-
</property>
179-
<property name="text">
180-
<string>Browse</string>
181-
</property>
182-
</widget>
183-
</item>
184-
</layout>
185-
</item>
186193
<item>
187194
<widget class="QDialogButtonBox" name="buttonBox">
188195
<property name="orientation">

‎python/plugins/GdalTools/tools/doSettings.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,25 @@ def __init__( self, iface ):
1818
self.setupUi( self )
1919

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

32+
# python modules
33+
self.leGdalPymodPath.setText( Utils.getGdalPymodPath() )
34+
QObject.connect( self.btnSetPymodPath, SIGNAL( "clicked()" ), self.setPymodPath )
35+
self.pymod_tooltip_label.setPixmap( QPixmap(':/icons/tooltip.png') )
36+
self.pymod_tooltip_label.setToolTip( self.tr( \
37+
u"""A list of colon-separated (Linux and MacOS) or
38+
semicolon-separated (Windows) paths to python modules.""") )
39+
3140
# help
3241
self.leGdalHelpPath.setText( Utils.getHelpPath() )
3342
QObject.connect( self.btnSetHelpPath, SIGNAL( "clicked()" ), self.setHelpPath )
@@ -44,6 +53,13 @@ def setBinPath( self ):
4453

4554
self.leGdalBinPath.setText( inputDir )
4655

56+
def setPymodPath( self ):
57+
inputDir = Utils.FileDialog.getExistingDirectory( self, self.tr( "Select directory with GDAL python modules" ) )
58+
if inputDir.isEmpty():
59+
return
60+
61+
self.leGdalPymodPath.setText( inputDir )
62+
4763
def setHelpPath( self ):
4864
inputDir = Utils.FileDialog.getExistingDirectory( self, self.tr( "Select directory with the GDAL documentation" ) )
4965
if inputDir.isEmpty():
@@ -53,6 +69,8 @@ def setHelpPath( self ):
5369

5470

5571
def accept( self ):
56-
Utils.setGdalPath( self.leGdalBinPath.text() )
72+
Utils.setGdalBinPath( self.leGdalBinPath.text() )
73+
Utils.setGdalPymodPath( self.leGdalPymodPath.text() )
5774
Utils.setHelpPath( self.leGdalHelpPath.text() )
5875
QDialog.accept( self )
76+

0 commit comments

Comments
 (0)
Please sign in to comment.