QGIS Python Programming Cookbook - Second Edition

Book description

Master over 170 recipes that will help you turn QGIS from a desktop GIS tool into a powerful automated geospatial framework

About This Book

  • Delve into the undocumented features of the QGIS API

  • Get a set of user-friendly recipes that can automate entire geospatial workflows by connecting Python GIS building blocks into comprehensive processes

  • This book has a complete code upgrade to QGIS 2.18 and 30 new, valuable recipes

  • Who This Book Is For

    This book is for geospatial analysts who want to learn more about automating everyday GIS tasks as well as programmers responsible for building GIS applications. The short, reusable recipes make concepts easy to understand and combine so you can build larger applications that are easy to maintain.

    What You Will Learn

  • Use Python and QGIS to produce captivating GIS visualizations and build complex map layouts

  • Find out how to effectively use the poorly-documented and undocumented features of the QGIS Python API

  • Automate entire geospatial workflows by connecting Python GIS building blocks into comprehensive processes

  • Create, import, and edit geospatial data on disk or in-memory

  • Change QGIS settings programmatically to control default behavior

  • Automatically generate PDF map books

  • Build dynamic forms for field input

  • In Detail

    QGIS is a desktop geographic information system that facilitates data viewing, editing, and analysis. Paired with the most efficient scripting language—Python, we can write effective scripts that extend the core functionality of QGIS.

    Based on version QGIS 2.18, this book will teach you how to write Python code that works with spatial data to automate geoprocessing tasks in QGIS. It will cover topics such as querying and editing vector data and using raster data. You will also learn to create, edit, and optimize a vector layer for faster queries, reproject a vector layer, reduce the number of vertices in a vector layer without losing critical data, and convert a raster to a vector. Following this, you will work through recipes that will help you compose static maps, create heavily customized maps, and add specialized labels and annotations. As well as this, we’ll also share a few tips and tricks based on different aspects of QGIS.

    Style and approach

    This book follows a recipe-based problem-solution approach to address and dispel challenges faced when implementing and using QGIS on a regular basis.

    Table of contents

    1. QGIS Python Programming Cookbook - Second Edition
      1. QGIS Python Programming Cookbook - Second Edition
      2. Credits
      3. About the Author
      4. About the Reviewer
      5. www.PacktPub.com
        1. Why subscribe?
      6. Customer Feedback
      7. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Sections
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        5. Conventions
        6. Reader feedback
        7. Customer support
          1. Downloading the example code
          2. Downloading the color images of this book 
          3. Errata
          4. Piracy
          5. Questions
      8. 1. Automating QGIS
        1. Introduction
        2. Installing QGIS 2.18 for development
          1. Getting ready
          2. How to do it...
            1. Installing PyQGIS using the Debian package manager
            2. Installing PyQGIS using the RPM package manager
            3. Setting the environment variables
            4. Setting the environment variables on Windows
            5. Setting environment variables on Linux
          3. How it works...
          4. There's more...
            1. Finding the PyQGIS path on Windows
            2. Finding the location of the QGIS Python installation on other platforms
        3. Using the QGIS Python console for interactive control
          1. How to do it...
          2. How it works...
        4. Automatically starting the Python console
          1. Getting ready
          2. How to do it...
          3. How it works...
        5. Using the Python's Script Runner plugin
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        6. Setting up your QGIS IDE
          1. Getting ready
          2. How to do it...
            1. Adding the Python interpreter to Windows
            2. Adding the PyQGIS module paths to the interpreter
            3. Adding the PyQGIS API to the IDE
            4. Adding environment variables
          3. How it works...
        7. Debugging QGIS Python scripts
          1. How to do it...
            1. Configuring QGIS
            2. Configuring Eclipse
            3. Testing the debugger
          2. How it works...
        8. Navigating the PyQGIS API
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        9. Creating a traditional QGIS plugin
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        10. Creating a Processing Toolbox plugin
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        11. Distributing a plugin
          1. Getting ready
          2. How to do it...
          3. How it works...
        12. Building a standalone application
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        13. Storing and reading global preferences
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        14. Storing and reading project preferences
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        15. Accessing the script path from within your script
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
      9. 2. Querying Vector Data
        1. Introduction
        2. Loading a vector layer from a file sample
          1. Getting ready
          2. How to do it...
          3. How it works...
        3. Loading a vector layer from a geodatabase
          1. Getting ready
          2. How to do it...
          3. How it works...
        4. Examining vector layer features
          1. Getting ready
          2. How to do it...
          3. How it works...
        5. Examining vector layer attributes
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        6. Filtering a layer by geometry
          1. Getting ready
          2. How to do it...
          3. How it works...
        7. Filtering a layer by attributes
          1. Getting ready
          2. How to do it...
          3. How it works...
        8. Buffering a feature
          1. Getting ready
          2. How to do it...
          3. How it works...
        9. Measuring the distance between two points
          1. Getting ready
          2. How to do it...
          3. How it works...
        10. Measuring distance along a line
          1. Getting ready
          2. How to do it...
          3. How it works...
        11. Calculating the area of a polygon
          1. Getting ready
          2. How to do it...
          3. How it works...
        12. Creating a spatial index
          1. Getting ready
          2. How to do it...
          3. How it works...
        13. Calculating the bearing of a line
          1. Getting ready
          2. How to do it...
          3. How it works...
        14. Loading data from a spreadsheet
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        15. Accessing layer metadata
          1. Getting ready
          2. How to do it...
          3. How it works...
      10. 3. Editing Vector Data
        1. Introduction
        2. Creating a vector layer in memory
          1. Getting ready
          2. How to do it...
          3. How it works...
        3. Adding a point feature to a vector layer
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        4. Adding a line feature to a vector layer
          1. Getting ready
          2. How to do it...
          3. How it works...
        5. Adding a polygon feature to a vector layer
          1. Getting ready
          2. How to do it...
          3. How it works...
        6. Adding a set of attributes to a vector layer
          1. Getting ready...
          2. How to do it...
          3. How it works...
        7. Adding a field to a vector layer
          1. Getting ready
          2. How to do it...
          3. How it works...
        8. Joining a shapefile attribute table to a CSV file
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        9. Changing vector layer geometry
          1. Getting ready
          2. How to do it...
          3. How it works...
        10. Changing a vector layer feature's attribute
          1. Getting ready
          2. How to do it...
          3. How it works...
        11. Removing data from a vector layer
          1. Getting ready
          2. How to do it....
          3. How it works...
        12. Deleting a vector layer feature's attribute
          1. Getting ready
          2. How to do it...
          3. How it works...
        13. Reprojecting a vector layer
          1. Getting ready
          2. How to do it...
          3. How it works...
        14. Converting a shapefile to KML or GeoJSON
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        15. Merging shapefiles
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        16. Splitting a shapefile
          1. Getting ready
          2. How to do it...
          3. How it works...
        17. Generalizing a vector layer
          1. Getting ready
          2. How to do it...
          3. How it works...
        18. Dissolving vector features
          1. Getting ready
          2. How to do it...
          3. How it works...
        19. Performing a union on vector shapes
          1. Getting ready
          2. How to do it...
          3. How it works...
        20. Rasterizing a vector layer
          1. Getting ready
          2. How to do it...
          3. How it works...
        21. Exporting a layer to the GeoPackage format
          1. Getting ready
          2. How to do it...
          3. How it works...
      11. 4. Using Raster Data
        1. Introduction
        2. Loading a raster layer
          1. Getting ready
          2. How to do it...
          3. How it works...
        3. Loading a NetCDF file
          1. Getting ready
          2. How to do it...
          3. How it works...
        4. Getting the cell size of a raster layer
          1. Getting ready
          2. How to do it...
          3. How it works...
        5. Obtaining the width and height of a raster
          1. Getting ready
          2. How to do it...
          3. How it works...
        6. Counting raster bands
          1. Getting ready
          2. How to do it...
          3. How it works...
        7. Swapping raster bands
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        8. Querying the value of a raster at a specified point
          1. Getting ready
          2. How to do it...
          3. How it works...
        9. Reprojecting a raster
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        10. Creating an elevation hillshade
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        11. Creating vector contours from elevation data
          1. Getting ready
          2. How to do it...
          3. How it works...
        12. Sampling a raster dataset using a regular grid
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        13. Adding elevation data to a line vertices using a digital elevation model
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        14. Creating a common extent for rasters
          1. Getting ready
          2. How to do it...
          3. How it works...
        15. Resampling raster resolution
          1. Getting ready
          2. How to do it...
          3. How it works...
        16. Counting the unique values in a raster
          1. Getting ready
          2. How to do it...
          3. How it works...
        17. Mosaicing rasters
          1. Getting ready
          2. How to do it...
          3. How it works...
        18. Converting a TIFF image to a JPEG image
          1. Getting ready
          2. How to do it...
          3. How it works...
        19. Creating pyramids for a raster
          1. Getting ready
          2. How to do it...
          3. How it works...
        20. Converting a pixel location to a map coordinate
          1. Getting ready
          2. How to do it...
          3. How it works...
        21. Converting a map coordinate to a pixel location
          1. Getting ready
          2. How to do it...
          3. How it works...
        22. Creating a KML image overlay for a raster
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        23. Classifying a raster
          1. Getting ready
          2. How to do it...
          3. How it works...
        24. Converting a raster to a vector
          1. Getting ready
          2. How to do it...
          3. How it works...
        25. Georeferencing a raster from control points
          1. Getting ready
          2. How to do it...
          3. How it works...
        26. Clipping a raster using a shapefile
          1. Getting ready
          2. How to do it...
          3. How it works...
      12. 5. Creating Dynamic Maps
        1. Introduction
        2. Accessing the map canvas
          1. Getting ready
          2. How to do it...
          3. How it works...
        3. Changing the map units
          1. Getting ready
          2. How to do it...
          3. How it works...
        4. Iterating over layers
          1. Getting ready
          2. How to do it...
          3. How it works...
        5. Symbolizing a vector layer
          1. Getting ready
          2. How to do it...
          3. How it works...
        6. Setting a transparent layer fill
          1. Getting ready
          2. How to do it...
          3. How it works...
        7. Using a filled marker symbol
          1. Getting ready
          2. How to do it...
        8. Rendering a single band raster using a color ramp algorithm
          1. Getting ready
          2. How to do it...
          3. How it works...
        9. Setting a feature's color using a column in a CSV file
          1. Getting ready
          2. How to do it...
          3. How it works...
        10. Creating a complex vector layer symbol
          1. Getting ready
          2. How to do it...
          3. How it works...
        11. Using icons as vector layer symbols
          1. Getting ready
          2. How to do it...
          3. How it works...
        12. Using an outline for font markers
          1. Getting ready
          2. How to do it...
          3. How it works...
        13. Using arrow symbols
          1. Getting ready
          2. How to do it...
          3. How it works...
        14. Creating a graduated vector layer symbol renderer
          1. Getting ready
          2. How to do it...
          3. How it works...
        15. Creating a categorized vector layer symbol
          1. Getting ready
          2. How to do it...
          3. How it works...
        16. Using live layer effects
          1. Getting ready
          2. How to do it...
          3. How it works...
        17. Creating inverted polygon shapeburst fills
          1. Getting ready
          2. How to do it...
          3. How it works...
        18. Creating a map bookmark
          1. Getting ready
          2. How to do it...
          3. How it works...
        19. Navigating to a map bookmark
          1. Getting ready
          2. How to do it...
          3. How it works...
        20. Setting scale-based visibility for a layer
          1. Getting ready
          2. How to do it...
          3. How it works...
        21. Using SVG for layer symbols
          1. Getting ready
          2. How to do it...
          3. How it works...
        22. Using pie charts for symbols
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        23. Using the 2.5D renderer
          1. Getting ready
          2. How to do it...
          3. How it works...
        24. Using XYZ tiled map services
          1. Getting ready
          2. How to do it...
          3. How it works...
        25. Using Esri ArcGIS map services
          1. Getting Ready
          2. How to do it...
          3. How it works...
        26. Labeling a feature
          1. Getting ready
          2. How to do it...
          3. How it works...
        27. Using null symbols
          1. Getting ready
          2. How to do it...
          3. How it works...
        28. Change map layer transparency
          1. Getting ready
          2. How to do it...
          3. How it works...
        29. Adding standard map tools to the canvas
          1. Getting ready
          2. How to do it...
          3. How it works...
        30. Using a map tool to draw points on the canvas
          1. Getting ready
          2. How to do it...
          3. How it works...
        31. Using a map tool to draw polygons or lines on the canvas
          1. Getting ready
          2. How to do it...
          3. How it works...
        32. Building a custom selection tool
          1. Getting ready
          2. How to do it...
          3. How it works...
        33. Creating a mouse coordinate tracking tool
          1. Getting ready
          2. How to do it...
          3. How it works...
      13. 6. Composing Static Maps
        1. Introduction
        2. Creating the simplest map renderer
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        3. Using the Map Composer
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        4. Adding labels to a map for printing
          1. Getting ready
          2. How to do it...
          3. How it works...
        5. Adding a scale bar to a map
          1. Getting ready
          2. How to do it...
          3. How it works...
        6. Adding a North arrow to the map
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        7. Adding a logo to the map
          1. Getting ready
          2. How to do it...
          3. How it works...
        8. Adding a vertical legend to the map
          1. Getting ready
          2. How to do it...
          3. How it works...
        9. Adding a horizontal legend
          1. Getting ready
          2. How to do it...
        10. Adding a custom shape to the map
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        11. Adding a grid to the map
          1. Getting ready
          2. How to do it...
          3. How it works...
        12. Adding a table to the map
          1. Getting ready
          2. How to do it...
          3. How it works...
        13. Adding a world file to a map image
          1. Getting ready
          2. How to do it...
          3. How it works...
        14. Saving a map to a project
          1. Getting ready
          2. How to do it...
          3. How it works...
        15. Loading a map from a project
          1. Getting ready
          2. How to do it...
          3. How it works...
      14. 7. Interacting with the User
        1. Introduction
        2. Using log files
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        3. Creating a simple message dialog
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        4. Creating a warning dialog
          1. Getting ready
          2. How to do it...
          3. How it works...
        5. Creating an error dialog
          1. Getting ready
          2. How to do it...
          3. How it works...
        6. Displaying a progress bar
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        7. Creating a simple text input dialog
          1. Getting ready
          2. How to do it...
          3. How it works...
        8. Creating a file input dialog
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        9. Creating a combobox
          1. Getting ready
          2. How to do it...
          3. How it works...
        10. Creating radio buttons
          1. Getting ready
          2. How to do it...
          3. How it works...
        11. Creating checkboxes
          1. Getting ready
          2. How to do it...
          3. How it works...
        12. Creating a dock widget
          1. Getting ready
          2. How to do it...
          3. How it works...
        13. Displaying a message in the status bar
          1. Getting ready
          2. How to do it...
          3. How it works...
        14. Pushing messages to the message bar
          1. Getting ready
          2. How to do it...
          3. How it works...
        15. Pushing widgets to the message bar
          1. Getting ready
          2. How to do it...
          3. How it works...
        16. Creating tabs
          1. Getting ready
          2. How to do it...
          3. How it works...
        17. Stepping the user through a wizard
          1. Getting ready
          2. How to do it...
          3. How it works...
        18. Keeping dialogs on top
          1. Getting ready
          2. How to do it...
          3. How it works...
      15. 8. QGIS Workflows
        1. Introduction
        2. Creating an NDVI
          1. Getting ready
          2. How to do it...
          3. How it works...
        3. Geocoding addresses
          1. Getting ready
          2. How to do it...
          3. How it works...
        4. Creating raster footprints
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        5. Performing network analysis
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        6. Routing along streets
          1. Getting ready
          2. How to do it...
          3. How it works...
        7. Tracking a GPS
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        8. Creating a mapbook
          1. Getting ready
          2. How to do it...
          3. How it works...
        9. Finding the least cost path
          1. Getting ready
          2. How to do it...
          3. How it works...
        10. Performing nearest neighbor analysis
          1. Getting ready
          2. How to do it...
          3. How it works...
        11. Creating a DEM from LIDAR
          1. Getting ready
          2. How to do it...
          3. How it works...
        12. Creating a heat map
          1. Getting ready
          2. How to do it...
          3. How it works...
        13. Creating a dot density map
          1. Getting ready
          2. How to do it...
          3. How it works...
        14. Collecting field data
          1. Getting ready
          2. How to do it...
          3. How it works...
        15. Computing road slope using elevation data
          1. Getting ready
          2. How to do it...
          3. How it works...
        16. Geolocating photos on the map
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        17. Image change detection
          1. Getting ready
          2. How to do it...
          3. How it works...
        18. Adjusting imprecise building footprints
          1. Getting ready
          2. How to do it...
          3. How it works...
        19. Visualizing multitemporal data
          1. Getting ready
          2. How to do it...
          3. How it works...
      16. 9. Other Tips and Tricks
        1. Introduction
        2. Creating tiles from a QGIS map
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        3. Adding a layer to geojson.io
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        4. Rendering map layers based on rules
          1. Getting ready
          2. How to do it...
          3. How it works...
        5. Creating a layer-definition file
          1. Getting ready
          2. How to do it...
          3. How it works...
        6. Using NULL values in PyQGIS
          1. Getting ready
          2. How to do it...
          3. How it works...
        7. Using generators for layer queries
          1. Getting ready
          2. How to do it...
          3. How it works...
        8. Using alpha values to show data density
          1. Getting ready
          2. How to do it...
          3. How it works...
        9. Using the __geo_interface__ protocol
          1. Getting ready
          2. How to do it...
          3. How it works...
        10. Getting the output file names from processing algorithms
          1. Getting ready
          2. How to do it...
          3. How it works...
        11. Generating points along a line
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        12. Using expression-based labels
          1. Getting ready
          2. How to do it...
          3. How it works...
        13. Creating dynamic forms in QGIS
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        14. Calculating length for all selected lines
          1. Getting ready
          2. How to do it...
          3. How it works...
        15. Using a different status bar CRS than the map
          1. Getting ready
          2. How to do it...
          3. How it works...
        16. Creating HTML labels in QGIS
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        17. Using OpenStreetMap points of interest in QGIS
          1. Getting ready
          2. How to do it...
          3. How it works...
        18. Changing the QGIS web proxy
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        19. Running QGIS scripts as scheduled tasks
          1. Getting ready
          2. How to do it...
          3. How it works...
        20. Visualizing data in 3D with WebGL
          1. Getting ready
          2. How to do it...
          3. How it works...
        21. Visualizing data on a globe
          1. Getting ready
          2. How to do it...
          3. How it works...
        22. Make a globe-like azimuthal orthographic projection
          1. Getting ready
          2. How to do it...
          3. How it works...
        23. Animating a layer
          1. Getting ready
          2. How to do it...

    Product information

    • Title: QGIS Python Programming Cookbook - Second Edition
    • Author(s): Joel Lawhead
    • Release date: March 2017
    • Publisher(s): Packt Publishing
    • ISBN: 9781787124837