Book Image

Building Mapping Applications with QGIS

By : Erik Westra
Book Image

Building Mapping Applications with QGIS

By: Erik Westra

Overview of this book

Table of Contents (16 chapters)
Building Mapping Applications with QGIS
About the Author
About the Reviewers
Free Chapter
Getting Started with QGIS


As software applications become more and more a part of people's lives, the concepts of location and space become more important. Developers are regularly finding themselves having to work with location-based data. Maps, geospatial data, and spatial calculations are increasingly becoming just another part of the everyday programming repertoire.

A decade ago, geospatial concepts and development was limited to experts in the Geographic Information Sciences. These people spent years working with maps and the complex mathematics that underlie them. Often coming from a university background, these specialists would spend years becoming familiar with a particular Geographic Information System (GIS), and would make a career of using that system to draw maps and process geospatial data.

While the ever-popular Google Maps meant that anyone can view and manipulate a map, the more advanced custom display and processing of geospatial data was still limited to those who used a professional GIS system. All this changed with the advent of freely available (and often open source) tools for manipulating and displaying geospatial data. Now, anybody can learn the necessary concepts and start building their own mapping applications from scratch. Rather than being limited to the minimal capabilities and restrictive licensing terms of Google Maps, developers can now build their own mapping systems to meet their own requirements, and there are no limits to what can be done.

While the necessary tools and libraries are freely available, the developer still needs to put them together into a workable system. Often, this is a rather complex process and requires a lot of understanding of geospatial concepts, as well as how to compile the necessary wrappers and configure the tools to work on a particular computer.

Fortunately, now there is an even easier way to include geospatial programming tools and techniques within your Python applications. Thanks to the development of the freely available QGIS system, it is now easy to install a complete geospatial development environment, which you can use directly from within your Python code. Whether you choose to build your application as a plugin for the QGIS system, or write a standalone mapping application using QGIS as an external library, you have complete flexibility in how you use geospatial capabilities within your code.

What this book covers

Chapter 1, Getting Started with QGIS, shows you how to install and run the QGIS application, and introduces the three main ways in which Python can be used with QGIS.

Chapter 2, The QGIS Python Console, explores the QGIS Python Console window, and explains how it acts as a useful tool while building your own custom mapping applications. It also gives you a taste of what can be done with Python and QGIS, and improves your confidence and familiarity with the QGIS environment.

Chapter 3, Learning the QGIS Python API, introduces the Python libraries available for the QGIS Python developer, and shows how these libraries can be used to work with geospatial data and create useful and interesting maps based on your geospatial data.

Chapter 4, Creating QGIS Plugins, introduces the concept of a QGIS plugin, and explains how to write a plugin using Python. We take an in-depth look at how plugins work, and how to create a useful geospatial application as a QGIS plugin. We also look at the possibilities and limitations of QGIS plugins.

Chapter 5, Using QGIS in an External Application, completes the process of building standalone Python applications that make use of the QGIS Python libraries. You will learn how to create a wrapper script to handle platform-specific dependencies, design and build a simple but complete standalone mapping application, and learn about the structure of an application built on top of QGIS. Along the way, you will become a far more competent QGIS programmer as you build your own turnkey mapping application from scratch.

Chapter 6, Mastering the QGIS Python API, delves once more into the PyQGIS library, looking at some more advanced aspects of this library, as well as various techniques for working with QGIS using Python.

Chapter 7, Selecting and Editing Features in a PyQGIS Application, looks at how Python programs built using PyQGIS can allow the user to select, add, edit, and delete geospatial features within a map interface.

Chapter 8, Building a Complete Mapping Application Using Python and QGIS, covers the process of designing and building a complete turnkey mapping application called "ForestTrails". You will design the application, implement the overall user interface, and construct a suitable high-resolution basemap for use by the application.

Chapter 9, Completing the ForestTrails Application, covers the completion of the implementation of the "ForestTrails" mapping application by implementing the various map-editing tools, as well as writing a feature to find the shortest available path between two points on the map.

What you need for this book

To follow through the examples in this book, you will need to install the following software on your computer:

  • QGIS Version 2.2 or later

  • Python Version 2.6 or later (but not Python 3.x)

  • GDAL/OGR Version 1.10 or later

  • PyQt4 Version 4.10 or later

  • Depending on your operating system, you might also need to install the Qt toolkit so that PyQt will work

All of this software can be freely downloaded, and works on Mac OS X, MS Windows, and Linux computers.

Who this book is for

This book is aimed at experienced Python developers who have some familiarity with maps and geospatial concepts. While the necessary concepts are explained as we go along, it would help to have at least some understanding of projections, geospatial data formats, and the like.


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: "This uses the QGIS_PREFIX environment variable we set earlier to tell QGIS where to find its resources."

A block of code is set as follows:

app = QApplication(sys.argv)

viewer = MapViewer("/path/to/shapefile.shp")


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

  def unload(self):
    self.iface.removePluginMenu("Test Plugin", self.action)

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

export PYTHONPATH="$PYTHONPATH:/Applications/"

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: "If you haven't already installed QGIS, click on the Download Now button on the main QGIS web page to download the QGIS software."


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 from your account at for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit and register to have the files e-mailed directly to you.

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.