Book Image

QGIS 2 Cookbook

By : Alex Mandel, Víctor Olaya Ferrero, Anita Graser, Alexander Bruy
Book Image

QGIS 2 Cookbook

By: Alex Mandel, Víctor Olaya Ferrero, Anita Graser, Alexander Bruy

Overview of this book

QGIS is a user-friendly, cross-platform desktop geographic information system used to make maps and analyze spatial data. QGIS allows users to understand, question, interpret, and visualize spatial data in many ways that reveal relationships, patterns, and trends in the form of maps. This book is a collection of simple to advanced techniques that are needed in everyday geospatial work, and shows how to accomplish them with QGIS. You will begin by understanding the different types of data management techniques, as well as how data exploration works. You will then learn how to perform classic vector and raster analysis with QGIS, apart from creating time-based visualizations. Finally, you will learn how to create interactive and visually appealing maps with custom cartography. By the end of this book, you will have all the necessary knowledge to handle spatial data management, exploration, and visualization tasks in QGIS.
Table of Contents (19 chapters)
QGIS 2 Cookbook
About the Authors
About the Reviewers


Location-based technology is the latest buzzword to explain tools related to spatial knowledge and analysis. For those who work on map making, geospatial science, or any number of other things with spatial data, Geographic Information Systems (GIS), which is the more traditional name for such tools, is a field of study with decades of innovation.

QGIS (previously known as Quantum GIS), a cross-platform, free, and open source software, provides a traditional desktop-based geographic information system. Unlike a traditional system though, it is highly customizable, extendable, and, by design, works in tandem with a ton of other GIS-related tools (more are added all the time).

QGIS is a crossing point of the free and open source geospatial world. While there are a great many tools in QGIS, it is not one massive application that does everything, and it was never really designed to be that from the beginning. It is rather a visual interface to much of the open source geospatial world. You can load data from proprietary and open formats into spatial databases of various flavors and then analyze the data with well-known analytical backends before creating a printed or web-based map to display and interact with your results. What's QGIS's role in all this? It's the place where you check your data along the way, build and queue the analysis, visualize the results, and develop cartographic end products.

If you need to test modify one layer before doing a batch of 1000, use QGIS. Want to make sure the results of that SQL query or script make sense, use QGIS. Need to tinker with alternative methods of displaying your data to find the right colors, lines, and layers to convey your message, use QGIS. Find something QGIS can't do, look at other tools it works with, search the plugin list, write a plugin, or submit a new feature request. With such a mentality, everything is possible—it just takes an understanding of how to get there.

This book is all about showing you how to do all these great things and all the new cool things you didn't know you wanted to do. When QGIS doesn't do what you need or doesn't work with the tools you want to use, we'll show you where to go and who to talk to about making your dream of new functionality a reality.

What this book covers

Over the course of 12 chapters, this book will take you from data input and output, through data management and analysis, to creating print and web output, as well as extending QGIS.

Chapter 1, Data Input and Output, covers loading and saving data with special instructions for trickier formats, batch conversions, and databases.

Chapter 2, Data Management, describes the basic manipulation of attributes, indexes, and queries to make the use of your data more efficient.

Chapter 3, Common Data Preprocessing Steps, deals with converting data into the formats you need for analysis, including vector to and from raster, transitioning through different types of vectors, and cutting your data to just the important areas.

Chapter 4, Data Exploration, explores methods for visualizing and understanding the information in your data.

Chapter 5, Classic Vector Analysis, shows the QGIS way of performing traditional analysis methods of vector layers.

Chapter 6, Network Analysis, dives into the methods for analyzing routes and networks.

Chapter 7, Raster Analysis I, covers raster analysis that is primarily related to topography and hydrology.

Chapter 8, Raster Analysis II, covers common raster analysis methods and introduces more advanced multispectral and classification data handling.

Chapter 9, QGIS and the Web, explores the use of live data from the Web and how to put up your own web map based on a QGIS project.

Chapter 10, Cartography Tips, reveals advanced tips and tricks to get the most out of the cartographic tools in QGIS.

