Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
QGIS Python Programming Cookbook, Second Edition

You're reading from   QGIS Python Programming Cookbook, Second Edition Automating geospatial development

Arrow left icon
Product type Paperback
Published in Mar 2017
Publisher Packt
ISBN-13 9781787124837
Length 464 pages
Edition 2nd Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Joel Lawhead Joel Lawhead
Author Profile Icon Joel Lawhead
Joel Lawhead
Arrow right icon
View More author details
Toc

Table of Contents (10) Chapters Close

Preface 1. Automating QGIS FREE CHAPTER 2. Querying Vector Data 3. Editing Vector Data 4. Using Raster Data 5. Creating Dynamic Maps 6. Composing Static Maps 7. Interacting with the User 8. QGIS Workflows 9. Other Tips and Tricks

Creating a Processing Toolbox plugin

The QGIS Processing Toolbox provides a powerful set of algorithms for QGIS Python programming, which we'll see throughout this book. You add your own scripts to the toolbox, and with the latest version of QGIS, you can now turn those scripts into plugins. You can install these plugins like any other QGIS plugin using Plugin Manager and then have those scripts appear in Processing Toolbox. This process is significantly easier than writing a traditional plugin from scratch.

Getting ready

First you need a script to package as a plugin. You can package multiple scripts into a single plugin, but to keep things simple, use one. This sample script will save the current map view as an image. Create the script using the following steps:

  1. In Processing Toolbox, expand the Scripts tree.
  2. Double-click on the Create new script tool.
  3. In the script editor, add the following code, specifying an output directory for your map images:
            from qgis.utils import iface 
            import datetime 
            c = iface.mapCanvas() 
            t = '{:%Y%m%d%H%M%S}'.format(datetime.datetime.now()) 
            img_path = '<output directory>/map{}.png' 
            c.saveAsImage(img_path.format(t), None, 'PNG') 
    
  4. Click on the Save As icon to save the image in your scripts directory as MapImage.py.
  5. The script will then appear in the User Scripts tree under Scripts in Processing Toolbox.
  6. Set up a map view and then double-click on the script to verify that an image has been created in the output directory.

How to do it...

Now you are ready to create the plugin from your sample script:

  1. Under the script tools section in Processing Toolbox, double-click on Create script collection plugin.
  2. Fill out the metadata fields in the dialog.
  3. Ensure the MapImage script is checked in the Script selector dialog.
  4. When you navigate to the output folder, create a new folder called MapImage:

    How to do it...

  5. Click on the OK button
  6. Navigate to the output folder and verify the plugin files were created.

How it works...

From a functional perspective, all the script does is copy the files to your user scripts directory. But the plugin packaging allows you to distribute your work to other users using the QGIS plugin framework.

There's more...

The Processing Toolbox has a script manager similar to the QGIS plugin manager, in which you can download scripts written by other users for processing, or as examples to write your own scripts. To access this script manager, expand the Scripts menu in the Processing Toolbox, then expand the Tools menu, and finally double-click Get scripts from on-line scripts collection. From there, you can browse a list of downloadable scripts.

You have been reading a chapter from
QGIS Python Programming Cookbook, Second Edition - Second Edition
Published in: Mar 2017
Publisher: Packt
ISBN-13: 9781787124837
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime