Graphics Reference
In-Depth Information
You will to have to tell your computer where to find the underlying shared libraries. We
will return to this later when we look at writing our own external applications; if you want
to see the details, skip ahead to Chapter 5 , Using QGIS in an External Application .
With the path set, you can now import the various parts of the PyQGIS library that you
want to use, for example:
from qgis.core import *
Now that we have access to the PyQGIS libraries, our next task is to initialize these librar-
ies. As mentioned earlier, we have to tell PyQGIS where to find the various QGIS re-
sources. We do this using the QgsApplication.setPrefixPath() function, like
this:
import os
QgsApplication.setPrefixPath(os.environ['QGIS_PREFIX'],
True)
This uses the QGIS_PREFIX environment variable we set earlier to tell QGIS where to
find its resources. With this done, you can then initialize the PyQGIS library by making
the following call:
QgsApplication.initQgis()
We can now use PyQGIS to do whatever we want in our application. When our program
exits, we also need to inform the PyQGIS library that we are exiting:
QgsApplication.exitQgis()
Putting all this together, our minimal Python application looks like this:
import os
from qgis.core import *
QgsApplication.setPrefixPath(os.environ['QGIS_PREFIX'],
True)
QgsApplication.initQgis()
# ...
Search WWH ::




Custom Search