Geography Reference
In-Depth Information
Think of the Python console as a workbench for trying methods and
using classes in the QGIS API. Once you get that under your belt, you're
ready for some real programming. We'll start out by creating a little
plugin using Python.
A PyQGIS Plugin
Writing plugins in Python is much simpler than using C++. Let's work
up a little plugin that implements something missing from the QGIS
interface. For this exercise, you'll need QGIS 0.9.x, Python, PyQt, and
the Qt developer tools.
Harrison just received the latest Birding Extraordinaire magazine, and
in it he finds an article that describes locations for the exotic Moose-
Finch. 5 The locations are in latitude and longitude, which don't mean
much to Harrison unless he's in his backyard. He fires up QGIS, adds
his layer containing the world boundaries, and begins hunting for the
coordinates. Sure, he can use the coordinate display in the status bar
to eventually find what he wants, but wouldn't it be nice to be able to
just zoom to the coordinates by entering them? Well, that's what our
little plugin will do for us (and Harrison).
Before we get started, we need to learn a little bit about how the plugin
mechanism works. When QGIS starts up, it scans certain directories
looking for both C++ and Python plugins. For a file (shared library,
DLL, or Python script) to be recognized as a plugin, it has to have a
specific signature. For Python scripts, it's pretty simple. Take a look
at your QGIS installation. By platform, here is where you'll find the
Python plugin directory (we'll assume your top-level install directory is
represented by . ):
• Linux and other Unix : ./share/qgis/python/plugins
• Mac OS X : ./Contents/MacOS/share/qgis/python/plugins
• Windows: .\share\qgis\python\plugins
For QGIS to find our Python plugin, we have to place it in a subdirec-
tory of the appropriate plugin directory for our platform. Each plugin
is contained in its own directory. When QGIS starts up, it will scan
each subdirectory in share/qgis/python/plugins and initialize any plugins
it finds. Once that's done, our Python plugin will show up in the QGIS
A mythical creature
5.
 
 
 
Search WWH ::




Custom Search