Mastering Geospatial Development with QGIS 3.x Third Edition An in-depth guide to becoming proficient in spatial data analysis using QGIS 3.4 and 3.6 with Python Shammunul Islam Simon Miles Kurt Menke, GISP Richard Smith Jr., GISP Luigi Pirelli John Van Hoesen, GISP BIRMINGHAM - MUMBAI Mastering Geospatial Development with QGIS 3.x Third Edition Copyright © 2019 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. Commissioning Editor: Richa Tripathi Acquisition Editor: Yogesh Deokar Content Development Editor: Unnati Guha Technical Editor: Dinesh Chaudhary Copy Editor: Safis Editing Project Coordinator: Manthan Patel Proofreader: Safis Editing Indexer: Priyanka Dhadke Graphics: Jisha Chirayil Production Coordinator: Aparna Bhagat First published: March 2015 Second edition: September 2016 Third edition: March 2019 Production reference: 1270319 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78899-989-2 www.packtpub.com mapt.io Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website. Why subscribe? Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals Improve your learning with Skill Plans built especially for you Get a free eBook or video every month Mapt is fully searchable Copy and paste, print, and bookmark content Packt.com Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.packt.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details. At www.packt.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks. Contributors About the authors Shammunul Islam is a consulting spatial data scientist at the Institute of Remote Sensing, Jahangirnagar University, and a senior consultant at ERI, Bangladesh. He develops applications for automating geospatial and statistical analysis in different domains such as in the fields of the environment, climate, and socio-economy. He also consults as a survey statistician and provides corporate training on data science to businesses. Shammunul holds an MA in Climate and Society from Columbia University, an MA in development studies, and a BSc in statistics. Simon Miles is a GIS/web developer working for the Royal Borough of Windsor and Maidenhead, and has been working in the GIS industry since 2003. Both in the office and at home, he works using open source technologies. His main kit or stack includes QGIS, PostGIS, Python, GDAL, Leaflet, JavaScript, and PHP. His main focus presently is on digital delivery/transformation, chiefly through the consolidation of data, applications, and legacy systems, and replacing these with APIs and web applications. In 2013, Simon helped to establish the UK QGIS user group, which holds regional meetings two or three times a year. Kurt Menke, GISP, a former Albuquerque, New Mexico, USA-based archaeologist, has an MS in geography from the University of New Mexico in 2000. He has founded Bird's Eye View to apply his expertise with GIS technology toward ecological conservation. His other focus areas are public health and education. He is an avid GIS proponent since he began writing MapServer applications in 2002. He has authored Discover QGIS. In 2015, Kurt was voted in as an OSGeo Charter Member. He is a FOSS4G educator and a co-author of the GeoAcademy. In 2015, he was awarded the Global Educator of the Year Team Award by GeoForAll as part of the GeoAcademy team. He authors an award-winning blog on FOSS4G technologies and their use in community health mapping. Richard Smith Jr., GISP, is an assistant professor of geographic information science in the School of Engineering and Computing Sciences at Texas A&M University – Corpus Christi. He has a Ph.D. in geography from the University of Georgia and holds an MSc in computer science and a BSc in geographic information science from Texas A&M University-Corpus Christi. Richard actively researches in cartography, systems integration, and the use of geospatial technology for disaster response. He is an advocate for FOSS4G and building a FOSS4G curriculum. He is also one of the co-authors of the FOSS4G Academy. Luigi Pirelli is a QGIS core developer and software analyst with a degree in computer science from Bari University. He worked for 15 years in the Satellite Ground Segment and Direct Ingestion for the European Space Agency. He is also involved in GFOSS world, contributing in QGIS, GRASS, & MapServer core, and developing and maintaining many QGIS plugins. Luigi is the founder of the OSGEO Italian local GFOSS chapter. He has taught PyQGIS, delivering training from basic to advanced levels, and supporting companies to develop their own specific QGIS plugins. He founded a local hackerspace group. Bricolabs.cc. He likes training groups on conflict resolution. Other than this book, he has also contributed to the Lonely Planet guide Cycling Italy. John Van Hoesen, GISP, is an associate professor of geology and environmental studies at Green Mountain College in rural west-central Vermont, USA. He earned an MS and a Ph.D. in geology from the University of Nevada, Las Vegas, in 2000 and 2003. He is a certified GISP with a broad background in the geosciences and has used some flavor of GIS to evaluate and explore geological processes and environmental issues since 1997. John has used and taught graduate, undergraduate, and continuing education courses using some variants of FOSS GIS since 2003. About the reviewers Giuseppe De Marco, a Ferentino based agricultural engineer, has a Bachelor's in agriculture from the University of Pisa. He started programming at an early age. He developed deep interests in geography and GIS during his bachelor’s. He then got introduced to GRASS and QGIS while working with Eris commercial products. Since QGIS 1.7.4, he has been developing plugins for it, In 2008, he partnered with his 2 colleagues and started Pienocampo (open field), a website that hosts plugins made by him which are also hosted on OGIS official repository. He likes studying geography, surveying, tree risk assessment, landscaping, bioengineering, and farm consulting. He also likes imparting knowledge on how to use QGIs and other open source software. Chima Obi is the lead geospatial analyst at AGERPoint Inc. He has over 4 years of experience as a geospatial analyst. His specialty includes processing LIDAR data and feature extraction from raster/imagery data utilizing Python and R programing, as well as exploring other open source geospatial tools. He got his Bachelors in soil science from the Federal University of Technology, Owerri, Nigeria in 2010. He moved to the United States where he obtained his master’s degree in environmental science and got a certification in Geospatial information systems in 2016. Prior to working at AGERPoint, he worked as a geospatial analyst at West Virginia District of Highways in 2015 and 2016. I would like to express my gratitude to my friends and family, and most importantly to my wife, for their wonderful encouragement and support. Also, my biggest thanks go to Packt Publishing for choosing me to be a part of this awesome book review. Packt is searching for authors like you If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea. Table of Contents Preface 1 Section 1: Introduction Chapter 1: A Refreshing Look at QGIS 7 Release schedules 8 QGIS downloading and installation 8 Installing QGIS on Windows 8 Installing QGIS on macOS 9 Installing QGIS on Ubuntu Linux 9 Installing QGIS only 9 Installing QGIS and other FOSSGIS packages 10 QGIS on Android 10 Installing QGIS on a Chromebook 11 QGIS in a browser 11 What's new? 11 A tour of QGIS 14 Familiarizing yourself with QGIS Desktop 14 Loading data into QGIS Desktop 15 Loading vector data 16 Loading raster data 17 Loading databases 19 Loading web services 19 Working with CRS 20 Working with tables 20 Creating table joins 22 Editing data 24 Snapping 25 Styling vector data 26 Styling raster data 28 Blending modes 30 Composing maps 30 Adding functionality with plugins 31 Custom QGIS variables 33 Summary 34 Section 2: Getting Started Chapter 2: Styling Raster and Vector Data 36 Choosing and managing colors 36 Table of Contents Knowing color picker components 39 Changeable panels in the color picker 40 Color ramp 40 Color wheel 41 Color swatches 41 Color sampler 43 Color picker components in the Layers panel 44 Managing color ramps 46 Managing the QGIS color ramp collection 46 Renaming a color ramp 48 Removing a color ramp 48 Exporting a color ramp 48 Importing a color ramp 49 Adding a color ramp 50 Adding a gradient color ramp 50 Adding a random color ramp 52 Adding a ColorBrewer color ramp 53 Adding a cpt-city color ramp 54 Editing a color ramp 56 Styling singleband rasters 56 Paletted raster band rendering 56 Singleband gray raster band rendering 58 Singleband pseudocolor raster band rendering 61 Singleband pseudocolor interpolations 64 Styling multiband rasters 65 Raster color rendering 65 Raster resampling 68 Styling vectors 69 Single-symbol vector styling 70 Categorized vector styling 73 Graduated vector styling 74 Rule-based vector styling 76 Point-displacement vector styling 79 Point cluster vector styling 81 Inverted polygons vector styling 81 Heatmap vector styling 83 2.5 D vector styling 85 Vector layer rendering 88 Layer transparency 88 Layer blending mode 88 Feature blending mode 89 Control feature rendering order 90 Using diagrams to display thematic data 91 Parameters that are common to all diagram types 92 Diagram size parameters 92 Diagram placement parameters 93 Adding attributes to diagrams 95 [ ii ] Table of Contents Creating a pie chart diagram 96 Creating a text diagram 98 Creating a histogram chart diagram 99 Saving, loading, and setting default styles 100 Saving a style 101 Loading a style 101 Setting and restoring a default style 101 Adding and renaming styles in the current QGIS project 102 Summary 102 Chapter 3: Creating Spatial Databases 103 Fundamental database concepts 104 Describing database tables 104 Knowing about table relationships 105 Reviewing the basics of the structured query language 105 Creating a spatial database 106 Connecting to a GeoPackage 108 Importing data into a GeoPackage 108 Importing a vector file 108 Importing a layer from map canvas 109 Working with tables 110 Creating a new table within an existing GeoPackage 110 Renaming a table 111 Editing an existing table field/column 111 Deleting a GeoPackage table 112 Exporting tables out of a GeoPackage 112 Creating queries and views 113 Using select statements 113 Creating a spatial view 114 Dropping a spatial view 114 Summary 115 Chapter 4: Preparing Vector Data for Processing 116 Merging vectors 117 Converting vector geometries 119 Creating polygon centroids 119 Converting lines into polygons 120 Converting polygons into lines 121 Creating polygons surrounding individual points 122 Voronoi polygons 122 Delaunay triangulation 125 Extracting nodes (vertices) 127 Simplifying and densifying features 128 Converting between multipart and singleparts features 129 Adding geometry columns to an attribute table 130 [ iii ]