Difference between revisions of "Creating a QGIS Plugin"

From CUOSGwiki
Jump to navigationJump to search
Line 61: Line 61:
 
The compile.bat file created [[#Python Bindings|earlier]] can now be relocated to the plugin folder. To find the plugin folder for your QGIS user profile, first open QGIS and then select '''Settings > User Profiles > ''default (may vary for you)'' > Open Active Profile Folder'''. A new window will then open and this will be profile folder. This can be seen below in '''Figures 4 & 5'''.
 
The compile.bat file created [[#Python Bindings|earlier]] can now be relocated to the plugin folder. To find the plugin folder for your QGIS user profile, first open QGIS and then select '''Settings > User Profiles > ''default (may vary for you)'' > Open Active Profile Folder'''. A new window will then open and this will be profile folder. This can be seen below in '''Figures 4 & 5'''.
   
[[File:MW5.png|500px]]
+
[[File:MW5.png]]
  +
[[File:MW4.png|500px]]
+
[[File:MW4.png]]
   
 
== Installing the New Plugin ==
 
== Installing the New Plugin ==

Revision as of 19:31, 4 December 2019

Disclaimer

This tutorial was created for Microsoft Windows platforms. It assumes a basic knowledge of GIS Environments and Python syntax.

Introduction

This tutorial contains instructions to aid you in the creation of your own QGIS plugins.

Data

As most of this tutorial is software based, the only data required is a shapefile layer for testing purposes at the end. One can find the shapefile used for testing here.

Be sure to keep all the downloaded files together,in the same project folder. Keep the folder pathway/filenames simple and clear.

      •	Ex.C:\Users\Larry\QGIS\Projects

Software Installation

Installing QGIS

The latest version of QGIS is good for this tutorial, it can either be the standalone QGIS version or OSGeo4W installer (both come equipped with some form of Qt creator). For QGIS installation refer to here.

Installing VS Code

Any kind of coding requires a text editor or IDE. If you have a preference then use it, but in this tutorial we will be doing things in VS Code. See here for installation.

Installing QGIS Plugins

Once QGIS is installed and open, go to the Plugins > Manage and Install Plugins menu and install: Plugin Reloader and Plugin Builder.

Plugin Builder: A useful plugin that creates all the files and boilerplate code required to get going with a plugin.

Plugin Reloader: A useful plugin that aids in testing and changing plugin code, without having to restart QGIS every time.

MW1.png Figure 1: QGIS plugins used.

Python Bindings

The plugin needs to be able to access the relevant Python bindings from the plugin folder, so in order to do this the path to the QGIS install must be indicated. To do this a 'Windows Batch File' (.bat) will need to be created with the following text (this can be done with notepad and the "save as" function):

@echo off
call "C:\OSGeo4W64\bin\o4w_env.bat"
call "C:\OSGeo4W64\bin\qt5_env.bat"
call "C:\OSGeo4W64\bin\py3_env.bat"

@echo on
pyrcc5 -o resources.py resources.qrc

Save this file to your project folder and name it compile.bat, but if you installed QGIS to a different path, replace C:\OSGeo4W64\bin\ with the location of your install.

Building the Basics

Now that all the installs have been completed, the basic plugin framework can be created. Open 'Plugin Builder' from the toolbar, Plugins > Plugin Builder > Plugin Builder and go through the dialog forms, as seen in Figures 2-3.

MW2 1.png Figure 2: Plugin builder input forms.

Be sure to fill the form with details pertaining to your plugin. The Class name will be the name of the Python Class/file containing the plugin instructions.

MW3.png Figure 3: Plugin builder final dialog prompt.

At the end of the dialogs you may receive a pyrcc5 error, but you can just ignore this.

Relocating the .bat

The compile.bat file created earlier can now be relocated to the plugin folder. To find the plugin folder for your QGIS user profile, first open QGIS and then select Settings > User Profiles > default (may vary for you) > Open Active Profile Folder. A new window will then open and this will be profile folder. This can be seen below in Figures 4 & 5.

MW5.png

MW4.png

Installing the New Plugin

Create the Plugin UI

Adding the Code

Testing the New Plugin

References