Book Image

QGIS Python Programming Cookbook, Second Edition - Second Edition

By : Joel Lawhead
Book Image

QGIS Python Programming Cookbook, Second Edition - Second Edition

By: Joel Lawhead

Overview of this book

QGIS is a desktop geographic information system that facilitates data viewing, editing, and analysis. Paired with the most efficient scripting language—Python, we can write effective scripts that extend the core functionality of QGIS. Based on version QGIS 2.18, this book will teach you how to write Python code that works with spatial data to automate geoprocessing tasks in QGIS. It will cover topics such as querying and editing vector data and using raster data. You will also learn to create, edit, and optimize a vector layer for faster queries, reproject a vector layer, reduce the number of vertices in a vector layer without losing critical data, and convert a raster to a vector. Following this, you will work through recipes that will help you compose static maps, create heavily customized maps, and add specialized labels and annotations. As well as this, we’ll also share a few tips and tricks based on different aspects of QGIS.
Table of Contents (16 chapters)
QGIS Python Programming Cookbook - Second Edition
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface

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:

  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.