QGIS Python Programming Cookbook - Second Edition
By Lawhead Joel
()
About this ebook
- Delve into the undocumented features of the QGIS API
- Get a set of user-friendly recipes that can automate entire geospatial workflows by connecting Python GIS building blocks into comprehensive processes
- This book has a complete code upgrade to QGIS 2.18 and 30 new, valuable recipes
This book is for geospatial analysts who want to learn more about automating everyday GIS tasks as well as programmers responsible for building GIS applications. The short, reusable recipes make concepts easy to understand and combine so you can build larger applications that are easy to maintain.
Read more from Lawhead Joel
Learning Geospatial Analysis with Python Rating: 5 out of 5 stars5/5Learning Geospatial Analysis with Python - Second Edition Rating: 0 out of 5 stars0 ratingsQGIS Python Programming Cookbook Rating: 0 out of 5 stars0 ratings
Related to QGIS Python Programming Cookbook - Second Edition
Related ebooks
Python Geospatial Analysis Cookbook Rating: 0 out of 5 stars0 ratingsProgramming ArcGIS 10.1 with Python Cookbook Rating: 4 out of 5 stars4/5Programming ArcGIS with Python Cookbook - Second Edition Rating: 4 out of 5 stars4/5PostGIS Cookbook Rating: 0 out of 5 stars0 ratingsOpenLayers 3.x Cookbook - Second Edition Rating: 0 out of 5 stars0 ratingsWeb Development with Django Cookbook - Second Edition Rating: 0 out of 5 stars0 ratingsData Visualization with D3.js Cookbook Rating: 0 out of 5 stars0 ratingsArcGIS for Desktop Cookbook Rating: 5 out of 5 stars5/5QGIS 2 Cookbook Rating: 5 out of 5 stars5/5matplotlib Plotting Cookbook Rating: 5 out of 5 stars5/5Windows Application Development Cookbook Rating: 0 out of 5 stars0 ratingsClojure Programming Cookbook Rating: 0 out of 5 stars0 ratingsNeo4j Cookbook Rating: 0 out of 5 stars0 ratingsPython Data Visualization Cookbook - Second Edition Rating: 0 out of 5 stars0 ratingsMastering PostGIS Rating: 0 out of 5 stars0 ratingsQGIS By Example Rating: 3 out of 5 stars3/5Practical GIS Rating: 0 out of 5 stars0 ratingsBuilding Mapping Applications with QGIS Rating: 0 out of 5 stars0 ratingsMastering QGIS Rating: 4 out of 5 stars4/5Python Geospatial Development - Third Edition Rating: 4 out of 5 stars4/5Python Geospatial Analysis Essentials Rating: 0 out of 5 stars0 ratingsArcPy and ArcGIS - Second Edition Rating: 5 out of 5 stars5/5Geospatial Development By Example with Python Rating: 5 out of 5 stars5/5Learning R for Geospatial Analysis Rating: 0 out of 5 stars0 ratingsLearning QGIS - Third Edition Rating: 4 out of 5 stars4/5Learning ArcGIS Geodatabases Rating: 5 out of 5 stars5/5Leaflet.js Essentials Rating: 4 out of 5 stars4/5Mastering QGIS - Second Edition Rating: 0 out of 5 stars0 ratingsGeoprocessing with Python Rating: 0 out of 5 stars0 ratingsBuilding Web Applications with ArcGIS Rating: 5 out of 5 stars5/5
Software Development & Engineering For You
Hand Lettering on the iPad with Procreate: Ideas and Lessons for Modern and Vintage Lettering Rating: 4 out of 5 stars4/5Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5Flow: A Handbook for Change-Makers, Mavericks, Innovators and Leaders Rating: 0 out of 5 stars0 ratingsPython For Dummies Rating: 4 out of 5 stars4/5PYTHON: Practical Python Programming For Beginners & Experts With Hands-on Project Rating: 5 out of 5 stars5/5Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5Level Up! The Guide to Great Video Game Design Rating: 4 out of 5 stars4/5How to Write Effective Emails at Work Rating: 4 out of 5 stars4/5Android App Development For Dummies Rating: 0 out of 5 stars0 ratingsSQL For Dummies Rating: 0 out of 5 stars0 ratingsModern C++ for Absolute Beginners: A Friendly Introduction to C++ Programming Language and C++11 to C++20 Standards Rating: 0 out of 5 stars0 ratingsOneNote: The Ultimate Guide on How to Use Microsoft OneNote for Getting Things Done Rating: 1 out of 5 stars1/5Lua Game Development Cookbook Rating: 0 out of 5 stars0 ratingsiOS App Development For Dummies Rating: 0 out of 5 stars0 ratingsSalesforce Certification: Earn Salesforce certifications and increase online sales real and unique practice tests included Kindle Rating: 0 out of 5 stars0 ratingsiPhone Application Development For Dummies Rating: 4 out of 5 stars4/5How Do I Do That In InDesign? Rating: 5 out of 5 stars5/5The Inmates Are Running the Asylum (Review and Analysis of Cooper's Book) Rating: 4 out of 5 stars4/5Git Essentials Rating: 4 out of 5 stars4/5How Do I Do That in Photoshop?: The Quickest Ways to Do the Things You Want to Do, Right Now! Rating: 4 out of 5 stars4/527 PROGRAM MANAGEMENT INTERVIEW TECHNIQUES - To Ace That Dream Job Offer ! Rating: 5 out of 5 stars5/5The Essential Persona Lifecycle: Your Guide to Building and Using Personas Rating: 4 out of 5 stars4/5How to Start a Business Analyst Career Rating: 5 out of 5 stars5/5Tiny Python Projects: Learn coding and testing with puzzles and games Rating: 5 out of 5 stars5/5DevOps For Dummies Rating: 4 out of 5 stars4/5Beginning C++ Programming Rating: 3 out of 5 stars3/5Learning Python Rating: 5 out of 5 stars5/5
Reviews for QGIS Python Programming Cookbook - Second Edition
0 ratings0 reviews
Book preview
QGIS Python Programming Cookbook - Second Edition - Lawhead Joel
Table of Contents
QGIS Python Programming Cookbook - Second Edition
Credits
About the Author
About the Reviewer
www.PacktPub.com
Why subscribe?
Customer Feedback
Preface
What this book covers
What you need for this book
Who this book is for
Sections
Getting ready
How to do it…
How it works…
There's more…
See also
Conventions
Reader feedback
Customer support
Downloading the example code
Downloading the color images of this book
Errata
Piracy
Questions
1. Automating QGIS
Introduction
Installing QGIS 2.18 for development
Getting ready
How to do it...
Installing PyQGIS using the Debian package manager
Installing PyQGIS using the RPM package manager
Setting the environment variables
Setting the environment variables on Windows
Setting environment variables on Linux
How it works...
There's more...
Finding the PyQGIS path on Windows
Finding the location of the QGIS Python installation on other platforms
Using the QGIS Python console for interactive control
How to do it...
How it works...
Automatically starting the Python console
Getting ready
How to do it...
How it works...
Using the Python's Script Runner plugin
Getting ready
How to do it...
How it works...
There's more...
Setting up your QGIS IDE
Getting ready
How to do it...
Adding the Python interpreter to Windows
Adding the PyQGIS module paths to the interpreter
Adding the PyQGIS API to the IDE
Adding environment variables
How it works...
Debugging QGIS Python scripts
How to do it...
Configuring QGIS
Configuring Eclipse
Testing the debugger
How it works...
Navigating the PyQGIS API
Getting ready
How to do it...
How it works...
There's more...
Creating a traditional QGIS plugin
Getting ready
How to do it...
How it works...
There's more...
Creating a Processing Toolbox plugin
Getting ready
How to do it...
How it works...
There's more...
Distributing a plugin
Getting ready
How to do it...
How it works...
Building a standalone application
Getting ready
How to do it...
How it works...
There's more...
Storing and reading global preferences
Getting ready
How to do it...
How it works...
There's more...
Storing and reading project preferences
Getting ready
How to do it...
How it works...
There's more...
Accessing the script path from within your script
Getting ready
How to do it...
How it works...
There's more...
2. Querying Vector Data
Introduction
Loading a vector layer from a file sample
Getting ready
How to do it...
How it works...
Loading a vector layer from a geodatabase
Getting ready
How to do it...
How it works...
Examining vector layer features
Getting ready
How to do it...
How it works...
Examining vector layer attributes
Getting ready
How to do it...
How it works...
There's more...
Filtering a layer by geometry
Getting ready
How to do it...
How it works...
Filtering a layer by attributes
Getting ready
How to do it...
How it works...
Buffering a feature
Getting ready
How to do it...
How it works...
Measuring the distance between two points
Getting ready
How to do it...
How it works...
Measuring distance along a line
Getting ready
How to do it...
How it works...
Calculating the area of a polygon
Getting ready
How to do it...
How it works...
Creating a spatial index
Getting ready
How to do it...
How it works...
Calculating the bearing of a line
Getting ready
How to do it...
How it works...
Loading data from a spreadsheet
Getting ready
How to do it...
How it works...
There's more...
Accessing layer metadata
Getting ready
How to do it...
How it works...
3. Editing Vector Data
Introduction
Creating a vector layer in memory
Getting ready
How to do it...
How it works...
Adding a point feature to a vector layer
Getting ready
How to do it...
How it works...
There's more...
Adding a line feature to a vector layer
Getting ready
How to do it...
How it works...
Adding a polygon feature to a vector layer
Getting ready
How to do it...
How it works...
Adding a set of attributes to a vector layer
Getting ready...
How to do it...
How it works...
Adding a field to a vector layer
Getting ready
How to do it...
How it works...
Joining a shapefile attribute table to a CSV file
Getting ready
How to do it...
How it works...
There's more...
Changing vector layer geometry
Getting ready
How to do it...
How it works...
Changing a vector layer feature's attribute
Getting ready
How to do it...
How it works...
Removing data from a vector layer
Getting ready
How to do it....
How it works...
Deleting a vector layer feature's attribute
Getting ready
How to do it...
How it works...
Reprojecting a vector layer
Getting ready
How to do it...
How it works...
Converting a shapefile to KML or GeoJSON
Getting ready
How to do it...
How it works...
There's more...
Merging shapefiles
Getting ready
How to do it...
How it works...
There's more...
Splitting a shapefile
Getting ready
How to do it...
How it works...
Generalizing a vector layer
Getting ready
How to do it...
How it works...
Dissolving vector features
Getting ready
How to do it...
How it works...
Performing a union on vector shapes
Getting ready
How to do it...
How it works...
Rasterizing a vector layer
Getting ready
How to do it...
How it works...
Exporting a layer to the GeoPackage format
Getting ready
How to do it...
How it works...
4. Using Raster Data
Introduction
Loading a raster layer
Getting ready
How to do it...
How it works...
Loading a NetCDF file
Getting ready
How to do it...
How it works...
Getting the cell size of a raster layer
Getting ready
How to do it...
How it works...
Obtaining the width and height of a raster
Getting ready
How to do it...
How it works...
Counting raster bands
Getting ready
How to do it...
How it works...
Swapping raster bands
Getting ready
How to do it...
How it works...
There's more...
Querying the value of a raster at a specified point
Getting ready
How to do it...
How it works...
Reprojecting a raster
Getting ready
How to do it...
How it works...
There's more...
Creating an elevation hillshade
Getting ready
How to do it...
How it works...
There's more...
Creating vector contours from elevation data
Getting ready
How to do it...
How it works...
Sampling a raster dataset using a regular grid
Getting ready
How to do it...
How it works...
There's more...
Adding elevation data to a line vertices using a digital elevation model
Getting ready
How to do it...
How it works...
There's more...
Creating a common extent for rasters
Getting ready
How to do it...
How it works...
Resampling raster resolution
Getting ready
How to do it...
How it works...
Counting the unique values in a raster
Getting ready
How to do it...
How it works...
Mosaicing rasters
Getting ready
How to do it...
How it works...
Converting a TIFF image to a JPEG image
Getting ready
How to do it...
How it works...
Creating pyramids for a raster
Getting ready
How to do it...
How it works...
Converting a pixel location to a map coordinate
Getting ready
How to do it...
How it works...
Converting a map coordinate to a pixel location
Getting ready
How to do it...
How it works...
Creating a KML image overlay for a raster
Getting ready
How to do it...
How it works...
There's more...
Classifying a raster
Getting ready
How to do it...
How it works...
Converting a raster to a vector
Getting ready
How to do it...
How it works...
Georeferencing a raster from control points
Getting ready
How to do it...
How it works...
Clipping a raster using a shapefile
Getting ready
How to do it...
How it works...
5. Creating Dynamic Maps
Introduction
Accessing the map canvas
Getting ready
How to do it...
How it works...
Changing the map units
Getting ready
How to do it...
How it works...
Iterating over layers
Getting ready
How to do it...
How it works...
Symbolizing a vector layer
Getting ready
How to do it...
How it works...
Setting a transparent layer fill
Getting ready
How to do it...
How it works...
Using a filled marker symbol
Getting ready
How to do it...
Rendering a single band raster using a color ramp algorithm
Getting ready
How to do it...
How it works...
Setting a feature's color using a column in a CSV file
Getting ready
How to do it...
How it works...
Creating a complex vector layer symbol
Getting ready
How to do it...
How it works...
Using icons as vector layer symbols
Getting ready
How to do it...
How it works...
Using an outline for font markers
Getting ready
How to do it...
How it works...
Using arrow symbols
Getting ready
How to do it...
How it works...
Creating a graduated vector layer symbol renderer
Getting ready
How to do it...
How it works...
Creating a categorized vector layer symbol
Getting ready
How to do it...
How it works...
Using live layer effects
Getting ready
How to do it...
How it works...
Creating inverted polygon shapeburst fills
Getting ready
How to do it...
How it works...
Creating a map bookmark
Getting ready
How to do it...
How it works...
Navigating to a map bookmark
Getting ready
How to do it...
How it works...
Setting scale-based visibility for a layer
Getting ready
How to do it...
How it works...
Using SVG for layer symbols
Getting ready
How to do it...
How it works...
Using pie charts for symbols
Getting ready
How to do it...
How it works...
There's more...
Using the 2.5D renderer
Getting ready
How to do it...
How it works...
Using XYZ tiled map services
Getting ready
How to do it...
How it works...
Using Esri ArcGIS map services
Getting Ready
How to do it...
How it works...
Labeling a feature
Getting ready
How to do it...
How it works...
Using null symbols
Getting ready
How to do it...
How it works...
Change map layer transparency
Getting ready
How to do it...
How it works...
Adding standard map tools to the canvas
Getting ready
How to do it...
How it works...
Using a map tool to draw points on the canvas
Getting ready
How to do it...
How it works...
Using a map tool to draw polygons or lines on the canvas
Getting ready
How to do it...
How it works...
Building a custom selection tool
Getting ready
How to do it...
How it works...
Creating a mouse coordinate tracking tool
Getting ready
How to do it...
How it works...
6. Composing Static Maps
Introduction
Creating the simplest map renderer
Getting ready
How to do it...
How it works...
There's more...
Using the Map Composer
Getting ready
How to do it...
How it works...
There's more...
Adding labels to a map for printing
Getting ready
How to do it...
How it works...
Adding a scale bar to a map
Getting ready
How to do it...
How it works...
Adding a North arrow to the map
Getting ready
How to do it...
How it works...
There's more...
Adding a logo to the map
Getting ready
How to do it...
How it works...
Adding a vertical legend to the map
Getting ready
How to do it...
How it works...
Adding a horizontal legend
Getting ready
How to do it...
Adding a custom shape to the map
Getting ready
How to do it...
How it works...
There's more...
Adding a grid to the map
Getting ready
How to do it...
How it works...
Adding a table to the map
Getting ready
How to do it...
How it works...
Adding a world file to a map image
Getting ready
How to do it...
How it works...
Saving a map to a project
Getting ready
How to do it...
How it works...
Loading a map from a project
Getting ready
How to do it...
How it works...
7. Interacting with the User
Introduction
Using log files
Getting ready
How to do it...
How it works...
There's more...
Creating a simple message dialog
Getting ready
How to do it...
How it works...
There's more...
Creating a warning dialog
Getting ready
How to do it...
How it works...
Creating an error dialog
Getting ready
How to do it...
How it works...
Displaying a progress bar
Getting ready
How to do it...
How it works...
There's more...
Creating a simple text input dialog
Getting ready
How to do it...
How it works...
Creating a file input dialog
Getting ready
How to do it...
How it works...
There's more...
Creating a combobox
Getting ready
How to do it...
How it works...
Creating radio buttons
Getting ready
How to do it...
How it works...
Creating checkboxes
Getting ready
How to do it...
How it works...
Creating a dock widget
Getting ready
How to do it...
How it works...
Displaying a message in the status bar
Getting ready
How to do it...
How it works...
Pushing messages to the message bar
Getting ready
How to do it...
How it works...
Pushing widgets to the message bar
Getting ready
How to do it...
How it works...
Creating tabs
Getting ready
How to do it...
How it works...
Stepping the user through a wizard
Getting ready
How to do it...
How it works...
Keeping dialogs on top
Getting ready
How to do it...
How it works...
8. QGIS Workflows
Introduction
Creating an NDVI
Getting ready
How to do it...
How it works...
Geocoding addresses
Getting ready
How to do it...
How it works...
Creating raster footprints
Getting ready
How to do it...
How it works...
There's more...
Performing network analysis
Getting ready
How to do it...
How it works...
There's more...
Routing along streets
Getting ready
How to do it...
How it works...
Tracking a GPS
Getting ready
How to do it...
How it works...
There's more...
Creating a mapbook
Getting ready
How to do it...
How it works...
Finding the least cost path
Getting ready
How to do it...
How it works...
Performing nearest neighbor analysis
Getting ready
How to do it...
How it works...
Creating a DEM from LIDAR
Getting ready
How to do it...
How it works...
Creating a heat map
Getting ready
How to do it...
How it works...
Creating a dot density map
Getting ready
How to do it...
How it works...
Collecting field data
Getting ready
How to do it...
How it works...
Computing road slope using elevation data
Getting ready
How to do it...
How it works...
Geolocating photos on the map
Getting ready
How to do it...
How it works...
There's more...
Image change detection
Getting ready
How to do it...
How it works...
Adjusting imprecise building footprints
Getting ready
How to do it...
How it works...
Visualizing multitemporal data
Getting ready
How to do it...
How it works...
9. Other Tips and Tricks
Introduction
Creating tiles from a QGIS map
Getting ready
How to do it...
How it works...
There's more...
Adding a layer to geojson.io
Getting ready
How to do it...
How it works...
There's more...
Rendering map layers based on rules
Getting ready
How to do it...
How it works...
Creating a layer-definition file
Getting ready
How to do it...
How it works...
Using NULL values in PyQGIS
Getting ready
How to do it...
How it works...
Using generators for layer queries
Getting ready
How to do it...
How it works...
Using alpha values to show data density
Getting ready
How to do it...
How it works...
Using the __geo_interface__ protocol
Getting ready
How to do it...
How it works...
Getting the output file names from processing algorithms
Getting ready
How to do it...
How it works...
Generating points along a line
Getting ready
How to do it...
How it works...
There's more...
Using expression-based labels
Getting ready
How to do it...
How it works...
Creating dynamic forms in QGIS
Getting ready
How to do it...
How it works...
There's more...
Calculating length for all selected lines
Getting ready
How to do it...
How it works...
Using a different status bar CRS than the map
Getting ready
How to do it...
How it works...
Creating HTML labels in QGIS
Getting ready
How to do it...
How it works...
There's more...
Using OpenStreetMap points of interest in QGIS
Getting ready
How to do it...
How it works...
Changing the QGIS web proxy
Getting ready
How to do it...
How it works...
There's more...
Running QGIS scripts as scheduled tasks
Getting ready
How to do it...
How it works...
Visualizing data in 3D with WebGL
Getting ready
How to do it...
How it works...
Visualizing data on a globe
Getting ready
How to do it...
How it works...
Make a globe-like azimuthal orthographic projection
Getting ready
How to do it...
How it works...
Animating a layer
Getting ready
How to do it...
QGIS Python Programming Cookbook - Second Edition
QGIS Python Programming Cookbook - Second Edition
Copyright © 2017 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: March 2015
Second edition: March 2017
Production reference: 1030317
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.
ISBN 978-1-78712-483-7
www.packtpub.com
Credits
About the Author
Joel Lawhead is a PMI-certified Project Management Professional (PMP), a certified Geographic Information Systems Professional, and the Chief Information Officer (CIO) for http://www.nvisionsolutions.com/, an award-winning firm specializing in geospatial technology integration and harsh-environment engineering. Joel builds geospatial systems for US government agencies, including NASA, NOAA, the US Department of Homeland Security, and the military. He also works with private organizations, including the National Oceans and Applications Research Center (NOARC) and The Ocean Cleanup. He has authored other books with Packt Publishing, including Learning Geospatial Analysis with Python, QGIS Python Programming Cookbook, and Learning Geospatial Analysis with Python, Second Edition. His cookbook recipes have been featured in two editions of the O’Reilly Python Cookbook. Joel began using Python in 1997 and combined it with geospatial software development in 2000. He is also the developer of the widely used open source Python Shapefile Library (PyShp) and maintains the geospatial technical blog, http://geospatialpython.com/, and Twitter feed, @SpatialPython, discussing the use of Python within the geospatial industry. In 2011, Joel reverse-engineered and published the undocumented shapefile spatial indexing format and assisted fellow geospatial Python developer, Marc Pfister, in reversing the compression algorithm, allowing developers around the world to create better integrated and more robust geospatial applications involving shapefiles. In 2002, Joel received the international Esri Special Achievement in GIS award for his work on the Real-Time Emergency Action Coordination Tool (REACT) for emergency management using geospatial analysis.
This book would not be possible without the support of my beautiful family, including my wife, Julie, and four children, Lauren, Will, Lillie, and Lainie, who inspire everything I do. I would also like to acknowledge my employers and coworkers at http://www.nvisionsolutions.com/, a bright, dynamic team of people dedicated to working together at the exciting bleeding-edge of geospatial technology. And finally, thank you to the excellent reviewers for this and my other books, especially Joshua Arnott, who patiently and faithfully burned clumsy sentences to the ground so that a more technically-correct phoenix could rise up the ranks of the Amazon category Top 100 list.
About the Reviewer
Joshua Arnott is an environmental scientist with over five years of experience working in water management consultancy. His expertise lies in environmental modeling, with a focus on hydrology, water resources, and geoinformatics. He has contributed to several GIS-related open source projects, including QGIS and Shapely. He maintains a blog about programming and GIS at https://snorfalorpagus.net/, and he likes cats just as much as everyone else on the Internet.
www.PacktPub.com
For support files and downloads related to your book, please visit www.PacktPub.com.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at service@packtpub.com for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
https://www.packtpub.com/mapt
Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt books and video courses, as well as industry-leading tools to help you plan your personal development and advance your career.
Why subscribe?
Fully searchable across every book published by Packt
Copy and paste, print, and bookmark content
On demand and accessible via a web browser
Customer Feedback
Thanks for purchasing this Packt book. At Packt, quality is at the heart of our editorial process. To help us improve, please leave us an honest review on this book's Amazon page at https://www.amazon.com/dp/1787124835.
If you'd like to join our team of regular reviewers, you can e-mail us at customerreviews@packtpub.com. We award our regular reviewers with free eBooks and videos in exchange for their valuable feedback. Help us be relentless in improving our products!
Preface
The open source geographic information system QGIS at version 2 now rivals even the most expensive commercial GIS software in both functionality and usability. It is also a showcase of the best geospatial open source technology available. It is not just a project in itself but the marriage of dozens of open source projects in a single, clean interface.
Geospatial technology is not just the application of technology to geography. It is a symphony of geography, mathematics, computer science, statistics, physics, and other fields. The underlying algorithms implemented by QGIS are so complex that only a handful of people in the world understand them all. Yet QGIS packages all of this complexity so well that school children, city managers, disease researchers, geologists, and many other professionals wield this powerful software with ease to make decisions that improve life on Earth.
But this book is about another feature of QGIS that makes it the best choice for geospatial work. QGIS has one of the most deeply integrated and well-designed Python interfaces of any software period. In the latest version, there is virtually no aspect of the program that is off-limits to Python, making it the largest geospatial Python library available. Almost without exception, the Python API, called PyQGIS, is consistent and predictable.
This book exploits the best features of QGIS to demonstrate over 170 reusable recipes that you can use to automate workflows in QGIS or build standalone GIS applications. Most recipes are very compact and have less than 20 lines of code. Even if you can’t find the exact solution you are looking for, you should be able to get very close. This book covers a lot of ground and pulls together fragmented ideas and documentation scattered throughout the Internet as well as the results of many hours of experimenting at the edges of the PyQGIS API.
What this book covers
Chapter 1, Automating QGIS, provides a brief overview of the different ways of using Python with QGIS, including the QGIS Python Console, standalone applications, plugins, and the Script Runner plugin. This chapter also covers setting and retrieving application settings and a few other Python-specific features.
Chapter 2, Querying Vector Data, covers extracting information from vector data without changing the data using Python. Topics include measuring, loading data from a database, filtering data, and other related processes.
Chapter 3, Editing Vector Data, introduces creating and updating data to add new information. It also teaches you how to break datasets apart based on spatial or database attributes as well as combine datasets. This chapter also teaches you how to convert data into different formats, change projections, simplify data, and more.
Chapter 4, Using Raster Data, demonstrates recipes for using and transforming raster data to create derivative products. This chapter highlights the capability of QGIS as a raster-processing engine and not just a vector GIS.
Chapter 5, Creating Dynamic Maps, transitions into recipes for controlling QGIS as a whole to control map, project, and application-level settings. It includes recipes for accessing external web services and building custom map tools.
Chapter 6, Composing Static Maps, shows you how to create printed maps using the QGIS Map Composer. You will learn to place reference elements on a map as well as design elements such as logos.
Chapter 7, Interacting with the User, teaches you to control QGIS GUI elements created by the underlying Qt framework to create interactive input widgets for scripts, plugins, or standalone applications.
Chapter 8, QGIS Workflows, contains more advanced recipes that result in a finished product or an extended capability. These recipes target actual tasks that geospatial analysts or programmers encounter on the job.
Chapter 9, Other Tips and Tricks, contains interesting recipes that fall outside the scope of previous chapters. Many of these recipes demonstrate multiple concepts within a single recipe that you may find useful for a variety of tasks.
What you need for this book
You will need the following software to complete all of the recipes in this book. If a specific version is not available, use the most recent version.
QGIS 2.18
Python 2.7.6 (should be included with QGIS itself)
IBM Java 8 JDK
IBM Eclipse Neon 4.6.0 or higher
Eclipse PyDev 5.5.0
Google Earth 7.1.7.2602 or higher
Who this book is for
This book is for geospatial analysts or programmers at any experience level who would like to learn more about controlling QGIS using Python. A basic knowledge of Python is essential and some experience with QGIS is helpful but not required.
Sections
In this book, you will find several headings that appear frequently (Getting ready, How to do it, How it works, There's more, and See also).
To give clear instructions on how to complete a recipe, we use these sections as follows:
Getting ready
This section tells you what to expect in the recipe, and describes how to set up any software or any preliminary settings required for the recipe.
How to do it…
This section contains the steps required to follow the recipe.
How it works…
This section usually consists of a detailed explanation of what happened in the previous section.
There's more…
This section consists of additional information about the recipe in order to make the reader more knowledgeable about the recipe.
See also
This section provides helpful links to other useful information for the recipe.
Conventions
In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.
Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: Open the startup.py file you created.
A block of code is set as follows:
import os
os.getcwd()
Any command-line input or output is written as follows:
sudo apt-get update
New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: From the Window menu, select Preferences.
Note
Warnings or important notes appear in a box like this.
Tip
Tips and tricks appear like this.
Reader feedback
Feedback from our readers is always welcome. Let us know what you think about this book-what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.
To send us general feedback, simply e-mail feedback@packtpub.com, and mention the book's