3. Your First Step Towards a QGIS Plugin
[qgis
gis
python
]
After making the required preparations, we can start creating the plugin prototype with one of these options:
1. Develop the plugin on your own from scratch
You could start right away on the greenfield with the help of the official QGIS Developer documentation. This path might be a very good idea when you’re planning to do a complex and sophisticated plugin; this will most probably be not your case since you’re reading this article. :)
2. Fork an existing and mature QGIS plugin
I’ve personally forked Thyrsis to ease the pain of defining a solid GitLab pipeline but choose any plugin which is regularly maintained, really. PRO tip: I highly recommend QuickWKT for small-size plugins! QuickWKT uses a simplified project structure which is not covered in this series.
Recommended if you’re familiar with software development; you’ll bite your way through, eventually.
3. Create a plugin skeleton
You can create a plugin base using the QGIS Plugin Builder to skip the necessary boilerplate and get right away into plugin coding. I highly recommend this method if a) you’re new into programming and b) you know nothing about how QGIS plugins work.
This article will guide you through the third option.
Open up QGIS and install the Plugin Builder which itself is a plugin. If you’re not sure how to install plugins in QGIS, refer to the official QGIS documentation
Activate the plugin (look for the “hammer” icon in your QGIS toolbar) and either follow the Plugin Builder documentation or follow the steps I outline:
1. Create the Geoapify plugin. Notice here that you can already set the minimum QGIS version which your plugin will support.
2. Specify how the plugin will be used. I’ve decided to use a tool button with a dock widget where we will show the status bar.
3. Make use and explore all features the Plugin Builder offers if you want to; since I’ve been using a different workflow with my main plugin, I’ve decided to skip them.
4. Finally, tell others where the plugin repository resides. I’ve also marked the plugin as experimental.
Once you’ve created the plugin template, you should have a plugin structure similar to the following:
What do these files do? Let’s dissect the most important ones:
file name | what it does |
---|---|
geoapify_plugin.py | This serves as the starting point for the entire plugin. run serves as the main method which gets executed by QGIS. |
geoapify_plugin_dockwidget_base.ui | User interface which we will later load and edit in QT Designer. If you need to have more components, such as modal windows for forms, it is a common practice to create a separate ui. file for each component. |
resources.qrc | This is an XML containing all external resources. For our purposes, we will only need a single plugin icon which will be put on top of the button in the QGIS toolbar. QT Designer knows how to work with QRC files, too, so you don’t necessarily need to edit this file manually. |
metadata.txt | While you won’t frequently make significant changes to this file, it’s important to know that this file serves as a convenient location to specify the minimum QGIS version supported by the plugin and the version of the plugin itself. |
Next, we will create a setup configuration file named setup.cfg
with the following contents:
This makes sure we’re using a unified code formatting and sets up testing.
Next, we will create a few automation scripts so that the development process is as smooth and least annoying as possible. make was the first choice for me and it worked perfectly for my setup.
The first make target is going install our plugin to the local QGIS plugin repository. In the root folder of your plugin, create a Makefile
with the following content:
Finally, we’ll need to reorganize our project structure a bit so that it is compatible with the qgis-plugin-ci toolkit which handles all the necessary QGIS plugin “plumbing” for us. The only files which will be left at the root folder are README.txt
, setup.cfg
and the Makefile
we’ve just created. The rest of files will go into plugin source directory which should be named exactly like your plugin. In my case, I created qgisapify
as a direct subfolder of the root directory.
In summary, this should be your project structure now:
We’re now all set up! Let’s run make
in the root folder to see what happens:
Notice that the qgis-plugin-ci
toolkit complains that we’re missing a CHANGELOG.md
; we’ll get to what this means later. Other than that, we can see that the ZIP of our plugin was extracted to the QGIS repository which means that our plugin will be available in QGIS! Let’s start QGIS and enable the plugin:
The first Geoapify Integration plugin draft in action.
The next task ahead of us is to implement an HTTP client which will communicate with the Geoapify API. As implied before, this part will be skipped in this series.
Next, we will focus on how to interact with PyQT, which is the core framework for all Python QGIS plugins.