QGIS 2.2 QGISTrainingManual
QGIS 2.2 QGISTrainingManual
QGIS 2.2 QGISTrainingManual
Release 2.2
QGIS Project
Cuprins
Course Introduction
1.1 Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.2 Preparing Exercise Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1
1
3
11
11
12
14
17
17
21
51
51
52
71
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
101
101
111
122
134
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
147
147
156
174
185
.
.
.
.
.
.
.
.
Module: Rasters
205
8.1 Lesson: Working with Raster Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
8.2 Lesson: Changing Raster Symbology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
8.3 Lesson: Terrain Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
9.4
10 Module: Plugins
251
10.1 Lesson: Installing and Managing Plugins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
10.2 Lesson: Useful QGIS Plugins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
11 Module: Online Resources
265
11.1 Lesson: Web Mapping Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
11.2 Lesson: Web Feature Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
12 Module: GRASS
283
12.1 Lesson: GRASS Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
12.2 Lesson: GRASS Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294
13 Module: Assessment
13.1 Create a base map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
13.2 Analyze the data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
13.3 Final Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
303
303
305
305
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
307
307
308
314
328
339
345
360
366
375
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
383
383
388
393
395
399
400
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
403
403
406
411
413
420
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
427
427
427
429
431
434
441
444
446
451
453
458
461
465
ii
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
17.14
17.15
17.16
17.17
17.18
17.19
17.20
17.21
17.22
17.23
17.24
17.25
17.26
17.27
17.28
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
466
475
485
496
507
512
515
516
525
531
536
538
542
543
544
547
547
550
563
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
567
567
567
567
568
569
570
570
570
570
572
20 Answer Sheet
20.1 Results For Adding Your First Layer . . . .
20.2 Results For An Overview of the Interface .
20.3 Results For Working with Vector Data . . .
20.4 Results For Symbology . . . . . . . . . . .
20.5 Results For Attribute Data . . . . . . . . .
20.6 Results For The Label Tool . . . . . . . . .
20.7 Results For Classification . . . . . . . . .
20.8 Results For Creating a New Vector Dataset
20.9 Results For Vector Analysis . . . . . . . .
20.10 Results For Raster Analysis . . . . . . . .
20.11 Results For Completing the Analysis . . . .
20.12 Results For WMS . . . . . . . . . . . . . .
20.13 Results For Database Concepts . . . . . .
20.14 Results For Spatial Queries . . . . . . . .
20.15 Results For Geometry Construction . . . .
20.16 Results For Simple Feature Model . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
573
573
573
573
574
579
580
584
585
589
599
604
609
612
615
615
617
21 Indici i tabele
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
619
iii
iv
CAPITOLUL 1
Course Introduction
1.1 Foreword
1.1.1 Background
In 2008 we launched the Gentle Introduction to GIS, a completely free, open content resource for people who
want to learn about GIS without being overloaded with jargon and new terminology. It was sponsored by the
South African government and has been a phenomenal success, with people all over the world writing to us to
tell us how they are using the materials to run University Training Courses, teach themselves GIS and so on. The
Gentle Introduction is not a software tutorial, but rather aims to be a generic text (although we used QGIS in all
examples) for someone learning about GIS. There is also the QGIS manual which provides a detailed functional
overview of the QGIS application. However, it is not structured as a tutorial, but rather as a reference guide. At
Linfiniti Consulting CC. we frequently run training courses and have realised that a third resource is needed - one
that leads the reader sequentially through learning the key aspects of QGIS in a trainer-trainee format - which
prompted us to produce this work.
This training manual is intended to provide all the materials needed to run a 5 day course on QGIS, PostgreSQL
and PostGIS. The course is structured with content to suit novice, intermediate and advanced users alike and has
many exercises complete with annotated answers throughout the text.
1.1.2 License
docs/training_manual/foreword/../_static/license.png
The Free Quantum GIS Training Manual by Linfiniti Consulting CC. is licensed under a Creative Commons
Attribution-NonCommercial-ShareAlike 3.0 Unported License. Based on a work at https://github.com/qgis/QGISTraining-Manual. Permissions beyond the scope of this license may be available at https://github.com/qgis/QGISTraining-Manual/blob/master/index.rst.
We have published this QGIS training manual under a liberal license that allows you to freely copy, modify and
redistribute this work. A complete copy of the license is available at the end of this document. In simple terms,
the usage guidelines are as follows:
You may not represent this work as your own work, or remove any authorship text or credits from this work.
You may not redistribute this work under more restrictive permissions than those under which it was provided to you.
If you add a substantive portion to the work and contribute it back to the project (at least one complete
module) you may add your name to the end of the authors list for this document (which will appear on the
front page)
If you contribute minor changes and corrections you may add yourself to the contributors list below.
If you translate this document in its entirety, you may add your name to the authors list in the form Translated by Joe Bloggs.
If you sponsor a module or lesson, you may request the author to include an acknowledgement in the
beginning of each lesson contributed, e.g.:
Note: This lesson was sponsored by MegaCorp.
If you are unsure about what you may do under this license, please contact us at [email protected] and
we will advise you if what you intend doing is acceptable.
If you publish this work under a self publishing site such as http:://lulu.com we request that you donate the
profits to the QGIS project.
You may not commercialise this work, except with the expressed permission of the authors. To be clear,
by commercialisation we mean that you may not sell for profit, create commercial derivative works (e.g.
selling content for use as articles in a magazine). The exception to this is if all the profits are given to the
QGIS project. You may (and we encourage you to do so) use this work as a text book when conducting
training courses, even if the course itself is commercial in nature. In other words, you are welcome to make
money by running a training course that uses this work as a text book, but you may not profit off the sales
of the book itself - all such profits should be contributed back to QGIS.
1.1.4 Authors
Rdiger Thiede ([email protected]) - Rudi has written the QGIS instructional materials and parts of the
PostGIS materials.
Tim Sutton ([email protected]) - Tim has overseen and guided the project and co-authored the PostgreSQL
and PostGIS parts. Tim also authored the custom sphinx theme used for this manual.
Horst Dster ([email protected] ) - Horst co-authored the PostgreSQL and PostGIS parts
Marcelle Sutton ([email protected]) - Marcelle provided proof-reading and editorial advice during the
creation of this work.
1.1.6 Sponsors
Cape Peninsula University of Technology
1.1.7 Data
Note:
The sample data used throughout the
http://qgis.org/downloads/data/training_manual_exercise_data.zip
manual
can
be
downloaded
here:
The sample data that accompanies this resource is freely available and comes from the following sources:
Streets and Places datasets from OpenStreetMap (http://www.openstreetmap.org/)
Property boundaries (urban and rural), water bodies from NGI (http://www.ngi.gov.za/)
SRTM DEM from the CGIAR-CGI (http://srtm.csi.cgiar.org/)
1.2.1
manual
can
be
downloaded
here:
Try Yourself
Note: These instructions assume you have a good knowledge of QGIS and are not intended to be used as teaching
material.
If you wish to replace the default data set with localised data for your course, this can easily be done with tools
built into QGIS. The region you choose to use should have a good mix of urban and rural areas, containing roads
of differing significance, area boundaries (such as nature reserves or farms) and surface water, such as streams and
rivers.
Open a new QGIS project
In the Vector menu dropdown, select OpenStreetMap -> Download Data. You can then manually enter the
co-ordinates of the region you wish to use, or you can use an existing layer to set the co-ordinates.
Choose a location to save the resulting .osm file and click Ok:
You can then open the .osm file using the Add Vector Layer button. You may need to select All files in the
browser window. Alternatively, you can drag and drop the file into the QGIS window.
In the dialog which opens,
multilinestrings layer:
This will load four layers into your map which relate to OSMs naming conventions (you may need to zoom in/out
to see the vector data).
We need to extract the useful data from these layers, rename them and create corresponding shape files:
First, double-click the multipolygons layer to open the Layer properties dialog.
In the General tab, click Query Builder to open the Query builder window.
This layer contains three fields whose data we will need to extract for use throughout the Training Manual:
building
natural (specifically, water)
landuse
You can sample the data your region contains in order to see what kind of results your region will yield. If you
find that landuse returns no results, then feel free to exclude it.
1.2. Preparing Exercise Data
Youll need to write filter expressions for each field to extract the data we need. Well use the building field as
an example here:
Enter the following expression into the text area: building != "NULL" and click Test to see how many
results the query will return. If the number of results is small, you may wish to have a look at the layers
Attribute Table to see what data OSM has returned for your region:
Click Ok and youll see that the layer elements which are not buildings have been removed from the map.
We now need to save the resulting data as a shapefile for you to use during your course:
Right-click the multipolygons layer and select Save As...
Make sure the file type is ESRI Shapefile and save the file in your new exercise_data directory,
under a directory called epsg4326.
Make sure No Symbology is selected (well add symbology as part of the course later on).
You can also select Add saved file to map.
Once the buildings layer has been added to the map, you can repeat the process for the natural and landuse
fields using the following expressions:
Note: Make sure you clear the previous filter (via the Layer properties dialog) from the multipolygons layer
before proceeding with the next filter expression!
The important thing is that you have 6 layers matching those shown above and that all those layers have some
data.
The last step is to create a spatiallite file from the landuse layer for use during the course:
Right-click the landuse layer and select Save as...
Select SpatialLite as the format and save the file as landuse under the epsg4326 directory.
Click Ok.
Delete the landuse.shp and its related files (if created).
1.2.2
For Module 6 (Creating Vector Data) and Module 8 (Rasters), youll also need raster images (SRTM DEM) which
cover the region you have selected for your course.
SRTM DEM can be downloaded from the CGIAR-CGI: http://srtm.csi.cgiar.org/
Youll need images which cover the entire region you have chosen to use.
Once you have downloaded the required file(s), they should be saved in the exercise_data directory under
raster/SRTM/.
In Module 6, Lesson 1.2 shows close-up images of three school sports fields which students are asked to digitize.
Youll therefore need to reproduce these images using your new SRTM DEM tiff file(s). There is no obligation to
use school sports fields: any three school land-use types can be used (e.g. different school buildings, playgrounds
or car parks).
For reference, the images in the example data are:
1.2.3
Having created your localised dataset, the final step is to replace the tokens in the conf.py file so that the
appropriate names will appear in your localised version of the Training Manual.
The tokens you need to replace are as follows:
majorUrbanName: this defaults to Swellendam. Replace with the name of the major town in your
region.
schoolAreaType1: this defaults to athletics field. Replace with the name of the largest school area
type in your region.
largeLandUseArea: this defaults to Bontebok National Park. Replace with the name of a large
landuse polygon in your region.
srtmFileName: this defaults to srtm_41_19.tif. Replace this with the filename of your SRTM
DEM file.
localCRS: this defaults to WGS 84 / UTM 34S. You should replace this with the correct CRS for your
region.
10
CAPITOLUL 2
Basic
In this category, the course assumes that you have little or no prior experience with theoretical GIS knowledge or
the operation of a GIS program.
Limited theoretical background will be provided to explain the purpose of an action you will be performing in the
program, but the emphasis is on learning by doing.
When you complete the course, you will have a better concept of the possibilities of GIS, and how to harness their
power via QGIS.
11
Intermediate
In this category, it is assumed that you have working knowledge and experience of the everyday uses of GIS.
Following the instructions for the beginner level will provide you with familiar ground, as well as to make you
aware of the cases where QGIS does things slightly differently from other software you may be used to. You will
also learn how to use analysis functions in QGIS.
When you complete the course, you should be comfortable with using QGIS for all of the functions you usually
need from a GIS for everyday use.
Advanced
In this category, the assumption is that you are experienced with GIS, have knowledge of and experience with
spatial databases, using data on a remote server, perhaps writing scripts for analysis purposes, etc.
Following the instructions for the other two levels will familiarize you with the approach that the QGIS interface
follows, and will ensure that you know how to access the basic functions that you need. You will also be shown
how to make use of QGIS plugin system, database access system, and so on.
When you complete the course, you should be well-acquainted with the everyday operation of QGIS, as well as
its more advanced functions.
12
Note: The screenshots for this course were taken in QGIS 2.0 running on MacOS. Depending on your setup, the
screens you encounter may well appear somewhat different. However, all the same buttons will still be available,
and the instructions will work on any OS. You will need QGIS 2.0 (the latest version at time of writing) to use this
course.
Lets get started right away!
2.2.1
Click on the Browse button and navigate to the file exercise_data/epsg4326/roads.shp (in your
course directory). With this file selected, click Open. You will see the original dialog, but with the file path
filled in. Click Open here as well. The data you specified will now load.
Congratulations! You now have a basic map. Now would be a good time to save your work.
Click on the Save As button:
Save the map under exercise_data/ and call it basic_map.qgs.
Check your results
2.2.2 In Conclusion
Youve learned how to add a layer and create a basic map!
13
2.3.1
14
Expanding collapsed items (by clicking the arrow or plus symbol beside them) will provide you with more information on the layers current appearance.
Right-clicking on a layer will give you a menu with lots of extra options. You will be using some of them before
long, so take a look around!
Some versions of QGIS have a separate Control rendering order checkbox just underneath the Layers list. Dont
worry if you cant see it. If it is present, ensure that its checked for now.
Note: A vector layer is a dataset, usually of a specific kind of object, such as roads, trees, etc. A vector layer can
consist of either points, lines or polygons.
Toolbars
Your most oft-used sets of tools can be turned into toolbars for basic access. For example, the File toolbar allows
you to save, load, print, and start a new project. You can easily customize the interface to see only the tools you
use most often, adding or removing toolbars as necessary via the View Toolbars menu.
Even if they are not visible in a toolbar, all of your tools will remain accessible via the menus. For example, if
you remove the File toolbar (which contains the Save button), you can still save your map by clicking on the File
menu and then clicking on Save.
2.3.2
Try Yourself 1
Try to identify the four elements listed above on your own screen, without referring to the diagram above. See if
you can identify their names and functions. You will become more familiar with these elements as you use them
in the coming days.
Check your results
2.3.3
Try Yourself 2
Try to find each of these tools on your screen. What is their purpose?
2.3. Lesson: An Overview of the Interface
15
1.
2.
3.
4.
5.
Note: If any of these tools is not visible on the screen, try enabling some toolbars that are currently hidden. Also
keep in mind that if there isnt enough space on the screen, a toolbar may be shortened by hiding some of its tools.
You can see the hidden tools by clicking on the double right arrow button in any such collapsed toolbar. You can
see a tooltip with the name of any tool by holding your mouse over the tool for a while.
Check your results
16
CAPITOLUL 3
In this module, you will create a basic map which will be used later as a basis for further demonstrations of QGIS
functionality.
3.1.1
Its important to know that the data you will be working with does not only represent where objects are in space,
but also tells you what those objects are.
From the previous exercise, you should have the roads layer loaded in your map. What you can see right now is
merely the position of the roads.
To see all the data available to you, with the roads layer selected in the Layers panel:
Click on this button:
It will show you a table with more data about the roads layer. This extra data is called attribute data. The lines
that you can see on your map represent where the roads go; this is the spatial data.
These definitions are commonly used in GIS, so its essential to remember them!
You may now close the attribute table.
Vector data represents features in terms of points, lines and polygons on a coordinate plane. It is usually used to
store discrete features, like roads and city blocks.
3.1.2
The Shapefile is a specific file format that allows you to store GIS data in an associated group of files. Each layer
consists of several files with the same name, but different file types. Shapefiles are easy to send back and forth,
and most GIS software can read them.
Refer back to the introductory exercise in the previous section for instructions on how to add vector layers.
Load the data sets into your map following the same method:
17
places
water
rivers
buildings
Check your results
3.1.3
Databases allow you to store a large volume of associated data in one file. You may already be familiar with
a database management system (DBMS) such as Microsoft Access. GIS applications can also make use of
databases. GIS-specific DBMSes (such as PostGIS) have extra functions, because they need to handle spatial
data.
Click on this icon:
(If youre sure you cant see it at all, check that the Manage Layers toolbar is enabled.)
It will give you a new dialog. In this dialog:
Click the New button.
In the same folder as the other data, you should find the file landuse.sqlite. Select it and click Open.
You will now see the first dialog again. Notice that the dropdown select above the three buttons now reads
land_use.db@..., followed by the path of the database file on your computer.
Click the Connect button. You should see this in the previously empty box:
18
19
... would result in roads and places being hidden as they run underneath urban areas.
To resolve this problem:
Click and drag on a layer in the Layers list.
Reorder them to look like this:
Youll see that the map now makes more sense visually, with roads and buildings appearing above the land use
regions.
3.1.5 In Conclusion
Now youve added all the layers you need from several different sources.
20
3.2.1
To change a layers symbology, open its Layer Properties. Lets begin by changing the color of the landuse layer.
Right-click on the landuse layer in the Layers list.
Select the menu item Properties in the menu that appears.
Note: By default, you can also access a layers properties by double-clicking on the layer in the Layers list.
In the Properties window:
Select the Style tab at the extreme left:
21
3.2.2
Try Yourself
3.2.3
This is good stuff so far, but theres more to a layers symbology than just its color. Next we want to eliminate the
lines between the different land use areas so as to make the map less visually cluttered.
Open the Layer Properties window for the landuse layer.
22
Under the Style tab, you will see the same kind of dialog as before. This time, however, youre doing more than
just quickly changing the color.
In the Symbol Layers panel, expand the Fill dropdown (if necessary) and select the Simple fill option:
Click on the Border style dropdown. At the moment, it should be showing a short line and the words Solid
Line.
Change this to No Pen.
Click OK.
Now the landuse layer wont have any lines between areas.
3.2.4
Try Yourself
Change the water layers symbology again so that it is has a darker blue outline.
Change the rivers layers symbology to a sensible representation of waterways.
Check your results
23
3.2.5
Sometimes you will find that a layer is not suitable for a given scale. For example, a dataset of all the continents
may have low detail, and not be very accurate at street level. When that happens, you want to be able to hide the
dataset at inappropriate scales.
In our case, we may decide to hide the buildings from view at small scales. This map, for example ...
... is not very useful. The buildings are hard to distinguish at that scale.
To enable scale-based rendering:
Open the Layer Properties dialog for the buildings layer.
Activate the General tab.
Enable scale-based rendering by clicking on the checkbox labeled Scale dependent visibility:
24
3.2.6
Now that you know how to change simple symbology for layers, the next step is to create more complex symbology. QGIS allows you to do this using symbol layers.
Go back to the landuse layers symbol properties panel (by clicking
Simple fill in the Symbol layers panel).
In this example, the current symbol has no outline (i.e., it uses the No Pen border style).
25
Select the Fill in the Symbol layers panel. Then click the Add symbol layer button:
Click on it and the dialog will change to look somewhat like this:
26
(It may appear somewhat different in color, for example, but youre going to change that anyway.)
Now theres a second symbol layer. Being a solid color, it will of course completely hide the previous kind of
symbol. Plus, it has a Solid Line border style, which we dont want. Clearly this symbol has to be changed.
Note: Its important not to get confused between a map layer and a symbol layer. A map layer is a vector (or
raster) that has been loaded into the map. A symbol layer is part of the symbol used to represent a map layer. This
course will usually refer to a map layer as just a layer, but a symbol layer will always be called a symbol layer, to
prevent confusion.
With the new Simple Fill layer selected:
Set the border style to No Pen, as before.
Change the fill style to something other than Solid or No brush. For example:
27
Click OK. Now you can see your results and tweak them as needed.
You can even add multiple extra symbol layers and create a kind of texture for your layer that way.
28
Its fun! But it probably has too many colors to use in a real map...
3.2.7
Try Yourself
Remembering to zoom in if necessary, create a simple, but not distracting texture for the buildings layer
using the methods above.
Check your results
3.2.8
When symbol layers are rendered, they are also rendered in a sequence, similar to the way the different map layers
are rendered. This means that in some cases, having many symbol layers in one symbol can cause unexpected
results.
Give the roads layer an extra symbol layer (using the method for adding symbol layers demonstrated above).
Give the base line a Pen width of 0.3, a white color and select Dashed Line from the Pen Style dropdown.
Give the new, uppermost layer a thickness of 1.3 and ensure that it is a Solid Line.
Youll notice that this happens:
29
30
Select Enable symbol levels. You can then set the layer ordering of each symbol by entering the corresponding
level number. 0 is the bottom layer.
In our case, we want to reverse the ordering, like this:
31
This will render the dashed, white line above the thick black line.
Click OK twice to return to the map.
The map will now look like this:
32
Also note that the meeting points of roads are now merged, so that one road is not rendered above another.
When youre done, remember to save the symbol itself so as not to lose your work if you change the symbol again
in the future. You can save your current symbol style by clicking the Save Style ... button under the Style tab of
the Layer Properties dialog. Generally, you should save as QGIS Layer Style File.
Save your style under exercise_data/styles. You can load a previously saved style at any time by clicking
the Load Style ... button. Before you change a style, keep in mind that any unsaved style you are replacing will be
lost.
3.2.9
Try Yourself
33
3.2.10
Try Yourself
Symbol levels also work for classified layers (i.e., layers having multiple symbols). Since we havent covered
classification yet, you will work with some rudimentary pre-classified data.
Create a new map and add only the roads dataset.
Apply the style advanced_levels_demo.qml provided in exercise_data/styles.
Zoom in to the Swellendam area.
Using symbol layers, ensure that the outlines of layers flow into one another as per the image below:
34
3.2.11
In addition to setting fill colors and using predefined patterns, you can use different symbol layer types entirely.
The only type weve been using up to now was the Simple Fill type. The more advanced symbol layer types allow
you to customize your symbols even further.
Each type of vector (point, line and polygon) has its own set of symbol layer types. First we will look at the types
available for points.
Point Symbol Layer Types
Open your basic_map project.
Change the symbol properties for the places layer:
35
You can access the various symbol layer types by selecting the
Simple marker layer in the Symbol layers panel, then click the Symbol layer type dropdown:
36
Investigate the various options available to you, and choose a symbol with styling you think is appropriate.
If in doubt, use a round Simple marker with a white border and pale green fill, with a size of 3,00 and an
Outline width of 0.5.
Line Symbol Layer Types
To see the various options available for line data:
Change the symbol layer type for the roads layers topmost symbol layer to Marker line:
Select the Simple marker layer in the Symbol layers panel. Change the symbol properties to match this
dialog:
37
38
39
40
41
As a result, you have a textured symbol for the water layer, with the added benefit that you can change the size,
shape and distance of the individual dots that make up the texture.
3.2.12
Note: To do this exercise, you will need to have the free vector editing software Inkscape installed.
Start the Inkscape program.
You will see the following interface:
42
You should find this familiar if you have used other vector image editing programs, like Corel.
First, well change the canvas to a size appropriate for a small texture.
Click on the menu item File Document Properties. This will give you the Document Properties dialog.
Change the Units to px.
Change the Width and Height to 100.
Close the dialog when you are done.
Click on the menu item View Zoom Page to see the page you are working with.
Select the Circle tool:
43
Click and drag on the page to draw an ellipse. To make the ellipse turn into a circle, hold the ctrl button
while youre drawing it.
Right-click on the circle you just created and open its Fill and Stroke:
Change the Stroke paint to a pale grey-blue and the Stroke style to a darker color with thin stroke:
44
45
Save it as landuse_symbol under the directory that the course is in, under exercise_data/symbols,
as an SVG file.
In QGIS:
Open the Layer Properties for the landuse layer.
Change the symbol structure to the following and find your SVG image via the Browse button:
46
47
Your landuse layer should now have a texture like the one on this map:
48
3.2.13 In Conclusion
Changing the symbology for the different layers has transformed a collection of vector files into a legible map.
Not only can you see whats happening, its even nice to look at!
49
50
CAPITOLUL 4
Classifying vector data allows you to assign different symbols to features (different objects in the same layer),
depending on their attributes. This allows someone who uses the map to easily see the attributes of various
features.
4.1.1
Open the attribute table for the places layer (refer back to the section Working with Vector Data if necessary).
Which field would be the most useful to represent in label form, and why?
Check your results
4.1.2 In Conclusion
You now know how to use the attribute table to see what is actually in the data youre using. Any dataset will only
be useful to you if it has the attributes that you care about. If you know which attributes you need, you can quickly
decide if youre able to use a given dataset, or if you need to look for another one that has the required attribute
data.
51
4.2.1
Before being able to access the Label tool, you will need to ensure that it has been activated.
Go to the menu item View Toolbars.
Ensure that the Label item has a check mark next to it. If it doesnt, click on the Label item, and it will be
activated.
Click on the places layer in the Layers list, so that it is highlighted.
Click on the following toolbar button:
This gives you the Layer labeling settings dialog.
Check the box next to Label this layer with....
Youll need to choose which field in the attributes will be used for the labels. In the previous lesson, you decided
that the NAME field was the most suitable one for this purpose.
Select name from the list:
52
Click OK.
The map should now have labels like this:
53
4.2.2
Depending on the styles you chose for your map in earlier lessons, youll might find that the labels are not
appropriately formatted and either overlap or are too far away from their point markers.
Open the Label tool again by clicking on its button as before.
Make sure Text is selected in the left-hand options list, then
update the text formatting options to match those shown here:
54
Thats the font problem solved! Now lets look at the problem of the labels overlapping the points, but before we
do that, lets take a look at the Buffer option.
Open the Label tool dialog.
Select Buffer from the left-hand options list.
Select the checkbox next to Draw text buffer, then choose options
to match those shown here:
55
Click Apply.
Youll see that this adds a colored buffer or border to the place labels, making them easier to pick out on the map:
56
Now we can address the positioning of the labels in relation to their point markers.
In the Label tool dialog, go to the Placement tab.
Change the value of Distance to 2mm and make sure that
Around point is selected:
57
Click Apply.
Youll see that the labels are no longer overlapping their point markers.
4.2.3
In many cases, the location of a point doesnt need to be very specific. For example, most of the points in the
places layer refer to entire towns or suburbs, and the specific point associated with such features is not that specific
on a large scale. In fact, giving a point that is too specific is often confusing for someone reading a map.
To name an example: on a map of the world, the point given for the European Union may be somewhere in Poland,
for instance. To someone reading the map, seeing a point labeled European Union in Poland, it may seem that the
capital of the European Union is therefore in Poland.
So, to prevent this kind of misunderstanding, its often useful to deactivate the point symbols and replace them
completely with labels.
In QGIS, you can do this by changing the position of the labels to be rendered directly over the points they refer
to.
Open the Layer labeling settings dialog for the places layer.
Select the Placement option from the options list.
Click on the Offset from point button.
58
This will reveal the Quadrant options which you can use to set the position of the label in relation to the point
marker. In this case, we want the label to be centered on the point, so choose the center quadrant:
Hide the point symbols by editing the layer style as usual, and setting the size of the Ellipse marker width
and height to 0:
59
60
If you were to zoom out on the map, you would see that some of the labels disappear at larger scales to avoid
overlapping. Sometimes this is what you want when dealing with datasets that have many points, but at other
times you will lose useful information this way. There is another possibility for handling cases like this, which
well cover in a later exercise in this lesson.
4.2.4
Return the label and symbol settings to have a point marker and a label offset of 2.00mm. You may like to
adjust the styling of the point marker or labels at this stage.
Check your results
Set the map to the scale 1:100000. You can do this by typing it into the Scale box in the Status Bar.
Modify your labels to be suitable for viewing at this scale.
Check your results
61
4.2.5
Now that you know how labeling works, theres an additional problem. Points and polygons are easy to label, but
what about lines? If you label them the same way as the points, your results would look like this:
We will now reformat the roads layer labels so that they are easy to understand.
Hide the Places layer so that it doesnt distract you.
Activate labels for the streets layer as before.
Set the font Size to 10 so that you can see more labels.
Zoom in on the Swellendam town area.
In the Label tool dialogs Advanced tab, choose the following settings:
62
Youll probably find that the text styling has used default values and the labels are consequently very hard to read.
Set the label text format to have a dark-grey or black Color and a light-yellow buffer.
The map will look somewhat like this, depending on scale:
63
Youll see that some of the road names appear more than once and thats not always necessary. To prevent this
from happening:
In the Label labelling settings dialog, choose the Rendering option and select the Merge connected lines to
avoid duplicate labels:
64
Click OK
Another useful function is to prevent labels being drawn for features too short to be of notice.
In the same Rendering panel, set the value of Suppress labeling of features smaller than ... to 5mm and note
the results when you click Apply.
Try out different Placement settings as well. As weve seen before, the horizontal option is not a good idea in this
case, so lets try the curved option instead.
Select the Curved option in the Placement panel of the Layer labeling settings dialog.
Heres the result:
65
As you can see, this hides a lot of the labels that were previously visible, because of the difficulty of making some
of them follow twisting street lines and still be legible. You can decide which of these options to use, depending
on what you think seems more useful or what looks better.
4.2.6
button.
It has one fields which is of interest to us now: place which defines the type of urban area for each object. We
can use this data to influence the label styles.
Navigate to the Text panel in the places Labels panel.
In the Italic dropdown, select Edit... to open the Expression string builder:
66
67
68
4.2.7
Note: Were jumping ahead a bit here to demonstrate some advanced labeling settings. At the advanced level,
its assumed that youll know what the following means. If you dont, feel free to leave out this section and come
back later when youve covered the requisite materials.
Open the Attribute Table for places.
Enter edit mode by clicking this button:
Add a new column:
69
Use this to set custom font sizes for each different type of place (i.e., each key in the PLACE field).
Check your results
4.2.8
We cant cover every option in this course, but be aware that the Label tool has many other useful functions. You
can set scale-based rendering, alter the rendering priority for labels in a layer, and set every label option using
layer attributes. You can even set the rotation, XY position, and other properties of a label (if you have attribute
fields allocated for the purpose), then edit these properties using the tools adjacent to the main Label tool:
(These tools will be active if the required attribute fields exist and you are in edit mode.)
Feel free to explore more possibilities of the labeling system.
4.2.9 In Conclusion
Youve learned how to use layer attributes to create dynamic labels. This can make your map a lot more informative
and stylish!
70
This makes the maps labeling difficult to read and even overwhelming if there are numerous different landuse
areas on the map.
The goal for this lesson: To learn how to classify vector data effectively.
4.3.1
71
In the new panel, change the Column to landuse and the Color ramp to Greens.
Click the button labeled Classify:
72
Click OK.
Youll see something like this:
73
Click the arrow (or plus sign) next to landuse in the Layer list, youll see the categories explained:
Now our landuse polygons are appropriately colored and are classified so that areas with the same land use are the
same color. You may wish to remove the black border from the landuse layer:
74
75
This empty category is used to color any objects which do not have a landuse value defined or which have a NULL
value. It is important to keep this empty category so that areas with a NULL value are still represented on the map.
You may like to change the color to more obviously represent a blank or NULL value.
Remember to save your map now so that you dont lose all your hard-earned changes!
4.3.2
If youre only following the basic-level content, use the knowledge you gained above to classify the buildings
layer. Set the categorisation against the building column and use the Spectral color ramp.
Note: Remember to zoom into an urban area to see the results.
4.3.3
There are four types of classification: nominal, ordinal, interval and ratio.
In nominal classification, the categories that objects are classified into are name-based; they have no order. For
example: town names, district codes, etc.
76
In ordinal classification, the categories are arranged in a certain order. For example, world cities are given a rank
depending on their importance for world trade, travel, culture, etc.
In interval classification, the numbers are on a scale with positive, negative and zero values. For example: height
above/below sea level, temperature above/below freezing (0 degrees Celsius), etc.
In ratio classification, the numbers are on a scale with only positive and zero values. For example: temperature
above absolute zero (0 degrees Kelvin), distance from a point, the average amount of traffic on a given street per
month, etc.
In the example above, we used nominal classification to assign each farm to the town that it is administered by.
Now we will use ratio classification to classify the farms by area.
Save your landuse symbology (if you want to keep it) by clicking on the Save Style ... button in the Style
dialog.
Were going to reclassify the layer, so existing classes will be lost if not saved.
Close the Style dialog.
Open the Attributes Table for the landuse layer.
We want to classify the landuse areas by size, but theres a problem: they dont have a size field, so well have to
make one.
Enter edit mode by clicking this button:
:align: center
Add a new column with this button:
77
Click OK.
The new field will be added (at the far right of the table; you may need to scroll horizontally to see it). However,
at the moment it is not populated, it just has a lot of NULL values.
To solve this problem, well need to calculate the areas.
Open the field calculator:
align center
Youll get this dialog:
78
Change the values at the top of the dialog to look like this:
79
80
Choose Gradient (if its not selected already) and click OK. Youll see this:
81
Youll be using this to denote area, with small areas as Color 1 and large areas as Color 2.
Choose appropriate colors.
In the example, the result looks like this:
82
Click OK.
Choose a suitable name for the new color ramp.
Click OK after filling in the name.
Now youll have something like this:
83
84
4.3.4
4.3.5
Its often useful to combine multiple criteria for a classification, but unfortunately normal classification only takes
one attribute into account. Thats where rule-based classification comes in handy.
Open the Layer Properties dialog for the landuse layer.
Switch to the Style tab.
Switch the classification style to Rule-based. Youll get this:
85
86
87
Add a new criterion "landuse" != residential AND AREA >= 0.00005 and choose a
mid-green color.
Add another new criterion "name" = |majorUrbanName| and assign it a darker grey-blue color
in order to indicate the towns importance in the region.
Click and drag this criterion to the top of the list.
These filters are exclusive, in that they collectively exclude some areas on the map (i.e. those which are smaller
that 0.00005, are not residential and are not Swellendam). This means that the excluded polygons take the style
of the default (no filter) category.
We know that the excluded polygons on our map cannot be residential areas, so give the default category a suitable
pale green color.
Your dialog should now look like this:
88
89
Now you have a map with Swellendam the most prominent residential area and other non-residential areas colored
according to their size.
4.3.6 In Conclusion
Symbology allows us to represent the attributes of a layer in an easy-to-read way. It allows us as well as the map
reader to understand the significance of features, using any relevant attributes that we choose. Depending on the
problems you face, youll apply different classification techniques to solve them.
90
CAPITOLUL 5
In this module, youll learn how to use the QGIS Map Composer to produce quality maps with all the requisite
map components.
5.1.1
QGIS allows you to create multiple maps using the same map file. For this reason, it has a tool called the Composer
Manager.
Click on the Project Composer Manager menu entry to open this tool. Youll see a blank Composer
manager dialog appear.
Click the Add button and give the new composer the name of Swellendam.
Click OK.
Click the Show button.
(You could also close the dialog and navigate to a composer via the File Print Composers menus, as in the
image below.)
91
Whichever route you take to get there, you will now see the Print Composer window:
92
5.1.2
In this example, the composition was already the way we wanted it. Ensure that yours is as well.
In the Print Composer window, check that the values under Composition Paper and Quality are set to
the following:
Size: A4 (210x297mm)
Orientation: Landscape
Quality: 300dpi
Now youve got the page layout the way you wanted it, but this page is still blank. It clearly lacks a map. Lets fix
that!
Click on the Add New Map button:
With this tool activated, youll be able to place a map on the page.
Click and drag a box on the blank page:
93
Note: Your map may look a lot different, of course! This depends on how your own project is set up. But not to
worry! These instructions are general, so they will work the same regardless of what the map itself looks like.
Be sure to leave margins along the edges, and a space along the top for the title.
Zoom in and out on the page (but not the map!) by using these buttons:
Zoom and pan the map in the main QGIS window. You can also pan the map using the Move item content
94
tool:
When zooming in, the map view will not refresh by itself. This is so that it doesnt waste your time redrawing the
map while youre zooming the page to where you want it, but it also means that if you zoom in or out, the map
will be at the wrong resolution and will look ugly or unreadable.
Force the map to refresh by clicking this button:
Remember that the size and position youve given the map doesnt need to be final. You can always come back
and change it later if youre not satisfied. For now, you need to ensure that youve saved your work on this map.
Because a Composer in QGIS is part of the main map file, youll need to save your main project. Go to the main
QGIS window (the one with the Layers list and all the other familiar elements you were working with before), and
save your project from there as usual.
5.1.3
Now your map is looking good on the page, but your readers/users are not being told whats going on yet. They
need some context, which is what youll provide for them by adding map elements. First, lets add a title.
Click on this button:
Click on the page, above the map, and a label will appear at the top of the map.
Resize it and place it in the top center of the page. It can be resized and moved in the same way that you
resized and moved the map.
As you move the title, youll notice that guidelines appear to help you position the title in the center of the page.
However, there is also a tool to help position the title relative to the map (not the page):
and click on the dropdown arrow next to it to reveal the positioning options
95
To make sure that you dont accidentally move these elements around now that youve aligned them:
Right-click on both the map and the label.
A small lock icon will appear in the corner to tell you that an element cant be dragged right now. You can always
right-click on an element again to unlock it, though.
Now the label is centered to the map, but not the contents. To center the contents of the label:
Select the label by clicking on it.
Click on the Item Properties tab in the side panel of the Composer window.
Change the text of the label to Swellendam:
Use this interface to set the font and alignment options:
Choose a large but sensible font (the example will use the default font with a size of 36) and set the
Horizontal Alignment to Center.
You can also change the font color, but its probably best to keep it black as per the default.
The default setting is not to add a frame to the titles text box. However, if you wish to add a frame, you can do
so:
In the Item Properties tab, scroll down until you see the Frame option.
Click the Frame checkbox to enable the frame. You can also change the frames color and width.
In this example, we wont enable the frame, so here is our page so far:
96
5.1.4
The map reader also needs to be able to see what various things on the map actually mean. In some cases, like the
place names, this is quite obvious. In other cases, its more difficult to guess, like the colors of the farms. Lets
add a new legend.
Click on this button:
Click on the page to place the legend, and move it to where you want it:
97
5.1.5
Not everything on the legend is necessary, so lets remove some unwanted items.
In the Item Properties tab, youll find the Legend items panel.
Select the buildings entry.
Delete it from the legend by clicking the minus button:
You can also rename items.
Select a layer from the same list.
Click the Edit button:
Rename the layers to Places, Roads and Streets, Surafce Water, and Rivers.
Set landuse to Hidden, then click the down arrow and edit each category to name them on the legend.
You can also reorder the items:
As the legend will likely be widened by the new layer names, you may wish to move and resize the legend and or
map. This is the result:
98
5.1.6
The button on the left is the Print button, which interfaces with a printer. Since the printer options will differ
depending on the model of printer that youre working with, its probably better to consult the printer manual or a
general guide to printing for more information on this topic.
The other three buttons allow you to export the map page to a file. There are three export formats to choose from:
Export as Image
Export as SVG
Export as PDF
Exporting as an image will give you a selection of various common image formats to choose from. This is probably
the simplest option, but the image it creates is dead and difficult to edit.
The other two options are more common.
If youre sending the map to a cartographer (who may want to edit the map for publication), its best to export
as an SVG. SVG stands for Scalable Vector Graphic, and can be imported to programs like Inkscape or other
vector image editing software.
If you need to send the map to a client, its most common to use a PDF, because its easier to set up printing
options for a PDF. Some cartographers may prefer PDF as well, if they have a program that allows them to import
and edit this format.
For our purposes, were going to use PDF.
Click the Export as PDF button:
99
5.1.7 In Conclusion
Close the Composer window.
Save your map.
Find your exported PDF using your operating systems file manager.
Open it.
Bask in its glory.
Congratulations on your first completed QGIS map project!
5.2 Assignment 1
Open your existing map project and revise it thoroughly. If you have noticed small errors or things youd have
liked to fix earlier, do so now.
While customizing your map, keep asking yourself questions. Is this map easy to read and understand for someone
whos unfamiliar with the data? If I saw this map on the Internet, or on a poster, or in a magazine, would it capture
my attention? Would I want to read this map if it wasnt mine?
If youre doing this course at a
Basic or
Intermediate level, read up on techniques from the more
advanced sections. If you see something youd like to do in your map, why not try to implement it?
If this course is being presented to you, the course presenter may require you to submit a final version of your
map, exported to PDF, for evaluation. If youre doing this course by yourself, its recommended that you evaluate
your own map using the same criteria. Your map will be evaluated on the overall appearance and symbology of
the map itself, as well as the appearance and layout of the map page and elements. Remember that the emphasis
for evaluating the appearance of maps will always be ease of use. The nicer the map is to look at and the easier it
is to understand at a glance, the better.
Happy customizing!
5.2.1 In Conclusion
The first four modules have taught you how to create and style a vector map. In the next four modules, youll learn
how to use QGIS for a complete GIS analysis. This will include creating and editing vector data; analyzing vector
data; using and analyzing raster data; and using GIS to solve a problem from start to finish, using both raster and
vector data sources.
100
CAPITOLUL 6
Creating maps using existing data is just the beginning. In this module, youll learn how to modify existing vector
data and create new datasets entirely.
6.1.1
Before you can add new vector data, you need a vector dataset to add it to. In our case, youll begin by creating
new data entirely, rather than editing an existing dataset. Therefore, youll need to define your own new dataset
first.
Youll need to open the New Vector Layer dialog that will allow you to define a new layer.
Navigate to and click on the menu entry Layer New New Shapefile Layer.
Youll be presented with the following dialog:
101
Its important to decide which kind of dataset you want at this stage. Each different vector layer type is built
differently in the background, so once youve created the layer, you cant change its type.
For the next exercise, were going to be creating new features which describe areas. For such features, youll need
to create a polygon dataset.
Click on the Polygon radio button:
102
This has no impact on the rest of the dialog, but it will cause the correct type of geometry to be used when the
vector dataset is created.
The next field allows you to specify the Coordinate Reference System, or CRS. A CRS specifies how to describe
a point on Earth in terms of coordinates, and because there are many different ways to do this, there are many
different CRSs. The CRS of this project is WGS84, so its already correct by default:
Next there is a collection of fields grouped under New attribute. By default, a new layer has only one attribute,
the id field (which you should see in the Attributes list) below. However, in order for the data you create to be
useful, you actually need to say something about the features youll be creating in this new layer. For our current
purposes, it will be enough to add one field called name.
Replicate the setup below, then click the Add to attributes list button:
103
104
6.1.2
When you create new data, it obviously has to be about objects that really exist on the ground. Therefore, youll
need to get your information from somewhere.
There are many different ways to obtain data about objects. For example, you could use a GPS to capture points
in the real world, then import the data into QGIS afterwards. Or you could survey points using a theodolite, and
enter the coordinates manually to create new features. Or you could use the digitizing process to trace objects
from remote sensing data, such as satellite imagery or aerial photography.
For our example, youll be using the digitizing approach. Sample raster datasets are provided, so youll need to
import them as necessary.
Click on the Add Raster Layer button:
Navigate to exercise_data/raster/.
Select the file 3420C_2010_327_RGB_LATLNG.tif.
Click Open. An image will load into your map.
Find the new image in the Layers list.
Click and drag it to the bottom of the list so that you can still see your other layers.
Find and zoom to this area:
Note: If your buildings layer symbology is covering part or all of the raster layer, you can temporarily disable
the layer by deselecting it in the Layers panel. You may also wish to hide the roads symbology if you find it
distracting.
Youll be digitizing these three fields:
105
In order to begin digitizing, youll need to enter edit mode. GIS software commonly requires this to prevent you
from accidentally editing or deleting important data. Edit mode is switched on or off individually for each layer.
To enter edit mode for the school_property layer:
Click on the layer in the Layer list to select it. (Make very sure that the correct layer is selected, otherwise
youll edit the wrong layer!)
Click on the Toggle Editing button:
If you cant find this button, check that the Digitizing toolbar is enabled. There should be a check mark next to the
View Toolbars Digitizing menu entry.
As soon as you are in edit mode, youll see the digitizing tools are now active:
Four other relevant buttons are still inactive, but will become active when we start interacting with our new data:
Start digitizing by clicking on a point somewhere along the edge of the field.
Place more points by clicking further along the edge, until the shape youre drawing completely covers the
field.
After placing your last point, right-click to finish drawing the polygon. This will finalize the feature and
show you the Attributes dialog.
Fill in the values as below:
107
108
the Edit Undo menu item or the ctrl + z keyboard shortcut to undo mistakes.
6.1.3
Try Yourself
Digitize the school itself and the upper field. Use this image to assist you:
6.1.4
Try Yourself
Create a new line feature called routes.shp with attributes id and type. (Use the approach above to
guide you.)
109
Were going to digitize two routes which are not already marked on the roads layer; one is a path, the other
is a track.
Our path runs along the southern edge of the suburb of Railton, starting and ending at marked roads:
One at a time, digitize the path and the track on the routes layer. Try to follow the routes as accurately as possible,
using points (left-click) at any corners or turns.
When creating each route, give them the type attribute value of path or track.
110
Youll probably find that only the points are marked; use the Layer Properties dialog to add styling to your routes.
Feel free to give different styles to the path and track.
Save your edits and toggle Edit mode.
Check your results
6.1.5 In Conclusion
Now you know how to create features! This course doesnt cover adding point features, because thats not really
necessary once youve worked with more complicated features (lines and polygons). It works exactly the same,
except that you only click once where you want the point to be, give it attributes as usual, and then the feature is
created.
Knowing how to digitize is important because its a very common activity in GIS programs.
6.2.1
To make topological editing easier, its best if you enable snapping. This will allow your mouse cursor to snap to
other objects while you digitize. To set snapping options:
Navigate to the menu entry Settings Snapping Options....
Set up your Snapping options dialog as shown:
111
Ensure that the box in the Avoid Int. column is checked (set to true).
Click OK to save your changes and leave the dialog.
Enter edit mode with the landuse layer selected.
Check under View Toolbars to make sure that your Advanced Digitizing toolbar is enabled.
Zoom to this area (enable layers and labels if necessary):
112
When prompted, give it a OGC_FID of 999, but feel free to leave the other values unchanged.
If youre careful while digitizing and allow the cursor to snap to the vertices of adjoining farms, youll notice that
there wont be any gaps between your new farm and the existing farms adjacent to it.
Note the undo/redo tools in the Advanced Digitizing toolbar:
6.2.2
Topology features can sometimes need to be updated. In our example, the landuse layer has some complex forest
areas which have recently been joined to form one area:
Instead of creating new polygons to join the forest areas, were going to use the Node Tool to edit the existing
polygons and join them.
Enter edit mode, if it isnt active already.
Select the Node Tool.
Pick an area of forest, select a corner and move it to an adjoining corner so two forest sections meet:
113
Click and drag the nodes until they snap into place.
114
Go ahead and join a few more areas using the Node Tool. You can also use the Add Feature tool if it is appropriate.
If you are using our example data, you should have a forest area looking something like this:
Dont worry if you have joined more, less or different areas of forest.
115
6.2.3
Move the slider from side to side and watch what happens:
116
6.2.4
It allows you to take a hole out of a feature, as long as the hole is bounded on all side by the feature. For example,
if youve digitized the outer boundaries of South Africa and you need to add a hole for Lesotho, youd use this
tool.
If you experiment with this tool, youll notice that the current snapping options prevent you from creating a ring in
the middle of the polygon. This would be fine if the area you wished to exclude linked to the polygons boundaries.
Disable snapping for the landuse layer via the dialog you used earlier.
Now try using the Add Ring tool tool to create a gap in the middle of the Bontebok National Part.
Delete your new feature by using the Delete Ring tool:
117
Note: You need to select a corner of the ring in order to delete it.
Check your results
6.2.5
It allows you to create an extra part of the feature, not directly connected to the main feature. For example, if
youve digitized the boundaries of mainland South Africa but you havent yet added the Prince Edward Islands,
youd use this tool to create them.
To use this tool, you must first select the polygon to which you wish to add the part by using the Select
Single Feature tool:
Now try using the Add Part tool to add an outlying area to the Bontebok National Part.
Delete your new feature by using the Delete Part tool:
Note: You need to select a corner of the part in order to delete it.
Check your results
6.2.6
118
119
6.2.7
The Split Features tool is similar to how you took part of the farm away, except that it doesnt delete either of the
two parts. Instead, it keeps them both.
120
At this point, it may seem as if nothing has happened. But remember that your symbology for the landuse
layer does not have any border, so the new division line will not be shown.
Use the Select Single Feature tool to select the corner you just split; the new feature will now be highlighted:
121
6.2.8
Now we will re-join the feature you just created to the original polygon:
Experiment with the Merge Selected Features and Merge Attributes of Selected Features tools.
Note the differences.
Check your results
6.2.9 In Conclusion
Topology editing is a powerful tool that allows you to create and modify objects quickly and easily, while ensuring
that they remain topologically correct.
122
6.3.1
Click Ok.
Select the roads layer in the Layers list.
Using the Identify tool, click on any street in the map.
Instead of the normal Identify dialog, youll see the now-familiar form instead:
123
6.3.2
If you are in edit mode, you can use this form to edit a features attributes.
Activate edit mode (if it isnt already activated).
Using the Identify tool, click on the main street running through Swellendam:
124
6.3.3
Its nice to edit things using a form, but you still have to enter everything by hand. Fortunately, forms have different
kinds of so-called widgets that allow you to edit data in various different ways.
Open the roads layers Layer Properties.
Switch to the Fields tab. Youll see this:
125
Click on the Line edit button in the same row as man_made and youll be given a new dialog.
Select Checkbox in the list of options:
126
Click OK.
Enter edit mode (if the roads layer is not already in edit mode.
Click on the Identify tool.
Click on the same main road you chose earlier.
Youll now see that the man_made attribute has a checkbox next to it denoting True (checked) or False
(unchecked).
6.3.4
Try Yourself
6.3.5
You can also design your own custom form completely from scratch.
Create a simple point layer named test-data with two attributes:
Name (text)
Age (text)
127
Capture a few points on your new layer using the digitizing tools so that you have a little data to play with.
You should be presented with the default QGIS generated attribute capture form each time you capture a
new point.
Note: You may need to disable Snapping if still enabled from earlier tasks.
128
6.3.6
Now we want to create our own custom form for the attribute data capture phase. To do this, you need to have
Qt4 Designer installed (only needed for the person who creates the forms). It should be provided as part of your
course materials, if youre using Windows. You may need to look for it if youre using another OS. In Ubuntu, do
the following in the terminal:
Note: At the time of writing, Qt5 is the latest version available. However, this process specifically requires Qt4
and is not necessarily compatible with Qt5.
sudo apt-get install qt4-designer
... and it should install automatically. Otherwise, look for it in the Software Center.
Start Designer by opening its Start Menu entry in Windows (or whatever approach is appropriate in your
OS).
In the dialog that appears, create a new dialog:
129
Look for the Widget Box along the left of your screen (default). It contains an item called Line Edit.
Click and drag this item into your form. This creates a new Line Edit in the form.
With the new line edit element selected, youll see its properties along the side of your screen (on the right
by default):
130
131
6.3.7
Go back to QGIS.
Double click the test-data layer in the legend to access its properties.
Click on the Fields tab in the Layer Properties dialog.
In the Attribute editor layout dropdown, select Provide ui-file.
Click the ellipsis button and choose the add_people.ui file you just created:
132
6.3.8 In Conclusion
Using forms, you can make life easier for yourself when editing or creating data. By editing widget types or
creating an entirely new form from scratch, you can control the experience of someone who digitizes new data for
that layer, thereby minimizing misunderstandings and unnecessary errors.
133
6.4.1
Use the school_property layer you created previously. The course materials include photos of each of the three
properties you digitized. What were going to do next is to associate each property with its image. Then well
create an action that will open the image for a property when clicking on the property.
6.4.2
The school_property layer has no way to associate an image with a property yet. First well create a field for this
purpose.
Open the Layer Properties dialog.
Click on the Fields tab.
Toggle editing mode:
134
135
136
After the field has been created, click on the Line edit button next to the new field.
Set it up for a File name:
137
138
Click on the browse button (the ... next to the image field).
Select the path for your image. The images are in exercise_data/school_property_photos/
and are named the same as the features they should be associated with.
Click OK.
Associate all of the images with the correct features using this method.
Save your edits and exit edit mode.
6.4.3
139
What to do next varies according to your operating system, so choose the appropriate course to follow:
Windows
Click on the Type dropdown and choose Open.
Ubuntu Linux
Under Action, write eog for the Gnome Image Viewer, or write display to use ImageMagick. Remember
to put a space after the command!
MacOS
Click on the Type dropdown and choose Mac.
Under Action, write :kbd:open . Remember to put a space after the command!
140
Click the Insert field button. QGIS will add the phrase [% "image" %] in the Action field.
Click the Add to action list button.
Click OK on the Layer Properties dialog.
Now we will test the new Action:
Click on the school_property layer in the Layers list so that it is highlighted.
Find the Run feature action button (on the same toolbar as the Open Attribute Table button):
Click on the down arrow to the right of this button. Theres only one action defined for this layer so far,
which is the one you just created.
Click the button itself to activate the tool.
Using this tool, click on any of the three school properties.
The image for that property will now open.
6.4.4
Lets say were looking at the map and want to know more about the area that a farm is in. Suppose you know
nothing of the area in question and want to find general information about it. Your first impulse, considering that
youre using a computer right now, would probably be to Google the name of the area. So lets tell QGIS to do
that automatically for us!
Open the attribute table for the landuse layer.
Well be using the name field for each of our landuse areas to search Google.
Close the attribute table.
Go back to Actions in Layer Properties.
In the field Action Properties Name, write Google Search.
What to do next varies according to your operating system, so choose the appropriate course to follow:
141
Windows
Under Type, choose Open. This will tell Windows to open an Internet address in your default browser, such
as Internet Explorer.
Ubuntu Linux
Under Action, write xdg-open. This will tell Ubuntu to open an Internet address in your default browser,
such as Chrome or Firefox.
MacOS
Under Action, write open. This will tell MacOS to open an Internet address in your default browser, such
as Safari.
Continue writing the command
Whichever command you used above, you need to tell it which Internet address to open next. You want it to visit
Google, and to search for a phrase automatically.
Usually when you use Google, you enter your search phrase into the Google Search bar. But in this
case, you want your computer to do this for you. The way you tell Google to search for something (if you dont want to use its search bar directly) is by giving your Internet browser the address
http://www.google.com/search?q=SEARCH_PHRASE, where SEARCH_PHRASE is what you want
to search for. Since we dont know what phrase to search for yet, well just enter the first part (without the search
phrase).
In the Action field, write http://www.google.com/search?q=. Remember to add a space after
your initial command before writing this in!
Now you want QGIS to tell the browser to tell Google to search for the value of name for any feature that you
could click on.
Select the name field.
Click Insert field:
142
What this means is that QGIS is going to open the browser and send it to the address
http://www.google.com/search?q=[% "name" %]. But [% "name" %] tells QGIS to use
the contents of the name field as the phrase to search for.
So if, for example, the landuse area you click on is named Marloth Nature Reserve, then QGIS is going to
send the browser to http://www.google.com/search?q=Marloth%20Nature%20Reserve, which
will cause your browser to visit Google, which will in turn search for Marloth Nature Reserve.
143
6.4.5
Above, youve seen how to open a webpage in an external browser. There are some shortcomings with this
approach in that it adds an unknowable dependency will the end-user have the software required to execute the
action on their system? As youve seen, they dont necessarily even have the same kind of base command for the
same kind of action, if you dont know which OS theyll be using. With some OS versions, the above commands
to open the browser might not work at all. This could be an insurmountable problem.
However, QGIS sits on top of the incredibly powerful and versatile Qt4 library. Also, QGIS actions can be
arbitrary, tokenized (i.e. using variable information based on the contents of a field attribute) Python commands!
Now youll see how to use a python action to show a web page. Its the same general idea as opening a site in an
external browser, but it requires no browser on the users system since it uses the Qt4 QWebView class (which is
a webkit based html widget) to display the content in a pop up window.
Instead of Google, lets use Wikipedia this time. So the URL you request will look like this:
http://wikipedia.org/wiki/SEARCH_PHRASE
To create the layer action:
Open the Layer Properties dialog and head over to the Actions tab.
Set up a new action using the following properties for the action: * Type: Python * Name: Wikipedia
* Action (all on one line):
from PyQt4.QtCore import QUrl; from PyQt4.QtWebKit import
QWebView; myWV = QWebView(None); myWV.load(QUrl(http://wikipedia.org/wiki/[%
"name" %])); myWV.show()
144
6.4.6 In Conclusion
Actions allow you to give your map extra functionality, useful to the end-user who views the same map in QGIS.
Due to the fact that you can use shell commands for any operating system, as well as Python, the skys the limit
in terms of the functions you could incorporate!
145
146
CAPITOLUL 7
Now that you have edited a few features, you must want to know what else one can do with them. Having features
with attributes is nice, but when all is said and done, this doesnt really tell you anything that a normal, non-GIS
map cant.
The key advantage of a GIS is this: a GIS can answer questions.
For the next three modules, well endeavor to answer a research question using GIS functions. For example, you
are an estate agent and you are looking for a residential property in Swellendam for clients who have the following
criteria:
1. It needs to be in Swellendam.
2. It must be within reasonable driving distance of a school (say 1km).
3. It must be more than 100m squared in size.
4. Closer than 50m to a main road.
5. Closer than 500m to a restaurant.
Within the next few modules, well harness the power of GIS analysis tools to locate suitable farm properties for
this new residential development.
7.1.1
The CRS that all the data as well as the map itself are in right now is called WGS84. This is a very common
Geographic Coordinate System (GCS) for representing data. But theres a problem, as we will see.
Save your current map.
Then open the map of the world which youll find under exercise_data/world/world.qgs.
Zoom in to South Africa by using the Zoom In tool.
Try setting a scale in the Scale field, which is in the Status Bar along the bottom of the screen. While over
South Africa, set this value to 1:5000000 (one to five million).
Pan around the map while keeping an eye on the Scale field.
147
Notice the scale changing? Thats because youre moving away from the one point that you zoomed into at
1:5000000, which was at the center of your screen. All around that point, the scale is different.
To understand why, think about a globe of the Earth. It has lines running along it from North to South. These
longitude lines are far apart at the equator, but they meet at the poles.
In a GCS, youre working on this sphere, but your screen is flat. When you try to represent the sphere on a flat
surface, distortion occurs, similar to what would happen if you cut open a tennis ball and tried to flatten it out.
What this means on a map is that the longitude lines stay equally far apart from each other, even at the poles
(where they are supposed to meet). This means that, as you travel away from the equator on your map, the scale
of the objects that you see gets larger and larger. What this means for us, practically, is that there is no constant
scale on our map!
To solve this, lets use a Projected Coordinate System (PCS) instead. A PCS projects or converts the data in a
way that makes allowance for the scale change and corrects it. Therefore, to keep the scale constant, we should
reproject our data to use a PCS.
7.1.2
QGIS allows you to reproject data on the fly. What this means is that even if the data itself is in another CRS,
QGIS can project it as if it were in a CRS of your choice.
To enable on the fly projection, click on the CRS Status button in the Status Bar along the bottom of the
QGIS window:
In the dialog that appears, check the box next to Enable on the fly CRS transformation.
Type the word global into the Filter field. One CRS (NSIDC EASE-Grid Global) should appear in the
list below.
Click on the NSIDC EASE-Grid Global to select it, then click OK.
Notice how the shape of South Africa changes. All projections work by changing the apparent shapes of
objects on Earth.
Zoom in to a scale of 1:5000000 again, as before.
Pan around the map.
Notice how the scale stays the same!
On the fly reprojection is also used for combining datasets that are in different CRSs.
Deactivate on the fly re-projection again:
Click on the CRS Status button again.
Un-check the Enable on the fly CRS transformation box.
Clicking OK.
In QGIS 2.0, the on the fly reprojection is automatically activated when
layers with different CRSs are loaded in the map. To understand what on the fly reprojection does, deactivate
this automatic setting:
Go to Settings Options...
On the left panel of the dialog, select CRS.
Un-check :guilabel:Automatically enable on the fly reprojection if layers
have different CRS. * Click OK.
148
Add another vector layer to your map which has the data for South Africa only. Youll find it as
exercise_data/world/RSA.shp.
What do you notice?
The layer isnt visible! But thats easy to fix, right?
Right-click on the RSA layer in the Layers list.
Select Zoom to Layer Extent.
OK, so now we see South Africa... but where is the rest of the world?
It turns out that we can zoom between these two layers, but we cant ever see them at the same time. Thats because
their Coordinate Reference Systems are so different. The continents dataset is in degrees, but the RSA dataset is in
meters. So, lets say that a given point in Cape Town in the RSA dataset is about 4 100 000 meters away from
the equator. But in the continents dataset, that same point is about 33.9 degrees away from the equator.
This is the same distance - but QGIS doesnt know that. You havent told it to reproject the data. So as far as its
concerned, the version of South Africa that we see in the RSA dataset has Cape Town at the correct distance of 4
100 000 meters from the equator. But in the continents dataset, Cape Town is only 33.9 meters away from the
equator! You can see why this is a problem.
QGIS doesnt know where Cape Town is supposed to be - thats what the data should be telling it. If the data tells
QGIS that Cape Town is 34 meters away from the equator and that South Africa is only about 12 meters from
north to south, then that is what QGIS will draw.
To correct this:
Click on the CRS Status button again and switch
Enable on the fly CRS transformation on again as before. * Zoom to the extents of the RSA dataset.
Now, because theyre made to project in the same CRS, the two datasets fit perfectly:
When combining data from different sources, its important to remember that they might not be in the same CRS.
On the fly reprojection helps you to display them together.
Before you go on, you probably want to have the on the fly reprojection to be automatically activated whenever
you open datasets having different CRS:
149
7.1.3
Remember when you calculated areas for the buildings in the Classification lesson? You did it so that you could
classify the buildings according to area.
Open your usual map again (containing the Swellendam data).
Open the attribute table for the buildings layer.
Scroll to the right until you see the AREA field.
Notice how the areas are all very small; probably zero. This is because these areas are given in degrees - the data
isnt in a Projected Coordinate System. In order to calculate the area for the farms in square meters, the data has
to be in square meters as well. So, well need to reproject it.
But it wont help to just use on the fly reprojection. On the fly does what it says - it doesnt change the data,
it just reprojects the layers as they appear on the map. To truly reproject the data itself, you need to export it to a
new file using a new projection.
Right-click on the buildings layer in the Layers list.
Select Save As... in the menu that appears. You will be shown the Save vector layer as... dialog.
Click on the Browse button next to the Save as field.
Navigate to exercise_data/
buildings_reprojected.shp.
and
specify
the
name
of
the
new
layer
as
150
Click OK.
Start a new map and load the reprojected layer you just created.
Refer back to the lesson on Classification to remember how you calculated areas.
Update (or add) the AREA field by running the same expression as before:
151
This will add an AREA field with the size of each building in square meters
To calculate the area in another unit of measurement, for example hectares, use the AREA field to create a
second column:
152
Look at the new values in your attribute table. This is much more useful, as people actually quote building size in
metres, not in degrees. This is why its a good idea to reproject your data, if necessary, before calculating areas,
distances, and other values that are dependent on the spatial properties of the layer.
7.1.4
There are many more projections than just those included in QGIS by default. You can also create your own
projections.
Start a new map.
Load the world/oceans.shp dataset.
Go to Settings Custom CRS... and youll see this dialog:
153
154
Click OK.
Enable on the fly reprojection.
Choose your newly defined projection (search for its name in the Filter field).
On applying this projection, the map will be reprojected thus:
155
7.1.5 In Conclusion
Different projections are useful for different purposes. By choosing the correct projection, you can ensure that the
features on your map are being represented accurately.
7.2.1
Before we start, it would be useful to give a brief overview of a process that can be used to solve any GIS problem.
The way to go about it is:
156
7.2.2
The problem
Lets start off the process by deciding on a problem to solve. For example, you are an estate agent and you are
looking for a residential property in Swellendam for clients who have the following criteria:
1. It needs to be in Swellendam.
2. It must be within reasonable driving distance of a school (say 1km).
3. It must be more than 100m squared in size.
4. Closer than 50m to a main road.
5. Closer than 500m to a restaurant.
7.2.3
The data
7.2.4
157
This will import the OSM data as separate layers into your map.
The data you just downloaded from OSM is in a geographic coordinate system, WGS84, which uses latitude and
longitude coordinates, as you know from the previous lesson. You also learnt that to calculate distances in meters,
we need to work with a projected coordinate system. Start by setting your projects coordinate system to a suitable
CRS for your data, in the case of Swellendam, WGS 84 / UTM zone 34S:
Open the Project Properties dialog, select CRS and filter the list to find WGS 84 / UTM zone 34S.
Click OK.
We now need to extract the information we need from the OSM dataset. We need to end up with layers representing
all the houses, schools, restaurants and roads in the region. That information is inside the multipolygons layer and
can be extracted using the information in its Attribute Table. Well start with the schools layer:
Right-click on the multipolygons layer in the Layers list and open the Layer Properties.
Go to the General menu.
Under Feature subset click on the [Query Builder] button to open the Query builder dialog.
In the Fields list on the left of this dialog until you see the field amenity.
Click on it once.
Click the All button underneath the Values list:
Now we need to tell QGIS to only show us the polygons where the value of amenity is equal to school.
Double-click the word amenity in the Fields list.
Watch what happens in the Provider specific filter expression
field below:
158
The word "amenity" has appeared. To build the rest of the query:
Click the = button (under Operators).
Double-click the value school in the Values list.
Click OK twice.
This will filter OSMs multipolygon layer to only show the schools in your region. You can now either:
Rename the filtered OSM layer to schools and re-import the multipolygons layer from
osm_data.osm, OR
Duplicate the filtered layer, rename the copy, clear the Query Builder and create your new query in the
Query Builder.
7.2.5
Using the above technique, use the Query Builder tool to extract the remaining data from OSM to create the
following layers:
roads (from OSMs lines layer)
restaurants (from OSMs multipolygons layer)
159
7.2.6
Some of the roads in OSMs dataset are listed as unclassified, tracks, path and footway. We want to
exclude these from our roads dataset.
Open the Query Builder for the roads layer, click Clear and build the following query:
"highway" != NULL AND "highway" != unclassified AND "highway" !=
track AND "highway" != path AND "highway" != footway
You can either use the approach above, where you double-clicked values and clicked buttons, or you can copy and
paste the command above.
This should immediately reduce the number of roads on your map:
7.2.7
Because we are going to be measuring distances within our layers, we need to change the layers CRS. To do this,
we need to select each layer in turn, save the layer to a new shapefile with our new projection, then import that
new layer into our map.
Note: In this example, we are using the WGS 84 / UTM zone 34S CRS, but you may use a UTM CRS which is
160
The new shapefile will be created and the resulting layer added to your map.
161
Note: If you dont have activated Enable on the fly CRS transformation or the Automatically enable on the fly
reprojection if layers have different CRS settings (see previous lesson), you might no be able to see the new layers
you just added to the map. In this case, you can focus the map on any of the layers by right click on any layer and
click Zoom to layer extent, or just enable any of the mentioned on the fly options.
Remove the old roads layer.
Repeat this process for each layer, creating a new shapefile and layer with _34S appended to the original name
and removing each of the old layers.
Once you have completed the process for each layer, right click on any layer and click Zoom to layer extent to
focus the map to the area of interest.
Now that we have converted OSMs data to a UTM projection, we can begin our calculations.
7.2.8
162
The Buffer distance is in meters because our input dataset is in a Projected Coordinate System that uses meter as
its basic measurement unit. This is why we needed to use projected data.
Save the resulting layer
roads_buffer_50m.shp.
under
exercise_data/residential_development/
as
163
If your new layer is at the top of the Layers list, it will probably obscure much of your map, but this gives us all
the areas in your region which are within 50m of a road.
However, youll notice that there are distinct areas within our buffer, which correspond to all the individual roads.
To get rid of this problem, remove the layer and re-create the buffer using the settings shown here:
164
Note that were now checking the Dissolve buffer results box.
Save the output under the same name as before (click Yes when it asks your permission to overwrite the old
one).
Click OK and close the Buffer(s) dialog again.
Once youve added the layer to the Layers list, it will look like this:
165
7.2.9
Use the same approach as above and create a buffer for your schools.
It needs to be 1 km in radius, and saved under the usual directory as schools_buffer_1km.shp.
Check your results
7.2.10
Now we have areas where the road is 50 meters away and theres a school within 1 km (direct line, not by road).
But obviously, we only want the areas where both of these criteria are satisfied. To do that, well need to use the
Intersect tool. Find it under Vector Geoprocessing Tools Intersect. Set it up like this:
166
The two input layers are the two buffers; the save location is as usual; and the file name is
road_school_buffers_intersect.shp. Once its set up like this, click OK and add the layer to the
Layers list when prompted.
In the image below, the blue areas show us where both distance criteria are satisfied at once!
167
You may remove the two buffer layers and only keep the one that shows where they overlap, since thats what we
really wanted to know in the first place:
7.2.11
Now youve got the area that the buildings must overlap. Next, you want to select the buildings in that area.
Click on the menu entry Vector Research Tools Select by location. A dialog will appear.
168
169
The buildings highlighted in yellow are those which match our criteria and are selected, while the buildings in
green are those which do not. We can now save the selected buildings as a new layer.
Right-click on the houses_34S layer in the Layers list.
Select Save Selection As....
Set the dialog up like this:
170
Now you have the selection as a separate layer and can remove the houses_34S layer.
7.2.12
We now have a layer which shows us all the buildings within 1km of a school and within 50m of a road. We now
need to reduce that selection to only show buildings which are within 500m of a restaurant.
Using the processes described above, create a new layer called houses_restaurants_500m which further
filters your well_located_houses layer to show only those which are within 500m of a restaurant.
Check your results
7.2.13
To see which buildings are the correct size (more than 100 square metres), we first need to calculate their size.
Open the attribute table for the houses_restaurants_500m layer.
Enter edit mode and open the field calculator.
Set it up like this:
171
If you cant find AREA in the list, try creating a new field as you did in the previous lesson of this module.
Click OK.
Scroll to the right of the attribute table; your AREA field now has areas in metres for all the buildings in your
houses_restaurants_500m layer.
Click the edit mode button again to finish editing, and save your edits when prompted.
Build a query as earlier in this lesson:
172
Click OK. Your map should now only show you those buildings which match our starting criteria and which
are more than 100m squared in size.
7.2.14
Try Yourself
Save your solution as a new layer, using the approach you learned above for doing so. The file should be
saved under the usual directory, with the name solution.shp.
7.2.15 In Conclusion
Using the GIS problem-solving approach together with QGIS vector analysis tools, you were able to solve a
problem with multiple criteria quickly and easily.
173
7.3.1
QGIS has many plugins that add to its basic functions. Many of these plugins are so useful that they ship along
with the program straight out of the box. Theyre still hidden by default, though. So in order to use them, you
need to activate them first.
To activate the Road Graph plugin:
Start the Plugin Manager by clicking on the QGIS main windows menu item Plugins Manage and
Install Plugins.... A dialog appears.
Select the plugin like this:
174
7.3.2
To have a layer to calculate on, first save your current map. If you havent already done so, save your roads_34S
layer to a shapefile by right-clicking the layer and selecting Save as.... Create a new map and load this layer into
it.
Since so many different configurations are possible when analyzing networks, the plugin doesnt assume anything
before youve set it up. This means that it wont do anything at all if you dont set it up first.
Click on the menu item Vector Road graph settings. A dialog will appear.
Make sure its set up like this (use defaults unless otherwise specified):
175
176
7.3.3
Find two points, on roads, on your map. They do not need to have any significance, but they should be connected
by roads and separated by a reasonable distance:
177
In the plugin panel, click on the Capture Point button next to the Start field:
178
179
7.3.4
Note: Section developed by Linfiniti and S Motala (Cape Peninsula University of Technology)
Add your restaurants_34S layer to the map (extract it from your analyis map if necessary).
Open the attribute table for the roads_34S layer and enter edit mode.
Add a new column with the name SPEED, and give it the type
Whole number (integer) with a width of 3.
In the main window, activate the Select Features by Rectangle tool:
180
(To select more than one road, hold the ctrl button and drag a box across any road that you want to include in
the selection.)
In the attribute table, select Show selected features.
181
Set the SPEED value for all the selected streets to 60:
In context, this means that youre setting the speed limit on those roads to 60 km/h.
Select any highways or major roads outside urban areas:
182
Set the SPEED value for all the selected streets to 120.
Close the attribute table, save your edits, and exit edit mode.
Check the Vector Road graph Road graph settings to ensure that its set up as explained previously in
this lesson, but with the Speed value set to the SPEED field you just created.
In the Shortest path panel, click the Start point button.
Set the starting point on a minor road on one side of Swellendam and the end point on a major road on the
other side of town:
183
In the Criterion drop-down list in the Shortest path panel, select Length.
Click Calculate. The route will be calculated for the shortest distance:
Notice the values of Length and Time in the Shortest path panel.
Set the Criterion to Time.
Click Calculate again. The route will be calculated for the shortest time:
You can switch back and forth between these criteria, recalculating each time, and note the changes in the Length
and Time taken. Remember that the assumption being made to arrive at the time taken to travel a route does not
184
account for acceleration, and assumes that you will be traveling at the speed limit at all times. In a real situation,
you may want to split roads into smaller sections and note the average or expected speed in each section, rather
than the speed limit.
If, on clicking Calculate, you see an error stating that a path could not be found, make sure that the roads you
digitized actually meet each other. If theyre not quite touching, either fix them by modifying the features, or set
the Topology tolerance in the plugins settings. If theyre passing over each other without intersecting, use the
Split features tool to split roads at their intersections:
Remember that the Split features tool only works in edit mode on selected features, though!
You might also find that the shortest route is also the quickest if this error is returned.
7.3.5 In Conclusion
Now you know how to use the Road Graph plugin to solve shortest-path problems.
7.4.1
In order to get a point dataset to work with, well create a random set of points.
To do so, youll need a polygon dataset defining the extents of the area you want to create the points in.
Well use the area covered by streets.
Create a new empty map.
Add your roads_34S layer, as well as the srtm_41_19.tif raster (elevation data) found in
exercise_data/raster/SRTM/.
Note: You might find that your SRTM DEM layer has a different CRS to that of the roads layer. If so, you can
reproject either the roads or DEM layer using techniques learnt earlier in this module.
Use the Convex hull(s) tool (available under Vector Geoprocessing Tools) to generate an area enclosing
all the roads:
185
186
187
188
Select random_points as the layer containing sampling points, and the SRTM raster as the band to get values
from.
Make sure that Add created layer to the TOC is checked.
Save the output under exercise_data/spatial_statistics/ as random_samples.shp.
Now you can check the sampled data from the raster file in the attributes table of the random_samples layer, they
will be in a column named srtm_41_19.tif.
A possible sample layer is shown here:
189
The sample points are classified by their value such that darker points are at a lower altitude.
Youll be using this sample layer for the rest of the statistical exercises.
7.4.2
190
Note: You can copy and paste the results into a spreadsheet. The data uses a (colon :) separator.
191
7.4.3
Create a new point layer in the same projection as the other datasets (WGS 84 / UTM 34S).
Enter edit mode and digitize three point somewhere among the other points.
Alternatively, use the same random point generation method as before, but specify only three points.
Save your new layer as distance_points.shp.
To generate a distance matrix using these points:
Open the tool Vector Analysis Tools Distance matrix.
Select the distance_points layer as the input layer, and the random_samples layer as the target layer.
Set it up like this:
192
193
7.4.4
Note: You can copy and paste the results into a spreadsheet. The data uses a (colon :) separator.
194
7.4.5
7.4.6
The histogram of a dataset shows the distribution of its values. The simplest way to demonstrate this in QGIS is
via the image histogram, available in the Layer Properties dialog of any image layer.
In your Layers list, right-click on the SRTM DEM layer.
Select Properties.
Choose the tab Histogram. You may need to click on the Compute Histogram button to generate the graphic.
You will see a graph describing the frequency of values in the image.
195
Select the Metadata tab, you can see more detailed information inside the Properties box.
The mean value is 332.8, and the maximum value is 1699! But those values dont show up on the histogram.
Why not? Its because there are so few of them, compared to the abundance of pixels with values below the mean.
Thats also why the histogram extends so far to the right, even though there is no visible red line marking the
frequency of values higher than about 250.
Therefore, keep in mind that a histogram shows you the distribution of values, and not all values are necessarily
visible on the graph.
(You may now close Layer Properties.)
7.4.7
Lets say you have a collection of sample points from which you would like to extrapolate data. For example, you
might have access to the random_samples dataset we created earlier, and would like to have some idea of what
the terrain looks like.
To start, launch the Grid (Interpolation) tool by clicking on the Raster Analysis Grid (Interpolation) menu
item.
In the Input file field, select random_samples.
196
As you can see, 100 sample points arent really enough to get a detailed impression of the terrain. It gives a very
general idea, but it can be misleading as well. For example, in the image above, it is not clear that there is a high,
unbroken mountain running from east to west; rather, the image seems to show a valley, with high peaks to the
west. Just using visual inspection, we can see that the sample dataset is not representative of the terrain.
7.4.8
Try Yourself
Use the processes shown above to create a new set of 1000 random points.
Use these points to sample the original DEM.
Use the Grid (Interpolation) tool on this new dataset as above.
Set the output filename to interpolation_1000.tif, with Power and Smoothing set to 5.0 and 2.0,
respectively.
The results (depending on the positioning of your random points) will look more or less like this:
197
The border shows the roads_hull layer (which represents the boundary of the random sample points) to explain
the sudden lack of detail beyond its edges. This is a much better representation of the terrain, due to the much
greater density of sample points.
Here is an example of what it looks like with 10 000 sample points:
Note: Its not recommended that you try doing this with 10 000 sample points if you are not working on a fast
computer, as the size of the sample dataset requires a lot of processing time.
198
7.4.9
Originally a separate project and then accessible as a plugin, the SEXTANTE software has been added to QGIS
as a core function from version 2.0. You can find it as a new QGIS menu with its new name Processing from
where you can access a rich toolbox of spatial analysis tools allows you to access various plugin tools from within
a single interface.
Activate this set of tools by enabling the Processing Toolbox
menu entry. The toolbox looks like this:
You will probably see it docked in QGIS to the right of the map. Note that the tools listed here are links to the
actual tools. Some of them are SEXTANTEs own algorithms and others are links to tools that are accessed from
external applications such as GRASS, SAGA or the Orfeo Toolbox. This external applications are installed with
QGIS so you are already able to make use of them. In case you need to change the configuration of the Processing
tools or, for example, you need to update to a new version of one of the external applications, you can access its
setting from Processing Options and configurations.
7.4.10
For a simple indication of the spatial distribution of points in the random_samples dataset, we can make use of
SAGAs Spatial Point Pattern Analysis tool via the Processing Toolbox you just opened.
In the Processing Toolbox, search for this tool Spatial Point Pattern Analysis.
Double-click on it to open its dialog.
199
Installing SAGA
Note: If SAGA is not installed on your system, the plugins dialog will inform you that the dependency is missing.
If this is not the case, you can skip these steps.
On Windows
Included in your course materials you will find the SAGA installer for Windows.
Start the program and follow its instructions to install SAGA on your Windows system. Take note of the
path you are installing it under!
Once you have installed SAGA, youll need to configure SEXTANTE to find the path it was installed under.
Click on the menu entry Analysis SAGA options and configuration.
In the dialog that appears, expand the SAGA item and look for SAGA folder. Its value will be blank.
In this space, insert the path where you installed SAGA.
On Ubuntu
Search for SAGA GIS in the Software Center, or enter the phrase sudo apt-get install saga-gis
in your terminal. (You may first need to add a SAGA repository to your sources.)
QGIS will find SAGA automatically, although you may need to restart QGIS if it doesnt work straight
away.
On Mac
Homebrew users can install SAGA with this command:
brew install saga-core
If you do not use Homebrew, please follow the instructions here:
http://sourceforge.net/apps/trac/saga-gis/wiki/Compiling%20SAGA%20on%20Mac%20OS%20X
After installing
Now that you have installed and configured SAGA, its functions will become accessible to you.
Using SAGA
Open the SAGA dialog.
SAGA produces three outputs, and so will require three output paths.
Save these three outputs under exercise_data/spatial_statistics/, using whatever file names
you find appropriate.
200
The output will look like this (the symbology was changed for this example):
201
The red dot is the mean center; the large circle is the standard distance, which gives an indication of how closely
the points are distributed around the mean center; and the rectangle is the bounding box, describing the smallest
possible rectangle which will still enclose all the points.
7.4.11
Often, the output of an algorithm will not be a shapefile, but rather a table summarizing the statistical properties
of a dataset. One of these is the Minimum Distance Analysis tool.
Find this tool in the Processing Toolbox as :guilabel:Minimum
Distance Analysis.
It does not require any other input besides specifying the vector point dataset to be analyzed.
Choose the random_points dataset.
Click OK. On completion, a DBF table will appear in the Layers list.
Select it, then open its attribute table. Although the figures may vary, your results will be in this format:
202
7.4.12 In Conclusion
QGIS allows many possibilities for analyzing the spatial statistical properties of datasets.
203
204
CAPITOLUL 8
Module: Rasters
Weve used rasters for digitizing before, but raster data can also be used directly. In this module, youll see how
its done in QGIS.
8.1.1
The Load Raster Layer dialog will open. The data for this project is in exercise_data/raster.
Either load them all in separately, or hold down ctrl and click on all four of them in turn, then open them
at the same time.
The first thing youll notice is that nothing seems to be happening in your map. Are the rasters not loading? Well,
there they are in the Layers list, so obviously they did load. The problem is that theyre not in the same projection.
Luckily, weve already seen what to do in this situation.
Select Project > Project Properties in the menu:
Select CRS tab in the menu:
Enable on the fly reprojection.
Set it to the same projection as the rest of your data (WGS 84 / UTM zone 33S).
Click OK.
The rasters should fit nicely:
205
There we have it - four aerial photographs covering our whole study area.
8.1.2
Now as you can see from this, your solution layer lies across all four photographs. What this means is that youre
going to have to work with four rasters all the time. Thats not ideal; it would be better to have one file for one
(composite) image, right?
Luckily, QGIS allows you to do exactly this, and without needing to actually create a new raster file, which could
take up a lot of space. Instead, you can create a Virtual Raster. This is also often called a Catalog, which explains
its function. Its not really a new raster. Rather, its a way to organize your existing rasters into one catalog: one
file for easy access.
To make a catalog:
Click on the menu item Raster Miscellaneous Build Virtual Raster (Catalog).
In the dialog that appears, check the box next to Use visible raster layers for input.
Enter exercise_data/residential_development as the output location.
Enter aerial_photos.vrt as the file name.
Check the Load into canvas when finished button.
206
Notice the text field below. What this dialog is actually doing is that its writing that text for you. Its a long
command that QGIS is going to run.
Note:
Keep in mind that the command text is editable, so you can customize the command further if
preferred. Search online for the initial command (in this case, gdalbuildvrt) for help on the syntax.
Click OK to run the command.
It may take a while to complete. When its done, it will tell you so with a message box.
Click OK to chase the message away.
Click Close on the Build Virtual Raster (Catalog) dialog. (Dont click OK again, otherwise its going to
start running that command again.)
207
You can now remove the original four rasters from the Layers list.
If necessary, click and drag the new aerial_photos raster catalog layer to the bottom of the Layers list so
that the other activated layers become visible.
8.1.3
The above methods allow you to virtually merge datasets using a catalog, and to reproject them on the fly.
However, if you are setting up data that youll be using for quite a while, it may be more efficient to create new
rasters that are already merged and reprojected. This improves performance while using the rasters in a map, but
it may take some time to set up initially.
Reprojecting rasters
Click on the menu item Raster Projections Warp (Reproject).
Note that this tool features a handy batch option for reprojecting the contents of whole directories. You can also
reproject virtual rasters (catalogs), as well as enabling a multithreaded processing mode.
208
Merging rasters
Click on the menu item Raster Miscellaneous Merge.
You can choose to process entire directories instead of single files, giving you a very useful built-in batch processing capability. You can specify a virtual raster as input file, too, and all of the rasters that it consists of will be
processed.
You can also add your own command line options using the Creation Options checkbox and list. This only applies
if you have knowledge of the GDAL librarys operation.
8.1. Lesson: Working with Raster Data
209
210
8.1.4 In Conclusion
QGIS makes it easy to include raster data into your existing projects.
8.2.1
Try Yourself
Start with the current map from the previous exercise analysis.qgs.
Use the Add Raster Layer button to load the new raster dataset.
Load the dataset srtm_41_19.tif, found under the directory exercise_data/raster/SRTM/.
Once it appears in the Layers list, rename it to DEM.
Zoom to the extent of this layer by right-clicking on it in the Layer List and selecting Zoom to Layer Extent.
This dataset is a Digital Elevation Model (DEM). Its a map of the elevation (altitude) of the terrain, allowing us
to see where the mountains and valleys are, for example.
Once its loaded, youll notice that its a basic stretched grayscale representation of the DEM. Its seen here with
the vector layers on top:
211
QGIS has automatically applied a stretch to the image for visualization purposes, and we will learn more about
how this works as we continue.
8.2.2
Open the Layer Properties dialog for the SRTM layer by right-clicking on the layer in the Layer tree and
selecting Properties option.
Switch to the Style tab.
212
These are the current settings that QGIS applied for us by default. Its just one way to look at a DEM, so lets
explore some others.
Change the Render type to Singleband pseudocolor, and use the default options presented.
Click the Classify button to generate a new color classification, and click OK to apply this classification to
the DEM.
213
214
This is an interesting way of looking at the DEM, but maybe we dont want to symbolize it using these colors.
Open Layer Properties dialog again.
Switch the Render Type back to Singleband gray.
Click OK to apply this setting to the raster.
You will now see a totally gray rectangle that isnt very useful at all.
215
This is because we have lost the default settings which stretch the color values to show them contrast.
Lets tell QGIS to again stretch the color values based on the range of data in the DEM. This will make QGIS
use all of the available colors (in Grayscale, this is black, white and all shades of gray in between).
Specify the Min and Max values as shown below.
Set the value Contrast enhancement to Stretch To MinMax:
216
But what are the minimum and maximum values that should be used for the stretch? The ones that are currently
under Min and Max values are the same values that just gave us a gray rectangle before. Instead, we should be
using the minimum and maximum values that are actually in the image, right? Fortunately, you can determine
those values easily by loading the minimum and maximum values of the raster.
Under Load min / max values, select Min / Max option.
Click the Load button:
Notice how the Custom min / max values have changed to reflect the actual values in our DEM:
217
218
The third button from the left Local Histogram Stretch will automatically stretch the minimum and maximum
values to give you the best contrast in the local area that youre zoomed into. Its useful for large datasets. The
button on the left Local Cumulative Cut Stretch ... will stretch the minimum and maximum values to constant
values across the whole image.
Click the fourth button from the left (Stretch Histogram to Full Dataset). Youll see the data is now correctly
represented as before.
You can try the other buttons in this toolbar and see how they alter the stretch of the image when zoomed in to
local areas or when fully zoomed out.
219
8.2.3 In Conclusion
These are only the basic functions to get you started with raster symbology. QGIS also allows you many other
options, such as symbolizing a layer using standard deviations, or representing different bands with different colors
in a multispectral image.
8.2.4 Reference
The SRTM dataset was obtained from http://srtm.csi.cgiar.org/
8.3.1
The DEM you have on your map right now does show you the elevation of the terrain, but it can sometimes seem
a little abstract. It contains all the 3D information about the terrain that you need, but it doesnt look like a 3D
object. To get a better look at the terrain, it is possible to calculate a hillshade, which is a raster that maps the
terrain using light and shadow to create a 3D-looking image.
To work with DEMs, you should use QGIS all-in-one DEM (Terrain models) analysis tool.
Click on the menu item Raster Analysis DEM (Terrain models).
In the dialog that appears, ensure that the Input file is the DEM layer.
Set the Output file to hillshade.tif in the directory exercise_data/residential_development.
Also make sure that the Mode option has Hillshade selected.
Check the box next to Load into canvas when finished.
You may leave all the other options unchanged.
Click OK to generate the hillshade.
When it tells you that processing is completed, click OK on the message to get rid of it.
Click Close on the main DEM (Terrain models) dialog.
You will now have a new layer called hillshade that looks like this:
220
That looks nice and 3D, but can we improve on this? On its own, the hillshade looks like a plaster cast. Cant
we use it together with our other, more colorful rasters somehow? Of course we can, by using the hillshade as an
overlay.
8.3.2
A hillshade can provide very useful information about the sunlight at a given time of day. But it can also be
used for aesthetic purposes, to make the map look better. The key to this is setting the hillshade to being mostly
transparent.
Change the symbology of the original DEM to use the Pseudocolor scheme as in the previous exercise.
Hide all the layers except the DEM and hillshade layers.
Click and drag the DEM to be beneath the hillshade layer in the Layers list.
Set the hillshade layer to be transparent by opening its Layer Properties and go to the Transparency tab.
Set the Global transparency to 50%:
Click OK on the Layer Properties dialog. Youll get a result like this:
221
Switch the hillshade layer off and back on in the Layers list to see the difference it makes.
Using a hillshade in this way, its possible to enhance the topography of the landscape. If the effect doesnt seem
strong enough to you, you can change the transparency of the hillshade layer; but of course, the brighter the
hillshade becomes, the dimmer the colors behind it will be. You will need to find a balance that works for you.
Remember to save your map when you are done.
Note:
For the next two exercises, please use a new map. Load only the DEM raster dataset into it
(exercise_data/raster/SRTM/srtm_41_19.tif). This is to simplify matters while youre working
with the raster analysis tools. Save the map as exercise_data/raster_analysis.qgs.
8.3.3
Another useful thing to know about the terrain is how steep it is. If, for example, you want to build houses on the
land there, then you need land that is relatively flat.
To do this, you need to use the Slope mode of the DEM (Terrain models) tool.
Open the tool as before.
Select the Mode option Slope:
222
223
8.3.4
The aspect of terrain refers to the direction its facing in. Since this study is taking place in the Southern Hemisphere, properties should ideally be built on a north-facing slope so that they can remain in the sunlight.
Use the Aspect mode of the DEM (Terain models) tool to calculate the aspect of the terrain.
Check your results
8.3.5
Think back to the estate agent problem, which we last addressed in the Vector Analysis lesson. Lets imagine
that the buyers now wish to purchase a building and build a smaller cottage on the property. In the Southern
Hemisphere, we know that an ideal plot for development needs to have areas on it that are north-facing, and with
a slope of less than five degrees. But if the slope is less than 2 degrees, then the aspect doesnt matter.
Fortunately, you already have rasters showing you the slope as well as the aspect, but you have no way of knowing
where both conditions are satisfied at once. How could this analysis be done?
224
225
8.3.6
Try Yourself
Now that youve done the aspect, create two separate new analyses of the DEM layer.
The first will be to identify all areas where the slope is less than or equal to 2 degrees.
The second is similar, but the slope should be less than or equal to 5 degrees.
Save them under exercise_data/residential_development/ as slope_lte2.tif and
slope_lte5.tif.
Check your results
8.3.7
Now you have three new analysis rasters of the DEM layer:
aspect_north: the terrain faces north
slope_lte2: the slope is at or below 2 degrees
slope_lte5: the slope is at or below 5 degrees
Where the conditions of these layers are met, they are equal to 1. Elsewhere, they are equal to 0. Therefore, if
you multiply one of these rasters by another one, you will get the areas where both of them are equal to 1.
The conditions to be met are: at or below 5 degrees of slope, the terrain must face north; but at or below 2 degrees
of slope, the direction that the terrain faces in does not matter.
226
Therefore, you need to find areas where the slope is at or below 5 degrees AND the terrain is facing north; OR the
slope is at or below 2 degrees. Such terrain would be suitable for development.
To calculate the areas that satisfy these criteria:
Open your Raster calculator again.
Use the Raster bands list, the Operators buttons, and your keyboard to build this expression in the Raster
calculator expression text area:
( aspect_north@1 = 1 AND slope_lte5@1 = 1 ) OR slope_lte2@1 = 1
Save
the
output
all_conditions.tif.
under
exercise_data/residential_development/
as
8.3.8
As you can see from the image above, the combined analysis has left us with many, very small areas where the
conditions are met. But these arent really useful for our analysis, since theyre too small to build anything on.
Lets get rid of all these tiny unusable areas.
Open the Sieve tool (Raster Analysis Sieve).
Set the Input file to all_conditions, and the Output file to all_conditions_sieve.tif (under
exercise_data/residential_development/).
Set both the Threshold and Pixel connections values to 8, then run the tool.
227
Once processing is done, the new layer will load into the canvas. But when you try to use the histogram stretch
tool to view the data, this happens:
228
Whats going on? The answer lies in the new raster files metadata.
View the metadata under the Metadata tab of the Layer Properties dialog. Look in the Properties section at
the bottom.
229
Whereas this raster, like the one its derived from, should only feature the values 1 and 0, it has the
STATISTICS_MINIMUM value of a very large negative number. Investigation of the data shows that this number
acts as a null value. Since were only after areas that werent filtered out, lets set these null values to zero.
Open the Raster Calculator again, and build this expression:
(all_conditions_sieve@1 <= 0) = 0
This will maintain all existing zero values, while also setting the negative numbers to zero; which will leave
all the areas with value 1 intact.
Save
the
output
under
all_conditions_simple.tif.
exercise_data/residential_development/
as
230
This is what was expected: a simplified version of the earlier results. Remember that if the results you get from
a tool arent what you expected, viewing the metadata (and vector attributes, if applicable) can prove essential to
solving the problem.
8.3.9 In Conclusion
Youve seen how to derive all kinds of analysis products from a DEM. These include hillshade, slope and aspect
calculations. Youve also seen how to use the raster calculator to further analyze and combine these results.
231
232
CAPITOLUL 9
Avei acum doua jumatai ale unei analize: o parte vector i o parte raster. n acest modul, vei afla cum sa le combinai.
Vei ncheia analiza i vei prezenta rezultatele finale.
9.1.1
Click on Raster Conversion Polygonize (Raster to Vector). The tool dialog will appear.
Set it up like this:
233
Change the field name (describing the values of the raster) to suitable.
Save the shapefile under exercise_data/residential_development as all_terrain.shp.
Now you have a vector file which contains all the values of the raster, but the only areas youre interested in are
those that are suitable; i.e., those polygons where the value of suitable is 1. You can change the style of this
layer if you want to have a clearer visualization of it.
9.1.2
Try Yourself
as
exercise_data/residential_development/
as
9.1.3
Although unnecessary for our current problem, its useful to know about the opposite conversion from the one
performed above. Convert to raster the suitable_terrain.shp vector file you just created in previous step.
Click on Raster Conversion Rasterize (Vector to Raster) to start this tool, then set it up as in the
screenshot below:
234
9.1.4 In Conclusion
Converting between raster and vector formats allows you to widen the applicability of data, and need not lead to
data degradation.
235
9.2.1
Try Yourself
Note: If you find that the Intersect tool does not produce any results, check the CRS settings of each of your
layers. The CRS must be the same for both the layers you are comparing. You may need to reproject one layer by
saving the layer as a new shapefile with the required CRS. In our example, the suitable_terrain layer was
reprojected to WGS 84 / UTM 34S and named suitable_terrain_34S.
236
9.2.2
Look at each of the buildings in your new_solution layer. Compare them with the suitable_terrain layer by
changing the symbology for the new_solution layer so that it has outlines only. What do you notice about some of
the buildings? Are they all suitable just because they intersect with the suitable_terrain layer? Why or why not?
Which ones would you deem to be unsuitable?
Check your results
9.2.3
You can see from the results that some buildings which were included were not really suitable, so we can now
refine the analysis.
We want to ensure that our analysis returns only those buildings which fall entirely within the
suitable_terrain layer. How would you achieve this? Use one or more Vector Analysis tools and remember that our buildings are all over 100m squared in size.
Check your results
9.2.4 In Conclusion
You have now answered the original research question, and can offer an opinion (with reasons, backed by analysis)
for a recommendation regarding which property to develop.
9.3 Assignment
Using the Map Composer, make a new map representing the results of your analysis. Include these layers:
places (with labels),
hillshade,
solution (or new_solution),
roads and
either aerial_photos or DEM.
Write a short explanatory text to accompany it. Include in this text the criteria that were used in considering a
house for purchase and subsequent development, as well as explaining your recommendations for which buildings
are suitable.
9.3. Assignment
237
238
Rename the rainfall raster to Rainfall (with an initial capital). Initially when you load them, the images
will be gray rectangles. Dont worry, this will be changed later.
Save the map.
In order to properly see whats going on, the symbology for the layers needs to be changed.
239
Change the value in the associated box to 2.00 (it should be set to 0.00 by default).
Under the heading Contrast enhancement, change the value of the Current dropdown list to Stretch to
MinMax.
Click OK. The Rainfall raster, if visible, should change colors, allowing you to see different brightness
values for each pixel.
Repeat this process for the DEM, but set the standard deviations used for stretching to 4.00.
Add another OR operator, then work your way through the list of districts above in a similar fashion.
The final query should be
240
Click OK. The districts shown in your map are now limited to those in the list above.
241
Wait while the data is loaded. All the streets will now be visible.
Change the raster symbology as before (see Changing the symbology of raster layers).
Save the map.
You can now hide the vector layers by unchecking the box next to them in the Layers list. This will make
the map render faster and will save you some time.
In order to create the hillshade, you will need to use a plugin that was written for this purpose.
9.4.13 Slope
Click on the menu item Raster Terrain analysis.
Select the Slope analysis type, with the clipped DEM as the input layer.
Specify an appropriate file name and location for output purposes.
Check the Add result to project box.
Click OK.
The slope image has been calculated and added to the map. However, as usual it is just a gray rectangle. To
properly see whats going on, change the symbology as follows.
Open the layer Properties dialog (as usual, via the right-click menu of the layer).
242
9.4.14 Aspect
Use the same approach as for calculating the slope, but select Aspect in the initial dialog box.
Remember to save the map periodically.
Set the Output layer field to an appropriate location and file name.
Click OK.
Now find the correct aspect (east-facing: between 45 and 135 degrees) using the same approach.
Build the following expression:
((aspect@1 < 45) OR (aspect@1 > 135)) = 0
Find the correct rainfall (greater than 1200mm) the same way. Build the following expression:
(rainfall@1 < 1200) = 0
Having reclassified all the rasters, you will now see them displayed as gray rectangles in your map (assuming
that they have been added to the map correctly). To properly display raster data with only two classes (1 and 0,
meaning true or false), you will need to change their symbology.
243
The only criterion that remains is that the area must be 250m away from urban areas. We will satisfy this requirement by ensuring that the areas we compute are 250m or more from the edge of a rural area. Hence, we need to
find all rural areas first.
See the earlier instructions for building the Streets query if you get stuck.
When youre done, close the Query dialog.
You should see a collection of polygons from the Zoning layer. You will need to save these to a new layer file.
On the right-click menu for Zoning, select Save as....
Save your layer under the Zoning directory.
Name the output file rural.shp.
Click OK.
Add the layer to your map.
Click the menu item Vector Geoprocessing Tools Dissolve.
Select the rural layer as your input vector layer, while leaving the Use only selected features box unchecked.
Under Dissolve field, select Dissolve all .
Save your layer under the Zoning directory.
Click OK. A dialog will appear asking whether you want to add the new layer to the TOC (Table of
Contents, referring to the Layers list).
Click Yes.
Close the Dissolve dialog.
Remove the rural and Zoning layers.
Save the map.
Now you need to exclude the areas that are within 250m from the edge of the rural areas. Do this by creating a
negative buffer, as explained below.
244
Click Save.
Click OK and wait for the processing to complete.
Select Yes on the dialog that appears.
Close the Buffer dialog.
Remove the rural_dissolve layer.
Save the map.
In order to incorporate the rural zones into the same analysis with the three existing rasters, it will need to be
rasterized as well. But in order for the rasters to be compatible for analysis, they will need to be the same size.
Therefore, before you can rasterize, youll need to clip the vector to the same area as the three rasters. A vector
can only be clipped by another vector, so you will first need to create a bounding box polygon the same size as the
rasters.
245
246
Click OK.
Create a new vector file from the results by saving the candidate_areas vector after the query is complete
(and only the areas with a value of 1 are visible). Use the Save as... function in the layers right-click menu
for this.
Save the file in the Rasterprac directory.
Name the file candidate_areas_only.shp.
Save your map.
247
Under the New field heading in the dialog that appears, enter the field name area. The output field type
should be an integer, and the field width should be 10.
In Field calculator expresion, type:
$area
This means that the field calculator will calculate the area of each polygon in the vector layer and will then
populate a new integer column (called area) with the computed value.
Click OK.
Do the same thing for another new field called id. In Field calculator expresion, type:
$id
This ensures that each polygon has a unique ID for identification purposes.
Click Toggle editing mode again, and save your edits if prompted to do so.
248
249
250
CAPITOLUL 10
Module: Plugins
Plugins allow you to extend the functionality QGIS offers. In this module, youll be shown how to activate and
use plugins.
10.1.1
To open the Plugin Manager, click on the menu item Plugins Manage and Install Plugins.
In the dialog that opens, find the Processing plugin:
Click in the box next to this plugin and uncheck it to uninstall it.
251
Click Close.
Looking at the menu, you will notice that the Processing menu is is now gone. This means that many of
the processing functions you have been using before have disappeared! This is because they are part of the
Processing plugin, which needs to be activated for you to use them.
Open the Plugin Manager again and reactivate the Processing plugin by clicking in the checkbox next to it
and clicking Close..
The Processing menu should be available again.
10.1.2
The list of plugins that you can activate and deactivate draws from the plugins that you currently have installed.
To install new plugins, select the Not Installed option in the Plugin Manager dialog. The plugins available
for you to install will be listed here. This list will vary depending on your existing system setup.
You can find information about each plugin by selecting it in the list of plugins displayed.
252
A plugin can be installed by clicking the Install Plugin button below the plugin information panel.
10.1.3
The plugins that are available to you for installation depend on which plugin repositories you are configured to
use.
QGIS plugins are stored online in repositories. By default, only the official repositories are active, meaning that
you can only access official plugins. These are usually the first plugins you want, because they have been tested
thoroughly and are often included in QGIS by default.
It is possible, however, to try out more plugins than the default ones. First, you want to configure additional
repositories. To do this:
Open the Settings tab in the Plugin Manager dialog:
253
You will now see the new plugin repo listed in the list of configured Plugin Repositories
254
You can also select the option to display Experimental Plugins by selecting the Show also experimental
plugins checkbox.
If you now switch back to the Get More tab, you will see that additional plugins are now available for
installation.
To install a plugin, simply click on it in the list and then click the Install plugin button.
10.1.4 In Conclusion
Installing plugins in QGIS is simple and effective!
10.2.1
the
srtm_41_19.tif
raster
dataset
in
it
(look
in
255
From the lesson on raster analysis, youre already familiar with raster analysis functions. You used GDAL tools
(accessible via Raster > Analysis) for this. However, you should also know about the Raster Terrain Analysis
plugin. This ships standard with newer versions of QGIS, and so you dont need to install it separately.
Open the Plugin Manager and check that the Raster Terrain Analysis plugin is enabled:
Open the Raster menu. You should see a Terrain analysis submenu.
Click on Terrain analysis Relief and input the following options:
Save the new file under exercise_data/plugins/relief.tif (create a new folder if necessary).
Leave the Output format and Z factor unchanged.
256
These are the colors that the plugin will use to create the relief.
If you like, you can change these colors by double-clicking on each rows color bar. For example:
257
This achieves a similar effect to when you used the semi-transparent hillshade as an overlay over another raster
layer. The advantage of this plugin is that it creates this effect using only one layer.
10.2.2
258
Open the Project Properties Dialog by selecting Project > Project Properties from the menu.
Enable on the fly projection and use the Google Mercator projection:
10.2. Lesson: Useful QGIS Plugins
259
Now use the plugin to give you a Google map of the area. You can click on Plugins OpenLayers Plugin
Add Google Hybrid Layer to add it:
This will load a new raster image in from Google that you can use as a backdrop, or to help you find out where
you are on the map. Here is such a layer, with our own vector road layer as overlay:
260
Note: You may need to drag your roads layer above the Google layer to make it visible above the background
layer. It may also be necessary to zoom to the extent of the roads layer to re-center the map.
10.2.3
261
262
10.2.4 In Conclusion
There are many useful plugins available for QGIS. Using the built-in tools for installing and managing these
plugins, you can find new plugins and make optimum use of them.
263
264
CAPITOLUL 11
When considering data sources for a map, there is no need to be restricted to data which you have saved on the
computer youre working on. There are online data sources which you can load data from as long as you are
connected to the Internet.
In this module, well cover two kinds of web-based GIS services: Web Mapping Services (WMS) and Web Feature
Services (WFS).
11.1.1
For this exercise, you can either use the basic map you made at the start of the course, or just start a new map and
load some existing layers into it. For this example, we used a new map and loaded the original places and landuse
layers and adjusted the symbology:
265
Load these layers into a new map, or use your original map with only these layers visible.
Before starting to add the WMS layer, first deactivate on the fly projection. This may cause the layers to
no longer overlap properly, but dont worry: well fix that later.
To add WMS layers, click on the Add WMS Layer button:
Remember how you connected to a SpatiaLite database at the beginning of the course. The landuse, places, and
water layers are in that database. To use those layers, you first needed to connect to the database. Using a WMS
is similar, with the exception that the layers are on a remote server.
To create a new connection to a WMS, click on the New button.
Youll need a WMS address to continue. There are several free WMS servers available on the Internet. One of
these is terrestris, which makes use of the OpenStreetMap dataset.
To make use of this WMS, set it up in your current dialog, like this:
266
267
Click Connect. In the list below, you should now see these new entries loaded:
Click once on the OSM-WMS layer. This will display its Coordinate Reference System:
Since were not using WGS 84 for our map, lets see all the CRSs we have to choose from.
Click the Change button. You will see a standard Coordinate Reference System Selector dialog.
We want a projected CRS, so lets choose WGS 84 / Psuedo Mercator.
269
Click OK.
Click Add and the new layer will appear in your map as OSM-WMS.
In the Layers list, click and drag it to the bottom of the list.
You will notice that your layers arent located correctly. This is because on the fly projection is disabled. Lets
enable it again, but using the same projection as the OSM-WMS layer, which is WGS 84 / Pseudo Mercator.
Enable on the fly projection.
In the CRS tab (Project Properties dialog), enter the value pseudo in the Filter field:
270
271
Note how the WMS layers streets and our own streets overlap. Thats a good sign!
The nature and limitations of WMS
By now you may have noticed that this WMS layer actually has many features in it. It has streets, rivers, nature
reserves, and so on. Whats more, even though it looks like its made up of vectors, it seems to be a raster, but you
cant change its symbology. Why is that?
This is how a WMS works: its a map, similar to a normal map on paper, that you receive as an image. What
usually happens is that you have vector layers, which QGIS renders as a map. But using a WMS, those vector
layers are on the WMS server, which renders it as a map and sends that map to you as an image. QGIS can display
this image, but cant change its symbology, because all that is handled on the server.
This has several advantages, because you dont need to worry about the symbology. Its already worked out, and
should be nice to look at on any competently designed WMS.
On the other hand, you cant change the symbology if you dont like it, and if things change on the WMS server,
then theyll change on your map as well. This is why you sometimes want to use a Web Feature Service (WFS)
instead, which gives you vector layers separately, and not as part of a WMS-style map.
This will be covered in the next lesson, however. First, lets add another WMS layer from the terrestris WMS
server.
11.1.2
Try Yourself
at
this
URL:
Load the BEDROCKGEOLOGY layer into the map (use the Add WMS Layer button as before). Remember
to check that its in the same WGS 84 / World Mercator projection as the rest of your map!
You might want to set its Encoding to JPEG and its Tile size option to 200 by 200, so that it loads faster:
272
11.1.3
Try Yourself
Hide all other WMS layers to prevent them rendering unnecessarily in the background.
Add the OGC WMS server at this URL: http://ogc.gbif.org:80/wms
Add the bluemarble layer.
Check your results
11.1.4
Try Yourself
11.1.5 In Conclusion
Using a WMS, you can add inactive maps as backdrops for your existing map data.
273
11.2.1
Start a new map. This is for demo purposes and wont be saved.
Ensure that on the fly re-projection is switched off.
Click the Add WFS Layer button:
and
the
URL
as
Click OK, and the new connection will appear in your Server connections.
Click the Connect. A list of the available layers will appear:
274
275
Click Add.
It may take a while to load the layer. When it has loaded, it will appear in the map. Here it is over the outlines of
Antarctica (available on the same server, and by the name of antarctica_country_border):
How is this different from having a WMS layer? That will become obvious when you see the layers attributes.
Open the south_poles_wfs layers attribute table. You should see this:
Since the points have attributes, we are able to label them, as well as change their symbology. Heres an example:
276
Add labels to your layer to take advantage of the attribute data in this layer.
Differences from WMS layers
A Web Feature Service returns the layer itself, not just a map rendered from it. This gives you direct access to the
data, meaning that you can change ts symbology and run analysis functions on it. However, this is at the cost of
much more data being transmitted. This will be especially obvious if the layers youre loading have complicated
shapes, a lot of attributes, or many features; or even if youre just loading a lot of layers. WFS layers typically
take a very long time to load because of this.
11.2.2
Although it is of possible to query a WFS layer after having loaded it, its often more efficient to query it before
you load it. That way, youre only requesting the features you want, meaning that you use far less bandwidth.
For example, on the WFS server were currently using, there is a layer called countries (excluding Antarctica).
Lets say that we want to know where South Africa is relative to the south_poles_wfs layer (and perhaps also the
antarctica_country_border layer) thats already been loaded.
There are two ways to do this. You can load the whole countries ... layer, and then build a query as usual once
its loaded. However, transmitting the data for all the countries in the world and then only using the data for South
Africa seems a bit wasteful of bandwidth. Depending on your connection, this dataset can take several minutes to
load.
The alternative is to build the query as a filter before even loading the layer from the server.
In the Add WFS Layer ... dialog, connect to the server we used before and you should see the list of available
layers.
Double-click next to the countries ... layer in the Filter field, or click Build query:
277
In the dialog that appears, build the query "Countryeng" = South Africa:
278
279
Click Add with the countries layer selected as above. Only the country with the Countryeng value of
South Africa will load from that layer:
You dont have to, but if you tried both methods, youll notice that this is a lot faster than loading all the countries
before filtering them!
Notes on WFS availability
It is rare to find a WFS hosting features you need, if your needs are very specific. The reason why Web Feature
Services are relatively rare is because of the large amounts of data that must be transmitted to describe a whole
feature. It is therefore not very cost-effective to host a WFS rather than a WMS, which sends only images.
280
The most common type of WFS youll encounter will therefore probably be on a local network or even on your
own computer, rather than on the Internet.
11.2.3 In Conclusion
WFS layers are preferable over WMS layers if you need direct access to the attributes and geometries of the layers.
However, considering the amount of data that needs to be downloaded (which leads to speed problems and also a
lack of easily available public WFS servers) its not always possible to use a WFS instead of a WMS.
281
282
CAPITOLUL 12
Module: GRASS
GRASS (Geographic Resources Analysis Support System) is a well-known open source GIS with a wide array of
useful GIS functions. It was first released in 1984, and has seen much improvement and additional functionality
since then. QGIS allows you to make use of GRASS powerful GIS tools directly.
12.1.1
To launch GRASS from within QGIS, you need to activate it as with any other plugin. First, open a new QGIS
project.
In the Plugin Manager, enable GRASS in the list:
283
Before you can use GRASS, you need to create a mapset. GRASS always works in a database environment,
which means that you need to import all the data you want to use into a GRASS database.
Click on the New mapset button:
284
Click Next.
GRASS needs to create a location, which describes the maximum extents of the geographic area youll be
working in.
Call the new location South_Africa:
285
Click Next.
Well be working with WGS 84, so search for and select this CRS:
286
Click Next.
Now select the region South Africa from the dropdown and click Set:
287
Click Next.
Create a mapset, which is the map file that youll be working with.
288
Once youre done, youll see a dialog asking you to confirm that the settings it displays are correct.
Click Finish.
Click OK on the success dialog.
12.1.2
Youll now have a blank map. To load data into GRASS, you need to follow a two-step process.
Load data into QGIS as usual.
exercise_data/epsg4326/) for now.
Use
the
roads.shp
dataset
(found
under
289
Set the loaded layer to roads and its GRASS versions name to g_roads to prevent confusion.
290
Note:
Note the extra import options provided under Advanced Options. These include the ability to add a
WHERE clause for the SQL query used for importing the data.
Click Run to begin the import.
When its done, click the View output button to see the newly imported GRASS layer in the map.
Close first the import tool (click the Close button to the immediate right of View output), then close the
GDAL Tools window.
Remove the original roads layer.
Now you are left with only the imported GRASS layer as displayed in your QGIS map.
12.1.3
Recall that our DEM is in the Projected CRS UTM 33S / WGS 84, but our GRASS project is in the Geographic
CRS WGS 84. So lets re-project the DEM first.
Load the srtm_41_19.tif dataset (found under exercise_data/raster/SRTM/) into the QGIS
map as usual, using QGIS Add Raster Layer tool.
291
Re-project it using GDAL Warp tool (Raster Projections Warp (Reproject)), setting it up as shown:
Save the raster under the same folder as the original, but with the file name DEM_WGS84.tif. Once it
appears in your map, remove the srtm_41_19.tif dataset from your Layers list.
Now that its reprojected, you can load it into your GRASS database.
Open the GRASS Tools dialog again.
292
Click Run.
When the process is done, click View output.
Close the current tab, and then Close the whole dialog.
293
12.1.4 In Conclusion
The GRASS workflow for ingesting data is somewhat different from the QGIS method because GRASS loads its
data into a spatial database structure. However, by using QGIS as a frontend, you can make the setup of a GRASS
mapset easier by using existing layers in QGIS as data sources for GRASS.
12.2.1
294
295
12.2.2
GRASS allows you to use a DEM to visualize your data in three dimensions. The tool youll use for this operates
on the GRASS Region, which at the moment is set to the whole extent of South Africa, as you set it up before.
To redefine the extent to cover only our raster dataset, click this button:
When this tool is activated, your cursor will turn into a cross whe over the QGIS map canvas.
Using this tool, click and drag a rectangle around the edges of the GRASS raster.
Click OK in the GRASS Region Settings dialog when done.
Search for the nviz tool:
296
Set it up as shown:
297
Remember to enable both Use region of this map buttons to the right of the two raster selection dropdown
menus. This will allow NVIZ to correctly assess the resolution of the rasters.
Click the Run button.
NVIZ will set up a 3D environment using the raster and vector selected. This may take some time, depending on
your hardware. When its done, you will see the map rendered in 3D in a new window:
298
Experiment with the height, z-exag, and View method settings to change your view of the data. The navigation
methods may take some getting used to.
After experimenting, close the NVIZ window.
12.2.3
Open the GRASS Tools dialogs Modules List tab and search for calc.
From the list of modules, select r.mapcalc (not r.mapcalculator, which is more basic).
Start the tool.
The Mapcalc dialog allows you to construct a sequence of analyses to be performed on a raster, or collection of
rasters. You will use these tools to do so:
299
When you click Run, your output should look like this:
300
This shows all the areas where the terrain is lower than 500 meters or higher than 1000 meters.
301
12.2.4 In Conclusion
In this lesson, we have covered only a few of the many tools GRASS offers. To explore the capabilities of GRASS
for yourself, open the GRASS Tools dialog and scroll down the Modules List. Or for a more structured approach,
look under the Modules Tree tab, which organizes tools by type.
302
CAPITOLUL 13
Module: Assessment
Use your own data for this section. You will need:
a point vector dataset of points of interest, with point names and multiple categories
a line vector dataset of roads
a polygon vector dataset of land use (using property boundaries)
a visual-spectrum image (such as an aerial photograph)
a DEM (downloadable from this URL if you dont have your own)
Label the points according to a unique attribute, such as place names. Use a small font and keep the labels
inconspicuous. The information should be available, but shouldnt be a main feature of the map.
Classify the points themselves into different colors based on a category. For example, categories could
include tourist destination, police station, and town center.
section.
Classify the point size by importance: the more significant a feature, the larger its point. However, dont
exceed the point size of 2.00.
For features that arent located at a single point (for example, provincial/regional names, or town names at
a large scale), dont assign any point at all.
303
Dont use point symbols to symbolize the layer at all. Instead, use labels centered over the points; the point
symbols themselves should have no size.
Use Data defined settings to style the labels into meaningful categories.
Add appropriate columns to the attribute data if necessary. When doing so, dont create fictional data rather, use the Field Calculator to populate the new columns, based on appropriate existing values in the
dataset.
Change the road symbology to a light color with a broad line. Make it somewhat transparent as well.
Create a symbol with multiple symbol layers. The resulting symbol should look like a real road. You can
use a simple symbol for this; for example, a black line with a thin white solid line running down the center.
It can be more elaborate as well, but the resulting map should not look too busy.
If your dataset has a high density of roads at the scale you want to show the map at, you should have two road
layers: the elaborate road-like symbol, and a simpler symbol at smaller scales. (Use scale-based visibility
to make them switch out at appropriate scales.)
All symbols should have multiple symbol layers. Use symbols to make them display correctly.
section above.
In addition, roads should be classified. When using realistic road-like symbols, each type of road should
have an appropriate symbol; for example, a highway should appear to have two lanes in either direction.
304
Classify the layer according to land use. Where appropriate, incorporate symbol layers, different symbol
types, etc. Keep the results looking subdued and uniform, however. Keep in mind that this will be part of a
backdrop!
Use rule-based classification to classify the land use into general categories, such as urban, rural, nature
reserve, etc.
13.2.1
Include raster analysis in your results. Consider at least one derived property of the raster, such as its aspect
or slope.
305
306
CAPITOLUL 14
In modules 1 through 13, you have already learned quite a lot about QGIS and how to work with it. If you are
interested in learning about some basic forestry applications of GIS, following this module will give you the ability
to apply what you have learned and will show you some new useful tools.
The development of this module has been sponsored by the European Union.
307
14.2.1
The first task you will have to do is to scan your map. If your map is too big, then you can scan it in different parts
but keep in mind that you will have to repeat preprocessing and georeferencing tasks for each part. So if possible,
scan the map in as few parts as possible.
If you are going to use a different map that the one provided with this manual, use your own scanner to scan the
map as an image file, a resolution of 300 DPI will do. If your map has colors, scan the image in color so that
you can later use those colors to separate information from your map into different layers (for ex., forest stands,
contour lines, roads...).
For this exercise you will use a previously scanned map, you can find it as rautjarvi_map.tif in the data
folder exercise_data/forestry
14.2.2
Open QGIS and set the projects CRS to ETRS89 / ETRS-TM35FIN in Project Project Properties CRS,
which is the currently used CRS in Finland. Make sure that Enable on the fly CRS transformation is checked,
since we will be working with old data that is another CRS.
308
309
Click OK.
The map contains several cross-hairs marking the coordinates in the map, we will use those to georeferenciate
this image. You can use the zooming and panning tools as you usually do in QGIS to inspect the image in the
Georeferencers window.
Zoom in to the left lower corner of the map and note that there is a cross-hair with a coordinate pair, x
and y, that as mentioned before are in KKJ / Finland zone 2 CRS.You will use this point as the first
ground control point for the georeferencing your map.
Select the Add point tool and click in the intersection of the cross-hairs (pan and zoom as needed).
In the Enter map coordinates dialogue write the coordinates that appear in the map (X: 2557000 and Y:
6786000).
Click OK.
The first coordinate for the georeferencing is now ready.
310
Look for other cross-hairs in the black lines image, they are separated 1000 meters from each other both in North
and East direction. You should be able to calculate the coordinates of those points in relation to the first one.
Zoom out in the image and move to the right until you find other cross-hair, and estimate how many kilometres
you have moved. Try to get ground control points as far from each other as possible. Digitize at least three more
ground control points in the same way you did the first one. You should en up with something similar to this:
With already three digitized ground control points you will be able to see the georeferencing error as a red line
coming out of the points. The error in pixels can be seen also in the GCP table in the dX[pixels] and dY[pixels]
columns. The error in pixels should not be higher than 10 pixels, if it is you should review the points you have
digitized and the coordinates you have entered to find what the problem is. You can use the image above as a
guide.
Once you are happy with your control points save your ground control points, in case that you will need them
later, and you will:
File Save GCP points as....
311
In
the
folder
exercise_dataforestrydigitizing,
:kdb:rautjarvi_map.tif.points.
name
the
file
312
Save the changes to your QGIS project, you will continue from this point for the next lesson.
14.2.3 In Conclusion
As you have seen, georeferencing a paper map is a relatively straight forward operation.
313
14.3.1
Open your map_digitizing.qgs project in QGIS, that you saved from the previous lesson.
Once you have scanned and georeferenced your map you could start to digitize directly by looking at the image
as a guide. That would most likely be the way to go if the image you are going to digitize from is, for example, an
aerial photograph.
If what you are using to digitize is a good map, as it is in our case, it is likely that the information is clearly
displayed as lines with different colors for each type of element. Those colors can be relatively easy extracted as
individual images using an image processing software like GIMP. Such separate images can be used to assist the
digitizing, as you will see below.
The first step will be to use GIMP to obtain an image that contains only the forest stands, that is, all those greenish
lines that you could see in the original scanned map:
Open GIMP (if you dont have it installed yet, download it from the internet or ask your teacher).
Open the original map image,
File Open,
rautjarvi_map.tif in the
exercise_data/forestry folder. Note that the forest stands are represented as green lines
(with the number of the stand also in green inside each polygon).
314
Now you can select the pixels in the image that are making up the forest stands borders(the greenish pixels):
Open the tool Select By color.
With the tool active, zoom into the image (Ctrl + mouse wheel) so that a forest stand line is close enough to
differentiate the pixels forming the line. See the left image below.
Click and drag the mouse cursor in the middle of the line so that the tool will collect several pixel color
values.
Release the mouse click and wait a few seconds. The pixels matching the colors collected by the tool will
be selected through the whole image.
Zoom out to see how the greenish pixels have been selected throughout the image.
If you are not happy with the result, repeat the click and drag operation.
You pixel selection should look something like the right image below.
315
Once you are done with the selection you need to copy this selection as a new layer and then save it as separate
image file:
Copy (Ctr+C) the selected pixels.
And paste the pixels directly (Ctr+V), GIMP will display the pasted pixels as a new temporary layer in the
Layers - Brushes panel as a Floating Selection (Pasted Layer).
Right click that temporary layer and select To New Layer.
Click the eye icon next to the original image layer to switch it off, so that only the Pasted Layer is visible:
316
Finally, select File Export..., set Select File Type (By Extension) as a TIFF image, select the
digitizing folder and name it rautjarvi_map_green.tif. Select no compression when asked.
You could do the same process with other elements in the image, for example extracting the black lines that
represent roads or the brown ones that represent the terrain contour lines. But for us, the forest stands is enough.
14.3.2
As you did in the previous lesson, you need to georeference this new image to be able to use it with the rest of
your data.
Note that you dont need to digitize the ground control points any more because this image is basically the same
image as the original map image, as far as the Georeferencer tool is concerned. Here are some things you should
remember:
This image is also, of course, in KKJ / Finland zone 2 CRS.
You should use the ground control points you saved, File Load GCP points.
Temember to review the Transformation settings.
Name the output raster as rautjarvi_green_georef.tif in the digitizing folder.
Check that the new raster is fitting nicely with the original map.
14.3.3
Having in mind the digitizing tools in QGIS, you might already be thinking that it would be helpful to snap to
those green pixels while digitizing. That is precisely what you are going to do next create points from those pixels
to use them later to help you follow the the forest stands borders when digitizing, by using the snapping tools
available in QGIS.
Use the Raster Conversion Polygonize (Raster to Vector) tool to vectorize your green lines to polygons.
If you dont remember how, you can review it in Lesson: Raster to Vector Conversion.
Save as rautjarvi_green_polygon.shp inside the digitizing folder.
Zoom in and see what the polygons look like. You will get something like this:
317
Next one option to get points out of those polygons is to get their centroids:
Open Vector Geometry tools Polygon centroids.
Set your the polygon layer you just got as the input file for the tool.
Name the output as green_centroids.shp inside the digitizing folder.
Check Add result to canvas.
Run the tool to calculate the centroids for the polygons.
318
Now you can remove the rautjarvi_green_polygon layer from the TOC.
Change the symbology of the centroids layer as:
Open the Layer Properties for green_centroids.
Go to the Style tab.
Set the Unit to Map unit.
Set the Size to 1.
It is not necessary to differentiate points from each other, you just need them to be there for the snapping tools to
use them. You can use those points now to follow the original lines much easily than without them.
14.3.4
Now you are ready to start with the actual digitizing work. You would start by creating a vector file of polygon
type, but for this exercise, there is a shapefile with part of the area of interest already digitized. You will just finish
digitizing the half of the forest stands that are left between the main roads (wide pink lines) and the lake:
319
320
With these snapping settings, whenever you are digitizing and get close enough to one of the points in the centroids
layer or any vertex of your digitized polygons, a pink cross will appear on the point that will be snapped to.
Finally, turn off the visibility of all the layers except forest_stands and rautjarvi_georef. Make sure
that the map image has not transparency any more.
A couple of important things to note before you start digitizing:
Dont try to be too accurate with the digitizing of the borders.
If a border is a straight line, digitize it with just two nodes. In general, digitize using as few nodes as
possible.
Zoom in to close ranges only if you feel that you need to be accurate, for example, at some corners or when
you want a polygon to connect with another polygon at a certain node.
Use the mouses middle button to zoom in/out and to pan as you digitize.
Digitize only one polygon at a time.
After digitizing one polygon, write the forest stand id that you can see from the map.
Now you can start digitizing:
Locate the forest stand number 357 in the map window.
Enable editing for the forest_stands.shp layer.
Select the Add feature tool.
Start digitizing the stand 357 by connecting some of the dots.
Note the pink crosses indicating the snapping.
321
When you are done, right click to end digitizing that polygon.
Enter the forest stand id (in this case 357).
Click OK.
If you were not prompted for the polygon id when you finished digitizing it, go to Settings Options
Digitizing and make sure that the Suppress attribute form pop-up after feature creation is not checked.
Your digitized polygon will look like this:
322
Now for the second polygon, pick up the stand number 358. Make sure that the Avoid int. is checked for the
forest_stands layer. This option does not allow intersecting polygons at digitizing, so that if you digitize
over an existing polygon, the new polygon will be trimmed to meet the border of the already existing polygon/s.
You can use this characteristic to automatically obtain a common border.
Begin digitizing the stand 358 at one of the common corners with the stand 357.
Then continue normally until you get to the other common corner for both stands.
Finally, digitize a few points inside polygon 358 making sure that the common border is not intersected.
See left image below.
Right click to finish editing the forest stand 358.
Enter the id as 358.
Click OK, your new polygon should show a common border with the stand 357 as you can seen in the image
on the right.
323
The part of the polygon that was overlapping the existing polygon has been automatically trimmed out and you
are left with a common border, as you intended it to be.
14.3.5
Now you have two forest stands ready. And a good idea on how to proceed. Continue digitizing on your own until
you have digitized all the forest stands that are limited by the main road and the lake.
It might look like a lot of work, but you will soon get used to digitizing the forest stands. It should take you about
15 minutes.
During the digitizing you might need to edit or delete nodes, split or merge polygons. You learned about the
necessary tools in ..create_vector_data/topo_editing, now is probably a good moment to go read
about them again.
Remember that having Enable topological editing activated, allows you to move nodes common to two polygons
so that the common border is edited at the same time for both polygons.
Your result will look like this:
324
14.3.6
It is possible that the forest inventory data you have for you map is also written in paper. In that case, you would
have to first write that data to a text file or a spreadsheet. For this exercise, the information from the inventory for
1994 (the same inventory as the map) is ready as a comma separated text (csv) file.
Open the rautjarvi_1994.csv file from the exercise_data\forestry in a text editor and note that
the inventory data file has an attribute called ID that has the numbers of the forest stands. Those numbers are the
same as the forest stands ids you have entered for your polygons and can be used to link the data from the text file to
your vector file. You can see the metadata for this inventory data in the file rautjarvi_1994_legend.txt
in the same folder.
Open the .csv in QGIS with the Layer Add Delimited Text Layer... tool. In the dialog, set it as follows:
325
14.3.7
The data from the .csv file is just linked to your vector file. To make this link permanent, so that the data is
actually recorded to the vector file you need to save the forest_stands layer as a new vector file. Close the
attribute table and right click the forest_stands layer to save it as forest_stands_1994.shp.
Open your new forest_stands_1994.shp in your map if you did not added yet. Then open the attribute
table. You notice that the names of the columns that you just added are no very useful. To solve this:
Add the plugin Table Manager as you have done with other plugins before.
326
Click on Save.
Select Yes to keep the layer style.
Close the Table Manager dialogue.
To finish gathering the information related to these forest stands, you might calculate the area and the perimeter
of the stands. You calculated areas for polygons in Lesson: Supplementary Exercise. Go back to that lesson if you
need to and calculate the areas for the forest stands, name the new attribute Area and make sure that the values
calculated are in hectares.
Now your forest_stands_1994.shp layer is ready and packed with all the available information.
Save your project to keep the current map presentation in case you need to come back later to it.
327
14.3.8 In Conclusion
It has taken a few clicks of the mouse but you now have your old inventory data in digital format and ready for
use in QGIS.
14.4.1
The National Land Survey of Finland has an open data policy that allows you downloading a variety of geographical data like aerial imagery, traditional topographic maps, DEM, LiDAR data, etc. The service can be accessed
also in English here. The aerial image used in this exercise has been created from two orthorectified CIR images
downloaded from that service (M4134F_21062012 and M4143E_21062012).
Open QGIS and set the projects CRS to ETRS89 / ETRS-TM35FIN in Project Project Properties
CRS.
Make sure that Enable on the fly CRS transformation is checked.
From the exercise_data\forestry\ folder, add the CIR image rautjarvi_aerial.tif and
containing the digitized lakes.
Then save the QGIS project as digitizing_2012.qgs.
The CIR images are from 2012. You can compare the stands that were created in 1994 with the situation almost
20 years ago.
Add your forest_stands_1994.shp layer.
Set its styling so that you can see through your polygons.
Review how the old forest stands follow (or not) what you might visually interpret as an homogeneous
forest.
Zoom and pan around the area. You probably will notice that some of the old forest stands might be still corresponding with the image but others are not.
This is a normal situation, as some 20 years have passed by and different forest operations have been done (harvesting, thinning...). It is also possible that the forest stands looked homogeneous back in 1992 to the person who
328
digitized them but as time has passed some forest has developed in different ways. Or simply the priorities for the
forest inventory were different that they are today.
Next, you will create new forest stands for this image without using the old ones. Later you can compare them to
see the differences.
14.4.2
Lets digitize the same area that was covered by the old inventory, limited by the roads and the lake. You dont
have to digitize the whole area, as in the previous exercise you can start with a vector file that already contains
most of the forest stands.
Remove the forest_stands_1994.shp layer.
Add the forest_stands_2012.shp layer, located in the exercise_data\forestry\ folder.
Set the styling of this layer so that the polygons have no fill and the borders are visible.
329
You can see that a region to the North of the inventory area is still missing. That will be your task, digitizing the
missing forest stands.
But before you start, spend some time reviewing the forest stands already digitized and the corresponding forest
in the image. Try to get an idea about how the stands borders are decided, it helps if you have some forestry
knowledge.
Some ideas about what you could identify from the images:
What forests are deciduous species (in Finland mostly birch forests) and which ones are conifers (in this
region pine or spruce). In CIR images, deciduous species will often come as bright red color whereas
conifers present dark green colors.
When a forest stand age changes, by looking at the sizes of the tree crowns that can be identified in the
imagery.
The different forest stands densities, for example forest stand were a thinning operation has recently been
done would clearly show spaces between the tree crowns and should be easy to differentiate from other
330
14.4.3
When digitizing the forest stands, you should try to get forest areas that are as homogeneous as possible in terms
of tree species, forest age, stand density... Dont be too detailed though, or you will end up making hundreds of
small forest stands that would not be useful at all. You should try to get stands that are meaningful in the context
of forestry, not too small (at least 0.5 ha) but not too big either (no more than 3 ha).
With this indications in mind, you can now digitize the missing forest stands.
Enable editing for forest_stands_2012.shp.
Set up the snapping and topology options as in the image.
Remember to click Apply or OK.
331
Start digitizing as you did in the previous lesson, with the only difference that you dont have any point layer
that you are snapping to. For this area you should get around 14 new forest stands. While digitizing, fill in the
Stand_id field with numbers starting at 901.
When you are finished your layer should look something like:
Now you have a new set of polygons defining the different forest stands for the current situation as can interpreted
from the CIR images. But you are obviously still missing the forest inventory data, right? For that you will still
need to visit the forest and get some sample data that you will use to estimate the forest attributes for each of the
forest stands. You will see how to do that in the next lesson.
332
For the moment, you still can improve your vector layer with some extra information that you have about conservation regulation that should be taken into account for this area.
14.4.4
tion
For the area you are working with, it has been researched that the following conservation regulations must be
taken into account while doing the forest planning:
Two locations of a protected species of Siberian flying squirrel (Pteromys volans) have been identified.
According to the regulation, an area of 15 meters around the spots must be left untouched.
A riparian forest of special interest growing along a stream in the area must be protected. In a visit to the
field, it was found that 20 meters to both sides of the stream must be protected.
You have one vector file containing the information about the squirrel locations and another containing the digitized stream running in the North area towards the lake. From the exercise_data\forestry\ folder, add
the vector files squirrel.shp and stream.shp.
For the protection of the squirrels locations, you are going to add a new attribute (column) to your new forest
stands that will contain information about point locations that have to be protected. That information will later be
available whenever a forest operation is planned, and the field team will be able to mark the area that has to be left
untouched before the work starts.
Open the attribute table for the squirrel layer.
You can see that there are two locations that are defined as Siberian flying squirrel, and that the area to be
protected is indicated by a distance of 15 meters from the locations.
To join the information about the squirrels to your forest stands, you can use the Join attributes by location:
Open menu:Vector > Data Management Tools > Join attributes by location.
Set the forest_stands_2012.shp layer as the gui:Target vector layer.
As gui:Join vector layer select the squirrel.shp point layer.
Name the output file as stands_squirrel.shp.
In gui:Output table select gui:Keep all records (including non-matching target records). So that you keep all
the forest stands in the layer instead of only keeping those that are spatially related to the squirrel locations.
Click gui:OK.
Select Yes when prompted to add the layer to the TOC.
Close the dialogue box.
333
Now you have a new forest stands layer, stands_squirrel where there are new attributes corresponding to
the protection information related to the Siberian flying squirrel.
Open the table of the new layer and order it so that the forest stands with information for the Protection attribute.
You should have now two forest stands where the squirrel has been located:
334
Although this information might be enough, look at what areas related to the squirrels should be protected. You
know that you have to leave a buffer of 15 meters around the squirrels location:
Open Vector Geoprocessing Tools Buffer.
Make a buffer of 15 meters for the squirrel layer.
Name the result squirrel_15m.shp.
335
You will notice that if you zoom in to the location in the Northern part of the area, the buffer area extends to
the neighbouring stand as well. This means that whenever a forest operation would take place in that stand, the
protected location should also be taken into account.
336
From your previous analysis, you did not get that stand to register information about the protection status. To
solve this problem:
Run the Join attributes by location tool again.
But this time use the squirrel_15m layer as join layer.
Name the output file as stands_squirrel_15m.shp.
337
Open the attribute table for the this new layer and note that now you have three forest stands that have the information about the protection locations. The information in the forest stands data will indicate to the forest manager
that there are protection considerations to be taken into account. Then he or she can get the location from the
squirrel dataset, and visit the area to mark the corresponding buffer around the location so that the operators
in the field can avoid disturbing the squirrels environment.
14.4.5
Following the same approach as indicated for the protected squirrel locations you can now update your forest
stands with protection information related to the stream identified in the field:
Remember that the buffer in this case is 20 meters around it.
338
You want to have all the protection information in the same vector file,
stands_squirrel_15m layer as the target.
so use the
14.4.6 In Conclusion
You have seen how to interpret CIR images to digitize forest stands. Of course it would take some practice to
make more accurate stands and usually using other information like soil maps would give better results, but you
know now the basis for this type of task. And adding information from other datasets resulted to be quite a trivial
task.
339
14.5.2
For the forest you are working with, the manager has decided that a systematic sampling design is the most
appropriate for this forest and has decided that a fixed distance of 80 meters between the sample plots and sampling
lines will yield reliable results (for this case, +- 5% average error at a probability of 68%). Variable size plots has
been decided to be the most effective method for this inventory, for growing and mature stands, but a 4 meters
fixed radius plots will be used for seedling stands.
In practice, you simply need to represented the sample plots as points that will be used by the field teams later:
Open your digitizing_2012.qgs project from the previous lesson.
Remove all the layers except for forest_stands_2012.
Save your project now as forest_inventory.qgs
Now you need to create a rectangular grid of points separated 80 meters from each other:
Open Vector Research Tools Regular points.
In the Area definitions select Input Boundary Layer.
And as input layer set the forest_stands_2012 layer.
In the Grid Spacing settings, select Use this point spacing and set it to 80.
Save the output as systematic_plots.shp in the forestry\sampling\ folder.
Check Add result to canvas.
Click OK.
Note: The suggested Regular points creates the systematic points starting in the corner upper-left corner of the
extent of the selected polygon layer. If you want to add some randomness to this regular points, you could use
a randomly calculated number between 0 and 80 (80 is the distance between our points), and then write it as the
Initial inset from corner (LH side) parameter in the tools dialog.
You notice that the tool has used the whole extent of your stands layer to generate a rectangular grid of points. But
you are only interested on those points that are actually inside your forest area (see the images below):
340
341
Nevertheless, the number values in the id field are not so good. It would be better if the naming would be
something like p_1, p_2.... You can create a new column for the systematic_plots_clip layer:
Go to the Attribute table for systematic_plots_clip.
Enable the edit mode.
Open the Field calculator and name the new column :kbd:Plot_id?.
Set the Output field typeto :kbd:Text (string).
In the Expression field, write, copy or construct this formula concat(P_, $rownum ). Remember
that you can also double click on the elements inside the Function list. The concat function can be found
under String and the $rownum parameter can be found under Record.
Click OK.
Disable the edit mode and save your changes.
Now you have a new column with plot names that are meaningful to you. For the systematic_plots_clip
layer, change the field used for labeling to your new Plot_id field.
342
14.5.3
The field teams will be probably using a GPS device to locate the sample plots you planned. The next step is
to export the points you created to a format that your GPS can read. QGIS allows you to save your point and
line vector data in GPS eXchange Format (GPX)<http://en.wikipedia.org/wiki/GPS_Exchange_Format>, which
is an standard GPS data format that can be read by most of the specialized software. You need to be careful with
selecting the CRS when you save your data:
Right click systematic_plots_clip and select Save as.
In Format select GPS eXchange Format [GPX].
14.5. Lesson: Systematic Sampling Design
343
The inventory sample plots are now in a standard format that can be managed by most of the GPS software. The
field teams can now upload the locations of the sample plots to their devices. That would be done by using the
specific devices own software and the plots_wgs84.gpx file you just saved. Other option would be to use the
GPS Tools plugin but it would most likely involve setting the tool to work with your specific GPS device. If you
are working with your own data and want to see how the tool works you can find out information about it in the
section Working with GPS Data in the QGIS User Manual.
Save your QGIS project now.
344
14.5.4 In Conclusion
You just saw how easily you can create a systematic sampling design to be used in a forest inventory. Creating
other types of sampling designs will involve the use of different tools within QGIS, spreadsheets or scripting to
calculate the coordinates of the sample plots, but the general idea remains the same.
14.6.1
Before we can automate the detailed maps of the forest area and our sampling plots, we need to create a map
template with all the elements we consider useful for the field work. Of course the most important will be a
properly styled but, as you have seen before, you will also need to add lots of other elements that complete the
printed map.
Open the QGIS project from the previous lesson forest_inventory.qgs. You should have at least the
following layers:
forest_stands_2012 (with a 50% transparency, green fill and darker green border lines).
systematic_plots_clip.
rautjarvi_aerial.
Save the project with a new name, map_creation.qgs.
To create a printable map, remember that you use the Composer Manager:
Open Project Composer Manager....
In the Composer manager dialog.
Click the Add button and name your composer forest_map.
Click OK.
Click the Show button.
Set up the printer options so that your maps will suit your paper and margins, for an A4 paper:
Open menuselection:Composer > Page Setup.
Size is A4 (217 x 297 mm).
Orientation is Landscape.
Margins (milimeters) are all set to 5.
345
In the Print Composer window, go to the Composition tab (on the right panel) and make sure that these settings
for Paper and quality are the same you defined for the printer:
Size: A4 (210x297mm).
Orientation: Landscape.
Quality: 300dpi.
Composing a map is easier if you make use of the canvas grid to position the different elements. Review the
settings for the composer grid:
In the Composition tab expand the Grid region.
Check that Spacing is set to 10 mm.
And that Tolerance is set to 2 mm.
You need to activate the use of the grid:
Open the View menu.
Check Show grid.
Check Snap to grid.
Notice that options for using guides are checked by default, which allows you to see red guiding lines when
you are moving elements in the composer.
Now you can start to add elements to your map canvas. Add first a map element so you can review how it looks
as you will be making changes in the layers symbology:
Click on the Add New Map button:
Click and drag a box on the canvas so that the map occupies most of it.
Notice how the mouse cursor snaps to the canvas grid. Use this function when you add other elements. If you
want to have more accuracy, change the grid Spacing setting. If for some reason you dont want to snap to the grid
at some point, you can always check or uncheck it in the View menu.
346
14.6.2
Leave the composer open but go back to the map. Lets add some background data and create some styling so that
the map content is as clear as possible.
Add the background raster basic_map.tif that you can find in the exercise_data\forestry\
folder.
When prompted select the ETRS89 / ETRS-TM35FIN CRS for the raster.
As you can see the background map is already styled. This type of ready to use cartography raster is very common.
It is created from vector data, styled in a standard format and stored as a raster so that you dont have to bother
styling several vector layers and worrying about getting a good result.
Now zoom to your sample plots, so that you can see only about four or five lines of plots.
The current styling of the sample plots is not the best, but how does it look in the map composer?:
While during the last exercises, the white buffer was OK on top of the aerial image, now that the background
image is mostly white you barely can see the labels. You can also check how it looks like on the composer:
Go to the Print Composer window.
Use the
14.6.3
347
You will use later the the green styling of the forest_stands_2012 layer. In order to keep it, and have a
visualization of it that shows only the stand borders:
Right click on forest_stands_2012 and select Duplicate
you get a new layer named forest_stands_2012 copy that you can use to define a different style,
for example with no filling and red borders.
Now you have two different visualizations of the forest stands and you can decide which one to display for your
detail map.
Go back to the Print composer window often to see what the map would look like. For the purposes of creating
detailed maps, you are looking for a symbology that looks good not at the scale of the whole forest area (left image
below) but at a closer scale (right image below). Remember to use Update preview and Set to map canvas extent
whenever you change the zoom in your map or the composer.
348
14.6.4
Once you have a symbology your happy with, you are ready to add some more information to your printed map.
Add at least the following elements:
Title.
A scale bar.
Grid frame for your map.
Coordinates on the sides of the grid.
You have created a similar composition already in Module: Creating Maps. Go back to that module as you need.
You can look at this example image for reference:
14.6.5
As you probably noticed in the suggested map template images, there are plenty of room on the right side of the
canvas. Lets see what else could go in there. For the purposes of our map, a legend is not really necessary, but an
overview map and some text boxes could add value to the map.
The overview map will help the field teams place the detail map inside the general forest area:
Add another map element to the canvas, right under the title text.
In the Item properties tab, open the Overview dropdown.
14.6. Lesson: Creating Detailed Maps with the Atlas Tool
349
Set the Overview frame to Map 0. This creates a shadowed rectangle over the smaller map representing the
extent visible in the bigger map.
Check also the Frame option with a black color and a Thickness of 0.30.
Notice that your overview map is not really giving an overview of the forest area which is what you want. You
want this map to represent the whole forest area and you want it to show only the background map and the
forest_stands_2012 layer, and not display the sample plots. And also you want to lock its view so it does
not change anymore whenever you change the visibility or order of the layers.
Go back to the map, but dont close the Print composer.
Right click the forest_stands_2012 layer and click on Zoom to Layer Extent.
Deactivate all layers except for basic_map and forest_stands_2012.
Go back to the Print composer.
With the small map selected, click the Set to map canvas extent to set its extents to what you can see in the
map window.
Lock the view for the overview map by checking Lock layers for map item under Main properties.
Now your overview map is more what you expected and its view will not change anymore. But, of course, now
your detail map is not showing anymore the stand borders nor the sample plots. Lets fix that:
Go to the map window again and select the layers you want to be visible (systematic_plots_clip,
forest_stands_2012 copy and Basic_map).
Zoom again to have only a few lines of sample plots visible.
Go back to the Print composer window.
Select the bigger map in your composer (
).
In Item properties click on Update preview and Set to map canvas extent.
350
Notice that only the bigger map is displaying the current map view, and the small overview map is keeping the
same view you had when you locked it.
Note also that the overview is showing a shaded frame for the extent shown in the detail map.
Your template map is almost ready. Add now two text boxes below the map, one containing the text Detailed map
zone: and the other one Remarks: . Place them as you can see in the image above.
You can also add a North arrow to the overview map:
Use the Add image tool,
14.6.6
The Atlas coverage is just a vector layer that will be used to generate the detail maps, one map for every feature
in the coverage. To get an idea of what you will do in the next , here is a full set of detail maps for the forest area:
351
The coverage could be any existing layer, but usually it makes more sense to create one for the specific purpose.
Lets create a grid of polygons covering the forest area:
In the QGIS map view, open Vector Research Tools Vector grid.
Set the tool as shown in this image:
352
353
14.6.7
354
That tells the Atlas tool to use the features (polygons) inside atlas_coverage as the focus for every detail
map. It will output one map for every feature in the layer. The Hidden coverage layer tells the Atlas to not show
the polygons in the output maps.
One more thing needs to be done. You need to tell the Atlas tool what map element is going to be updated for
every output map. By now, you probably can guess that the map to be changed for every feature is the one you
have prepared to contain detail views of the sample plots, that is the bigger map element in your canvas:
Select the bigger map element.
Go to the Item properties tab.
In the list, check Controlled by atlas.
And set the Marging around feature to 10%. The view extent will be 10% bigger than the polygons, which
means that your detail maps will have a 10% overlap.
355
Now you can use the preview tool for Atlas maps to review what your maps will look like:
Activate the Atlas previews using the button
Atlas.
You can use the arrows in the Atlas tool bar or in the Atlas menu to move through maps that will be created.
Note that some of them cover areas that are not interesting. Lets do something about it and save some trees by not
printing those useless maps.
14.6.8
Besides removing the polygons for those areas that are not interesting, you can also customize the text labels in
your map to be generated with content from the Attribute table of your coverage layer:
Go back to the map view.
Enable editing for the atlas_coverage layer.
356
Select the polygons that are selected (in yellow) in the image below.
Remove the selected polygons.
Disable editing and save the edits.
You can go back to the Print Composer and check that the previews of the Atlas use only the polygons you left in
the layer.
The coverage layer you are using does not yet have useful information that you could use to customize the content
of the labels in your map. The first step is to create them, you can add for example a zone code for the polygon
areas and a field with some remarks for the field teams to have into account:
Open the Attribute table for the atlas_coverage layer.
Enable editing.
Use the
357
In the Expression box write No remarks.. This will set all the default value for all the polygons.
The forest manager will have some information about the area that might be useful when visiting the area. For
example, the existence of a bridge, a swamp or the location of a protected species. The atlas_coverage layer
is probably in edit mode still, add the following text in the Remarks field to the corresponding polygons (double
click the cell to edit it):
For the Zone 2: Bridge to the North of plot 19.
p_13 and p_14..
For the Zone 6: Difficult to transit in swamp to the North of the lake..
For the Zone 7: Siberian squirrel to the South East of p_94..
Disable editing and save your edits.
Almost ready, now you have to tell the Atlas tool that you want some of the text labels to use the information from
the atlas_coverage layers attribute table.
Go back to the Print Composer.
Select the text label containing Detailed map....
Set the Font size to 12.
Set the cursor at the end of the text in the label.
In the Item properties tab, inside the Main properties click on Insert an expression.
In the Function list double click on the field Zone under Field and Values.
Click OK.
The text inside the box in the Item properties should show Detail map inventory zone: [%
"Zone" %]. Note that the [% "Zone" %] will be substituted by the value of the field Zone for the
corresponding feature from the atlas_coverage.
Test the contents of the label by looking at the different Atlas preview maps.
Do the same for the labels with the text Remarks: using the field whit the zone information. You can leave a
break line before you enter the expression. You can see the result for the preview of zone 2 in the image below:
358
Use the Atlas preview to browse through all the maps you will be creating soon and enjoy!
14.6.9
Last but not least, printing or exporting your maps to image files or PDF files. You can use the Atlas Export
Atlas as Images... or Atlas Export Atlas as PDF.... Currently the SVG export format is not working properly
and will give a poor result.
Lets print the maps as a single PDF that you can send to the field office for printing:
Go to the Atlas generation tab on the right panel.
Under the Output check the Single file export when possible. This will put all the maps together into a PDF
file, if this option is not checked you will get one file for every map.
Open Composer Export as PDF....
Save
the
PDF
file
as
inventory_2012_maps.pdf
exercise_data\forestry\samplig\map_creation\ folder.
in
your
359
In the Print Composer, save your map as a composer template as forestry_atlas.qpt in your
exercise_data\forestry\map_creation\ folder. Use Composer Save as Template. You will be
able to use this template again and again.
Close the Print Composer and save your QGIS project.
14.6.10 In Conclusion
You have managed to create a template map that can be used to automatically generate detail maps to be used in
the field to help navigate to the different plots. As you noticed, this was not an easy task but the benefit will come
when you need to create similar maps for other regions and you can use the template you just saved.
14.7.1
The field teams visited the forest and with the help of the information you provided, gathered information about
the forest at every sample plot.
Most often the information will be collected into paper forms in the field, then typed to a spreadsheet. The sample
plots information has been condensed into a .csv file that can be easily open in QGIS.
Continue with the QGIS project from the lesson about designing the inventory, you probably named it
forest_inventory.qgs.
First, add the sample plots measurements to your QGIS project:
Go to Layer Add Delimited Text Layer....
360
Browse
to
the
file
systematic_inventory_results.csv
exercise_data\forestry\results\.
located
in
14.7.2
You can calculate the averages for this whole forest area from the inventory results for the some interesting
parameters, like the volume and the number of stems per hectare. Since the systematic sample plots represent
equal areas, you can directly calculate the averages of the volumes and number of stems per hectare from the
sample_plots_results layer.
You can calculate the average of a field in a vector layer using the Basic statistics tool:
Open Vector Analysis Tools Basic statistics.
Select the sample_plots_results as the Input Vector Layer.
Select Vol as Target field.
Click OK.
The average volume in the forest is 135.2 m3/ha.
You can calculate the average for the number of stems in the same way, 2745 stems/ha.
361
14.7.3
You can make use of those same systematic sample plots to calculate estimates for the different forest stands
you digitized previously. Some of the forest stands did not get any sample plot and for those you will not get
information. You could have planned some extra sample plots when you planned the systematic inventory, so
that the field teams would have measured a few extra sample plots for this purpose. Or you could send a field
team later to get estimates of the missing forest stands to complete the stand inventory. Nevertheless, you will get
information for a good number of stands just using the planned plots.
What you need is to get the averages of the sample plots that are falling within each of the forest stands. When
you want to combine information based on their relative locations, you perform a spatial join:
Open the Vector Data Management Join attributes by location tool.
Set forest_stands_2012 as the Target vector layer. The layer you want the results for.
Set sample_plots_results as the Join vector layer. The layer you want to calculate estimates from.
Check Take summary of intersecting features.
Check to calculate only the Mean.
Name the result as forest_stands_2012_results.shp
exercise_data\forestry\results\ folder.
and
save
it
in
the
Finally select Keep all records..., so you can check later what stands did not get information.
Click OK.
Accept adding the new layer to your project when prompted.
Close the Join attributes by location tool.
Open the Attribute table for forest_stands_2012_results and review the results you got. Note that a
number of forest stands have NULL as the value for the calculations, those are the ones having no sample plots.
Select them all review them in the map, they are some of the smaller stands:
362
Lets calculate now the same averages for the whole forest as you did before, only this time you will use the
averages you got for the stands as the bases for the calculation. Remember that in the previous situation, each
sample plot represented a theoretical stand of 80x80 m. Now you have to consider the area of each of the stands
individually instead. That way, again, the average values of the parameters that are in, for example, m3/ha for the
volumes are converted to total volumes for the stands.
You need to first calculate the areas for the stands and then calculate total volumes and stem numbers for each of
them:
In the Atributes table enable editing.
Open the Field calculator.
Create a new field called area.
363
364
As you can see, the total sum of the stands areas is 66.04 ha. Note that the area of the missing forest stands is
only about 7 ha.
In the same way, you can calculate that the total volume for these stands is 8908 m3/ha and the total number of
stems is 179594 stems.
Using the information from the forest stands, instead of directly using that from the sample plots, gives the following average estimates:
184.9 m3/ha and
2719 stems/ha.
Save your QGIS project, forest_inventory.qgs.
14.7.4 In Conclusion
You managed to calculate forest estimates for the whole forest using the information from your systematic sample
plots, first without considering the forest characteristics and also using the interpretation of the aerial image into
forest stands. And you also got some valuable information about the particular stands, which could be used to
plan the management of the forest in the coming years.
365
14.8.1
Managing LiDAR data within QGIS is possible using the Processing framework and the algorithms provided by
LAStools.
You can obtain a digital elevation model (DEM) from a LiDAR point cloud and then create a hillshade raster that is
visually more intuitive for presentation purposes. First you will have to set up the :guilabel:Processingframework
settings to properly work with LAStools:
Close QGIS, if you have already started it.
An old lidar plugin might be installed by default in your system in the folder C:/Program Files/QGIS
Valmiera/apps/qgis/python/plugins/processing/.
If you have a folder named lidar, delete it. This is valid for some installations of QGIS 2.2 and 2.4.
366
367
14.8.2
You have already used the Processing toolbox in Lesson: Spatial Statistics to run some SAGA algorithms. Now
you are going to use it to run LAStools programs:
Open Processing Toolbox.
In the dropdown menu at the bottom, select Advanced interface.
You should see the Tools for LiDAR data category.
368
Expand it to see the tools available, and expand also the LAStools category (the number of algorithms may
vary).
Scroll down until you find the lasview algorithm, double click it to open.
At Input LAS/LAZ file, browse to exercise_data\forestry\lidar\ and select the
rautjarvi_lidar.laz file.
369
Click Run.
Now you can see the LiDAR data in the just a little LAS and LAZ viewer dialog window:
There are many things you can do within this viewer, but for now you can just click and drag on the viewer to pan
the LiDAR point cloud to see what it looks like.
Note:
370
If you want to know further details on how the LAStools work, you can read the README text files
about each of the tools, in the C:\lastools\bin\ folder. Tutorials and other materials are available at the
Rapidlasso webpage.
Close the viewer when you are ready.
Creating a DEM with LAStools can be done in two steps, first one to classify the point cloud into ground and
no ground points and then calculating a DEM using only the ground points.
Go back to the Processing Toolbox.
Note the Search... box, write lasground.
Double click to open the lasground tool and set it as shown in this image:
The output file is saved to the same folder where the rautjarvi_lidar.laz is located and it is named
rautjarvi_lidar_1.las.
You can open it with lasview if you want to check it.
371
The brown points are the points classified as ground and the gray ones are the rest, you can click the letter g to
visualize only the ground points or the letter u to see only the unclassified points. Click the letter a to see all
the points again. Check the lasview_README.txt file for more commands. If you are interested, also this
tutorial about editing LiDAR points manually will show you different operations within the viewer.
Close the viewer again.
In the Processing Toolbox, search for las2dem.
Open the las2dem tool and set it as shown in this image:
372
The result DEM is added to your map with the generic name Output raster file.
Note: The lasground and las2dem tools require licensing. You can use the unlicensed tool as indicated in the
license file, but you get the diagonals you can appreciate in the image results.
14.8.3
For visualization purposes, a hillshade generated from a DEM gives a better visualization of the terrain:
Open Raster Terrain analysis Hillshade.
As the Output layer, browse to exercise_data\forestry\lidar\ and name the file
hillshade.tif.
Leave the rest of parameters with the default settings.
373
374
14.8.4 In Conclusion
Using LiDAR data to get a DEM, specially in forested areas, gives good results with not much effort. You could
also use ready LiDAR derived DEMs or other sources like the SRTM 9m resolution DEMs. Either way, you can
use them to create a hillshade raster to use in your map presentations.
375
The goal for this lesson: Create a map to present the inventory results using a hillshade raster as background.
14.9.1
Open the QGIS project from the parameters calculations lesson, forest_inventory.qgs. Keep at least the
following layers:
forest_stands_2012_results.
basic_map.
rautjarvi_aerial.
lakes (if you dont have it, add it from the exercise_data\forestry\ folder).
You are going to present the average volumes of your forest stands in a map. If you open the Attribute table for
the forest_stands_2012_results layer, you can see the NULL values for the stands without information.
To be able to get also those stands into your styling you should change the NULL values to, for example, -999,
knowing that those negative numbers mean there is no data for those polygons.
For the forest_stands_2012_results layer:
Open the Attribute table and enable editing.
Select the polygons with NULL values.
Use the calculator to update the values of the MEANVol field to -999 only for the selected features.
Disable editing and save the changes.
Now you can use a saved style for this layer:
Go to the Style tab.
Click on Load Style.
Select the forest_stands_2012_results.qml from the exercise_data\forestry\results\
folder.
Click OK.
376
377
14.9.2
378
is using the Hard light mode for the Layer blending mode. Note that the different modes apply different filters
combining the underlying and overlying layers, in this case the hillshade raster and your forest stands are used.
You can read about these modes in the User Guide.
Try with different modes and see the differences in your map. Then choose the one you like better for your final
map.
14.9.3
Use a template prepared in advanced to present the results. The template forest_map.qpt is located in the
exercise_data\forestry\results\ folder. Load it using the Project Composer Manager... dialog.
379
Open the map composer and edit the final map to get a result you are happy with.
The map template you are using will give a map similar to this one:
380
381
14.9.4 In Conclusion
Through this module you have seen how a basic forest inventory can be planned and presented with QGIS. Many
more forest analysis are possible with the variety of tools that you can access, but hopefully this manual has given
you a good starting point to explore how you could achieve the specific results you need.
382
CAPITOLUL 15
Relational Databases are an important part of any GIS system. In this module, youll learn about Relational
Database Management System (RDBMS) concepts and you will use PostgreSQL to create a new database to store
data, as well as learning about other typical RDBMS functions.
15.1.2 Tables
In relational databases and flat file databases, a table is a set of data elements (values) that is organized using a
model of vertical columns (which are identified by their name) and horizontal rows. A table has a specified number
of columns, but can have any number of rows. Each row is identified by the values appearing in a particular column
subset which has been identified as a candidate key. - Wikipedia
id | name | age
----+-------+----1 | Tim
| 20
2 | Horst | 88
(2 rows)
383
| name |
+-------+
| Tim
|
| Horst |
A field:
| Horst |
15.1.4 Records
A record is the information stored in a table row. Each record will have a field for each of the columns in the table.
2 | Horst |
88
15.1.5 Datatypes
Datatypes restrict the kind of information that can be stored in a column. - Tim and Horst
There are many kinds of datatypes. Lets focus on the most common:
String - to store free-form text data
Integer - to store whole numbers
Real - to store decimal numbers
Date - to store Horsts birthday so no one forgets
Boolean - to store simple true/false values
You can tell the database to allow you to also store nothing in a field. If there is nothing in a field, then the field
content is referred to as a null value:
insert into person (age) values (40);
select * from person;
Result:
id | name | age
----+-------+----1 | Tim
| 20
2 | Horst | 88
4 |
| 40 <-- null for name
(3 rows)
There are many more datatypes you can use - check the PostgreSQL manual!
Try Yourself
Write down the properties which make up a simple address and which we would want to store in our database.
Check your results
384
Address Structure
The properties that describe an address are the columns. The type of information stored in each column is its
datatype. In the next section we will analyse our conceptual address table to see how we can make it better!
15.1.8 Normalisation
One of the main ideas in a database is to avoid data duplication / redundancy. The process of removing redundancy
from a database is called Normalisation.
Normalization is a systematic way of ensuring that a database structure is suitable for general-purpose querying
and free of certain undesirable characteristics - insertion, update, and deletion anomalies - that could lead to a loss
of data integrity. - Wikipedia
There are different kinds of normalisation forms.
Lets take a look at a simple example:
Table "public.people"
Column |
Type
|
Modifiers
----------+------------------------+----------------------------------------id
| integer
| not null default
|
| nextval(people_id_seq::regclass)
|
|
name
| character varying(50) |
address | character varying(200) | not null
phone_no | character varying
|
Indexes:
"people_pkey" PRIMARY KEY, btree (id)
select * from people;
id |
name
|
address
| phone_no
--+---------------+-----------------------------+------------1 | Tim Sutton
| 3 Buirski Plein, Swellendam | 071 123 123
2 | Horst Duester | 4 Avenue du Roix, Geneva
| 072 121 122
(2 rows)
Imagine you have many friends with the same street name or city. Every time this data is duplicated, it consumes
space. Worse still, if a city name changes, you have to do a lot of work to update your database.
385
15.1.10 Indexes
A database index is a data structure that improves the speed of data retrieval operations on a database table. Wikipedia
Imagine you are reading a textbook and looking for the explanation of a concept - and the textbook has no index!
You will have to start reading at one cover and work your way through the entire book until you find the information
you need. The index at the back of a book helps you to jump quickly to the page with the relevant information:
create index person_name_idx on people (name);
15.1.11 Sequences
A sequence is a unique number generator. It is normally used to create a unique identifier for a column in a table.
In this example, id is a sequence - the number is incremented each time a record is added to the table:
id |
name
|
address
| phone_no
---+--------------+-----------------------------+------------1 | Tim Sutton
| 3 Buirski Plein, Swellendam | 071 123 123
2 | Horst Duster | 4 Avenue du Roix, Geneva
| 072 121 122
With a little work we can split it into two tables, removing the need to repeat the street name for individuals who
live in the same street:
select * from streets;
id |
name
----+-------------1 | Plein Street
(1 row)
386
and:
select * from people;
id |
name
| house_no | street_id | phone_no
----+--------------+----------+-----------+------------1 | Horst Duster |
4 |
1 | 072 121 122
(1 row)
We can then link the two tables using the keys streets.id and people.streets_id.
If we draw an ER Diagram for these two tables it would look something like this:
The ER Diagram helps us to express one to many relationships. In this case the arrow symbol show that one
street can have many people living on it.
Try Yourself
Our people model still has some normalisation issues - try to see if you can normalise it further and show your
thoughts by means of an ER Diagram.
Check your results
387
15.1.14 Transactions
When adding, changing, or deleting data in a database, it is always important that the database is left in a good
state if something goes wrong. Most databases provide a feature called transaction support. Transactions allow
you to create a rollback position that you can return to if your modifications to the database did not run as planned.
Take a scenario where you have an accounting system. You need to transfer funds from one account and add them
to another. The sequence of steps would go like this:
remove R20 from Joe
add R20 to Anne
If something goes wrong during the process (e.g. power failure), the transaction will be rolled back.
15.1.15 In Conclusion
Databases allow you to manage data in a structured way using simple code structures.
388
Press Y and Enter and wait for the download and installation to finish.
15.2.2 Help
PostgreSQL has very good online documentation.
Type in your normal log in password when prompted (you need to have sudo rights).
Now, at the postgres users bash prompt, create the database user. Make sure the user name matches your unix
login name: it will make your life much easier, as postgres will automatically authenticate you when you are
logged in as that user:
createuser -d -E -i -l -P -r -s qgis
Enter a password when prompted. You should use a different password to your login password.
What do those options mean?
-d,
-E,
-i,
-l,
-P,
-r,
-s,
--createdb
--encrypted
--inherit
--login
--pwprompt
--createrole
--superuser
Now you should should leave the postgres users bash shell environment by typing:
exit
Type q to exit.
389
You can verify the existence of your new database by using this command:
psql -l
Type q to exit.
serial and varchar are data types. serial tells PostgreSQL to start an integer sequence (auto-number) to
populate the id automatically for every new record. varchar(50) tells PostgreSQL to create a character field
of 50 characters in length.
You will notice that the command ends with a ; - all SQL commands should be terminated this way. When you
press enter, psql will report something like this:
NOTICE:
390
That means your table was created successfully, with a primary key streets_pkey using streets.id.
Note: If you hit return without entering a ;, then you will get a prompt like this: address-#. This is because
PG is expecting you to enter more. Enter ; to run your command.
To view your table schema, you can do this:
\d streets
Try Yourself
Use the approach shown above to make a table called people:
Add fields such as phone number, home address, name, etc. (these arent all valid names: change them to make
them valid). Make sure you give the table an ID column with the same data-types as above.
Check your results
391
That tells the people table that its street_id fields must match a valid street id from the streets table.
The more usual way to create a constraint is to do it when you create the table:
create table people (id serial not null primary key,
name varchar(50),
house_no int not null,
street_id int references streets(id) not null,
phone_no varchar null);
\d people
After adding the constraint, our table schema looks like this now:
Table "public.people"
Column
|
Type
|
Modifiers
-----------+-----------------------+--------------------------------id
| integer
| not null default
|
| nextval(people_id_seq::regclass)
name
| character varying(50) |
house_no | integer
| not null
street_id | integer
| not null
phone_no | character varying
|
Indexes:
"people_pkey" PRIMARY KEY, btree (id)
Foreign-key constraints:
"people_streets_fk" FOREIGN KEY (id) REFERENCES streets(id)
392
Note: In our current example, the above command would not work. Why not? See why
If you used the same drop table command on the people table, it would be successful:
drop table people;
Note: If you actually did enter that command and dropped the people table, now would be a good time to
rebuild it, as you will need it in the next exercises.
15.2.12 In Conclusion
You have now seen how to create a brand new database, starting completely from scratch.
393
Result:
select * from streets;
id |
name
----+------------1 | High street
(1 row)
Try Yourself
Use the INSERT command to add a new street to the streets table.
Check your results
Note: Recall that in this example, we defined phone numbers as strings, not integers.
At this point, you should have an error report if you try to do this without first creating a record for Main Street in
the streets table.
You should have also noticed that:
You cant add the street using its name
You cant add a street using a street id before first creating the street record on the streets table
Remember that our two tables are linked via a Primary/Foreign Key pair. This means that no valid person can be
created without there also being a valid corresponding street record.
Using the above knowledge, add the new person to the database.
Check your results
In later sessions we will go into more detail on how to select and filter data.
394
Be very careful using such update statements - if more than one record matches your WHERE clause, they will all
be updated!
A better solution is to use the primary key of the table to reference the record to be changed:
update streets set name=New Main Road where id=2;
15.3.8 In Conclusion
Now you know how to add new data to the existing models you created previously. Remember that if you want to
add new kinds of data, you may want to modify and/or create new models to contain that data.
395
The goal of this lesson: To learn how to create queries that will return useful information.
Note: If you did not do so in the previous lesson, add the following people objects to your people table. If you
receive any errors related to foreign key constraints, you will need to add the Main Road object to your streets
table first
insert into people (name,house_no, street_id, phone_no)
values (Joe Bloggs,3,2,072 887 23 45);
insert into people (name,house_no, street_id, phone_no)
values (Jane Smith,55,3,072 837 33 35);
insert into people (name,house_no, street_id, phone_no)
values (Roger Jones,33,1,072 832 31 38);
insert into people (name,house_no, street_id, phone_no)
values (Sally Norman,83,1,072 932 31 32);
Result:
name
| house_no
--------------+---------Joe Bloggs
|
3
Roger Jones |
33
Jane Smith
|
55
Sally Norman |
83
(4 rows)
You can sort the results by the values of more than one column:
select name, house_no from people order by name, house_no;
Result:
name
| house_no
--------------+---------Jane Smith
|
55
Joe Bloggs
|
3
Roger Jones |
33
Sally Norman |
83
(4 rows)
15.4.2 Filtering
Often you wont want to see every single record in the database - especially if there are thousands of records and
you are only interested in seeing one or two.
Here is an example of a numerical filter which only returns objects whose house_no is less than 50:
select name, house_no from people where house_no < 50;
name
| house_no
-------------+---------Joe Bloggs |
3
Roger Jones |
33
(2 rows)
You can combine filters (defined using the WHERE clause) with sorting (defined using the ORDER BY):
396
select name, house_no from people where house_no < 50 order by house_no;
name
| house_no
-------------+---------Joe Bloggs |
3
Roger Jones |
33
(2 rows)
Here we used the LIKE clause to find all names with an s in them. Youll notice that this query is case-sensitive,
so the Sally Norman entry has not been returned.
If you want to search for a string of letters regardless of case, you can do a case in-sensitive search using the
ILIKE clause:
select name, house_no from people where name ilike %r%;
name
| house_no
--------------+---------Roger Jones |
33
Sally Norman |
83
(2 rows)
15.4.3 Joins
What if you want to see the persons details and their streets name instead of the ID? In order to do that, you need
to join the two tables together in a single query. Lets look at an example:
select people.name, house_no, streets.name
from people,streets
where people.street_id=streets.id;
Note: With joins, you will always state the two tables the information is coming from, in this case people and
streets. You also need to specify which two keys must match (foreign key & primary key). If you dont specify
that, you will get a list of all possible combinations of people and streets, but no way to know who actually lives
on which street!
Here is what the correct output will look like:
name
| house_no |
name
--------------+----------+------------Joe Bloggs
|
3 | Low Street
Roger Jones |
33 | High street
Sally Norman |
83 | High street
Jane Smith
|
55 | Main Road
(4 rows)
We will revisit joins as we create more complex queries later. Just remember they provide a simple way to combine
the information from two or more tables.
397
15.4.4 Sub-Select
Sub-selections allow you to select objects from one table based on the data from another table which is linked via
a foreign key relationship. In our case, we want to find people who live on a specific street.
First, lets do a little tweaking of our data:
insert
insert
insert
update
update
Lets take a quick look at our data after those changes: we can reuse our query from the previous section:
select people.name, house_no, streets.name
from people,streets
where people.street_id=streets.id;
Result:
name
| house_no |
name
--------------+----------+------------Roger Jones |
33 | High street
Sally Norman |
83 | High street
Jane Smith
|
55 | Main Road
Joe Bloggs
|
3 | Low Street
(4 rows)
Now lets show you a sub-selection on this data. We want to show only people who live in street_id number
1:
select people.name
from people, (
select *
from streets
where id=1
) as streets_subset
where people.street_id = streets_subset.id;
Result:
name
-------------Roger Jones
Sally Norman
(2 rows)
Although this is a very simple example and unnecessary with our small data-sets, it illustrates how useful and
important sub-selections can be when querying large and complex data-sets.
Result:
count
-------
398
4
(1 row)
Result:
count | street_id
-------+----------2 |
1
1 |
3
1 |
2
(3 rows)
Note: Because we have not used an ORDER BY clause, the order of your results may not match what is shown
here.
Try Yourself
Summarise the people by street name and show the actual street names instead of the street_ids.
Check your results
15.4.6 In Conclusion
Youve seen how to use queries to return the data in your database in a way that allows you to extract useful
information from it.
399
As you can see the only change is the create view roads_count_v as part at the beginning. We can
now select data from that view:
select * from roads_count_v;
Result:
count |
name
-------+------------1 | Main Road
2 | High street
1 | Low Street
(3 rows)
(This example also shows the best practice convention of using UPPER CASE for all SQL keywords.)
You will see that we have added an ORDER BY clause so that our view rows are nicely sorted:
select * from roads_count_v;
count |
name
-------+------------2 | High street
1 | Low Street
1 | Main Road
(3 rows)
15.5.4 In Conclusion
Using views, you can save a query and access its results as if it were a table.
The goal for this lesson: To learn how to create new rules for the database.
In the next step, create a rule that logs every change of a phone_no in the people table into the people_log table:
create rule people_log as on update to people
where NEW.phone_no <> OLD.phone_no
do insert into people_log values (OLD.name);
Now, thanks to the rule we created, the people_log table will look like this:
select * from people_log;
name
|
time
------------+---------------------------Joe Bloggs | 2014-01-11 14:15:11.953141
(1 row)
Note: The value of the time field will depend on the current date and time.
15.6.2 In Conclusion
Rules allow you to automatically add or change data in your database to reflect changes in other parts of the
database.
401
402
CAPITOLUL 16
Spatial Databases allow the storage of the geometries of records inside a Database as well as providing functionality for querying and retrieving the records using these Geometries. In this module we will use PostGIS, an
extension to PostgreSQL, to learn how to setup a spatial database, import data from shapefiles into the database
and make use of the geographic functions that PostGIS offers.
While working through this section, you may want to keep a copy of the PostGIS cheat sheet available from
Boston GIS user group. Another useful resource is the online PostGIS documentation.
There are also some more extensive tutorials on PostGIS and Spatial Databases available from Boundless Geo:
Introduction to PostGIS
Spatial Database Tips and Tricks
See also PostGIS online.
403
Note: If you are using PostGIS 1.5 and a version of PostgreSQL lower than 9.1, you will need to follow a different
set of steps in order to install the postgis extensions for your database. Please consult the PostGIS Documentation
for instructions on how to do this. There are also some instructions in the previous version of this manual.
This is the command were looking for: st_pointfromtext. To page through the list, use the down arrow,
then press q to quit back to the psql shell.
Try running this command:
select st_pointfromtext(POINT(1 1));
Result:
st_pointfromtext
-------------------------------------------0101000000000000000000F03F000000000000F03F
(1 row)
404
The query were looking for now is st_astext. Lets combine it with the previous query:
select st_astext(st_pointfromtext(POINT(1 1)));
Result:
st_astext
-----------POINT(1 1)
(1 row)
Here, we entered the string POINT(1,1), turned it into a point using st_pointfromtext(), and turned it
back into a human-readable form with st_astext(), which gave us back our original string.
One last example before we really get into the detail of using PostGIS:
select st_astext(st_buffer(st_pointfromtext(POINT(1 1)),1.0));
What did that do? It created a buffer of 1 degree around our point, and returned the result as text.
You can use standard SQL queries (as we have learned from our introductory sections), to view and manipulate
this table - though its not a good idea to update or delete any records unless you know what you are doing.
One SRID you may be interested in is EPSG:4326 - the geographic / lat lon reference system using the WGS 84
ellipsoid. Lets take a look at it:
405
Result:
srid
| 4326
auth_name | EPSG
auth_srid | 4326
srtext
| GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS
84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],TOWGS84[0,
0,0,0,0,0,0],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,
AUTHORITY["EPSG","8901"]],UNIT["degree",0.01745329251994328,
AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]]
proj4text | +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs
The srtext is the projection definition in well known text (you may recognise this from .prj files in your shapefile
collection).
16.1.7 In Conclusion
You now have PostGIS functions installed in your copy of PostgreSQL. With this youll be able to make use of
PostGIS extensive spatial functions.
406
The model defines geospatial data from Point, Linestring, and Polygon types (and aggregations of them to Multi
objects).
For further information, have a look at the OGC Simple Feature for SQL standard.
This adds a constraint to the table so that it will only accept a point geometry or a null value.
407
Why? geometry_columns is used by certain applications to be aware of which tables in the database contain
geometry data.
Note: If the above INSERT statement causes an error, run this query first:
select * from geometry_columns;
If the column :kbd:f_table_name contains the value :kbd:people, then
this table has already been registered and you dont need to do anything
more.
The value 2 refers to the number of dimensions; in this case, two: x and y.
The value 4326 refers to the projection we are using; in this case, WGS 84, which is referred to by the number
4326 (refer to the earlier discussion about the EPSG).
Try Yourself
Add an appropriate geometry_columns entry for your new cities layer
Check your results
Note: In the new entry above, you will need to specify which projection (SRID) you want to use. This is because
you entered the geometry of the new point using a plain string of text, which does not automatically add the correct
projection information. Obviously, the new point needs to use the same SRID as the data-set it is being added to,
so you need to specify it.
If at this point you were using a graphical interface, for example, specifying the projection for each point would
be automatic. In other words, you usually wont need to worry about using the correct projection for every point
you want to add if youve already specified it for that data-set, as we did earlier.
Now is probably a good time to open QGIS and try to view your people table. Also, we should try editing /
adding / deleting records and then performing select queries in the database to see how the data has changed.
To load a PostGIS layer in QGIS, use the Layer Add PostGIS Layers menu option or toolbar button:
408
409
To see whether QGIS has found the address database and that your username and password are correct, click
Test Connect. If it works, check the boxes next to Save Username and Save Password. Then click OK to create
this connection.
410
Back in the Add PostGIS Layers dialog, click Connect and add layers to your project as usual.
Try Yourself
Formulate a query that shows a persons name, street name and position (from the the_geom column) as plain text.
Check your results
16.2.8 In Conclusion
You have seen how to add spatial objects to your database and view them in GIS software.
16.3.1 shp2pgsql
shp2pgsql is a commandline tool to import ESRI shapefiles to the database. Under Unix, you can use the following
command for importing a new PostGIS table:
shp2pgsql -s <SRID> -c -D -I <path to shapefile> <schema>.<table> | \
psql -d <databasename> -h <hostname> -U <username>
Under Windows, you have to perform the import process in two steps:
shp2pgsql -s <SRID> -c -D -I <path to shapefile> <schema>.<table> > import.sql
psql psql -d <databasename> -h <hostname> -U <username> -f import.sql
This is a known issue regarding the creation in situ of a spatial index for the data youre importing. To avoid the
error, exclude the -I parameter. This will mean that no spatial index is being created directly, and youll need to
create it in the database after the data have been imported. (The creation of a spatial index will be covered in the
next lesson.)
16.3.2 pgsql2shp
pgsql2shp is a commandline tool to export PostGIS Tables, Views or SQL select queries. To do this under Unix:
pgsql2shp -f <path to new shapefile> -g <geometry column name> \
-h <hostname> -U <username> <databasename> <table | view>
411
16.3.3 ogr2ogr
ogr2ogr is a very powerful tool to convert data into and from postgis to many data formats. ogr2ogr is part of the
GDAL/OGR Software and has to be installed separately. To export a table from PostGIS to GML, you can use
this command:
ogr2ogr -f GML export.gml PG:dbname=<databasename> user=<username>
host=<hostname> <Name of PostGIS-Table>
16.3.4 SPIT
SPIT is a QGIS plugin which is delivered with QGIS. You can use SPIT for uploading ESRI shapefiles to PostGIS.
Once youve added the SPIT plugin via the Plugin Manager, look for this button:
Clicking on it or selecting Database > Spit > Import Shapefiles to PostgreSQL from the menu will give you the
SPIT dialog:
You can add shapefiles to the database by clicking the Add button, which will give you a file browser window.
412
16.3.5 DB Manager
You may have noticed another option in the Database menu labeled DB Manager. This is a new tool in QGIS
2.0 that provides a unified interface for interacting with spatial databases including PostGIS. It also allows you to
import and export from databases to other formats. Since the next module is largely devoted to using this tool, we
will only briefly mention it here.
16.3.6 In Conclusion
Importing and exporting data to and from the database can be done in many various ways. Especially when using
disparate data sources, you will probably use these functions (or others like them) on a regular basis.
Result:
id |
name
| house_no | street_id |
phone_no
|
the_geom
----+--------------+----------+-----------+---------------+----------------6 | Fault Towers |
34 |
3 | 072 812 31 28 | 01010008040C0
(1 row)
Note: the_geom value above was truncated for space on this page. If you want to see the point in human-readable
coordinates, try something similar to what you did in the section View a point as WKT, above.
How do we know that the query above returns all the points within 2 degrees? Why not 2 meters? Or any other
unit, for that matter?
Check your results
413
Result:
Table "public.people"
Column
|
Type
|
Modifiers
-----------+-----------------------+---------------------------------------id
| integer
| not null default
|
| nextval(people_id_seq::regclass)
name
| character varying(50) |
house_no | integer
| not null
street_id | integer
| not null
phone_no | character varying
|
the_geom | geometry
|
Indexes:
"people_pkey" PRIMARY KEY, btree (id)
"people_geo_idx" gist (the_geom) <-- new spatial key added
"people_name_idx" btree (name)
Check constraints:
"people_geom_point_chk" CHECK (st_geometrytype(the_geom) = ST_Point::text
OR the_geom IS NULL)
Foreign-key constraints:
"people_street_id_fkey" FOREIGN KEY (street_id) REFERENCES streets(id)
Next, import the data provided in the exercise_data/postgis/ directory. Refer back to the previous lesson
for instructions, but remember that youll need to create a new PostGIS connection to the new database. You can
import from the terminal or via SPIT. Import the files into the following database tables:
points.shp into building
lines.shp into road
polygons.shp into region
Load these three database layers into QGIS via the Add PostGIS Layers dialog, as usual. When you open their
attribute tables, youll note that they have both an id field and a gid field created by the PostGIS import.
Now that the tables are imported, we can use PostGIS to query the data. Go back to your terminal (command line)
and enter the psql prompt by running:
414
psql postgis_demo
Well demo some of these select statements by creating views from them, so that you can open them in QGIS and
see the results.
Select by location
Get all the buildings in the KwaZulu region:
SELECT a.id, a.name, st_astext(a.the_geom) as point
FROM building a, region b
WHERE st_within(a.the_geom, b.the_geom)
AND b.name = KwaZulu;
Result:
id | name |
point
----+------+-----------------------------------------30 | York | POINT(1622345.23785063 6940490.65844485)
33 | York | POINT(1622495.65620524 6940403.87862489)
35 | York | POINT(1622403.09106394 6940212.96302097)
36 | York | POINT(1622287.38463732 6940357.59605424)
40 | York | POINT(1621888.19746548 6940508.01440885)
(5 rows)
415
Select neighbors
Show a list of all the names of regions adjoining the Hokkaido region:
SELECT b.name
FROM region a, region b
WHERE st_touches(a.the_geom, b.the_geom)
AND a.name = Hokkaido;
Result:
name
-------------Missouri
Saskatchewan
Wales
(3 rows)
As a view:
CREATE VIEW vw_regions_adjoining_hokkaido AS
SELECT b.gid, b.name, b.the_geom
FROM region a, region b
WHERE TOUCHES(a.the_geom, b.the_geom)
AND a.name = Hokkaido;
In QGIS:
Note the missing region (Queensland). This may be due to a topology error. Artifacts such as this can alert us to
potential problems in the data. To solve this enigma without getting caught up in the anomalies the data may have,
we could use a buffer intersect instead:
CREATE VIEW vw_hokkaido_buffer AS
SELECT gid, ST_BUFFER(the_geom, 100) as the_geom
FROM region
WHERE name = Hokkaido;
In this query, the original buffer view is used as any other table would be. It is given the alias a, and its geometry
field, a.the_geom, is used to select any polygon in the region table (alias b) that intersects it. However,
Hokkaido itself is excluded from this select statement, because we dont want it; we only want the regions adjoining it.
In QGIS:
417
It is also possible to select all objects within a given distance, without the extra step of creating a buffer:
CREATE VIEW vw_hokkaido_distance_select AS
SELECT b.gid, b.name, b.the_geom
FROM region a, region b
WHERE ST_DISTANCE (a.the_geom, b.the_geom) < 100
AND a.name = Hokkaido
AND b.name != Hokkaido;
This achieves the same result, without need for the interim buffer step:
418
Result:
name
--------Beijing
Berlin
Atlanta
(3 rows)
419
ST_CONVEXHULL(ST_COLLECT(a.the_geom)) AS the_geom
FROM building a
GROUP BY a.name;
16.4.5 In Conclusion
You have seen how to query spatial objects using the new database functions from PostGIS.
Take a look at the results in QGIS. (You may need to right-click on the streets layer in the Layers panel, and
choose Zoom to layer extent.)
Now create some more streets entries - some in QGIS and some from the command line.
420
Note: A polygon requires double brackets around its coordinate list; this is to allow you to add complex polygons
with multiple unconnected areas. For instance
insert into cities (name, the_geom)
values (Tokyo Outer Wards, SRID=4326;POLYGON((20 10, 20 20, 35 20, 20 10),
(-10 -30, -5 0, -15 -15, -10 -30)));
If you followed this step, you can check what it did by loading the cities dataset into QGIS, opening its attribute
table, and selecting the new entry. Note how the two new polygons behave like one polygon.
421
16.5.9 Clipping
To clip a subpart of your data you can use the ST_INTERSECT() function. To avoid empty geometries, use:
where not st_isempty(st_intersection(a.the_geom, b.the_geom))
422
423
To create a linestring from a new point layer called points, you can run the following command:
select ST_LineFromMultiPoint(st_collect(the_geom)), 1 as id
from (
select the_geom
from points
order by id
) as foo;
To see how it works without creating a new layer, you could also run this command on the people layer, although
of course it would make little real-world sense to do this.
424
As the result, you will get all records from table_a which are not stored in table_b.
16.5.13 Tablespaces
You can define where postgres should store its data on disk by creating tablespaces:
CREATE TABLESPACE homespace LOCATION /home/pg;
When you create a database, you can then specify which tablespace to use e.g.:
425
16.5.14 In Conclusion
Youve learned how to create more complex geometries using PostGIS statements. Keep in mind that this is mostly
to improve your tacit knowledge when working with geo-enabled databases through a GIS frontend. You usually
wont need to actually enter these statements manually, but having a general idea of their structure will help you
when using a GIS, especially if you encounter errors that would otherwise seem cryptic.
426
CAPITOLUL 17
17.1 Introduction
This guide describes how to use QGIS processing framework. It assumes no previous knowledge of the processign
framework or any of the applications that it relies on. It assumes basic knowledge of QGIS. The chapters about
scripting assume you have some basic knowledge of Python and maybe the QGIS Python API.
This guide is designed for self-study or to be used for running a processing workshop.
Examples in this guide use QGIS 2.0. They might not work or not be available in versions other than that ones.
This guide is comprised of a set of small exercises of progressive complexity. If you have never used the processing
framework, you should start from the very beginning. If you have some previous experience, feel free to skip
lessons. They are more or less independent from each other, and each one introduces some new concept or some
new element, which is indicated in the chapter title and the short introduction at the begining of each chapter. That
should make it easy to locate lessons dealing with a particular topic.
For a more systematic description of all the framework components and their usage, it is recommended to check
the corresponding chapter in the QGIS manual. Use it as a support text along with this guide.
All the exercises in this guide use free data set that can be downloaded here. The zip file to download contains
several folders corresponding to each one of the lessons in this guide. In each of them you will find a QGIS project
file. Just open it and you will be ready to start the lesson.
Enjoy!
427
428
knowing anything about grammar or syntax, and having no knowledge about storytelling. You might get a result,
but it is likely to have no value at all. Please, dont fool yourself and think that after reading this guide you are
already capable of performing spatial analysis and get sound results. You need to study spatial analysis as well.
Here is a good reference that you can read to learn more about spatial data analysis.
Geospatial Analysis (3rd Edition): A Comprehensive Guide to Principles, Techniques and Software Tools Michael
John De Smith, Michael F. Goodchild, Paul A. Longley
It is available online here
If you cannot find that menu, you have to enable the plugin by going to the plugin manager and activating it.
429
The main element that we are going to work with is the toolbox. Click on the corresponding menu entry and you
will see the toolbox docked at the right side of the QGIS window.
The toolbox contains a list of all the available algorithms, divided in groups. There are two ways of displaying
and organizing those algorithms: the advanced mode and the simplified mode.
By default, you will see the simplified mode, which groups algorithms according to the kind of operation they
perform. Although some of the algorithms that you will see in the toolbox depend on other external applications
(most of them do, in fact), you will not see any mention to those applications. The origin of algorithms is hidden
in this mode, which is a facade that simplifies using algorithms through the processing framework.
Examples in this guide only use the simplified mode. The advanced mode has some additional features and
algorithms, but it requires understanding the applications that are called, so they are a more advanced topic. Some
of these more advanced ideas are introduced in the final lessons of this book, but for the rest of them we will just
use the simplified interface.
You can change between the simplified and the advanced mode by using the selector on the bottom part of the
toolbox.
The toolbox box, when using the advanced mode, looks like this.
430
If you have reached this point, now you are ready to use geoalgorithms. There is no need to configure anything
else by now. We can already run our first algorithm, which we will do in the next lesson.
431
Now go to the text box at the top of the toolbox. That is the search box, and if you type text in it, it will filter the
list of algorithms so just those ones contaning the entered text are shown. Type centroids and you should see
something like this.
The search box is a very practical way of finding the algorithm you are looking for.
To execute an algorithm, you just have to double-click on its name in the toolbox. When you double-click on the
Centroids algorithm, you will see the following dialog.
432
All algorithms have a similar interface, which basically contains input parameters that you have to fill, and outputs
that you have to select where to store. In this case, the only inputs we have are a vector layer with polygons and
a selector to select whether we want several centroids for a single feature in case it is a multipart features, or the
algorithm should generate just one centroid for each feature.
Select the Polygons layer as input. The other field will have no effect at all, since the input layer has no multi-part
features.
The algorithm has a single output, which is the centroids layer. There are two options to define where a data output
is saved: enter a filepath or save it to a temporary filename
In case you want to set a destination and not save the result in a temporary file, the format of the output is defined by
the filename extension. To select a format, just select the corresponding file extension (or add it if you are directly
typing the filepath instead). If the extension of the filepath you entered does not match any of the supported ones,
a default extension (usually .dbf for tables, .tif for raster layers and .shp for vector ones) will be appended
to the filepath and the file format corresponding to that extension will be used to save the layer or table.
In all the exercises in this guide, we will be saving results to a temporary file, since there is no need to save them
for a later use. Feel free to save them to a permament location if you want to.
Notice that temporary files are deleted once you close QGIS. If you create a project with an output that was saved
as a temporary output, QGIS will complain when you try to open back the project later, since that output file will
not exist.
Once you have configured the algorithm dialog, press Run to run the algorithm.
You will get the following output.
433
The output has the same CRS as the input. Geoalgorithms assumes all input layers share the in the same CRS and
does not perform any reprojection. Except in the case of some special algorithms (for instance, reprojection ones),
the outputs will also have that same CRS. We will see more about this soon.
Try yourself saving it using different file formats (use, for instance, shp and geojson as extensions). Also, if
you do not want the layer to be loaded in QGIS after it is generated, you can check off the check box that is found
below the output path box.
434
The next thing we need is the polygon layer. We are going to create a regular grid of polygons using the Create
grid algorithm, which has the following parameters dialog.
435
The inputs required to create the grid are all numbers. When you have to enter a numerical value, you have two
options: typing it directly on the corresponding box or clicking the button on the righthand side to get to a dialog
like the one shown next.
436
The dialog contains a simple calculator, so you can type expressions such as 11 * 34.7 + 4.6, and the result
will be computed and put in the corresponding text box in the parameters dialog. Also, it contains constants that
you can use, and values from other layers available.
In this case, we want to create a grid that covers the extent of the inut points layer, so we should use its coordinates
to calculate the center coordinate of the grid and its width and height, since those are the parameters that the
algorithm takes to create the grid. With a little bit of math, try to do that yourself using the calculator dialog and
the constants from the input points layer.
Select Rectangles (polygons) in the Type field.
As in the case of the last algorithm, we have to enter the CRS here as well. Select EPSG:4326 as the target CRS,
as we did before.
In the end, you should have a parameters dialog like this:
437
438
The last step is to count the points in each on of the rectangles of that graticule. We will use the Count points in
polygons algorithm.
439
configuration dialog.
In the Output folder entry that you will find in the General group, type the path to your destination folder.
440
Now when you run an algorithm, just use the filename instead of the full path. For instance, with the configuration
shown above, if you enter graticule.shp as the output path for the algorithm that we have just used, the
result will be saved in D:\processing_output\graticule.shp. You can still enter a full path in case
you want a result to be saved in a different folder.
Try yourself the Create grid algorithm with different grid sizes, and also with different types of grids. We will use
the hexagonal one in a later chapter.
If there are no input layer, it will use the project CRS, unless the algorithm contains a specific CRS field (as
it happenend in the last lesson with the graticule algorithm)
Open the project corresponding to this lesson and you will see two layers named 23030 and 4326. They both
contain the same points, but in different CRSs (EPSG:23030 and EPSG:4326). They appear in the same place
441
because QGIS is reprojecting on the fly to the project CRS (EPSG:4326), but they are not actually the same layer.
Open the Export/Add geometry columns algorithm.
This algorithm add new columns to the attributes table of a vector layer. The content of the columns depend on
the type of geometry of the layer. In the case of points, it adds new columns with the X and Y coordinates of each
point.
In the list of available layers that you will find in the input layer field, you will see each one with its corresponding
CRS. That means that, although they appear in the same place in your canvas, they will be treated differently.
Select the 4326 layer.
The other parameter of the algorithm allows to set how the algorithm uses coordinates to calculate the new value
that it will add to the resulting layers. Most algorithms do not have an option like that, and just use the coordinates
directly. Select the Layer CRS option to just use coordinates as they are. This is how almost all geoalgorithms
work.
You should get a new layer with exactly the same points as the other two layers. If you right click on the name of
the layer and open its properties, you will see that it shares the same CRS of the input layer, that is, EPSG:4326.
When the layer is loaded into QGIS, you will not be asked to enter the CRS of the layer, since QGIS already
knows about it.
If you open the attributes table of the new layer you will see that it contains two new fields with the X and Y
coordinates of each point.
442
Those coordinate values are given in the layer CRS, since we chose that option. However, even if you choose
another option, the output CRS of the layer would have been the same, since the input CRS is used to set the CRS
of the output layer. Choosing another option will cause the values to be different, but not the resulting point to
change or the CRS of the ouput layer to be different to the CRS of the input one.
Now do the same calculation using the other layer. You should find the resulting layer rendered exactly in the
same place as the other ones, and it will have the EPSG:23030 CRS, since that was the one of the input layer.
If you go to its attribute table, you will see values that are different to the ones in the first layer that we created.
This is because the original data is different (it uses a different CRS), and those coordinates are taken from it.
What should you learn from this? The main idea behind these examples is that geoalgorithms use the layer as it is
in its original data source, and completely ignore the reprojections that QGIS might be doing before rendering. In
other words, do not trust what you see in the canvas, but always have in mind that the original data will be used.
That is no so important in this case, since we are just using one single layer at a time, but in an algorithm that
needs several of them (such as a clip algorithm), layers that appear to match or overlay might be very far one from
each other, since they might have different CRSs.
Algorithms performs no reprojection (except in the reprojection algorithm that we will see next), so it is up to you
to make sure that layers have matching CRSs.
An interesting module that deals with CRSs is the reprojection one. It represents a particular case, since it has an
input layer (the one to reproject), but it will not use its CRS for the the output one.
Open the Reprojection algorithm.
443
Select any of the layers as input, and select EPSG:23029 as the destination CRS. Run the algorithm and you will
get a new layer, identical to the input one, but with a different CRS. It will appear on the same region of the canvas,
like the other ones, since QGIS will reproject it on the fly, but its original coordinates are different. You can see
that by running the Add geometry columns algorithm using this new layer as input, and veryfing that the added
coordinates are different to the ones in the attribute tables of both of the two layers that we had computed before.
17.7 Selection
Note: In this lesson we will see how processing algorithms handle selections in vector layers that are used as
inputs, and how to create a selection using a particular type of algorithm.
Unlike other analysis plugins in QGIS, you will not find in processing geoalgorithms any Use only selected
features checkbox or similar. The behaviour regarding selection is set for the whole plugin and all its algorithms,
and not for each algorithm execution. Algorithms follow the following simple rules when using a vector layer.
If the layer has a selection, only selected features are used.
If there is no selection, all features are used.
You can test that yourself by selecting a few points in any of the layers that we used in the last chapter, and running
the reprojection algorithm on them. The reprojected layer that you will obtain will contain only those points that
were selected, unless there was no selection, which will cause the resulting layer to contain all points from the
origin layer.
To make a selection, you can use any of the available methods and tools in QGIS. However, you can also use a
geoalgorithm to do so. Algorithms for creating a selection are found in the toolbox under Vector/Selection
444
Leaving the default values, it will select 10 points from the current layer.
17.7. Selection
445
You will notice that this algorithm does not produce any output, but modifies the input layer (not the layer itself,
but its selection). This is an uncommon behaviour, since all the other algorithms will produce new layers and not
alter the input layers.
Since the selction is not part of the data itself, but something that only exist within GIS, these selection algorithms
only must be used selecting a layer that is open in QGIS, and not with the file selection option that you can find in
the corresponding parameter value box.
The selection we have just made, like most of the ones created by the rest of the selection algorithms, can also be
done manually from QGIS, so you might be wondering what is the point on using an algorithm for that. Although
now this might not make much sense to you, we will later see how to create models and scripts. If you want to
make a selection in the middle of a model (which defines a processing workflow), only a geoalgorithm algorithms
can be added to a model, and other QGIS elements and operations cannot be added. That is the reason why some
processing algorithms duplicate functionality that is also available in other QGIS elements.
By now, just remember that selections can be made using processing geoalgorithms, and that algorithms will only
use the selected features if a selection exists, or all features otherwise.
446
The SAGA path should already be configured and pointing to the folder where SAGA is installed.
If you have installed QGIS not using the OSGeo4W installer, then you must enter the path to your SAGA installation (which you must have installed separately) there. The required version is SAGA 2.1
In case you are using Linux, you do not have to set the path to your SAGA installation in the processing configuration. Instead, you must install SAGA and make sure that the SAGA folder is in PATH, so it can be called from
the console (just open a console and type saga_cmd to check it). Under Linux, the target version for SAGA is
also 2.1, but in some installations (such as the OSGeo Live DVD) you might have just 2.0.8 available. There are
some 2.1 packages available, but they are not commonly installed and might have some issues, so if you prefer to
use the more common and stable 2.0.8, you can do it by enabling 2.0.8 compatibility in the configuration dialog,
under the SAGA group
447
Once SAGA is installed, you can launch a SAGA algorithm double clicking on its name, as with any other
algorithm. Since we are using the simplified interface, you do not know which algorithms are based on SAGA or
in another external application, but if you happen to doubleclick on one of them and the corresponding application
is not installed, you will see something like this.
In our case, and assuming that SAGA is correctly installed and configured, you should not see this window, and
you will get to the parameters dialog instead.
Lets try with a SAGAbased algorithm, the one called Split shapes layer randomly.
448
Use the points layer in the project corresponding to this lesson as input, and the default parameter values, and you
will get something like this (the split is random, so your result might be different).
The input layer has been split in two layer, each one with the same number of points. This result has been
computed by SAGA, and later taken by QGIS and added to the QGIS project.
If all goes fine, you will not notice any difference between this SAGAbased algorithm and one of the others that
we have previously run. However, SAGA might, for some reason, not be able to produce a result and not generate
the file that QGIS is expecting. In that case, there will be problems adding the result to the QGIS project, and an
error message like this will be shown.
449
This kind of problems might happen, even if SAGA (or any other application that we are calling from the processing framework) is correctly installed, and it is important to know how to deal with them. Lets produce one of
those error messages.
Open the Create graticule from extent algorithm and use the following values.
We are using width and height values that is larger than the specified extent, so SAGA cannot produce any output.
450
In other words, the parameter values are wrong, but they are not checked until SAGA gets them and tries to create
the graticule. Since it cannot create it, it will not produce the expected layer, and you will see the error message
shown above.
Understanding this kind of problems will help you solve them and find an explanation to what is happening. As
you can see in the error message, a test is performed to check that the connection with SAGA is working correctly,
indicating you that there might be a problem in how the algorithm was executed. This applies not only to SAGA,
but also to other external applications as well.
In the next lesson we will introduce the processing log, where information about commands run by geoalgorithms
is kept, and you will see how to get more detail when issues like this appear.
It contains four blocks of information: Info, Error, Warnings and Algorithms. Here is a description of all of them.
Info. Some algorithms might leave here information about their execution. For instance, those algorithms
that call an external application usually log the console output of that application to this entry. If you have
a look at it, you will see that the output of the SAGA algorithm that we just run (and that fail to execute
because input data was not correct) is stored here.
451
This is helpful to understand what is going on. Advanced users will be able to analyze that output to find out
why the algorithm failed. IF you are not an advanced user, this will be useful for others to help you diagnose the
problem you are having, which might be a problem in the installation of the external software or an issue with the
data you provided.
Warnings. Even if the algorithm could be executed, some algorithms might leave warnings in case the result
might not be right. For instance, when executing an interpolation algorithm with a very small amount of
points. The algorithm can run and will produce a result, but it is likely that it will not be correct, since more
points should be used. Its a good idea to regularly check for this type of warnings if you are not sure about
some aspect of a given algorithm.
Error. Errors that appear and are not directly related to external applications are logged in this section.
Algorithms. All algorithms that are executed, even if they are executed from the GUI and not from the
console (which will be explained later in this manual) are stored in this part of the log as a console call.
That means that everytime you run an algorithm, a console command is added to the log, and you have the
full history of your working session. Here is how that history looks like
452
This can be very useful when starting working with the console, to learn about the syntax of algorithms. We will
use it when we discuss how to run analysis commands from the console.
The history is also interactive, and you can rerun any previous algorithm just by doubleclicking on its entry.
This is an easy way of replicating the work we already did before.
For instance, try the following. Open the data corresponding to the first chapter of this manual and run the
algorithm explained there. Now go to the log dialog and locate the last algorithm in the list, which corresponds to
the algorithm you have just run. Doubleclick on it an a new result should be produced, just like when you run it
using the normal dialog and calling it from the toolbox.
17.9.1
Advanced
You can also modify the algorithm. Just copy it, open the Plugins Python console, click on Import class Import Processing class, then paste it to re-run the analysis; change the text at will. To display the resulting file, type
iface.addVectorLayer(/path/filename.shp, Layer name in legend, ogr).
453
your toolbox.
In this lesson we will be performing some calculation with the raster calculator, most of them rather simple. This
will let us see how it is used and how it deals with some particular situations that it might find. Understanding that
is important to later get the expected results when using the calculator, and also to understand certain techniques
that are commonly applied with it.
Open the QGIS project corresponding to this lesson and you will see that it contains several raster layers.
Now open the toolbox and open the dialog corresponding to the raster calculator.
Select the DEM in the layers field and type a * 3.28084 in the formula field.
Warning: for non English users: use always ., not ,
454
Click Run to run the algorithm. You will get a layer that has the same appearance of the input layer, but with
different values. The input layer that we used has valid values in all its cells, so the last parameter has no effect at
all.
Lets now perform another calculation, this time on the accflow layer. This layer contains values of accumulated
flow, a hydrological parameter. It contains those values only within the area of a given watershed, with nodata
values outside of it. As you can see, the rendering is not very informative, due to the way values are distributed.
Using the logarithm of that flow accumulation will yield a much more informative representation. We can calculate
that using the raster calculator.
Open the algorithm dialog again, select the accflow layer as the only input layer, and enter the following formula:
log(a).
Here is the layer that you will get.
If you select the Identify tool to know the value of a layer at agiven point, select the layer that we have just created,
and click on a point outside of the basin, you will see that it contains a nodata value.
455
For the next exercise we are going to use two layers instead of one, and we are going to get a DEM with valid
elevation values only within the basin defined in the second layer. Open the calculator dialog and select both layers
of the project in the input layers field. Enter the following formula in the corresponding field:
a/a * b
a refers to the accumulated flow layer (since it is the first one to appear in the list) and b refers to the DEM. What
we are doing in the first part of the formula here is to divide the accumulated flow layer by itself, which will result
in a value of 1 inside the basin, and a nodata value outside. Then we multiply by the DEM, to get the elevation
value in those cells inside the basin (DEM * 1 = DEM) and the nodata value outside (DEM * no_data =
no_data)
Here is the resulting layer.
This technique is used frequently to mask values ina raster layer, and is useful whenever you want to perform
calculations for a region other that the arbitrary rectangular region that is used by raster layer. For instance, an
elevation histogram of a raster layer doesnt have much meaning. If it is instead computed using only values
corresponding to a basin (as in he case above), the result that we obtain is a meaningful one that actually gives
456
As you can see, we can use the calculator not only to do simple algebraic operations, but also to run more complex
calculation involving conditional sentences, like the one above.
The result has a value of 1 inside the range we want to work with, and no-data in cells outside of it.
The no-data value comes from the 0/0 expression. Since that is an undetermined value, SAGA will add a NaN
(Not a Number) value, which is actually handled as a no-data value. With this little trick you can set a no-data
value without needing to know what the nodata value of the cell is.
Now you just have to multiply it by the slope layer included in the project, and you will get the desired result.
All that can be done in a single operation with the calculator. We leave that as an exercise for the reader.
457
458
This time the parameters window should look like this before pressing the OK button.
459
Both fields are of type integer, and the result would be truncate to an integer. Thats why we have added the
float() function, to indicate that we want floating point number a result. You can use other Python functions
as needed, since the calculator supports python commands (the raster calculator, however, doesnt).
Since the formula field accepts Python syntax, we can have a new field with male or female text strings instead
of those ratio value, using the following formula instead.
male if MALES > FEMALES else female
A more powerful python field calculator is available in the Advanced Python field calculator, which will not be
detailed here
460
461
All the parameters, except for the last two ones, are used to define which layer is to be rasterized, and configure
how the rasterization process should work. The two last parameters, on the other hand, define the characteristics
of the output layer. That means that they define the area that is covered (which is not necesarily the same area
covered by the input vector layer), and the resolution/cellsize (which cannot be infered from the vector layer, since
vector layers do not have a cellsize).
The first thing you can do is to type the 4 defining values explained before, separated by commas.
That doesnt need any extra explanation. While this is the most flexible option, it is also the less practical in some
cases, and thats why other options are implemented. To access them, you have to click on the button on the
righthand side of the extent text box.
462
Here you can select the extent of the canvas (the extent covered by the current zoom), or the extension any of the
available layers. Select it and click on OK, and the text box will be automatically filled with the corresponding
values.
The second option is Select extent on canvas. In this case, the algorithm dialog disappears and you can click and
drag on the QGIS canvas to define the desired extent.
Once you release the mouse button, the dialog will reappear and the text box will already have the values corresponding to the defined extent.
The last option is Use min covering extent from input layers, which is the default option. This will compute the
min covering extent of all layers used to run the algorithm, and there is no need to enter any value in the text box.
In the case of a single input layer, as in the algorithm we are running, the same extent can be obtained by selecting
that same input layer in the Use layer/canvas extent that we already saw. However, when there are several input
layers, the min covering extent does not correspond to any of the input layer extent, since it is computed from all
of them together.
We will use this last method to execute our rasterization algorith.
Fill the parameters dialog as shown next, and press OK.
463
You will get a rasterized layer that covers exactly the area covered by the original vector layer.
In some cases, the last option, Use min covering extent from input layers, might not be avaialble. This will happen
in those algorithm that do not have input layers, but just parameters of other types. In that case, you will have to
464
The algorithm is rather simple, and you just have to select the layer to use and one of its field (a numeric one).
The output is of type HTML, but the corresponding box works exactly like the one that you can find in the case
of a raster or vector output. You can enter a filepath or leave it blank to save to a temporary file. In this case,
however, only the html and htm extensions are allowed, so there is no way of altering the output format by using
a different one.
Run the algorithm selecting the only layer in the project as input, and the POP2000 field, and a new dialog like
the one shown next will appear once the algorithm is executed and the parameters dialog is closed.
465
This is the Results viewer. It keeps all the HTML result generated during the current session, easily accesible, so
you can check them quickly whenever you need it. As it happens with layers, if you have saved the output to a
temporary file, it will be deleted once you close QGIS. If you have saved to a non-temporary path, the file will
remain, but it will not appear in the Results viewer the next time you open QGIS.
Some algorithms generate text that cannot be divided into other more detailed outputs. That is the case if, for
instance, the algorithm captures the text output from an external process. In other cases, the output is presented as
text, but internally is divided into several smaller outputs, usually in the form of numeric values. The algorithm
that we have just executed is one of them. Each one of those values is handled as a single output, and stored in a
variable. This has no importance at all now, but once we move to the graphical modeler, you will see that it will
allow us to use those values as numeric inputs for other algorithms.
The dataset contains shapefiles with cholera deaths and pump locations, and an OSM rendered map in TIFF
format. Open the corresponding QGIS project for this lesson.
The first thing to do is to calculting the Voronoi diagram (a.k.a. Thyessen polygons) of the pumps layer, to get the
influence zone of each pump. The Voronoi Diagram algorithm can be used for that.
467
468
469
The new field will be called DEATHS, and we use the COUNT field as weighting field. The resulting table clearly
reflects that the number of deaths in the polygon corresponding to the first pump is much larger than the other
ones.
470
Another good way of visualizing the dependence of each point in the Cholera_deaths layer with a point in
the Pumps layer is to draw a line to the closest one. This can be done with the Distance to closest hub tool, and
using the configuration shown next.
471
472
Although the number of lines is larger in the case of the central pump, do not forget that this does not represent the
number of deaths, but the number of locations where cholera cases were found. It is a representative parameter,
but it is not considering that some locations might have more cases than other.
A density layer will also give us a very clear view of what is happening. We can create it with the Kernel density
algorithm. Using the Cholera_deaths layer and its COUNT field as weight field, and with a radius of 100, we get
something like this.
473
The resulting layer has the extent and cellsize of the streets raster layer.
Remember that, to get the output extent, you do not have to type it. Click on the button on the right-hand side and
select Use layer/canvas extent.
474
Select the streets raster layer and its extent will be automatically added to the text field. You must do the same
with the cellsize, selecting the cellsize of that layer as well.
Combining with the pumps layer, we see that there is one pump clearly in the hotspot where the maximum density
of death cases is found.
475
476
To buffer it, we use the Fixed distance buffer algorithm, with the following parameter values.
Here is the resulting bounding box obtained using the parameters shown above
477
It is a rounded box, but we can easily get the equivalent box with square angles, by running the Polygon from layer
extent algorithm on it. We could have buffered the city limits first, and then calculate the extent rectangle, saving
one step.
You will notice that the rasters has a different projection from the vector. We should therefore reproject them
before proceeding further, using the Warp (reproject) tool.
478
With this layer that contains the bounding box of the raster layer that we want to obtain, we can crop both of the
raster layers, using the Clip Grid with Polygons algorithm.
479
Once the layers have been cropped, they can be merged using the Merge raster layers algorithm
A cellsize is needed for the merged layer. We will use the same one of the input ones. You do not need to know it
480
in advance before calling the algorithm. Just click on the button in the righthand size of the text field and you will
have a dialog to enter small mathematical formulas, and a list of frequently used values, among them the cellsizes
and bounding coordinates of all available layers.
Note: You can save time merging first and then cropping, and you will avoid calling the clipping algorith twice.
However, if there are several layers to merge and they have a rather big size, you will end up with a large layer
than it can later be difficult to process. In that case, you might have to call the clipping algorithm several times,
which might be time consuming, but dont worry, we will soon see that there are some additional tools to automate
that operation. In this example, we just have two layers, so you shouldnt worry about that now.
With that, we get the final DEM we want.
481
482
483
The slope produced by the Slope, Aspect, Curvature algorithm is expressed in radians, but degrees are a more
practical and common unit. The Metric conversions algorithm will help us to do the conversion (but in case you
didnt know that algorithm existed, you could use the raster calculator that we have already used).
484
Reprojecting the converted slope layer back with the Reproject raster layer, we get the final layer we wanted.
docs/training_manual/processing/img/cutting_merging/reproject_back.png
The reprojection processes have caused the final layer to contain data outside the bounding box that we calculated
in one of the first steps. This can be solved by clipping it again, as we did to obtain the base DEM.
485
The first module to execute is Catchment area (Parallel). You can use anyone of the others named Catchment
area. They have different algorithms underneath, but the results are basically the same.
Select the DEM in the Elevation field, and leave the default values for the rest of parameters.
486
This algorithm calculates many layers, but the Catchment Area one is the only one we will be using.
You can get rid of the other ones if you want.
The rendering of the layer is not very informative.
To know why, you can have a look at the histogram and you will see that values are not evenly distributed (there
are a few cells with very high value, those corresponding to the channel network). Calculating the logarithm of the
catchment area value yields a layer that conveys much more information (you can do it using the raster calculator).
487
The catchment area (also known as flow accumulation), can be used to set a threshold for channel initiation. This
can be done using the Channel network algorithm. Here is how you have to set it up.
488
Use the original catchment area layer, not the logarithm one. That one was just for rendering purposes.
If you increase the Initiation threshold value, you will get a more sparse channel network. If you decrease it, you
will get a denser one. With the proposed value, this is what you get.
489
The image above shows just the resulting vector layer and the DEM, but there should be also a raster one with the
same channel network. That raster one will be, in fact, the one we will be using.
Now, we will use the Watersheds basins algorithm to delineate the subbasins corresponding to that channel network, using as outlet points all the junctions in it. Here is how you have to set the corresponding parameters
dialog.
490
This is a raster result. You can vectorise it using the Vectorise grid classes algorithm.
17.16. Hydrological analysis
491
Now, lets try to compute statistics about the elevation values in one of the subbasins. The idea is to have a layer
that just represents the elevation within that subbasin and then pass it to the module that calculates those statistics.
492
First, lets clip the original DEM with the polygon representing a subbasin. We will use the Clip Grid with Polygon
algorithm. If we select a single subbasin polygon and then call the clipping algorithm, we can clip the DEM to the
area covered by that polygon, since the algorithm is aware of the selection.
Select a polygon,
493
The element selected in the input field is, or course, the DEM we want to clip.
You will get something like this.
494
We will use both the basin calculations procedure and the statistics calculation in other lessons, to find out how
other elements can help us automate both of them and work more effectively.
17.16. Hydrological analysis
495
As you can see, there are two mandatory inputs: Slope and Catchment area. There is also an optional input, but
we will not be using it, so we can ignore it.
The data for this lesson contains just a DEM, so we do not have any of the required inputs. However, we know
how to calculate both of them from that DEM, since we have already seen the algorithms to compute slope and
catchment area. So we can first compute those layers and then use them for the TWI algorithm.
Here are the parameter dialogs that you should use to calculate the 2 intermediate layers.
496
497
And this is how you have to set the parameters dialog of the TWI algorithm.
498
This is the result that you will obtain (the default singleband pseudocolor inverted palette has been used for
rendering).
What we will try to do now is to create an algorithm that calculates the TWI from a DEM in jut one single step.
That will save us work in case we later have to compute a TWI layer from another DEM, since we will need just
17.17. Starting with the graphical modeler
499
one single step to do it instead of the 3 ones above. All the processes that we need are found in the toolbox, so
what we have to do is to define the workflow to wrap them. This is where the graphical modeler comes in.
Open the modeler by selecting its menu entry in the processing menu.
Two things are needed to create a model: setting the inputs that it will need, and defining the algorithm that
it contains. Both of them are done by adding elements from the two tabs in the lefthand side of the modeler
window: Inputs and Algorithms
Lets start with the inputs. In this case we do not have much to add. We just need a raster layer with the DEM,
and that will be our only input data.
Double click on the Raster layer input and you will see the following dialog.
Here wil have to define the input we want. Since we expect this raster layer to be a DEM, we will call it DEM.
Thats the name that the user of the model will see when running it. Since we need that layer to work, we will
define it as a mandatory layer.
Here is how the dialog should be configured.
500
Now lets move to the Algorithms tab. The first algorithm we have to run is the Slope, aspect, curvatures algorithm.
Locate it in the algorithm list, doubleclick on it and you will see the dialog shown below.
501
This dialog is very similar to the one that you can find when running the algorithm from the toolbox, but the
element that you can use as parameter values are not taken from the current QGIS project, but from the model
itself. That means that, in this case, we will not have all the raster layers of our project available for the Elevation
field, but just the ones defined in our model. Since we have added just one single raster input named DEM, that
will be the only raster layer that we will see in the list corresponding to the Elevation parameter.
Output generated by an algorithm are handled a bit differently when the algorithm is used as a part of a model.
Instead of selecting the filepath where you want to save each output, you just have to specify if that ouput is an
intermediate layer (and you do not want it to be preserved after the model has been executed), or it is a final one.
In this case, all layers produced by this algorithm are intermediate. We will only use one of them (the slope layer),
but we do not want to keep it, since we just need it to calculate the TWI layer, which is the final resutl that we
want to obtain.
When layers are not a final result, you should just leave the corresponding field. Otherwise, you have to enter a
name that will be used to identify the layer in the parameters dialog that will be shown when you run the model
later.
There is not much to select in this first dialog, since we do not have but just one layer in or model (The DEM input
that we created). Actually, the default configuration of the dialog is the correct one in this case, so you just have
to press OK. This is what you will no have in the modeler canvas.
502
The second algorithm we have to add to our model is the catchment area algorithm. We will use the algorithm
named Catchment area (Paralell). We will use the DEM layer again as input, and none of the ouputs it produces
are final, so here is how you have to fill the corresponding dialog.
503
504
The last step is to add the Topographic Wetness Index algorithm, with the following configuration.
In this case, we will not be using the DEM as input, but instead, we will use the slope and catchment area layers
that are calcualted by the algorithms that we previously added. As you add new algorithms, the outputs they
produce become available for other algorithms, and using them you link the algorithms, creating the workflow.
In this case, the output TWI layer is a final layer, so we have to indicate so. In the corresponding textbox, enter
505
Enter a name and a group name in the upper part of the model window, and then save it clicking on the Save
button.
You can save it anywhere you want an open it later, but if you save it in the models folder (which is the folder that
you will see when the save file dialog appears), you model will also be available in the toolbox as well. So stay on
that folder and save the model with the filename that you prefer.
Now close the modeler dialog and go to the toolbox. In the Models entry you will find you model.
You can run it just like any normal algorithm, doubleclicking on it.
506
As you can see, the parameters dialog, contain the inut that you added to the model, along with the outputs that
you set as final when adding the corresponding algorithms.
Run it using the DEM as input and you will get the TWI layer in just one single step.
507
This model contains all the steps needed to complete the calculation, but it just has one input: the DEM That
means that the threshold for channel definition use a fixed value, which makes the model not as useful as it could
be. That is not a problem, since we can edit the model, and that is exactly what we will do.
First, lets add a numerical input. That will ask the user for a numerical input that we can use when such a value is
needed in any of the algorithms included in our model. Click on the Numerical value entry in the inputs tree, and
you will see the corresponding dialog. Fill it with the values shown next.
508
The input that we have just added is not used, so the model hasnt actually changed. We have to link that input
to the algorithm that uses it, in this case the Channel network one. To edit an algorithm that already exists in the
modeler, just double click on its corresponding box in the canvas. If you click on the Channel network algorithm,
you will see something like this.
The dialog is filled with the current values used by the algorithm. You can see that the threshold parameter has a
fixed value of 1000000 (this is also the default value of the algorithm, but any other value could be put in there).
However, you might notice that the parameter is not entered in a common text box, but in an option menu. If you
509
The input that we added is there and we can select it. Whenever an algorithm in a model requires a umerical value,
you can hardcode it and directly type it, or you can use any of the available inputs and values (remember that some
algorithms generate sinlge numerical values. We will see more about this soon.). In the case of a string parameter,
you will also see string inputs and you will be able to select on of them or type the desired fixed value.
Select the Threshold input in the Threshold parameter and click on OK to apply the changes to your model. Now
the design of the model should look like this.
510
The model is now complete. Try to run it using the DEM that we have used in previous lessons, and with different
threshold values. Here you have a sample of the result obtained for different values. You can compare with the
result for the default value, which is the one we obtained in the hydrological analysis lesson.
Threshold = 100000
Threshold = 10000000
511
Note: In this lesson we will see how to use numeric outputs in the modeler
For this lesson, we are going to modify the hydrological model that we created in the last chapter (open it in the
modeler before starting), so we can automate the calculation of a valid threshold value and we do not have to ask
the user to enter it. Since that value refers to the variable in the threshold raster layer, we will extract it from that
layer, based on some simple statistical analysis.
Starting with the aforemetioned model, lets do the following modifications:
First, calculate statistics of the flow accumulation layer using the Raster layer statistics algorithm.
This will generate a set of statistical values that will now be available for all numeric fields in other algorithms.
If you double click on the Channel network algorithm to modify it, as we did in the last lesson, you will see now
that you have other options apart from the numeric input that you added.
512
However, none of this values is suitable for being used as a valid threshold, since they will result in channel
networks that will not be very realistic. We can, instead, derive some new parameter based on them, to get a better
result. For instance, we can use the mean plus 2 times the standard deviation.
To add that arithmetical operation, we can use the calculator that you will find in the
Geoalgorithms/modeler/modeler-tools group. This group contains algorithms that are not very useful outside of the modeler, but that provide some useful functionality when creating a model.
The parameters dialog of the calculator algorithm looks like this:
513
As you can see, the dialgo is different to the other ones we have ssen, but you have in there the same variables that
were available in the Threshold field in the Channel network algorithm. Enter the above formula and click on OK
to add the algorithm.
If you expand the outputs entry, as shown above, you will see that the model is connected to two of the values,
namely the mean and the standard deviation, which are the ones that we have used in the formula.
Adding this new algorithm will add a new numeric value. If you go again to the Channel network algorithm, you
can now select that value in the Threshold parameter.
514
docs/training_manual/processing/img/modeler_hydro_calculator/final_model.png
We are not using the numeric input that we added to the model, so it can be removed. Rightclick on it and select
Remove
Our new model is now finished.
Note: In this lesson we will see how to use a model within a bigger model
We have already created a few models, and in this lesson we are going to see how we can combine them on a
single bigger one. A model behaves like any other algorithm, which means that you can add a model that you have
already craeted as part of another one that you create after that.
In this case, we are going to expand our hydrological model, by adding the mean TWI value in each of the basins
that it generates as result. To do that, we need to calculate the TWI, and the to compute statistics. Since we have
already created a model to calculate TWI from a DEM, it is a good idea to reuse that model instead of adding the
algorithms it contains individually.
515
Lets start with the model we used as starting point for the last lesson.
First, we will add the TWI model. for it to be available, it should have been saved on the models folder, since
otherwise it will not be shown in the toolbox or the algorithms list in the modeler. Make sure you have it available.
Add it to the current model and use the input DEM as its input. The output is a temporary one, since we just want
the TWI layer to compute the statistics. The only output of this model we are creating will still be the vector layer
with the watersheds.
Here is the corresponding parameters dialog:
Now we have a TWI layer that we can use along with the watersheds vector layer, to generate a new one which
contains the values of the TWI corresponding to each watershed.
This calculation is done using the Grid statistics in polygons algorithm. Use the layers mentioned above as input,
to create the final result.
The output of the Vectorize grid classes algorithm was originally our final output, but now we just want it as an
intermediate result. To change that, we have to edit the algorithm. Just doubleclick on it to see its parameters
dialog, and delete the name of the output. That will make it a temporary output, as it is by default.
This is how the final model should look like:
As you see, using a model in another model is nothing special, and you can add it just like you add another
algorithm, as long as the model is saved in the models folder and is available in the toolbox.
17.21 Interpolation
Note: This chapter shows how to interpolate point data, and will show you another real example of performing
spatial analysis
In this lesson, we are going to interpolate points data to obtain a raster layer. Before doing it, we will have to do
some data preparation, and after interpolating we will add some extra processing to modify the resulting layer, so
we will have a complete analysis routine.
Open the example data for this lesson, which should look like this.
516
The data correspond to crop yield data, as produced by a modern harvester, and we will use it to get a raster layer
of crop yield. We do not plan to do any further analysis with that layer, but just to use it as a background layer for
easily identifying the most productive areas and also those where productivity can be improved.
The first thing to do is to cleanup the layer, since it contains redundant points. These are caused by the movement
of the harvester, in places where it has to do a turn or it changes its speed for some reason. The Point filter
algorithm will be useful for this. We will use it twice, to remove points that can be considered outliers both in the
upper and lower part of the distribution.
For the first execution, use the following parameter values. [Note: this does not work, resulting file empty]
17.21. Interpolation
517
Now for the next one, use the configuration shown below.
518
Notice that we are not using the original layer as input, but the output of the previous run instead.
The final filter layer, with a reduced set of points, should look similar to the original one, but it contains a smaller
number of points. You can check that by comparing their attribute tables.
Now lets rasterize the layer using the Shapes to grid algorithm.
17.21. Interpolation
519
The Filtered points layer refers to the resulting one of the second filter. It has the same name as the one produced
by the first filter, since the name is assigned by the algorithm, but you should not use the first one. Since we will
not be using it for anything else, you can safely remove it from your project to avoid confusion, and leave just the
last filtered layer.
The resulting raster layer looks like this.
520
It is already a raster layer, but it is missing data in some of its cells. It only contain valid values in those cells that
contained a point from the vector layer that we have just rasterized, and a nodata value in all the other ones. To
fill the missing values, we can use the Close gaps algorithm.
17.21. Interpolation
521
522
To restrict the area covered by the data to just the region where crop yield was measured, we can clip the raster
layer with the provided limits layer
17.21. Interpolation
523
And for a smoother result (less accurate but better for rendering in the background as a support layer), we can
apply a Gaussian filter to the layer.
524
With the above parameters you will get the following result
525
The selection is random, so your selection might differ from the selection shown in the above image.
Now run the Shapes to grid to get the first raster layer, and then run the Close gaps algorithm to fill the nodata
526
To check the quality of the interpolation, we can now use the points that are not selected. At this points, we know
the real elevation (the value in the points layer) and the interpolated elevation (the value in the interpolated raster
layer). We can compare the by computing the differences between those values.
Since we are going to use the points that are not selected, first, lets invert the selection.
The points contain the original values, but not the interpolated ones. To add them in a new field, we can use the
17.22. More interpolation
527
The raster layer to select (the algorithm supports multiple raster, but we just need one) is the resulting one from
the interpolation. WE have renamed it to interpolate and that layer name is the one that will be used for the name
of the field to add.
Now we have a vector layer that contains both values, with points that were not used for the interpolation.
528
Now, we will use the fields calculator for this task. Open the Field calculator algorithm and run it with the
following parameters.
529
If your field with the values from the raster layer has a different name, you should modify the above formula
accordingly. Running this algorithm, you will get a new layer with just the points that we havent used for the
interpolation, each of them containing the difference between the two elevation values.
Representing that layer according to that value will give us a first idea of where the largest discrepancies are found.
Interpolating that layer will get you a raster layer with the estimated error in all points of the interpolated area.
530
Your results might differ from these ones, since there is a random component introduced when running the random
selection, at the beginning of this lesson.
531
You will recognize our well-known DEM from previous chapters and a set of watersheds extracted from it. Imagine that you need to cut the DEM into several smaller layers, each of them containing just the elevation data
corresponding to a single watershed. That will be useful if you later want to calculate some parameters related to
each watershed, such as its mean elevation or it hypsographic curve.
This can be a lengthy and tedious task, especially if the number of watersheds is large. However, it is a task that
can be easily automated, as we will see.
The algorithm to use for clipping a raster layer with a polygon layer is called Clip grid with polygons, and has the
following parameters dialog.
532
Yo can run it using the watersheds layer and the DEM as input, and you will get the following result.
As you can see, the area covered by all the watershed polygons is used.
You can have the DEM clipped with just a single watershed by selecting the desired watershed and then running
17.23. Iterative execution of algorithms
533
Since only selected features are used, only the selected polygon will be used to crop the raster layer.
Doing this for all the watersheds will produce the result we are looking for, but it doesnt look like a very practical
way of doing it. Instead, lets see how to automate that select and crop routine.
First of all, remove the previous selection, so all polygons will be used again. Now open the Clip grid with polygon
algorithm and select the same inputs as before, but this time click on the button that you will find in the righthand
534
side of the vector layer input where you have selected the watersheds layer.
This button will cause the selected input layer to be split into as many layer as feature are found in it, each of
them containing a single polygon. With that, the algorithm will be called repeatedly, one for each one of those
single-polygon layers. The result, instead of just one raster layer in the case of this algorithm, will be a set of
raster layers, each one of them corresponding to one of the executions of the algorithm.
Heres the result that you will get if you run the clipping algorithm as explained.
535
For each layer, the black and white color palette, (or whatever palette you are using), is adjusted differently, from
its minimum to its maximum values. Thats the reason why you can see the different pieces and the colors do not
seem to match in the border between layers. Values, however, do match.
If you enter an output filename, resulting files will be named using that filename and a number corresponding to
each iteration as suffix.
536
Add the model to you models folder, so it is available in the toolbox, and now execute it.
Select the DEM and watersheds basins, and do not forget to toggle the button that indicates that the algorithm has
to be run iteratively.
The algorithm will be run several times, and the corresponding tables will be created and open in your QGIS
project.
537
We can make this example more complex by extending the model and computing some slope statistics. Add the
Slope, aspect, curvatures algorithm to the model, and then the Raster statistics algorithm, which should use the
slope output as its only input.
If you now run the model, apart from the tables you will get a set of pages with statistics. These pages will be
available in the results dialog
538
Sometimes a given algorithm has to be executed repeteadly with different inputs. This is, for instance, the case
when a set of input files have to be converted from one format to another, or when several layers in a given
projection must be converted into another projection.
In that case, calling the algorithm repeatedly on the toolbox is not the best option. Instead, the batch processing
interface should be used, which greatly simplifies performing a multiple execution of a given algorithm. To run
an algorithm as a batch process, find it in the toolbox, and instead of doubleclicking on it, rightclick on it and
select Run as batch process
For this example, we will use the Reproject algorithm, so find it and do as described above. You will get to the
following dialog.
If you have a look at the data fo this lesson, you will see that it contains a set of three shapefiles, but no QGIS
project file. This is because, when an algorithm is run as a batch process, laye inputs are not selected from the
current QGIS project, but from files instead. that makes it easier to process large amounts of layers, such as, for
instance, all the layers in a given folder.
Each row in the table of the batch processing dialog represents a single execution of the algorithm. Cells in a row
correspond to the parameter needed by the algorithm, which are not arranged one above the other, as in the normal
singleexecution dialog, but horizontally in that row.
Defining the batch process to run is one by filling the table with the corresponding values, and the dialog itself
contains several tools to make this task easier.
Lets start filling the fields one by one. The first column to fill is the Input layer one. Instead of entering the
names of each one of the layers we want to process, you can select all of them and let the dialog put one in each
row. Click on the button in the upper-left cell, and in the file selection dialog that will popup, select the three
539
files to reproject. Since only one of them is needed for each row, the remaining ones will be used to fill the rows
underneath.
The default number of rows is 3, which is exactly the number of layers we have to convert, but if you select more
layers, new rows will be added automatically. If you want to fill the entries manually, you can add more rows
using the Add row button.
We are going to convert all those layers to the EPSG:23029 CRS, so we have to select that CRS in the second
field. We want the same on for all rows, but we do not have to do it for every single row. Instead, set that CRS for
the first row (the one at the top) using the button in the corresponding cell, and then double click on the column
header. That causes all the cells in the column to be filled using the value of the top cell.
540
Finally, we have to select an output file for each execution, which will contain the corresponding reprojected layer.
Once again, lets do it just for the first column. Click on the button in the upper cell and, in a folder where you
want to put your output files, enter a filename (for instance, reprojected.shp).
Now, when you click OK on the file selection dialog, the file does not automatically gets written to the cell, but an
input box like the following one is shown instead.
If you select the first option, only the current cell will be filled. If you select any of the other ones, all the rows
below will be filled with a given pattern. In this case, we are going to select the Fill with parameter value option,
and then the Input Layer value in the drop down menu below. That will cause the value in the Input Layer (that
is, the layer name) to be added to the filename we have added, making each output filename different. The batch
processing table should now look like this.
541
The last column sets whether or not to add the resulting layers to the current QGIS project. Leave the default Yes
option, so you can see your results in this case.
Click on OK and the batch process will be run. If everything went fine, all your layers will have been processed,
and 3 new layers would have been created.
Note: This lesson shows another example of the batch processing interface, but this time using a model instead
of a built-in algorithm
Model are just like any other algorithm, and they can be used in the batch processing interface. To demonstrate
that, here is a brief example that we can do using our already well-known hydrological model.
Make sure you have the model added to your toolbox, and then run it in batch mode. This is what the batch
processing dialog should look like.
docs/training_manual/processing/img/batch_modeler/dialog.png
Add rows up to a total of 5. Select the DEM file corresponding to this lesson as the input for all of them. Then
enter 5 different threshold values as shown next.
542
docs/training_manual/processing/img/batch_modeler/filled.png
As you see the batch processing interface can be run not just to run the same process on different datasets but also
on the same dataset with different parameters.
Click on OK and you should get 5 new layers with watersheds corresponding to the specified 5 threshold values.
17.27.1 GRASS
GRASS is a free and open source GIS software suite for geospatial data management and analysis, image processing, graphics and maps production, spatial modeling, and visualization
It is installed by deafult on Windows through the OSGeo4W standalone installer (32 and 64 bit), and it is packaged
for all major Linux distributions.
17.27.2 R
R is a free and open source software environment for statistical computing and graphics.
It has to be installed separately, together with a few necessary libraries (LIST).
The beauty of Processing implementation is that you can add your own scripts, simplex or complex ones, and they
may then be used as any other module, piped into more complex workflows, etc.
17.27.3 OTB
OTB (also known as Orfeo ToolBox) is a free and open source library of image processing algorithms. It is
installed by deafult on Windows through the OSGeo4W standalone installer (32 bit). Paths should be configured
in Processing.
In a standard OSgeo4W Windows installation, the paths will be:
OTB application folder
C:\OSGeo4W\apps\orfeotoolbox\applications
OTB command line tools folder C:\OSGeo4W\bin
17.27.4 Others
TauDEM is a suite of Digital Elevation Model (DEM) tools for the extraction and analysis of hydrologic information. Availability in various operating system varies.
LASTools is a set of mixed, free and proprietary commands to process and analyze LiDAR data. Availability in
various operating system varies.
543
544
17.28.1 Interpolation
The project shows a gradient in rainfall, from south to north. Lets use different methods for interpolation, all
based on vector points.shp, parameter RAIN:
Warning: set cell size to 500
GRASS v.surf.rst
SAGA Multilevel B-Spline Interpolation
SAGA Inverse Distance Weighted [Power: 4; Search range: Global]
GDAL Grid (Inverse Distance to a power) [Power:4]
GDAL Grid (Moving average) [Radius1&2: 50000]
17.28.2 Contour
Various methods to draw contour lines [always step= 10]:
GRASS r.contour.step
SAGA Contour Lines from Grid
GDAL Contour
545
546
CAPITOLUL 18
In this module you will learn about how to use Spatial Databases with QGIS to manage, display and manipulate
data in the database as well as performing analysis by querying. We will primarily use PostgreSQL and PostGIS (which were covered in previous sections), but the same concepts are applicable to other spatial database
implementations including spatialite.
18.1.1
We have already briefly looked at how to add tables from a database as QGIS layers, now lets look at this in a
bit more detail and look at the different ways this can be done in QGIS. Lets start by looking at the new Browser
interface.
Start a new empty map in QGIS.
Open the Browser by clicking the Browser tab at the bottom of the Layer Panel
Open the PostGIS portion of the tree and you should find your previously configured connection available
(you may need to click the Refresh button at the top of the browser window).
547
Double clicking on any of the table/layers listed here will add it to the Map Canvas.
Right Clicking on a table/layer in this view will give you a few options. Click on the Properties item to look
at the properties of the layer.
548
Note: Of course you can also use this interface to connect to PostGIS databases hosted on a server external to
your workstation. Right clicking on the PostGIS entry in the tree will allow you to specify connection paramaters
for a new connection.
18.1.2
Now that we have seen how to add an entire table as a QGIS layer it might be nice to learn how to add a filtered
set of records from a table as a layer by using queries that we learned about in previous sections.
Start a new empty map with no layers
Click the Add PostGIS Layers button or select Layer > Add PostGIS Layers from the menu.
In the Add PostGIS Table(s) dialog that comes up, connect to the postgis_demo connection.
Expand the public schema and you should find the three tables we were working with previously.
Click the lines layer to select it, but instead of adding it, click the Set Filter button to bring up the Query
Builder dialog.
Construct the following expression using the buttons or by entering it directly:
"roadtype" = major
Click OK to complete editing the filter and click Add to add the filtered layer to your map.
549
18.1.3 In Conclusion
You have seen how to interact with spatial databases using the QGIS Browser and how to add layers to your map
based on a query filter.
18.2.1
You should first open the DB Manager interface by selecting Database > DB Manager > DB Manager on the
menu or by selecting the DB Manager icon on the toolbar.
You should already see the previous connections we have configured and be able to expand the myPG section and
its public schema to see the tables we have worked with in previous sections.
The first thing you may notice is that you can now see some metadata about the Schemas contained in your
database.
550
Schemas are a way of grouping data tables and other objects in a PostgreSQL database and a container for permissions and other constraints. Managing PostgreSQL schemas is beyond the scope of this manual, but you can
find more information about them in the PostgreSQL documentation. You can use the DB Manager to create new
Schemas, but will need to use a tool like pgAdmin III or the command line interface to manage them effectively.
DB Manager can also be used to manage the tables within your database. We have already looked at various ways
to create and manage tables on the command line, but now lets look at how to do this in DB Manager.
First, its useful to just look at a tables metadata by clicking on its name in tree and looking in the Info tab.
551
In this panel you can see the General Info about the table as well the information that the PostGIS extension
maintains about the geometry and spatial reference system.
If you scroll down in the Info tab, you can see more information about the Fields, Constraints and Indexes for the
table you are viewing.
552
Its also very useful to use DB Manager to simply look at the records in the database in much the same way you
might do this by viewing the attribute table of a layer in the Layer Tree. You can browse the data by selecting the
Table tab.
553
There is also a Preview tab which will show you the layer data in a map preview.
Right Clicking on a layer in the tree and clicking Add to Canvas will add this layer to your map.
So far we have only been viewing the database its schemas and tables and their metadata, but what if we wanted
to alter the table to add an additional column perhaps? DB Manager allows you to do this directly.
Select the table you want to edit in the tree
Select Table > Edit Table from the menu to open the Table Properties dialog.
You can use this dialog to Add Columns, Add geometry columns, edit existing columns or to remove a column
completely.
Using the Constraints tab, you can manage which fields are used as the primary key or to drop existing constraints.
554
The Indexes tab can be used to add and delete both spatial and normal indexes.
555
18.2.2
Now that we have gone through the process of working with existing tables in our database, lets use DB Manager
to create a new table.
If it is not already open, open the DB Manager window, and expand the tree until you see the list of tables
already in your databse.
From the menu select Table > Create Table to bring up the Create Table dialog.
Use the default Public schema and name the table places.
Add the id, place_name, and elevation fields as shown below
Make sure the id field is set as the primary key.
Click the checkbox to Create geometry column and make sure it is set to a POINT type and leave it named
geom and specify 4326 as the SRID.
Click the checkbox to Create spatial index and click Create to create the table.
556
Dismiss the dialog letting you know that the table was created and click Close to close the Create Table
Dialog.
You can now inspect your table in the DB Manager and you will of course find that there is no data in it. From
here you can Toggle Editing on the layer menu and begin to add places to your table.
18.2.3
The DB Manager will also let you do some basic Database Administration tasks. It is certainly not a substitute for
a more complete Database Administration tool, but it does provide some functionality that you can use to maintain
your database.
Database tables can often become quite large and tables which are being modified frequently can end up leaving
around remnants of records that are no longer needed by PostgreSQL. The VACUUM command takes care of
doing a kind of garbage collection to compact and optionall analyze your tables for better performance.
Lets take a look at how we can perform a VACUUM ANALYZE command from within DB Manager.
Select one of your tables in the DB Manager Tree.
Select Table > Run Vacuum Analyze from the menu.
Thats it! PostgreSQL will perform the operation. Depending on how big your table is, this may take some time to
complete.
You can find more information about the VACUUM ANALYZE process in the PostgreSQL Documentation
557
18.2.4
DB Manager also provides a way for you to write queries against your database tables and to view the results. We
have already seen this type of functionality in the Browser panel, but lets look at it again here with DB Manager.
Select the lines table in the tree.
Select the SQL window button in the DB Manager toolbar.
Click the checkbox for Load as new layer to add the results to your map.
Select the id column as the Column with unique integer values and the geom column as the Geometry
column.
Enter roads_primary as the Layer name (prefix).
Click Load now! to load the results as a new layer into your map.
558
The layers that matched your query are now displayed on your map. You can of course use this query tool to
execute any arbitrary SQL command including many of the ones we looked at in previous modules and sections.
559
Dismiss the dialog letting you know that the import was successful
Click the Refresh button on the DB Manager Toolbar.
You can now inspect the table in your database by clicking on it in the Tree. Verify that the data has been
reprojected by checking that the Spatial ref: is listed as WGS 84 (4326)
560
Right clicking on the table in the Tree and a selecting Add to Canvas will add the table as a layer in your map.
561
Dismiss the dialog letting you know the export was successful and close the DB Manager.
You can now inspect the shapefile you created with the Browser panel.
562
18.2.7 In Conclusion
You have now seen how to use the DB Manager interface in QGIS to Manage your spatial databases, to execute
sql queries against your data and how to import and export data.
18.3.1
Using the Browser panel, we can create a new spatialite database and get it setup for use in QGIS.
Right click on the Spatialite entry in the Browser tree and select Create Database.
Specify where on your filesystem you want to store the file and name it qgis-sl.db.
Again right click on the Spatialite entry in the Browser tree and now select the New Connection item. Find
the file you created in the last step and open it.
Now that you have configured your new database you will find that the entry in Browser tree has nothing underneath it and the only thing you can do at this point is to delete the connection. This is of course because we havent
added any tables to this database. Lets go ahead and do that.
Find the button to create a new layer and use the dropdown to create a new new Spatialite layer, or select
Layer > New > New Spatialite Layer.
Select the database we created in the previous steps in the drop down.
Give the layer the name places.
Tick the checkbox next to Create an auto-incrementing primary key.
Add 2 attributes as shown in below
Click OK to create the table.
563
Click the refresh button at the top of the Browser and you should now see your places table listed.
564
You can right click on the table and view its properties as we did in the previous exercise.
From here you can start an editing session and start adding data to your new database directly.
We also learned about how to import data into a database using the DB Manager and you can use this same
technique to import data into your new spatialite DB.
18.3.2 In Conclusion
You have seen how to create spatialite databases and add tables to them and to use these tables as layers in QGIS.
565
566
CAPITOLUL 19
To add materials to this course, you must follow the guidelines in this Appendix. You are not to alter the conditions
in this Appendix except for clarification. This is to ensure that the quality and consistency of this manual can be
maintained.
foreword/index
introduction/index
You will note that this is a list of directory names, followed by the name index. This directs the top-level index
file to the index files in each directory. The order in which they are listed determines the order they will have in
the document.
Add the name of your new module (i.e., the name you gave the new directory), followed by /index, to
this list, wherever you want your module to appear.
Remember to keep the order of the modules logical, such that later modules build on the knowledge presented in earlier modules.
Open your new modules own index file ([module name]/index.rst).
Along the top of the page, write a line of 80 asterisks (*). This represents a module heading.
567
Follow this with a line containing the markup phrase |MOD| (which stands for module), followed by the
name of your module.
End this off with another line of 80 asterisks.
Leave a line open beneath this.
Write a short paragraph explaining the purpose and content of the module.
Leave one line open, then add the following text:
.. toctree::
:maxdepth: 2
lesson1
lesson2
... where lesson1, lesson2, etc., are the names of your planned lessons.
The module-level index file will look like this:
*******************************************************************************
|MOD| Module Name
*******************************************************************************
Short paragraph describing the module.
.. toctree::
:maxdepth: 2
lesson1
lesson2
If you cant describe the goal of the lesson in one or two sentences, consider breaking the subject matter up
into multiple lessons.
Each lesson will be subdivided into multiple sections, which will be addressed next.
569
Write the instructions on how to complete the assignment, using links and images where needed.
To end it off, include a link back to the try yourself section by writing this line:
:ref:Back to text <backlink-answer-name>
To make this link work, add the following line above the heading to the try yourself section:
.. _backlink-answer-name:
Remember that each of these lines shown above must have a blank line above and below it, otherwise it could
cause errors while creating the document.
570
19.9.2 Emphasis
To emphasize a crucial term which is not a new concept, write the term in bold by enclosing it in double
asterisks (**).
Use this sparingly! If used too much, it can seem to the reader that you are shouting or being condescending.
This sample text shows how to use **emphasis** in a sentence. Include the
punctuation mark if it is followed by a **comma,** or at the **end of the
sentence.**
19.9.3 Images
When adding an image, save it to the folder _static/lesson_name/.
Include it in the document like this:
.. image:: /static/training_manual/lesson_name/image_file.extension
:align: center
Remember to leave a line open above and below the image markup.
This also applies if you are mentioning the name of a tool without requiring the user to click a button.
571
572
CAPITOLUL 20
Answer Sheet
Preparation
You should see a lot of lines, symbolizing roads. All these lines are in the vector layer that you just loaded to
create a basic map.
Back to text
Overview (Part 1)
Refer back to the image showing the interface layout and check that you remember the names and functions of the
screen elements.
Back to text
20.2.2
Overview (Part 2)
1. Save as
2. Zoom to layer
3. Help
4. Rendering on/off
5. Measure line
Back to text
Shapefiles
places
water
buildings
rivers and
roads.
Back to text
20.3.2
Databases
All the vector layers should be loaded into the map. It probably wont look nice yet though (well fix the ugly
colors later).
Back to text
Colors
Verify that the colors are changing as you expect them to change.
It is enough to change only the water layer for now. An example is below, but may look different depending
on the color you chose.
Note: If you want to work on only one layer at a time and dont want the other layers to distract you, you can
hide a layer by clicking in the check box next to its name in the Layers list. If the box is blank, then the layer is
hidden.
574
Back to text
20.4.2
Symbol Structure
20.4.3
Symbol Layers
Customize your buildings layer as you like, but remember that it has to be easy to tell different layers apart
on the map.
Heres an example:
575
Back to text
20.4.4
Symbol Levels
576
The lowest symbol layer is a broad, solid yellow line. On top of it there is a slightly thinner solid gray line.
If your symbol layers resemble the above but youre not getting the result you want, check that your symbol
levels look something like this:
577
578
Back to text
20.4.5
Symbol Levels
Attribute Data
The NAME field is the most useful to show as labels. This is because all its values are unique for every object and
are very unlikely to contain NULL values. If your data contains some NULL values, do not worry as long as most
of your places have names.
579
Back to text
Your map should now show the marker points and the labels should be offset by 2.0 mm: The style of the markers
and labels should allow both to be clearly visible on the map:
Back to text
20.6.2
580
581
Enter a space in this field and click Apply to achieve the same effect. In our case, some of the place names
are very long, resulting in names with multiple lines which is not very user friendly. You might find this
setting to be more appropriate for your map.
Back to text
20.6.3
Still in edit mode, set the FONT_SIZE values to whatever you prefer. The example uses 16 for towns, 14
for suburbs, 12 for localities and 10 for hamlets.
Remember to save changes and exit edit mode.
Return to the Text formatting options for the places layer and select FONT_SIZE in the Attribute field of
the font size data override dropdown:
582
Back to text
583
Use the same method as in the first exercise of the lesson to get rid of the borders:
The settings you used might not be the same, but with the values Classes = 6 and Mode = Natural Breaks (Jenks)
(and using the same colors, of course), the map will look like this:
584
Back to text
Digitizing
The symbology doesnt matter, but the results should look more or less like this:
Back to text
20.8.2
The exact shape doesnt matter, but you should be getting a hole in the middle of your feature, like this one:
585
Undo your edit before continuing with the exercise for the next tool.
Back to text
20.8.3
586
static/training_manual/c
Undo your edit before continuing with the exercise for the next tool.
Back to text
20.8.4
Merge Features
Use the Merge Selected Features tool, making sure to first select both of the polygons you wish to merge.
Use the feature with the OGC_FID of 1 as the source of your attributes (click on its entry in the dialog, then
click the Take attributes from selected feature button):
Note:
If youre using a different dataset, it is highly likely that your original polygons OGC_FID will not be 1.
Just choose the feature which has an OGC_FID.
Note: Using the Merge Attributes of Selected Features tool will keep the geometries distinct, but give them the
same attributes.
Back to text
20.8.5
Forms
For the TYPE, there is obviously a limited amount of types that a road can be, and if you check the attribute table
for this layer, youll see that they are predefined.
Set the widget to Value Map and click Load Data from Layer.
587
Select roads in the Label dropdown and highway for both the Value and Description options:
Back to text
588
For the purpose of this exercise, the OSM layers which we are interested in are multipolygons and lines.
The multipolygons layer contains the data we need in order to produce the houses, schools and
restaurants layers. The lines layer contains the roads dataset.
The Query Builder is found in the layer properties:
Using the Query Builder against the multipolygon layer, create the following queries for the houses,
schools, restaurants and residential layers:
589
590
591
Once you have entered each query, click OK. Youll see that the map updates to show only the data you have
selected. Since you need to use again the multipolygon data from the OSM dataset, at this point, you can use
one of the following methods:
Rename the filtered OSM layer and re-import the layer from osm_data.osm, OR
Duplicate the filtered layer, rename the copy, clear the query and create your new query in the Query Builder.
Note: Although OSMs building field has a house value, the coverage in your area - as in ours - may not be
complete. In our test region, it is therefore more accurate to exclude all buildings which are defined as anything
other than house. You may decide to simply include buildings which are defined as house and all other values
that have not a clear meaning like yes.
To create the roads layer, build this query against OSMs lines layer:
592
You should end up with a map which looks similar to the following:
Back to text
593
20.9.2
594
To this:
The first image shows the buffer with the Segments to approximate value set to 5 and the second shows the value
set to 20. In our example, the difference is subtle, but you can see that the buffers edges are smoother with the
higher value.
Back to text
595
20.9.3
596
597
Your map should now show only those buildings which are within 50m of a road, 1km of a school and 500m of a
restaurant:
598
Back to text
Calculate Aspect
599
600
Your result:
Back to text
20.10.2
601
For the 5 degree version, replace the 2 in the expression and file name with 5.
Your results:
2 degrees:
602
5 degrees:
603
Back to text
Raster to Vector
Open the Query Builder by right-clicking on the all_terrain layer in the Layers list, select the General tab.
Then build the query "suitable" = 1.
Click OK to filter out all the polygons where this condition isnt met.
When viewed over the original raster, the areas should overlap perfectly:
static/training_manual/c
You can save this layer by right-clicking on the all_terrain layer in the Layers list and choosing Save As...,
then continue as per the instructions.
Back to text
604
20.11.2
You may notice that some of the buildings in your new_solution layer have been sliced by the Intersect
tool. This shows that only part of the building - and therefore only part of the property - lies on suitable terrain.
We can therefore sensibly eliminate those buildings from our dataset
Back to text
20.11.3
605
If it is greater than 100 meters in radius, then subtracting 100 meters from its size (from all directions) will result
in a part of it being left in the middle.
Therefore, you can run an interior buffer of 100 meters on your existing suitable_terrain vector layer. In the
output of the buffer function, whatever remains of the original layer will represent areas where there is suitable
terrain for 100 meters beyond.
To demonstrate:
Go to Vector Geoprocessing Tools Buffer(s) to open the Buffer(s) dialog.
606
Use the suitable_terrain layer with 10 segments and a buffer distance of -100. (The distance is automatically in meters because your map is using a projected CRS.)
Save
the
output
in
exercise_data/residential_development/
suitable_terrain_continuous100m.shp.
as
If necessary, move the new layer above your original suitable_terrain layer.
Your results will look like something like this:
607
Now use the Select by Location tool (Vector Research Tools Select by location).
Set up like this:
608
under
exercise_data/residential_development/
as
Back to text
Your map should look like this (you may need to re-order the layers):
609
Back to text
20.12.2
Use the same approach as before to add the new server and the appropriate layer as hosted on that server:
610
If you zoom into the Swellendam area, youll notice that this dataset has a low resolution:
Therefore, its better not to use this data for the current map. The Blue Marble data is more suitable at global or
national scales.
Back to text
20.12.3
You may notice that many WMS servers are not always available. Sometimes this is temporary, sometimes it is
permanent. An example of a WMS server that worked at the time of writing is the World Mineral Deposits WMS
611
For our theoretical address table, we might want to store the following properties:
House Number
Street Name
Suburb Name
City Name
Postcode
Country
When creating the table to represent an address object, we would create columns to represent each of these properties and we would name them with SQL-compliant and possibly shortened names:
house_number
street_name
suburb
city
postcode
country
Back to text
20.13.2
The major problem with the people table is that there is a single address field which contains a persons entire
address. Thinking about our theoretical address table earlier in this lesson, we know that an address is made up
of many different properties. By storing all these properties in one field, we make it much harder to update and
query our data. We therefore need to split the address field into the various properties. This would give us a table
which has the following structure:
id |
name
| house_no | street_name
|
city
|
phone_no
--+---------------+----------+----------------+------------+----------------1 | Tim Sutton
|
3
| Buirski Plein | Swellendam | 071 123 123
2 | Horst Duester |
4
| Avenue du Roix | Geneva
| 072 121 122
Note: In the next section, you will learn about Foreign Key relationships which could be used in this example to
further improve our databases structure.
Back to text
20.13.3
612
id |
name
| house_no | street_id | phone_no
---+--------------+----------+-----------+------------1 | Horst Duster |
4 |
1 | 072 121 122
The street_id column represents a one to many relationship between the people object and the related street
object, which is in the streets table.
One way to further normalise the table is to split the name field into first_name and last_name:
id | first_name | last_name | house_no | street_id | phone_no
---+------------+------------+----------+-----------+-----------1 |
Horst
|
Duster
|
4
|
1
| 072 121 122
We can also create separate tables for the town or city name and country, linking them to our people table via one
to many relationships:
id | first_name | last_name | house_no | street_id | town_id | country_id
---+------------+-----------+----------+-----------+---------+-----------1 |
Horst
|
Duster |
4
|
1
|
2
|
1
Back to text
20.13.4
The schema for the table (enter d people) looks like this:
613
Table "public.people"
Column
|
Type
|
Modifiers
-----------+-----------------------+------------------------------------id
| integer
| not null default
|
| nextval(people_id_seq::regclass)
name
| character varying(50) |
house_no
| integer
| not null
street_id | integer
| not null
phone_no
| character varying
|
Indexes:
"people_pkey" PRIMARY KEY, btree (id)
Note: For illustration purposes, we have purposely omitted the fkey constraint.
Back to text
20.13.5
The reason the DROP command would not work in this case is because the people table has a Foreign Key
constraint to the streets table. This means that dropping (or deleting) the streets table would leave the people table
with references to non-existent streets data.
Note: It is possible to force the streets table to be deleted by using the CASCADE command, but this would
also delete the people and any other table which had a relationship to the streets table. Use with caution!
Back to text
20.13.6
The SQL command you should use looks like this (you can replace the street name with a name of your choice):
insert into streets (name) values (Low Road);
Back to text
20.13.7
If you look at the streets table again (using a select statement as before), youll see that the id for the Main
Road entry is 2.
Thats why we could merely enter the number 2 above. Even though were not seeing Main Road written out
fully in the entry above, the database will be able to associate that with the street_id value of 2.
Note: If you have already added a new street object, you might find
that the new Main Road has an ID of 3 not 2.
Back to text
614
20.13.8
Result:
count |
name
------+------------1 | Low Street
2 | High street
1 | Main Road
(3 rows)
Note: You will notice that we have prefixed field names with table names
(e.g. people.name and streets.name). This needs to be done whenever the field name is ambiguous (i.e. not
unique across all tables in the database).
Back to text
The units being used by the example query are degrees, because the CRS that the layer is using is WGS 84. This
is a Geographic CRS, which means that its units are in degrees. A Projected CRS, like the UTM projections, is in
meters.
Remember that when you write a query, you need to know which units the layers CRS is in. This will allow you
to write a query that will return the results that you expect.
Back to text
20.14.2
Back to text
Creating Linestrings
615
Back to text
20.15.2
Linking Tables
then it means that while experimenting with creating polygons for the cities table, you must have deleted some of
them and started over. Just check the entries in your cities table and use any id which exists.
Back to text
616
Populating Tables
Back to text
20.16.2
Back to text
20.16.3
Adding Geometry
select people.name,
streets.name as street_name,
st_astext(people.the_geom) as geometry
from
streets, people
where people.street_id=streets.id;
Result:
name
| street_name |
geometry
--------------+-------------+--------------Roger Jones | High street |
Sally Norman | High street |
Jane Smith
| Main Road
|
Joe Bloggs
| Low Street |
Fault Towers | Main Road
| POINT(33 -33)
(5 rows)
As you can see, our constraint allows nulls to be added into the database.
Back to text
617
618
CAPITOLUL 21
Indici i tabele
genindex
modindex
search
619