Mastering QGIS - Sample Chapter
Mastering QGIS - Sample Chapter
ee
$ 49.99 US
32.99 UK
P U B L I S H I N G
pl
Mastering QGIS
Mastering QGIS
Sa
m
C o m m u n i t y
E x p e r i e n c e
D i s t i l l e d
Mastering QGIS
Go beyond the basics and unleash the full power of QGIS with
practical, step-by-step examples
Foreword by Phillip Davis,
Director, National Information Security & Geospatial Technology Consortium
Dr. Luigi Pirelli is a freelance software analyst and developer with an honors degree in
computer science from the University of Bari. He worked for 15 years in Satellite Ground
Segment and Direct Ingestion for the European Space Agency. Since 2006, he has been
involved with the GFOSS world, contributing to QGIS, GRASS, and MapServer core
and developing and maintaining many QGIS plugins. He actively participates in QGIS
Hackmeetings. He is the founder of the OSGEO Italian Local Chapter
(http://gfoss.it/drupal/) and now lives in Spain and contributes to this
GFOSS community. During the last few years, he started teaching PyQGIS by
organizing trainings from basic to advanced levels and supporting companies
to develop their specific QGIS plugins.
He is the founder of the local hackerspace group Bricolabs.cc that is focused on Open
Hardware. He likes cycling, repairing everything, and trainings groups on conflict
resolution. Besides this book, he has also contributed to Lonely Planet Cycling Italy.
Dr. John Van Hoesen is an associate professor of geology and environmental studies at
Green Mountain College in rural west-central Vermont. He earned an MS in 2000 and a
PhD in geology from the University of Nevada, Las Vegas, in 2003. He is a certified GIS
Professional (GISP) with a broad background in geosciences and has been using some
flavor of GIS to evaluate and explore geologic processes and environmental issues since
1997. He has used and taught some variant of FOSS GIS since 2003, and over the last 3
years, he has taught graduate, undergraduate, and continuing education courses using
only FOSS GIS software.
Mastering QGIS
Welcome to Mastering QGIS. The goal of this book is to help intermediate and advanced
users of GIS develop a deep understanding of the capabilities of QGIS while building the
technical skills that would facilitate in making the shift from a proprietary GIS software
package to QGIS.
QGIS embodies the open source community's spirit. It seamlessly works with other free
and open source geospatial software, such as SAGA, GDAL, GRASS, and fTools, and
supports standards and formats that are published by myriad organizations. QGIS is about
freedom in the geospatial world: freedom to choose your operating system, freedom from
licensing fees, freedom to customize, freedom to look under the hood, and freedom
to contribute to the development of QGIS. As you work through this book, we believe
that you will be amazed at how much capability and freedom QGIS provides.
QGIS has rapidly risen from the initial version written by Gary Sherman in 2002 to
become a globally used and developed volunteer-led project. In 2009, QGIS version 1.0
was released as an Open Source Geospatial Foundation (OSGeo) project and continues
to be rapidly adopted worldwide. The enduring support of the open source community
has really delivered QGIS to a point where it is now a top-shelf product that should
be in all GIS users' toolboxes, and we want this book to be your tour guide and
reference as you learn, use, and contribute to QGIS.
Chapter 4, Preparing Vector Data for Processing, covers techniques useful for turning
raw vector data into a more usable form. The chapter will start with data massaging and
modification techniques such as merging, creating indices, checking for geometry errors,
and basic geoprocessing tools. Next, advanced field calculations will be covered,
followed by complex spatial and aspatial queries. The chapter will end by defining new
or editing existing coordinate reference systems.
Chapter 5, Preparing Raster Data for Processing, covers the preparation of raster
data for further processing using the GDAL menu tools and the Processing Toolbox
algorithms. Specifically, these include reclassification, resampling, rescaling,
mosaics, generating pyramids, and interpolation. The chapter will conclude
by converting raster to vector.
Chapter 6, Advanced Data Creation and Editing, provides advanced ways to create
vector data. As there is a great deal of data in tabular format, this chapter will cover
mapping coordinates and addresses from tables. Next, georeferencing of imagery
into a target coordinate reference system will be covered. The final portion of the
chapter will cover testing topological relationships in vector data and correcting
any errors via topological editing.
Chapter 7, The Processing Toolbox, begins with an explanation and exploration
of the QGIS Processing Toolbox. Various algorithms and tools, available in the toolbox,
will be used to complete common spatial analyses and geoprocessing tasks for both raster
and vector formats. To illustrate how these processing tools might be applied to realworld questions, two hypothetical scenarios are illustrated by relying heavily on
GRASS and SAGA tools.
Chapter 8, Automating Workflows with the Graphical Modeler, covers the purpose
and use of the graphical modeler to automate analysis workflows. In the chapter,
you will develop an automated tool/model that can be added to the Processing Toolbox.
Chapter 9, Creating QGIS Plugins with PyQGIS and Problem Solving, covers the
foundational information to create a Python plugin for QGIS. Information about the
API and PyQGIS help will be covered first, followed by an introduction to the iface and
QGis classes. Next, the steps required to create and structure a plugin will be covered.
The chapter will be wrapped up after providing you with information on creating
graphical user interfaces and setting up debugging environments to debug code easily.
Chapter 10, PyQGIS Scripting, provides topics for integrating Python analysis
scripts with QGIS outside of the Processing Toolbox. Layer loading and management
are first covered, followed by an exploration of the vector data structure. Next,
programmatic launching of other tools and external programs are covered. Lastly,
the QGIS map canvas is covered with respect to how a script can interact
with the map canvas and layers within.
Loading data
Editing data
Styling data
Composing a map
[1]
deb-src
Chapter 1
Depending on the release version of Ubuntu you are using, you will need to specify
the release name as trusty, saucy, or precise. For the latest list of QGIS releases for
Ubuntu versions, visit download.qgis.org.
With the appropriate repository added, you can proceed with the QGIS installation
by running the following commands:
sudo apt-get update
sudo apt-get install qgis python-qgis
To install the GRASS plugin (recommended), install the optional package by running
this command:
sudo apt-get install qgis-plugin-grass
[3]
Tour of QGIS
QGIS is composed of two programs: QGIS Desktop and QGIS Browser. Desktop
is used for managing, displaying, analyzing, and styling data. Browser is used to
manage and preview data. This section will give you a brief tour of the graphical
user interface components of both QGIS Desktop and QGIS Browser.
QGIS Desktop
The QGIS interface is divided into four interface types: menu bar, toolbars, panels,
and map display. The following screenshot shows QGIS Desktop with all four
interface types displayed:
The map display shows the styled data added to the QGIS project and, by default,
takes up the majority of the space in QGIS Desktop. The menu bar, displayed
across the top, provides access to most of QGIS Desktop's functionality. The
toolbars provide quick access to QGIS Desktop functionality. The toolbars can
be arranged to either float independently or dock at the top, bottom, left, or right
sides of the application. The panels, such as Browser and Layers, provide a variety
of functionality and can be arranged to either float independently or dock above,
below, right, or left of the map display.
[4]
Chapter 1
There are four toolbars that are particularly useful, and it is recommended that you
enable them:
The File toolbar provides quick access to create, open, and save QGIS projects
and create and manage print composers
The Manage Layers toolbar contains tools to add vector, raster, database,
web service, text layers, and create new layers
The Map Navigation toolbar contains tools that are useful for panning,
zooming, and refreshing the map display
QGIS Desktop offers a number of customization options. You can toggle the visibility
of toolbars by navigating to View | Toolbars, or by right-clicking on the menu bar or
the enabled toolbar button, which will open a context menu allowing you to toggle the
toolbar and panel visibility. You can assign shortcut keys to operations by navigating
to Settings | Configure shortcuts. You can also change application options, such as
interface language and rendering options by navigating to Settings | Options.
QGIS Browser
The QGIS Browser interface (shown in the following screenshot) is composed of
three parts: toolbar, data tree view, and information panel.
[5]
The data tree view is an expandable tree listing of all geospatial data files on your
computer and through connections. The information display, which takes most of
the space on the application, contains four tabs that provide different views of the
selected data in the data tree listing, and they are as follows:
Param: This tab displays details of data that is accessed through connections,
such as a database or WMS.
Metadata: This tab displays the metadata (if any) of the selected data.
Preview: This tab renders the selected data. You can zoom into the data
using your mouse wheel and pan using the arrow keys on your keyboard.
Attribute: This tab displays the attribute table associated with the selected
data. You can sort the columns by clicking on the column headings.
The toolbar provides access to four functions. The Refresh function reloads the data
tree view while the Manage WMS function opens the WMS management screen
allowing you to manage the WMS connections. The New Shapefile function opens
the new vector layer dialog allowing new shapefiles to be created. Finally, the Set
layer CRS function allows you to define the coordinate reference system of the
geospatial data file that is selected in the data tree view.
Loading data
One strength of QGIS is its ability to load a large number of data types. In this
section, we will cover loading various types of data into QGIS Desktop.
In general, data can be loaded in four ways. The first way, which will be covered
in detail in this section, is to use the Add Layer menu under Layer and select the
appropriate type of data that you wish to load. The second way is to open the
Browser panel, navigate to the data you wish to load, and then drag the data onto
the map display or onto the Layers panel. The third way to load data is to enable the
Manage Layers toolbar and click on the button representing the data type that you
wish to load. The fourth way is to locate the data in QGIS Browser, drag the data,
and drop it onto the QGIS Desktop map display or the Layers panel.
[6]
Chapter 1
The source type contains four options: File, Directory, Database, and Protocol.
When you choose a source type, the source interface will change to display the
appropriate options. Let's take a moment to discuss what type of data these four
source types can load:
File: This can load flat files that are stored on disk. The commonly used flat
file types are as follows:
Directory: This can load data stored on disk that is encased in a directory.
The commonly used directory types are as follows:
Database: This can load databases that are stored on disk or those
available through service connections. The commonly used database
types are as follows:
ODBC
MSSQL
MySQL
PostgreSQL
Protocol: This can load protocols that are available at a specific URI. QGIS
currently supports loading the GeoJSON protocol.
GeoTIFF (.tif/.tiff)
Rasterlite (.sqlite)
Loading databases
QGIS supports PostGIS, SpatiaLite, MSSQL, and Oracle databases. Regardless of the
type of database you wish to load, the loading sequence is very similar. Therefore,
instead of covering specific examples, the general sequence will be covered.
First, click on Add Layer under Layer and then choose the database type you wish to
load. This will open a window with options for adding the data stored in a database.
As an example, the following screenshot shows the window that opens when you
navigate to Layer | Add Layer | Add SpatiaLite Layer:
[8]
Chapter 1
Note that the window will look the same for any database
you choose, except for the window name.
To load data from a database, we must first create a connection to the database. To
create a new connection, click on the New button to open a connection information
window. Depending on the database type you are connecting to, different connection
options will be shown. Once you have created a database connection, select it from
the drop-down list and click on Connect; you will see a list of all layers contained
within the database display. If there are a large number of tables, you can select
Search options and perform a search on the database. To load a layer, select it in the
list and click on Add. If you only wish to load a portion of the layer, select the layer
and then click on Set Filter to open the query builder. If you set a query and then
add the layer, only the filtered features will be added.
Web services
QGIS supports the loading of OGC-compliant web services such as WMS/WMTS,
WCS, and WFS. Loading a web service is similar to loading a database service. In
general, you will create a new server connection, connect to the server to list the
available services, and add the service to the QGIS project.
[9]
To enable the "on the fly" projection, perform the following steps:
1. Click on Project Properties under Project.
2. Choose the CRS tab and Enable 'on the fly' CRS transformation.
3. Set the CRS that you wish to apply to the project and make all layers that are
not set to the project's CRS transform "on the fly".
To view the CRS for a layer, perform the following steps:
1. Open the layer's properties by either navigating to Layer | Properties or by
right-clicking on the layer in the Layers panel.
2. Choose Properties from the context menu and then choose the General tab.
3. If the layer's CRS is not set or is incorrect, click on Specify to open the CRS
selector window and select the correct CRS.
To project a layer to a different CRS, perform the following steps:
1. Right-click on the layer in the Layers panel and then choose Save As from
the context menu.
2. In the Save vector layer as dialog, set the file format and filename, then set
CRS to Selected CRS and click on Change to set the target CRS, and save
the file.
[ 10 ]
Chapter 1
To create a new CRS or modify an existing CRS, perform the following steps:
1. Click on Custom CRS under Settings to open the Custom Coordinate
Reference System Definition window.
2. Click on the Add new CRS button to add a new entry to the CRS list.
3. With the new CRS selected, we can set the name and parameters of the CRS.
The CRS properties are set using the Proj.4 format. To modify an existing
CRS, click on Copy existing CRS and select the CRS from which you wish to
copy parameters; otherwise, enter the parameters manually.
Proj.4 is another Open Source Geospatial Foundation (http://
osgeo.org) project used by QGIS, and it is similar to OGR and
GDAL. This project is for managing coordinate systems and
projections. For a detailed user manual for the Proj.4 format
used to specify the CRS parameters in QGIS, download it from
ftp://ftp.remotesensing.org/proj/OF90-284.pdf.
Table joins
Let's say that you need to make a map of the total population by county. However,
the counties' GIS layer does not have population as an attribute. Instead, this data is
contained in an Excel spreadsheet. It is possible to join additional tabular data to an
existing attribute table.
There are two requirements, which are as follows:
The two tables need to share fields with attributes to match for joining
To create a join, load both the GIS layer and the standalone table into QGIS
Desktop. QGIS will accept a variety of standalone table file formats including Excel
spreadsheets, .dbf files, and comma delimited text files. You can load this tabular
data using the Add Vector Layer menu by navigating to Layer | Add Layer and
setting the file type filter to All files (*) (*.*) as shown in the following screenshot:
[ 12 ]
Chapter 1
Once the data is loaded, a join can be completed by following these steps:
1. Select the GIS layer in the Layers panel that will receive the new data from
the join.
2. Navigate to Layer | Properties and choose the Joins menu.
3. Click on the add join button (the one with green plus sign).
4. Choose the Join Layer, Join Field, and Target Field values. The Join Layer
and Join Field values represent the standalone table. The Target Field value
is the column in the attribute table on which the join will be based.
Although in this example the join field and the target field
have the same name, this is not a requirement. The two
fields merely need to hold the same unique ID.
5. At this point, you can choose Cache the join in virtual memory, Create
attribute index on the join field, and Choose which fields are joined. The
last option lets you to choose which fields from the join layer to append to
the attribute table. At this point, the Add vector join window will look like
the following screenshot.
6. Once created, the join will be listed on the Joins tab. The extra attribute
columns from the join layer will be appended to the attribute table, where the
value in the join field matched the value in the target field.
[ 13 ]
7. Joins can be removed by clicking on the remove join button (the one with red
minus sign).
Editing data
Vector data layers can be edited within QGIS Desktop. Editing allows you to add,
delete, and modify features in vector datasets. The first step is to put the dataset into
edit mode. Select the layer in the Layers panel and click on Toggle Editing under
Layer. Alternatively, you can right-click on a layer in the Layers panel and choose
Toggle Editing from the context menu. Multiple layers can be edited at a time. The
layer currently being edited is the one selected in the Layers panel. Once you are in
the edit mode, the digitizing toolbar (shown in the following screenshot) can be used
to add, delete, and modify features.
[ 14 ]
Chapter 1
From left to right, the tools in the digitizing toolbar are as follows:
The Current Edits tool allows you to manage your editing session. Here, you
can save and rollback edits for one or more selected layers.
The Save Layer Edits tool allows you to save edits for the selected layer(s)
during an editing session.
The Add Features tool will change to the appropriate geometry depending
on whether a point, line, or polygon layer is selected. Points and vertices of
lines and polygons are created by clicking. To complete a line or polygon
feature, right-click. After adding a feature, you will be prompted to enter
the attributes.
Features can be moved with the Move tool by clicking on them and dragging
them to the new position.
Individual feature vertices can be moved with the Node tool. Click on a
feature once with the tool to select it and the vertices will change into red
boxes. Click again on an individual vertex to select it. The selected vertex
will turn into a dark-blue box. Now, the vertex can be moved to the desired
location. Additionally, edges between vertices can be selected and moved.
To add vertices to a feature, simply double-click on the edge where you want
the vertex to be added. Selected vertices can be deleted by pressing the Delete
key on the keyboard.
Features can be deleted, cut, copied, and pasted using the Delete Selected,
Cut Features, Copy Features, and Paste Features tools.
Snapping
Snapping is an important editing consideration. It is a specified distance (tolerance)
within which vertices of one feature will automatically align with vertices of another
feature. The specific snapping tolerance can be set for the whole project or per layer.
The method for setting the snapping tolerance for a project varies according to the
operating system, which is as follows:
[ 15 ]
In addition to setting the snapping tolerance, here the snapping mode can also be set
to vertex, segment, or vertex and segment. Snapping can be set for individual layers
by navigating to Settings | Snapping Options. Individual layer snapping settings
will override those of the project. The following screenshot shows examples of
multiple snapping option choices.
Single Symbol: This is the default rendering in which one symbol is applied
to all the features in a layer.
Graduated: This allows you to classify the data by a numeric field attribute
into discrete categories. You can specify the parameters of the classification
(classification type and number of classes) and use the Set column
expression button to enhance the styling with a SQL expression.
[ 16 ]
Chapter 1
Point displacement: If you have a point layer with stacked points, this option
can be used to displace the points so that they are all visible.
The following screenshot shows the Style properties available for a vector data layer:
In the preceding screenshot, the renderer is the layer symbol. For a given symbol,
you can work with the first level, which gives you the ability to change the
transparency and color. You can also click on the second level, which gives you
control over parameters such as fill, border, fill style, border style, join style, border
width, and X/Y offsets. These parameters change depending on the geometry of
your layer. You can also use this hierarchy to build symbol layers, which are styles
built from several symbols that are combined vertically.
[ 17 ]
Multiband color: This is for rasters with multiple bands. It allows you to
choose the band combination that you prefer.
The following is a screenshot of the Style tab of a raster file's Layer Properties
showing where the aforementioned style choices are located:
[ 18 ]
Chapter 1
Contrast enhancement
Another important consideration with raster styling is the settings that are used for
contrast enhancement when rendering the data. Let's start by loading the Jemez_
dem.img image and opening the Style menu under Layer Properties (shown in the
figure below). This is an elevation layer and the data is being stretched on a blackto-white color ramp from the Min and Max values listed under Band rendering. By
default, these values only include those that are from 2 percent to 98 percent of the
estimation of the full range of values in the dataset, and cut out the outlying values.
This makes rendering faster, but it is not necessarily the most accurate.
Next, we will change these settings to get a full stretch across all the data values in
the raster. To do this, perform the following steps:
1. Under the Load min/max section, choose Min / max and under Accuracy,
choose Actual (slower).
2. Click on Load.
[ 19 ]
3. You will notice that the minimum and maximum values change. Click
on Apply.
Default singleband contrast enhancement (left) and more accurate contrast enhancement (right)
Blending modes
The blending modes allow for more sophisticated rendering between GIS layers.
Historically, these tools have only been available in graphics programs and they
are a fairly new addition to QGIS. Previously, only layer transparency could be
controlled. There are now 13 different blending modes that are available: Normal,
Lighten, Screen, Dodge, Addition, Darken, Multiply, Burn, Overlay, Soft light, Hard
light, Difference, and Subtract. These are much more powerful than simple layer
transparency, which can be effective but typically results in the underneath layer
being washed out or dulled. With blending modes, you can create effects where
the full intensity of the underlying layer is still visible. Blending mode settings
can be found at the bottom of the Style menu under Layer Properties in the Layer
Rendering section along with the Layer transparency slider. They are available for
both vector and raster datasets.
[ 20 ]
Chapter 1
In this example of using blending modes, we want to show vegetation data (Jemez_
vegetation.tif) in combination with a hillshade image (Jemez_hillshade.img).
Both data sets are loaded and the vegetation data is dragged to the top of the layer
list. Vegetation is then styled with a Singleband pseudocolor renderer; you can do
this by performing the following steps:
1. Choose Random colors.
2. Set Mode to Equal interval.
3. Set the number of Classes to 13.
4. Click on Classify.
5. Click on Apply.
The following screenshot shows what the Style properties should look like after
following the preceding steps.
[ 21 ]
At the bottom of the Style menu under Layer Properties, set the Blending mode to
Multiply and the Contrast to 45 and click on Apply. The blending mode allows all
the details of both the datasets to be seen. Experiment with different blending modes
to see how they change the appearance of the image. The following screenshot shows
an example of how blending and contrast settings can work together to make a raster
'pop' off the screen:
[ 22 ]
Chapter 1
Composing maps
With QGIS, you can compose maps that can be printed or exported to image and
graphic files. To get started, click on New Print Composer under Project. Give the
new composition a name, click on OK, and the composer window will open.
The composer presents you with a blank sheet of paper upon which you can craft
your map. Along the left-hand side, there are a series of tools on the Composer Items
toolbar. The lower portion of the toolbar contains buttons for adding map elements
to your map. These include the map body, images, text, a legend, a scale bar, graphic
shapes, arrows, attribute tables, and HTML frames. Map elements become graphics
on the composition canvas. By selecting a map element, graphic handles will appear
around the perimeter. These can be used to move and resize the element. The upper
portion of the Composer Items toolbar contains tools for panning the map data,
moving other graphic content, and zooming and panning on the map composition.
The majority of the map customization options can be found in the composer tabs. To
specify the sheet size and orientation, use the Composition tab. Once map elements
have been added to the map, they can be customized with the Item properties tab.
The options available on the Item properties tab change according to the type of
map element that is selected. The Atlas generation tab allows you to generate a map
book. For example, a municipality could generate an atlas by using a map sheet GIS
layer and specifying which attribute column contains the map sheet number for each
polygon. The Items tab allows you to toggle individual map elements on and off.
[ 23 ]
The toolbars across the top contain tools for aligning graphics (the Composer Item
Actions toolbar), navigating around the map composition (the Paper Navigation
toolbar), and tools for managing, saving, and exporting compositions (the Composer
toolbar). Maps can be exported as images, PDFs, and SVG graphic files. To export
the map, click on the Composer menu and select one from among Export as image...,
Export as SVG..., or Export as PDF... depending on your needs. The following is a
screenshot showing parts of the composer window.
[ 24 ]
Chapter 1
You can explore the QGIS plugin ecosystem by navigating to Plugins | Manage and
Install Plugins. This opens the Plugins Manager window (shown in figure below)
that will allow you to browse all plugins, those that are installed, and those that are
not installed, and adjust the settings. If there are installed plugins with available
upgrades, there will also be an Upgradable option. The search bar can be used to
enter search terms and find available plugins related to the topic. This is the first
place to look if there's a tool or extra type of functionality that you need! To install a
plugin, simply select it and click on the Install Plugin button. Installed plugins can
be toggled on and off by checking the box next to each.
You will be notified by a link at the bottom of the QGIS Desktop application if there
are updates available for your installed plugins. Clicking on the link will open the
Plugins Manager window, where the Upgrades tab will allow you to install all
or some of the available updates. Plugins themselves may show up as individual
buttons, toolbars, or as items under the appropriate menu, such as Plugins, Vector,
Raster, Database, Web, or Processing.
To add a base map to QGIS, enable the OpenLayer plugin.
It appears under the Web menu and allows you to add
base maps from OpenStreetMap, Google Maps, Bing Maps,
Map Quest, OSM/Stamen, and Apple Maps. This plugin
requires an Internet connection.
[ 25 ]
Summary
This chapter provided a refresher in the basics of Desktop and QGIS Browser. We
covered how to install the software on several platforms and described the layout
of both QGIS Desktop and QGIS Browser. We then covered how to load vector,
raster, and database data layers. Next, you were shown how to work with coordinate
reference systems and style data. We covered the basics of working with tables,
including how to perform a table join. The chapter concluded with a refresher on
composing maps and how to find, install, and manage plugins.
The next chapter will cover creating spatial databases. Data is the foundation of any
GIS. Now that you have had a refresher on the basics of QGIS, it is time to learn
how to expand your work to include spatial databases. In Chapter 2, Creating Spatial
Databases, you will learn how to create and manage spatial databases within QGIS.
[ 26 ]
www.PacktPub.com
Stay Connected: