You are previewing Mastering QGIS.
O'Reilly logo
Mastering QGIS

Book Description

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

In Detail

QGIS is the leading alternative to 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 QGIS basics, this book will take you all the way through to creating your first custom QGIS plugin. 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. From the refresher, you will learn how to create, populate, and manage a spatial database and 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 and raster data for processing and discover advanced data creation and editing techniques. The last third of the book covers more technical aspects of QGIS, including working with the Processing Toolbox, how to automate workflows with batch processing, and how to create graphical models. Finally, you will learn how to create and run Python data processing scripts and write your own QGIS plugin with pyqgis.

What You Will Learn

  • Create and manage a spatial database

  • Learn advanced techniques to style GIS data

  • Prepare both vector and raster data for processing

  • Understand how to work 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

  • 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 files e-mailed directly to you.

    Table of Contents

    1. Mastering QGIS
      1. Table of Contents
      2. Mastering QGIS
      3. Credits
      4. Foreword
      5. About the Authors
      6. About the Reviewers
        1. Support files, eBooks, discount offers, and more
          1. Why subscribe?
          2. Free access for Packt account holders
      8. 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
      9. 1. A Refreshing Look at QGIS
        1. 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
        2. Tour of QGIS
          1. QGIS Desktop
          2. QGIS Browser
        3. Loading data
          1. Loading vector data
          2. Loading raster data
          3. Loading databases
          4. Web services
        4. Working with coordinate reference systems
        5. Working with tables
          1. Table joins
        6. Editing data
          1. Snapping
          2. Styling vector data
          3. Styling raster data
          4. Contrast enhancement
          5. Blending modes
        7. Composing maps
        8. Adding functionality with plugins
        9. Summary
      10. 2. Creating Spatial Databases
        1. Fundamental database concepts
          1. Database tables
          2. Table relationships
          3. 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 as a shapefile
        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
      11. 3. Styling Raster and Vector Data
        1. Choosing and managing colors
          1. Always available color picker components
          2. Changeable panels in the color picker
            1. Color ramp
            2. Color wheel
            3. Color swatches
            4. Color sampler
        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. Add a ColorBrewer color ramp
              4. Adding a cpt-city color ramp
            6. Editing a color ramp
        3. Styling single band 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
        9. Vector layer rendering
        10. Using diagrams to display thematic data
          1. Parameters common to all diagram types
            1. Diagram size parameters
            2. Diagram position parameters
            3. Adding attributes to diagrams
          2. Creating a pie chart diagram
          3. Creating a text chart 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
        12. Summary
      12. 4. Preparing Vector Data for Processing
        1. Merging shapefiles
        2. Creating spatial indices
        3. Checking for geometry errors
        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. Advanced field calculations
          1. Exploring the field calculator interface
          2. Writing advanced field calculations
            1. The first example – calculating and formatting the current date
            2. The second example – inserting geometric values
            3. The third example – calculating a population-dependent label string
        8. Complex spatial and aspatial queries
        9. Summary
      13. 5. Preparing Raster Data for Processing
        1. Reclassifying rasters
          1. Converting datasets from floating-point to integer rasters
          2. Resampling rasters
            1. Installing and troubleshooting SAGA on different platforms
              1. Windows
              2. Mac OS X
              3. Linux
        2. Rescaling rasters
        3. Creating a raster mosaic
        4. Generating raster overviews (pyramids)
        5. Converting between raster and vector data models
          1. Converting from raster to vector
          2. Converting from vector to raster (rasterize)
        6. Creating raster surfaces via interpolation
        7. Summary
      14. 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. Ground control points
          2. Using the Georeferencer GDAL plugin
          3. The first example – georeferencing using a second dataset
            1. Getting started
            2. Entering the ground control points
            3. Transformation settings
            4. Completing the operation
          4. 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
      15. 7. 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 a new slope raster and the land use raster
            3. Combining reclassified slope and land use layers
            4. Calculating the cumulative cost raster using r.cost
            5. Calculating the cost path using least-cost paths
          3. Evaluating a viewshed
            1. Clipping elevation to the boundary of a 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 the SAGA Raster calculator
            4. Finding proximity to surface water using GDAL Proximity
            5. Querying the proximity for 1,000 meters of water using the GDAL Raster calculator
            6. Reclassifying land use using the Reclassify grid values tool
            7. Combining raster layers using the 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. Summary
      16. 8. 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 inputs
        5. Adding algorithms
        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. 9. 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 StackExchange 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
        3. Exploring iface and QGis
        4. Exploring a QGIS API in the Python Console
        5. 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. The plugin file structure – where and what to customize
              1. Exploring main plugin files
              2. Plugin Builder generated files
        6. 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 the GUI logic
            3. Modifying the 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
        7. 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
        8. Debugging session example
          1. Creating a PyDev project for TestPlugin
          2. Adding breakpoints
          3. Debugging in action
        9. Summary
      18. 10. PyQGIS Scripting
        1. Where to learn Python basics
          1. Tabs or spaces, make your choice!
        2. Loading layers
          1. Managing 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 the current Map Tool
            2. Creating a 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
      19. Index