Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying QGIS Python Programming Cookbook, Second Edition
  • Table Of Contents Toc
  • Feedback & Rating feedback
QGIS Python Programming Cookbook, Second Edition

QGIS Python Programming Cookbook, Second Edition - Second Edition

By : Joel Lawhead
1.5 (2)
close
close
QGIS Python Programming Cookbook, Second Edition

QGIS Python Programming Cookbook, Second Edition

1.5 (2)
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 (10 chapters)
close
close

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.

Visually different images
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
QGIS Python Programming Cookbook, Second Edition
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist download Download options font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon