Geography Reference
In-Depth Information
Let's summarize the process of creating a plugin. First we have a bit
of work to do to get the GUI in order. This includes setting up our
resources file, designing the dialog box, and writing the class needed
to bootstrap it. Then we create the actual plugin code, including the
methods needed to initialize the GUI when the plugin is activated, and
clean up after itself when it is unloaded. Finally, we implement the
run ( ) method where the real work of showing the dialog box, collecting
the input, and zooming the map takes place. While we stretched out the
explanation, there really isn't all that much hand written code involved
in making the plugin. In fact, for the ZoomToPoint plugin there are less
than 80 lines of actual code.
Our plugin is pretty rough. We don't do any error checking—you can
submit blank values for x and y. There are a number of enhancements
you could add to the plugin, including the ability to “remember” the
x, y, and scale values that you used the previous go. If you got really
fancy, you could also figure out how to set a marker at the point after
you zoom. Come to think of it, once you add those features, send them
to me, and I'll include them in the next release of the plugin. Just to
prove it works, you can see the plugin and the values we just entered
in Figure 13.3 , on the next page. Behind it you'll see the map zoomed
to the coordinates we specified. Notice the magnifying glass icon with
the blue dot in the middle on the upper left of the toolbar. That's the
icon we specified for our plugin, and it indeed shows up on the toolbar.
If we were to look in the Plugins menu, we would find an entry for Zoom
to Point as well.
Writing a QGIS plugin in Python is pretty easy. Some plugins won't
require a GUI at all. For example, you might write a plugin that returns
the map coordinates for the point you click the map. Such a plugin
wouldn't require any user input and could use a standard Qt Message-
Box to display the result. You can also write plugins for QGIS in C++,
but that's another story and one I'll let you write. 7
A PyQGIS Application
A stand-alone application is a step beyond a QGIS plugin. In some
ways, they are very similar. We need to create a GUI and use the same
imports. On the other hand, we do not have to write all that code to
7. Actually, you can find information on writing QGIS plugins in C++ on the QGIS wiki
at http://wiki.qgis.org .
 
 
Search WWH ::




Custom Search