Chapter 11, Extending QGIS, shows you how to take QGIS beyond the out-of-the-box features with plugins, customization, and add-on tools.

Chapter 12, Up and Coming, hints at the future with cutting-edge plugins and how to participate in the future development of QGIS.

What you need for this book

We recommend installing QGIS 2.8 or later; you will need at least QGIS 2.4. During the writing of this book, several new versions were released, approximately every 4 months, and most recently, 2.14 was released. Most of the recipes will work on older versions, but some may require 2.6 or newer. In general, if you can, upgrade to the latest stable release or Long Term Support (LTS) version.

There are also a lot of side interactions with other software throughout many of these recipes, including—but not limited to—Postgis 2+, GRASS 6.4+, SAGA 2.0.8+, and Spatialite 4+. On Windows, most of these can be installed using OSGeo4W; on Mac, you may need some additional frameworks from Kyngchaos, or if you're familiar with Brew, you can use the OSGeo4Mac Tap. For Linux users, in particular Ubuntu and Debian, refer to the UbuntuGIS PPA and the DebianGIS blend.

Does all of this sound a little too complicated? If yes, then consider using a virtual machine that runs OSGeo-Live ( All the software is preinstalled for you and is known to work together.

Lastly, you will need data. For the most part, we've provided a lot of free and open data from a variety of sources, including the OSGeo Educational dataset (North Carolina), Natural Earth Data, OpenFlights, Wake County, City of Davis, and Armed Conflict Location & Event Data Project (ACLED). A full list of our data sources is provided here if you would like additional data.

We recommend that you try methods with the sample data first, only because we tested it. Feel free to try using your own data to test many of the recipes; however, just remember that you might need to alter the structure to make it work. After all, that's what you'll be working with normally.

The following are the data sources for this book:

  • OSGeo Educational Data:

  • Wake County, USA:

  • Natural Earth Data:

  • City of Davis, USA:

  • Stamen Designs:

  • Armed Conflict Location & Event Data Project:

Who this book is for

This book is for anyone who wants to do more with QGIS. It's aimed at an intermediate to advanced audience that already has some experience using GIS (any). The goal is to master the fundamentals of QGIS and launch you, the reader, to the next level of being a QGIS power user and community member.

Whatever your current level of experience with QGIS, you will find a lot of new ways to put your skills to good use. For those who are new to GIS, this book shows you many of the things that are possible with GIS. For those of you who are new to QGIS, this book is a guide on how to do the things you know from elsewhere with QGIS. For advanced users, this book is a reference and cheat sheet to the common tools you use often but can't quite remember how they work. And for all users, this book is filled with unknown and barely documented aspects of QGIS that you didn't think possible but likely want to use.


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.


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: "Create a Spatialite database if you don't already have one and name it cookbook.db."

A block of code is set as follows:

LINESTRING(0 1, 0 2, 1 3),1,50
LINESTRING(0 -1, 0 -2, 1 -3),2,60
LINESTRING(0 1, 0 3, 5 4),3,70

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

LINESTRING(0 1, 0 2, 1 3),1,50
LINESTRING(0 -1, 0 -2, 1 -3),2,60
LINESTRING(0 1, 0 3, 5 4),3,70

Any command-line input or output is written as follows:

-gcp sourceX sourceY destinationX destinationY

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: "Check the Use visible raster layers checkbox or choose SELECT."


Warnings or important notes appear in a box like this.


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 , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for this book from your account at If you purchased this book elsewhere, you can visit and register to have the files e-mailed directly to you.

You can download the code files by following these steps:

  1. Log in or register to our website using your e-mail address and password.

  2. Hover the mouse pointer on the SUPPORT tab at the top.

  3. Click on Code Downloads & Errata.

  4. Enter the name of the book in the Search box.

  5. Select the book for which you're looking to download the code files.

  6. Choose from the drop-down menu where you purchased this book from.

  7. Click on Code Download.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR / 7-Zip for Windows

  • Zipeg / iZip / UnRarX for Mac

  • 7-Zip / PeaZip for Linux

Downloading the color images of this book

We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from


Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to and enter the name of the book in the search field. The required information will appear under the Errata section.


Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.


If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.