0%

Book Description

Over 140 recipes to help you turn QGIS from a desktop GIS tool into a powerful automated geospatial framework

In Detail

QGIS Python Programming 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 come across recipes that will help you to compose static maps, create heavily customized maps, and add specialized labels and annotations. Apart from this, the book will also share a few tips and tricks based on different aspects of QGIS.

What You Will Learn

  • Build a library of reusable scripts with ScriptRunner
  • Create, import, and edit geospatial data on disk or in memory
  • Get to know more about dynamic mapping
  • Create and add features to static maps
  • Create a mapbook
  • Reproject a vector layer
  • Geolocate photos on a map
  • Combine multiple rasters into one image

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 http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Table of Contents

  1. QGIS Python Programming Cookbook
    1. Table of Contents
    2. QGIS Python Programming Cookbook
    3. Credits
    4. About the Author
    5. About the Reviewers
    6. www.PacktPub.com
      1. Support files, eBooks, discount offers, and more
        1. Why Subscribe?
        2. Free Access for Packt account holders
    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 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
            1. Setting the environment variables on Windows
            2. Setting the 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. Using the Python ScriptRunner plugin
        1. Getting ready
        2. How to do it…
        3. How it works…
      5. Setting up your QGIS IDE
        1. Getting ready
        2. How to do it…
          1. Adding the QGIS Python interpreter on 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…
      6. Debugging QGIS Python scripts
        1. How to do it…
          1. Configuring QGIS
          2. Configuring Eclipse
          3. Testing the debugger
        2. How it works…
      7. Navigating the PyQGIS API
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
      8. Creating a QGIS plugin
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
      9. Distributing a plugin
        1. Getting ready
        2. How to do it…
        3. How it works…
      10. Creating a standalone application
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more...
      11. Storing and reading global preferences
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
      12. Storing and reading project preferences
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
      13. 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 spatial database
        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 intermediate
        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 the distance along a line sample
        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...
    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...
        4. There's more…
      3. Adding a point feature to a vector layer
        1. Getting ready
        2. How to do it...
        3. How it works...
      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. Moving 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. Deleting a vector layer feature
        1. Getting ready
        2. How to do it...
        3. How it works...
      12. Deleting a vector layer 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
        1. Getting ready
        2. How to do it...
        3. How it works...
      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 shapes
        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...
    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. Getting the cell size of a raster layer
        1. Getting ready
        2. How to do it...
        3. How it works...
      4. Obtaining the width and height of a raster
        1. Getting ready
        2. How to do it...
        3. How it works...
      5. Counting raster bands
        1. Getting ready
        2. How to do it...
        3. How it works...
      6. Swapping raster bands
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      7. Querying the value of a raster at a specified point
        1. Getting ready
        2. How to do it...
        3. How it works...
      8. Reprojecting a raster
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      9. Creating an elevation hillshade
        1. Getting ready
        2. How to do it...
        3. How it works...
      10. Creating vector contours from elevation data
        1. Getting ready
        2. How to do it...
        3. How it works...
      11. Sampling a raster dataset using a regular grid
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      12. Adding elevation data to line vertices using a digital elevation model
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      13. Creating a common extent for rasters
        1. Getting ready
        2. How to do it...
        3. How it works...
      14. Resampling raster resolution
        1. Getting ready
        2. How to do it...
        3. How it works...
      15. Counting the unique values in a raster
        1. Getting ready
        2. How to do it...
        3. How it works...
      16. Mosaicing rasters
        1. Getting ready
        2. How to do it...
        3. How it works...
      17. Converting a TIFF image to a JPEG image
        1. Getting ready
        2. How to do it...
        3. How it works...
      18. Creating pyramids for a raster
        1. Getting ready
        2. How to do it...
        3. How it works...
      19. Converting a pixel location to a map coordinate
        1. Getting ready
        2. How to do it...
        3. How it works...
      20. Converting a map coordinate to a pixel location
        1. Getting ready
        2. How to do it...
        3. How it works...
      21. Creating a KML image overlay for a raster
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      22. Classifying a raster
        1. Getting ready
        2. How to do it...
        3. How it works...
      23. Converting a raster to a vector
        1. Getting ready
        2. How to do it...
        3. How it works...
      24. Georeferencing a raster from control points
        1. Getting ready
        2. How to do it...
        3. How it works...
      25. 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. Rendering a single band raster using a color ramp algorithm
        1. Getting ready
        2. How to do it...
        3. How it works…
      7. Creating a complex vector layer symbol
        1. Getting ready
        2. How to do it…
        3. How it works…
      8. Using icons as vector layer symbols
        1. Getting ready
        2. How to do it…
        3. How it works…
      9. Creating a graduated vector layer symbol renderer
        1. Getting ready
        2. How to do it...
        3. How it works...
      10. Creating a categorized vector layer symbol
        1. Getting ready
        2. How to do it...
        3. How it works...
      11. Creating a map bookmark
        1. Getting ready
        2. How to do it...
        3. How it works...
      12. Navigating to a map bookmark
        1. Getting ready
        2. How to do it...
        3. How it works...
      13. Setting scale-based visibility for a layer
        1. Getting ready
        2. How to do it...
        3. How it works...
      14. Using SVG for layer symbols
        1. Getting ready
        2. How to do it...
        3. How it works...
      15. Using pie charts for symbols
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      16. Using the OpenStreetMap service
        1. Getting ready
        2. How to do it...
        3. How it works...
      17. Using the Bing aerial image service
        1. Getting ready
        2. How to do it...
        3. How it works...
      18. Adding real-time weather data from OpenWeatherMap
        1. Getting ready
        2. How to do it...
        3. How it works...
      19. Labeling features
        1. Getting ready
        2. How to do it...
        3. How it works...
      20. Changing map layer transparency
        1. Getting ready
        2. How to do it...
        3. How it works...
      21. Adding standard map tools to the canvas
        1. Getting ready
        2. How to do it...
        3. How it works...
      22. Using a map tool to draw points on the canvas
        1. Getting ready
        2. How to do it...
        3. How it works...
      23. Using a map tool to draw polygons or lines on the canvas
        1. Getting ready
        2. How to do it...
        3. How it works...
      24. Building a custom selection tool
        1. Getting ready
        2. How to do it...
        3. How it works...
      25. 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 the 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 legend to the map
        1. Getting ready
        2. How to do it...
        3. How it works...
      9. Adding a custom shape to the map
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      10. Adding a grid to the map
        1. Getting ready
        2. How to do it...
        3. How it works...
      11. Adding a table to the map
        1. Getting ready
        2. How to do it...
        3. How it works...
      12. Adding a world file to a map image
        1. Getting ready
        2. How to do it...
        3. How it works...
      13. Saving a map to a project
        1. Getting ready
        2. How to do it...
        3. How it works...
      14. 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...
      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 tabs
        1. Getting ready
        2. How to do it...
        3. How it works...
      13. Stepping the user through a wizard
        1. Getting ready
        2. How to do it...
        3. How it works...
      14. 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. There's more...
      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...
      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 heat map
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      12. Creating a dot density map
        1. Getting ready
        2. How to do it...
        3. How it works...
      13. Collecting field data
        1. Getting ready
        2. How to do it...
        3. How it works...
      14. Computing road slope using elevation data
        1. Getting ready
        2. How to do it...
        3. How it works...
      15. Geolocating photos on the map
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      16. Image change detection
        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...
      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 style 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. Generating points along a line
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      11. Using expression-based labels
        1. Getting ready
        2. How to do it...
        3. How it works...
      12. Creating dynamic forms in QGIS
        1. Getting ready
        2. How to do it...
        3. How it works...
      13. Calculating length for all selected lines
        1. Getting ready
        2. How to do it...
        3. How it works...
      14. Using a different status bar CRS than the map
        1. Getting ready
        2. How to do it...
        3. How it works...
      15. Creating HTML labels in QGIS
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      16. Using OpenStreetMap's points of interest in QGIS
        1. Getting ready
        2. How to do it...
        3. How it works...
      17. Visualizing data in 3D with WebGL
        1. Getting ready
        2. How to do it...
        3. How it works...
      18. Visualizing data on a globe
        1. Getting ready
        2. How to do it...
        3. How it works...
    17. Index