Mastering QGIS (Làm chủ QGIS) - Kurt Menke, Dr. Richard Smith

LI
lienhejb
0 downloads
486
0

Generating preview...

Document content

Page 2

Mastering QGIS Second Edition Go beyond the basics and unleash the full power of QGIS with practical, step-by-step examples Kurt Menke, GISP Dr. Richard Smith Jr., GISP Dr. Luigi Pirelli Dr. John Van Hoesen, GISP BIRMINGHAM - MUMBAI www.allitebooks.com

Page 3

Mastering QGIS Second Edition Copyright © 2016 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, and its dealers and distributors will be held liable for any damages caused or alleged to be 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. First published: March 2015 Second edition: September 2016 Production reference: 1270916 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78646-037-0 www.packtpub.com www.allitebooks.com

Page 4

Credits Authors Project Coordinator Kurt Menke, GISP Nidhi Joshi Dr. Richard Smith Jr., GISP Dr. Luigi Pirelli Proofreader Safis Editing Dr. John Van Hoesen, GISP Indexer Reviewer Mariammal Chettiyar Paolo Corti Graphics Commissioning Editor Disha Haria Veena Pagare Production Coordinator Acquisition Editor Nilesh Mohite Vinay Argekar Cover Work Content Development Editor Nilesh Mohite Aishwarya Pandere Technical Editor Suwarna Patil Copy Editors Vikrant Phadke Safis Editing www.allitebooks.com

Page 6

Foreword Since the QGIS project was started in 2002 by Gary Sherman, the project has done what many people would have thought to be impossible—provide a viable alternative to the incumbent de facto GIS applications that is free, open source, and accessible to everyone on this planet who owns a computer. One of the joys of contributing to and participating in the QGIS project has been watching how the community of people and the body of knowledge have grown up around the project. There are few better examples of this than the work Kurt, Rick, John, and Luigi (the four co-authors of this book) have put into Mastering QGIS. Writing a book about QGIS is no easy task, partly because of the sheer depth of functionality that QGIS offers, and partly because of the fact that QGIS is rapidly evolving. In creating this second edition of Mastering QGIS, the authors have added some great new content to cover some of the important new functionality that has been added to QGIS. They have also revised the existing content to ensure thay everything is still fresh and relevant to QGIS 2.14. Having such an in-depth book about QGIS really lowers the barrier of entry to learning, and mastering, QGIS. I am looking forward to the day when GIS is used as commonly and routinely as a spreadsheet or a word processor. Being able to visualize and analyze the world around us is the most basic necessity if we are to promote good stewardship of the earth, responsible decision making, and humane and equitable conditions for the people that inhabit the earth. QGIS is a key element in this equation—being free and open source software really does make QGIS something that can be used by anyone on this planet who has access to a computer. Mastering QGIS provides a fantastic resource for those who wish to take full advantage of the opportunity offered by QGIS by having someone to guide them in their learning endeavors. www.allitebooks.com

Page 7

