Graphics Reference
In-Depth Information
Writing an external application
The final way to work with Python and QGIS is to write a completely standalone Python
program that imports the QGIS libraries and works with them directly. In many ways, this
is an ideal way of writing your own custom mapping applications, because your program
doesn't have to run within the existing QGIS user interface. There are, however, a few
things you need to be aware of when you attempt to use Python and QGIS in this way:
1. Your Python program needs to be able to find the QGIS Python libraries before it
can be run. Since these are bundled into the QGIS application itself, you will need
to add the directory where the PyQGIS libraries are installed in your Python path.
2. You also need to tell the PyQGIS libraries where the QGIS application's resources
are stored.
3. As the application is running outside the QGIS application, you won't have access
to the iface variable. You also can't use those parts of the PyQGIS library that
assume you are running inside QGIS.
None of this is too onerous, though it can trip you up the first time you attempt to access
PyQGIS from your external Python code. Let's take a look at how we can avoid these traps
when writing your own Python programs.
Firstly, to allow your program to access the PyQGIS libraries, you need to modify your
Python path (and possibly some other environment variables) before you can import any of
the QGIS packages. For MS Windows, you can do this by running the following in the
command line:
SET OSGEO4W_ROOT=C:\OSGeo4W
SET QGIS_PREFIX=%OSGEO4W_ROOT%\apps\qgis
SET PATH=%PATH%;%QGIS_PREFIX%\bin
SET PYTHONPATH=%QGIS_PREFIX%\python;%PYTHONPATH%
If you are running Mac OS X, the following commands will set up the Python path for you:
export PYTHONPATH="$PYTHONPATH:/Applications/QGIS.app/
Contents/Resources/python"
export DYLD_FRAMEWORK_PATH="/Applications/QGIS.app/Contents/
Frameworks"
export QGIS_PREFIX="/Applications/QGIS.app/Contents/
Resources"
Search WWH ::




Custom Search