You are previewing Mastering QGIS - Second Edition.
O'Reilly logo
Mastering QGIS - Second Edition

Book Description

Go beyond the basics and unleash the full power of QGIS with practical, step-by-step examples

About This Book

  • This book is your one-stop solution to all of your GIS needs using the open source QGIS

  • Master QGIS by learning about database integration, geoprocessing tools, Python scripts, advanced cartography, and custom plugins

  • This example-rich, practical guide will help you create sophisticated analyses and maps

  • Who This Book Is For

    If you are a GIS professional, a consultant, a student, or perhaps a fast learner who wants to go beyond the basics of QGIS, then this book is for you. It will prepare you to realize the full potential of QGIS.

    What You Will Learn

  • Create and manage a spatial database

  • Get to know advanced techniques to style GIS data

  • Prepare both vector and raster data for processing

  • Add heat maps, live layer effects, and labels to your maps

  • Master LAStools and GRASS integration with the Processing Toolbox

  • Edit and repair topological data errors

  • Automate workflows with batch processing and the QGIS Graphical Modeler

  • Integrate Python scripting into your data processing workflows

  • Develop your own QGIS plugin

  • In Detail

    QGIS is an open source solution to GIS. It is widely used by GIS professionals all over the world. It is the leading alternative to the proprietary GIS software. Although QGIS is described as intuitive, it is also by default complex. Knowing which tools to use and how to apply them is essential to producing valuable deliverables on time.

    Starting with a refresher on the QGIS basics, this book will take you all the way through to creating your first custom QGIS plugin. From the refresher, we will recap how to create, populate, and manage a spatial database. You’ll also walk through styling GIS data, from creating custom symbols and color ramps to using blending modes. In the next section, you will discover how to prepare vector, heat maps, and create live layer effects, labeling, and raster data for processing. You’ll also discover advanced data creation and editing techniques.

    The last third of the book covers the more technical aspects of QGIS such as using LAStools and GRASS GIS’s integration with the Processing Toolbox, how to automate workflows with batch processing, and how to create graphical models. Finally, you will see how to create and run Python data processing scripts and write your own QGIS plugin with pyqgis.

    By the end of the book, you will understand how to work with all the aspects of QGIS, and will be ready to use it for any type of GIS work.

    Style and approach

    This step-by-step comprehensive guide will let you dig your teeth into working with spatial databases, creating your own QGIS plugins, and creating your own custom graphical models.

    Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at If you purchased this book elsewhere, you can visit and register to have the code file.

    Table of Contents

    1. Mastering QGIS - Second Edition
      1. Mastering QGIS - Second Edition
      2. Credits
      3. Foreword
      4. About the Authors
      5. About the Reviewer
        1. eBooks, discount offers, and more
          1. Why subscribe?
      7. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Downloading the example code
          2. Downloading the color images of this book
          3. Errata
          4. Piracy
          5. Questions
      8. 1. A Refreshing Look at QGIS
        1. Release schedules
        2. QGIS download and installation
          1. Installing QGIS on Windows
          2. Installing QGIS on Mac OS X
          3. Installing QGIS on Ubuntu Linux
            1. Installing QGIS only
            2. Installing QGIS and other FOSSGIS packages
            3. Installing QGIS on a Chromebook
        3. Tour of QGIS
          1. Familiarizing yourself with QGIS Desktop
          2. User interface themes
          3. Familiarizing yourself with QGIS Browser
        4. Loading data into QGIS Desktop
          1. Loading vector data
          2. Loading raster data
          3. Loading databases
          4. Loading web services
        5. Working with CRS
        6. Working with tables
          1. Creating table joins
        7. Editing data
          1. Snapping
          2. Styling vector data
          3. Styling raster data
          4. Enhancing raster display with contrast enhancement
          5. Blending modes
        8. Composing maps
        9. Adding functionality with plugins
          1. Custom QGIS variables
            1. Using Project level variables in the Print Composer
            2. Using variables in data defined overrides
            3. Using Layer level variables
        10. Summary
      9. 2. Creating Spatial Databases
        1. Fundamental database concepts
          1. Describing database tables
          2. Knowing about table relationships
          3. Reviewing the basics of Structured Query Language
        2. Creating a spatial database
        3. Importing data into a SpatiaLite database
          1. Importing KML into SpatiaLite
          2. Importing a shapefile into SpatiaLite
          3. Importing tables into SpatiaLite
        4. Exporting tables out of SpatiaLite
        5. Managing tables
          1. Creating a new table
          2. Renaming a table
          3. Editing table properties
          4. Deleting a table
          5. Emptying a table
        6. Creating queries and views
          1. Creating a SQL query
          2. Creating a spatial view
          3. Dropping a spatial view
        7. Summary
      10. 3. Styling Raster and Vector Data
        1. Choosing and managing colors
          1. Always-available color picker components
          2. Changeable panels in color picker
            1. Color ramp
            2. Color wheel
            3. Color swatches
            4. Color sampler
          3. Color picker components available in the Layers panel
        2. Managing color ramps
          1. Managing the QGIS color ramp collection
            1. Renaming a color ramp
            2. Removing a color ramp
            3. Exporting a color ramp
            4. Importing a color ramp
            5. Adding a color ramp
              1. Adding a Gradient color ramp
              2. Adding a Random color ramp
              3. Adding a ColorBrewer color ramp
              4. Adding a cpt-city color ramp
            6. Editing a color ramp
        3. Styling singleband rasters
          1. Paletted raster band rendering
          2. Singleband gray raster band rendering
          3. Singleband pseudocolor raster band rendering
        4. Styling multiband rasters
        5. Creating a raster composite
        6. Raster color rendering
        7. Raster resampling
        8. Styling vectors
          1. Single-symbol vector styling
          2. Categorized vector styling
          3. Graduated vector styling
          4. Rule-based vector styling
          5. Point-displacement vector styling
          6. Inverted polygons vector styling
          7. Heatmap vector styling
          8. 2.5 D vector styling
        9. Vector layer rendering
          1. Layer transparency
          2. Layer blending mode
          3. Feature blending mode
          4. Control feature rendering order
        10. Using diagrams to display thematic data
          1. Parameters common to all diagram types
            1. Diagram size parameters
            2. Diagram placement parameters
            3. Adding attributes to diagrams
          2. Creating a pie chart diagram
          3. Creating a text diagram
          4. Creating a histogram chart diagram
        11. Saving, loading, and setting default styles
          1. Saving a style
          2. Loading a style
          3. Setting and restoring a default style
          4. Adding and renaming styles in the current QGIS project
        12. Summary
      11. 4. Preparing Vector Data for Processing
        1. Merging shapefiles
        2. Creating spatial indices
        3. Checking for geometry errors
          1. Check Geometries plugin
        4. Converting vector geometries
          1. Creating polygon centroids
          2. Converting polygons to lines and lines to polygons
          3. Creating polygons surrounding individual points
          4. Extracting nodes from lines and polygons
          5. Simplifying and densifying features
          6. Converting between multipart and singlepart features
          7. Adding geometry columns to an attribute table
        5. Using basic vector geoprocessing tools
          1. Spatial overlay tools
            1. Using the Clip and Difference tools
            2. Using the Intersect and Symmetrical Difference tools
            3. Overlaying polygon layers with Union
          2. Creating buffers
          3. Generating convex hulls
          4. Dissolving features
        6. Defining coordinate reference systems
          1. Understanding the PROJ.4 definition format
          2. Defining a new custom coordinate reference system
        7. Viewing a statistical summary of vector layers
        8. Advanced field calculations
          1. Exploring the field calculator interface
          2. Writing advanced field calculations
            1. The first example – calculating and formatting current date
            2. The second example – calculating with geometry
            3. The third example – calculating a population-dependent label string
        9. Conditional formatting for attribute table cells
        10. Complex spatial and aspatial queries
        11. Summary
      12. 5. Preparing Raster Data for Processing
        1. Reclassifying rasters for analysis
          1. Using the Slicer plugin
          2. Converting datasets from floating point to integer rasters
          3. Resampling rasters for analysis
        2. Rescaling raster values
        3. Aligning raster pixels
        4. Creating a raster mosaic
        5. Generating raster overviews (pyramids)
        6. Converting between raster and vector data models
          1. Converting from raster to vector
          2. Converting from vector to raster (rasterize)
        7. Creating raster surfaces via interpolation
        8. Summary
      13. 6. Advanced Data Creation and Editing
        1. Creating points from coordinate data
          1. Mapping well-known text representations of geometry
        2. Geocoding address-based data
          1. How address geocoding works
          2. The first example – geocoding using web services
          3. The second example – geocoding using local street network data
        3. Georeferencing imagery
          1. Understanding ground control points
          2. Using the Georeferencer GDAL plugin
          3. The first example – georeferencing using a second dataset
            1. Getting started
            2. Entering ground control points
            3. Transformation settings
            4. Completing the operation
            5. The second example – georeferencing using a point file
        4. Checking the topology of vector data
          1. Installing the topology checker
          2. Topological rules
            1. Rules for point features
            2. Rules for line features
            3. Rules for polygon features
          3. Using the topology checker
        5. Repairing topological errors via topological editing
          1. Example 1 – resolving duplicate geometries
          2. Example 2 – repairing overlaps
            1. Setting the editing parameters
            2. Repairing an overlap between polygons
          3. Example 3 – repairing a gap between polygons
        6. Summary
      14. 7. Advanced Data Visualization
        1. Learn to use live layer effects
        2. Creating beautiful effects with inverted polygon shapeburst fills
          1. Creating coastal vignettes
          2. Study area mask
        3. Creating 3D views with QGIS2ThreeJS
          1. Creating 3D views of terrain
          2. Extruding features
        4. Creating an Atlas
          1. Basic Atlas configuration
          2. Dynamic titles
          3. Dynamic legends
          4. Highlighting the coverage feature
        5. Summary
      15. 8. The Processing Toolbox
        1. About the Processing Toolbox
          1. Configuring the Processing Toolbox
          2. Understanding the Processing Toolbox
          3. Using the Processing Toolbox
        2. Performing raster analyses with GRASS
          1. Calculating shaded relief
          2. Calculating the least-cost path
            1. Calculating the slope using r.slope
            2. Reclassifying the new slope raster and the land use raster
            3. Combining the reclassified slope and land use layers
            4. Calculating the cumulative cost raster using r.cost
            5. Calculating the cost path using LCP
          3. Evaluating a viewshed
            1. Clipping the elevation to the boundary of the park using GDAL
            2. Calculating viewsheds for towers using r.viewshed
            3. Combining viewsheds using r.mapcalculator
            4. Calculating raster statistics using r.stats
        3. SAGA
          1. Evaluating a habitat
            1. Calculating elevation ranges using the SAGA Raster calculator
            2. Clipping land use to the park boundary using Clip grid with polygon
            3. Querying land use for only surface water using SAGA Raster calculator
            4. Finding proximity to surface water using GDAL Proximity
            5. Querying the proximity for 1,000 meters of water using GDAL Raster calculator
            6. Reclassifying land use using the Reclassify grid values tool
            7. Combining raster layers using SAGA Raster calculator
        4. Exploring hydrologic analyses with TauDEM
          1. Removing pits from the DEM
          2. Calculating flow directions across the landscape
          3. Calculating the upstream area above Fort Klamath
          4. Calculating a stream network raster grid
          5. Creating a watershed-specific vector stream network
        5. R
          1. Exploring summary statistics and histograms
        6. LAStools and Fusion
          1. Exploring LIDAR data
          2. Converting LAS/LAZ files
          3. Creating surfaces from LIDAR data
        7. Summary
      16. 9. Automating Workflows with the Graphical Modeler
        1. An introduction to the graphical modeler
        2. Opening the graphical modeler
        3. Configuring the modeler and naming a model
        4. Adding data inputs to your model
        5. Adding algorithms to your model
        6. Running a model
        7. Editing a model
        8. Documenting a model
        9. Saving, loading, and exporting models
        10. Executing model algorithms iteratively
        11. Nesting models
        12. Using batch processing with models
        13. Converting a model into a Python script
        14. Summary
      17. 10. Creating QGIS Plugins with PyQGIS and Problem Solving
        1. Webography – where to get API information and PyQGIS help
          1. PyQGIS cookbook
          2. API documentation
          3. The QGIS community, mailing lists, and IRC channel
            1. Mailing lists
            2. IRC channel
            3. The Stack Exchange community
            4. Sharing your knowledge and reporting issues
        2. The Python Console
          1. Getting sample data
          2. My first PyQGIS code snippet
        3. My second PyQGIS code snippet – looping the layer features
        4. Exploring iface and QGis
        5. Exploring a QGIS API in the Python Console
        6. Creating a plugin structure with Plugin Builder
          1. Installing Plugin Builder
          2. Locating plugins
          3. Creating my first Python plugin – TestPlugin
            1. Setting mandatory plugin parameters
            2. Setting optional plugin parameters
            3. Generating the plugin code
            4. Compiling the icon resource
            5. Plugin file structure – where and what to customize
              1. Exploring main plugin files
              2. Plugin Builder-generated files
        7. A simple plugin example
          1. Adding basic logic to TestPlugin
            1. Modifying the layout with Qt Designer
              1. Adding two pull-down menus
            2. Modifying GUI logic
            3. Modifying plugin logic
              1. Classifying layers
              2. Populating the combobox
                1. Understanding self
              3. Showing and running the dialog
              4. Some improvements
              5. More detail of the code
        8. Setting up a debugging environment
          1. What is a debugger?
          2. Installing Aptana
          3. Setting up PYTHONPATH
          4. Starting the Pydevd server
          5. Connecting QGIS to the Pydevd server
          6. Connecting using the Remote Debug QGIS plugin
        9. Debugging session example
          1. Creating a PyDev project for TestPlugin
          2. Adding breakpoints
          3. Debugging in action
        10. Summary
      18. 11. PyQGIS Scripting
        1. Where to learn Python basics
          1. Tabs or spaces, make your choice!
        2. How to load layers
          1. How to manage rasters
            1. Exploring QgsRasterLayer
            2. Visualizing the layer
          2. Managing vector files
          3. Managing database vectors
        3. Vector structure
          1. The basic vector methods
          2. Describing the vector structure
            1. Describing the header
            2. Describing the rows
              1. Exploring QgsGeometry
        4. Iterating over features
          1. Describing the iterators
        5. Editing features
          1. Updating canvas and symbology
          2. Editing through QgsVectorDataProvider
            1. Changing a feature's geometry
              1. Changing a feature's attributes
            2. Deleting a feature
            3. Adding a feature
          3. Editing using QgsVectorLayer
            1. Discovering the QgsVectorLayerEditBuffer class
              1. Changing a feature's geometry
            2. Changing a feature's attributes
            3. Adding and removing a feature
        6. Running Processing Toolbox algorithms
          1. Looking for an algorithm
          2. Getting algorithm information
          3. Running algorithms from the console
          4. Running your own processing script
            1. Creating a test Processing Toolbox script
            2. Looking at the custom script
            3. Running the script
        7. Running an external algorithm or command
          1. Running a simple command
        8. Interacting with the map canvas
          1. Getting the map canvas
          2. Explaining Map Tools
          3. Setting the current Map Tool
          4. Getting point-click values
            1. Getting current Map Tool
            2. Creating the new Map Tool
            3. Creating a map canvas event handler
            4. Creating a Map Tool event handler
            5. Setting up the new Map Tool
          5. Using point-click values
          6. Exploring the QgsRubberBand class
        9. Summary