Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
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

You're reading from   QGIS Python Programming Cookbook Over 140 recipes to help you turn QGIS from a desktop GIS tool into a powerful automated geospatial framework

Arrow left icon
Product type Paperback
Published in Mar 2015
Publisher
ISBN-13 9781783984985
Length 340 pages
Edition 1st 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 (11) 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 Index

Using the Python ScriptRunner plugin

The QGIS Python ScriptRunner plugin provides a middle ground for QGIS automation, between the interactive console and the overhead of plugins. It provides a script management dialog that allows you to easily load, create, edit, and run scripts for large-scale QGIS automation.

Getting ready

Install the ScriptRunner plugin using the QGIS plugin manager. Then, run the plugin from the Plugin menu to open the ScriptRunner dialog. Configure a default editor to edit scripts using the following steps:

  1. Find the gear icon that represents the ScriptRunner Preferences settings dialog box and click on it.
  2. In the General Options section, check the Edit Scripts Using: checkbox.
  3. Click on the button to browse to the location of a text editor on your system.
  4. Click on the Open button.
  5. Click on the OK button in the Preferences dialog.

How to do it…

  1. In the ScriptRunner dialog, click on the New Script icon, as shown in the following screenshot:
    How to do it…
  2. Browse to the directory where you can save your script, name the script, and save it.
  3. Verify that the new script is loaded in ScriptRunner.
  4. Right-click (or control-click on a Mac) on the script name in ScriptRunner and select Edit Script in External Editor.
  5. In the editor, replace the template code with the following code:
    from PyQt4.QtCore import *
    from PyQt4.QtGui import *
    from qgis.core import *
    from qgis.gui import *
    
    def run_script(iface):
        layer =  QgsVectorLayer('Polygon?crs=epsg:4326', 'Mississippi' , "memory")
    pr = layer.dataProvider()
        poly = QgsFeature()
        geom = QgsGeometry.fromWkt("POLYGON ((-88.82 34.99,-88.09 34.89,-88.39 30.34,-89.57 30.18,-89.73 31,-91.63 30.99,-90.87 32.37,-91.23 33.44,-90.93 34.23,-90.30 34.99,-88.82 34.99))")
        poly.setGeometry(geom)
        pr.addFeatures([poly])
        layer.updateExtents()
    QgsMapLayerRegistry.instance().addMapLayers([layer])
  6. Click on the Run Script icon, which is represented by a green-colored arrow.
  7. Close the ScriptRunner plugin.
  8. Verify that the memory layer polygon was added to the QGIS map, as shown in the following screenshot:
    How to do it…

How it works…

ScriptRunner is a simple but powerful idea. It allows you to build a library of automation scripts and use them from within QGIS, but without the overhead of building a plugin or a standalone application. All the Python and system path variables are set correctly and inherited from QGIS; however, you must still import the QGIS and Qt libraries.

You have been reading a chapter from
QGIS Python Programming Cookbook
Published in: Mar 2015
Publisher:
ISBN-13: 9781783984985
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 $19.99/month. Cancel anytime
Banner background image