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

Using the Python's Script Runner plugin

The QGIS Python Script Runner 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 Script Runner plugin using the QGIS plugin manager. Then, run the plugin from the Plugin menu to open the Script Runner dialog. Configure a default editor to edit the scripts using the following steps:

  1. Find the gear icon that represents the Script Runner 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 navigate 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 Script Runner dialog, click on the New Script icon, as shown in the following screenshot:

    How to do it...

  2. Navigate to the directory where you wish to save and name your script.
  3. Verify that the new script is loaded in Script Runner.
  4. Right-click (or control-click on a Mac) on the script name in Script Runner 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 Script Runner plugin.
  8. Verify that the memory layer polygon is added to the QGIS map, as shown in the following screenshot:

    How to do it...

How it works...

Script Runner is a simple but powerful idea. It allows you to build a library of automation scripts and use them from within QGIS 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.

There's more...

The Script Runner plugin makes managing lots of scripts easier. But there is also a script editor built into the PyQGIS console. This editor is a panel in the QGIS interface that you can use to create, edit, and run scripts. You can trigger the editor using the fourth icon from the left on the PyQGIS console toolbar.

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