As you delve deeper into the world of QGIS and all the great things that the authors have to show you, take a moment to join our community (http://qgis.org), or start your own local community in your country. We welcome your participation and contribution of ideas, bug reports, code, and documentation, and we welcome you to the diverse and ever-growing group of people all around the world who are using QGIS to make the world a better place! Happy QGISing! Tim Sutton, QGIS Project Chairman www.allitebooks.com

Page 8

About the Authors A former archaeologist, Kurt Menke, GISP is based out of Albuquerque, New Mexico, USA. He received a master's degree in geography from the University of New Mexico in 2000. That same year, he founded Bird's Eye View (http://www.BirdsEyeViewGIS.com) to apply his expertise with GIS technology towards ecological conservation. Along with conservation, his other focus areas are public health and education. He is an avid open source GIS proponent since he began writing MapServer applications in 2002. He has used QGIS since 2007. Recently he also authored Discover QGIS for Locate Press. In 2015, Kurt was honored to be voted in as an OsGeo Charter Member. He is an experienced 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. Kurt authors an award winning blog on FOSS4G technologies and their use in community health mapping (http://communityhealthmaps.nlm.nih.gov/). Special thanks go to the QGIS development team and the QGIS community for their continued dedication and work that makes QGIS what it is. I would like to thank all my co-authors for being such a joy to work with. I'd also like to acknowledge Karl Benedict for originally introducing me to the world of FOSS4G and Jeffery Cavner for his ongoing camaraderie. Finally I'd like to thank my beautiful wife, Sarah, for her steady support and encouragement. www.allitebooks.com

Page 9

Dr. 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 PhD in geography from the University of Georgia and holds a master of science in computer science and a bachelor of science 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 FOSS4G curriculum. He is also one of the co-authors of the FOSS4G Academy (http://foss4geo.org). He has collaborated with other writers in his field, but Mastering QGIS is his first book effort. I would like to thank my wife and daughter for putting up with my late-night and weekend writing sessions. I would also like to thank my co-author Kurt Menke for being patient with my edits. Outside those directly involved or affected by the writing of the book, I'd like to thank my academic and life mentors Dr. Stacey Lyle, Dr. Thomas Hodler, Dr. Gary Jeffress, and Dr. Robin Murphy for providing support and well-wishes as I work through the beginnings of my career. In addition to teaching me more than you know, you have inspired me to have the confidence to teach and write. To those of you reading this, I hope I do my mentors justice by providing a clear and useful text to assist you in Mastering QGIS. Dr. Luigi Pirelli is a freelance software analyst and developer with a degree with honors in computer science at Bari University. He worked for 15 years in the Satellite Ground Segment and Direct Ingestion for the European Space Agency. Since 2006, he has been involved in the GFOSS world, contributing in QGIS, GRASS, and Mapserver core, and developing and maintaining many QGIS plugins. He actively participates in QGIS Hackmeetings. Luigi is the founder of the OSGEO Italian local GFOSS chapter. Now, he lives in Spain contributing to this GFOSS community. During the last few years, he started teaching PyQGIS, organizing training from basic to advanced level, and supporting companies to develop their specific QGIS plugins. www.allitebooks.com

Page 10

He is also the founder the local hackerspace group Bricolabs.cc, focused on open hardware. He likes cycling, repairing everything, and training groups on conflict resolution. Other than this book, he has also contributed to the Lonely Planet guide Cycling Italy. A special thanks to the QGIS developer community and core developers because the project is managed in a really open way, allowing contribution from everyone. I want to thank everyone I worked with. From every one of them I learned, and without them, I wouldn't be here making my contributions to free software and this book. Special thanks to my friends and neighbors who helped me with my son during writing this pages. I would like to dedicate this book to my partner and especially to my son for the patience he had seeing me hours sitting in front of a computer without playing with him. Dr. 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 PhD in geology from the University of Nevada, Las Vegas, in 2000 and 2003. He is a certified GIS Professional (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 some variants of FOSS GIS since 2003 and over the last three years has taught graduate, undergraduate, and continuing education courses using only FOSS GIS software. www.allitebooks.com

Page 11

About the Reviewer Paolo Corti is an environmental engineer based in Cambridge, USA. He has more than 15 years of experience in the GIS field. After working with proprietary solutions for some years, he proudly moved to open source technologies and Python since almost a decade. He is a Geospatial Engineer Fellow at the Center for Geographic Analysis of the University of Harvard. In the past, he has worked as a software architect, developer and analyst for organizations such as UN World Food Programme, EU Joint Research Centre, World Bank, and the Italian government. He is an OSGeo Charter member and a member of the pycsw and GeoNode Project Steering Committees. He is the co-author of PostGIS Cookbook by Packt, and writes a popular blog on open source geospatial technologies at http://www. paolocorti.net.

Page 12

www.PacktPub.com eBooks, discount offers, and more 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.PacktPub. com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at customercare@packtpub.com for more details. At www.PacktPub.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. TM https://www2.packtpub.com/books/subscription/packtlib Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books. Why subscribe? • Fully searchable across every book published by Packt • Copy and paste, print, and bookmark content • On demand and accessible via a web browser

Page 14

Table of Contents Preface xi Chapter 1: A Refreshing Look at QGIS 1 Release schedules 2 QGIS download and installation 2 Installing QGIS on Windows 2 Installing QGIS on Mac OS X 2 Installing QGIS on Ubuntu Linux 3 Installing QGIS only 3 Installing QGIS and other FOSSGIS packages 4 Installing QGIS on a Chromebook 4 Tour of QGIS 5 Familiarizing yourself with QGIS Desktop 5 User interface themes 6 Familiarizing yourself with QGIS Browser 8 Loading data into QGIS Desktop 9 Loading vector data 9 Loading raster data 10 Loading databases 11 Loading web services 12 Working with CRS 14 Working with tables 15 Creating table joins 16 Editing data 19 Snapping 20 Styling vector data 21 Styling raster data 22 Enhancing raster display with contrast enhancement 24 Blending modes 25 [ i ]

Page 15

Table of Contents Composing maps 28 Adding functionality with plugins 29 Custom QGIS variables 31 Using Project level variables in the Print Composer 32 Using variables in data defined overrides 34 Using Layer level variables 35 Summary 37 Chapter 2: Creating Spatial Databases 39 Fundamental database concepts 39 Describing database tables 40 Knowing about table relationships 41 Reviewing the basics of Structured Query Language 41 Creating a spatial database 42 Importing data into a SpatiaLite database 45 Importing KML into SpatiaLite 45 Importing a shapefile into SpatiaLite 47 Importing tables into SpatiaLite 48 Exporting tables out of SpatiaLite 53 Managing tables 54 Creating a new table 54 Renaming a table 56 Editing table properties 56 Deleting a table 58 Emptying a table 58 Creating queries and views 58 Creating a SQL query 59 Creating a spatial view 60 Dropping a spatial view 62 Summary 62 Chapter 3: Styling Raster and Vector Data 63 Choosing and managing colors 64 Always-available color picker components 65 Changeable panels in color picker 66 Color ramp 66 Color wheel 67 Color swatches 67 Color sampler 69 Color picker components available in the Layers panel 70 Managing color ramps 71 Managing the QGIS color ramp collection 72 Renaming a color ramp 73 Removing a color ramp 73 [ ii ]

Page 16

Table of Contents Exporting a color ramp 73 Importing a color ramp 74 Adding a color ramp 75 Editing a color ramp 79 Styling singleband rasters 79 Paletted raster band rendering 80 Singleband gray raster band rendering 81 Singleband pseudocolor raster band rendering 84 Styling multiband rasters 87 Creating a raster composite 89 Raster color rendering 90 Raster resampling 93 Styling vectors 94 Single-symbol vector styling 94 Categorized vector styling 96 Graduated vector styling 97 Rule-based vector styling 99 Point-displacement vector styling 102 Inverted polygons vector styling 104 Heatmap vector styling 106 2.5 D vector styling 108 Vector layer rendering 111 Layer transparency 111 Layer blending mode 111 Feature blending mode 112 Control feature rendering order 113 Using diagrams to display thematic data 114 Parameters common to all diagram types 115 Diagram size parameters 115 Diagram placement parameters 116 Adding attributes to diagrams 118 Creating a pie chart diagram 118 Creating a text diagram 120 Creating a histogram chart diagram 122 Saving, loading, and setting default styles 124 Saving a style 125 Loading a style 125 Setting and restoring a default style 125 Adding and renaming styles in the current QGIS project 126 Summary 126 [ iii ]

Page 17

Table of Contents Chapter 4: Preparing Vector Data for Processing 127 Merging shapefiles 128 Creating spatial indices 129 Checking for geometry errors 130 Check Geometries plugin 131 Converting vector geometries 134 Creating polygon centroids 135 Converting polygons to lines and lines to polygons 136 Creating polygons surrounding individual points 136 Extracting nodes from lines and polygons 138 Simplifying and densifying features 138 Converting between multipart and singlepart features 140 Adding geometry columns to an attribute table 142 Using basic vector geoprocessing tools 143 Spatial overlay tools 144 Using the Clip and Difference tools 145 Using the Intersect and Symmetrical Difference tools 146 Overlaying polygon layers with Union 148 Creating buffers 148 Generating convex hulls 150 Dissolving features 151 Defining coordinate reference systems 152 Understanding the PROJ.4 definition format 152 Defining a new custom coordinate reference system 154 Viewing a statistical summary of vector layers 156 Advanced field calculations 159 Exploring the field calculator interface 159 Writing advanced field calculations 162 The first example – calculating and formatting current date 162 The second example – calculating with geometry 163 The third example – calculating a population-dependent label string 164 Conditional formatting for attribute table cells 167 Complex spatial and aspatial queries 171 Summary 175 Chapter 5: Preparing Raster Data for Processing 177 Reclassifying rasters for analysis 178 Using the Slicer plugin 179 Converting datasets from floating point to integer rasters 181 Resampling rasters for analysis 182 Rescaling raster values 185 Aligning raster pixels 186 [ iv ]

Page 18

Table of Contents Creating a raster mosaic 188 Generating raster overviews (pyramids) 191 Converting between raster and vector data models 194 Converting from raster to vector 194 Converting from vector to raster (rasterize) 196 Creating raster surfaces via interpolation 197 Summary 200 Chapter 6: Advanced Data Creation and Editing 201 Creating points from coordinate data 202 Mapping well-known text representations of geometry 206 Geocoding address-based data 209 How address geocoding works 211 The first example – geocoding using web services 212 The second example – geocoding using local street network data 214 Georeferencing imagery 218 Understanding ground control points 218 Using the Georeferencer GDAL plugin 219 The first example – georeferencing using a second dataset 223 Getting started 223 Entering ground control points 223 Transformation settings 226 Completing the operation 232 The second example – georeferencing using a point file 232 Checking the topology of vector data 236 Installing the topology checker 236 Topological rules 236 Rules for point features 237 Rules for line features 237 Rules for polygon features 238 Using the topology checker 239 Repairing topological errors via topological editing 243 Example 1 – resolving duplicate geometries 244 Example 2 – repairing overlaps 245 Setting the editing parameters 245 Repairing an overlap between polygons 247 Example 3 – repairing a gap between polygons 249 Summary 250 Chapter 7: Advanced Data Visualization 251 Learn to use live layer effects 252 Creating beautiful effects with inverted polygon shapeburst fills 256 Creating coastal vignettes 257 Study area mask 259 [ v ]

Page 19

Table of Contents Creating 3D views with QGIS2ThreeJS 262 Creating 3D views of terrain 262 Extruding features 265 Creating an Atlas 269 Basic Atlas configuration 270 Dynamic titles 272 Dynamic legends 274 Highlighting the coverage feature 274 Summary 276 Chapter 8: The Processing Toolbox 277 About the Processing Toolbox 278 Configuring the Processing Toolbox 278 Understanding the Processing Toolbox 281 Using the Processing Toolbox 285 Performing raster analyses with GRASS 285 Calculating shaded relief 287 Calculating the least-cost path 289 Calculating the slope using r.slope 290 Reclassifying the new slope raster and the land use raster 291 Combining the reclassified slope and land use layers 292 Calculating the cumulative cost raster using r.cost 294 Calculating the cost path using LCP 295 Evaluating a viewshed 297 Clipping the elevation to the boundary of the park using GDAL 298 Calculating viewsheds for towers using r.viewshed 299 Combining viewsheds using r.mapcalculator 301 Calculating raster statistics using r.stats 302 SAGA 304 Evaluating a habitat 304 Calculating elevation ranges using the SAGA Raster calculator 305 Clipping land use to the park boundary using Clip grid with polygon 306 Querying land use for only surface water using SAGA Raster calculator 306 Finding proximity to surface water using GDAL Proximity 307 Querying the proximity for 1,000 meters of water using GDAL Raster calculator 308 Reclassifying land use using the Reclassify grid values tool 310 Combining raster layers using SAGA Raster calculator 311 Exploring hydrologic analyses with TauDEM 313 Removing pits from the DEM 314 Calculating flow directions across the landscape 315 Calculating the upstream area above Fort Klamath 316 Calculating a stream network raster grid 317 Creating a watershed-specific vector stream network 318 [ vi ]

Page 20

Table of Contents R 320 Exploring summary statistics and histograms 320 LAStools and Fusion 327 Exploring LIDAR data 327 Converting LAS/LAZ files 332 Creating surfaces from LIDAR data 333 Summary 338 Chapter 9: Automating Workflows with the Graphical Modeler 339 An introduction to the graphical modeler 340 Opening the graphical modeler 340 Configuring the modeler and naming a model 342 Adding data inputs to your model 345 Adding algorithms to your model 348 Running a model 353 Editing a model 356 Documenting a model 358 Saving, loading, and exporting models 360 Executing model algorithms iteratively 362 Nesting models 364 Using batch processing with models 368 Converting a model into a Python script 369 Summary 370 Chapter 10: Creating QGIS Plugins with PyQGIS and Problem Solving 371 Webography – where to get API information and PyQGIS help 371 PyQGIS cookbook 372 API documentation 372 The QGIS community, mailing lists, and IRC channel 373 Mailing lists 373 IRC channel 374 The Stack Exchange community 374 Sharing your knowledge and reporting issues 374 The Python Console 376 Getting sample data 377 My first PyQGIS code snippet 377 My second PyQGIS code snippet – looping the layer features 378 Exploring iface and QGis 378 Exploring a QGIS API in the Python Console 380 Creating a plugin structure with Plugin Builder 380 Installing Plugin Builder 381 Locating plugins 381 [ vii ]

Showing the first 20 pages. Download the document to see it all.

Comments (0)

Sign in to join the discussion

No comments yet. Be the first!