QGIS Python Programming Cookbook

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

QGIS Python Programming Cookbook

This ebook is included in a Mapt subscription
Joel Lawhead

6 customer reviews
Over 140 recipes to help you turn QGIS from a desktop GIS tool into a powerful automated geospatial framework
$0.00
$39.99
$49.99
$29.99p/m after trial
RRP $39.99
RRP $49.99
Subscription
eBook
Print + eBook
Start 30 Day Trial
Subscribe and access every Packt eBook & Video.
 
  • 4,000+ eBooks & Videos
  • 40+ New titles a month
  • 1 Free eBook/Video to keep every month
Start Free Trial
 
Preview in Mapt

Book Details

ISBN 139781783984985
Paperback340 pages

Book Description

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 this powerful tool.

Table of Contents

Chapter 1: Automating QGIS
Introduction
Installing QGIS for development
Using the QGIS Python console for interactive control
Using the Python ScriptRunner plugin
Setting up your QGIS IDE
Debugging QGIS Python scripts
Navigating the PyQGIS API
Creating a QGIS plugin
Distributing a plugin
Creating a standalone application
Storing and reading global preferences
Storing and reading project preferences
Accessing the script path from within your script
Chapter 2: Querying Vector Data
Introduction
Loading a vector layer from a file sample
Loading a vector layer from a spatial database
Examining vector layer features
Examining vector layer attributes
Filtering a layer by geometry
Filtering a layer by attributes
Buffering a feature intermediate
Measuring the distance between two points
Measuring the distance along a line sample
Calculating the area of a polygon
Creating a spatial index
Calculating the bearing of a line
Loading data from a spreadsheet
Chapter 3: Editing Vector Data
Introduction
Creating a vector layer in memory
Adding a point feature to a vector layer
Adding a line feature to a vector layer
Adding a polygon feature to a vector layer
Adding a set of attributes to a vector layer
Adding a field to a vector layer
Joining a shapefile attribute table to a CSV file
Moving vector layer geometry
Changing a vector layer feature's attribute
Deleting a vector layer feature
Deleting a vector layer attribute
Reprojecting a vector layer
Converting a shapefile to KML
Merging shapefiles
Splitting a shapefile
Generalizing a vector layer
Dissolving vector shapes
Performing a union on vector shapes
Rasterizing a vector layer
Chapter 4: Using Raster Data
Introduction
Loading a raster layer
Getting the cell size of a raster layer
Obtaining the width and height of a raster
Counting raster bands
Swapping raster bands
Querying the value of a raster at a specified point
Reprojecting a raster
Creating an elevation hillshade
Creating vector contours from elevation data
Sampling a raster dataset using a regular grid
Adding elevation data to line vertices using a digital elevation model
Creating a common extent for rasters
Resampling raster resolution
Counting the unique values in a raster
Mosaicing rasters
Converting a TIFF image to a JPEG image
Creating pyramids for a raster
Converting a pixel location to a map coordinate
Converting a map coordinate to a pixel location
Creating a KML image overlay for a raster
Classifying a raster
Converting a raster to a vector
Georeferencing a raster from control points
Clipping a raster using a shapefile
Chapter 5: Creating Dynamic Maps
Introduction
Accessing the map canvas
Changing the map units
Iterating over layers
Symbolizing a vector layer
Rendering a single band raster using a color ramp algorithm
Creating a complex vector layer symbol
Using icons as vector layer symbols
Creating a graduated vector layer symbol renderer
Creating a categorized vector layer symbol
Creating a map bookmark
Navigating to a map bookmark
Setting scale-based visibility for a layer
Using SVG for layer symbols
Using pie charts for symbols
Using the OpenStreetMap service
Using the Bing aerial image service
Adding real-time weather data from OpenWeatherMap
Labeling features
Changing map layer transparency
Adding standard map tools to the canvas
Using a map tool to draw points on the canvas
Using a map tool to draw polygons or lines on the canvas
Building a custom selection tool
Creating a mouse coordinate tracking tool
Chapter 6: Composing Static Maps
Introduction
Creating the simplest map renderer
Using the map composer
Adding labels to a map for printing
Adding a scale bar to the map
Adding a north arrow to the map
Adding a logo to the map
Adding a legend to the map
Adding a custom shape to the map
Adding a grid to the map
Adding a table to the map
Adding a world file to a map image
Saving a map to a project
Loading a map from a project
Chapter 7: Interacting with the User
Introduction
Using log files
Creating a simple message dialog
Creating a warning dialog
Creating an error dialog
Displaying a progress bar
Creating a simple text input dialog
Creating a file input dialog
Creating a combobox
Creating radio buttons
Creating checkboxes
Creating tabs
Stepping the user through a wizard
Keeping dialogs on top
Chapter 8: QGIS Workflows
Introduction
Creating an NDVI
Geocoding addresses
Creating raster footprints
Performing network analysis
Routing along streets
Tracking a GPS
Creating a mapbook
Finding the least cost path
Performing nearest neighbor analysis
Creating a heat map
Creating a dot density map
Collecting field data
Computing road slope using elevation data
Geolocating photos on the map
Image change detection
Chapter 9: Other Tips and Tricks
Introduction
Creating tiles from a QGIS map
Adding a layer to geojson.io
Rendering map layers based on rules
Creating a layer style file
Using NULL values in PyQGIS
Using generators for layer queries
Using alpha values to show data density
Using the __geo_interface__ protocol
Generating points along a line
Using expression-based labels
Creating dynamic forms in QGIS
Calculating length for all selected lines
Using a different status bar CRS than the map
Creating HTML labels in QGIS
Using OpenStreetMap's points of interest in QGIS
Visualizing data in 3D with WebGL
Visualizing data on a globe

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

Authors

Table of Contents

Chapter 1: Automating QGIS
Introduction
Installing QGIS for development
Using the QGIS Python console for interactive control
Using the Python ScriptRunner plugin
Setting up your QGIS IDE
Debugging QGIS Python scripts
Navigating the PyQGIS API
Creating a QGIS plugin
Distributing a plugin
Creating a standalone application
Storing and reading global preferences
Storing and reading project preferences
Accessing the script path from within your script
Chapter 2: Querying Vector Data
Introduction
Loading a vector layer from a file sample
Loading a vector layer from a spatial database
Examining vector layer features
Examining vector layer attributes
Filtering a layer by geometry
Filtering a layer by attributes
Buffering a feature intermediate
Measuring the distance between two points
Measuring the distance along a line sample
Calculating the area of a polygon
Creating a spatial index
Calculating the bearing of a line
Loading data from a spreadsheet
Chapter 3: Editing Vector Data
Introduction
Creating a vector layer in memory
Adding a point feature to a vector layer
Adding a line feature to a vector layer
Adding a polygon feature to a vector layer
Adding a set of attributes to a vector layer
Adding a field to a vector layer
Joining a shapefile attribute table to a CSV file
Moving vector layer geometry
Changing a vector layer feature's attribute
Deleting a vector layer feature
Deleting a vector layer attribute
Reprojecting a vector layer
Converting a shapefile to KML
Merging shapefiles
Splitting a shapefile
Generalizing a vector layer
Dissolving vector shapes
Performing a union on vector shapes
Rasterizing a vector layer
Chapter 4: Using Raster Data
Introduction
Loading a raster layer
Getting the cell size of a raster layer
Obtaining the width and height of a raster
Counting raster bands
Swapping raster bands
Querying the value of a raster at a specified point
Reprojecting a raster
Creating an elevation hillshade
Creating vector contours from elevation data
Sampling a raster dataset using a regular grid
Adding elevation data to line vertices using a digital elevation model
Creating a common extent for rasters
Resampling raster resolution
Counting the unique values in a raster
Mosaicing rasters
Converting a TIFF image to a JPEG image
Creating pyramids for a raster
Converting a pixel location to a map coordinate
Converting a map coordinate to a pixel location
Creating a KML image overlay for a raster
Classifying a raster
Converting a raster to a vector
Georeferencing a raster from control points
Clipping a raster using a shapefile
Chapter 5: Creating Dynamic Maps
Introduction
Accessing the map canvas
Changing the map units
Iterating over layers
Symbolizing a vector layer
Rendering a single band raster using a color ramp algorithm
Creating a complex vector layer symbol
Using icons as vector layer symbols
Creating a graduated vector layer symbol renderer
Creating a categorized vector layer symbol
Creating a map bookmark
Navigating to a map bookmark
Setting scale-based visibility for a layer
Using SVG for layer symbols
Using pie charts for symbols
Using the OpenStreetMap service
Using the Bing aerial image service
Adding real-time weather data from OpenWeatherMap
Labeling features
Changing map layer transparency
Adding standard map tools to the canvas
Using a map tool to draw points on the canvas
Using a map tool to draw polygons or lines on the canvas
Building a custom selection tool
Creating a mouse coordinate tracking tool
Chapter 6: Composing Static Maps
Introduction
Creating the simplest map renderer
Using the map composer
Adding labels to a map for printing
Adding a scale bar to the map
Adding a north arrow to the map
Adding a logo to the map
Adding a legend to the map
Adding a custom shape to the map
Adding a grid to the map
Adding a table to the map
Adding a world file to a map image
Saving a map to a project
Loading a map from a project
Chapter 7: Interacting with the User
Introduction
Using log files
Creating a simple message dialog
Creating a warning dialog
Creating an error dialog
Displaying a progress bar
Creating a simple text input dialog
Creating a file input dialog
Creating a combobox
Creating radio buttons
Creating checkboxes
Creating tabs
Stepping the user through a wizard
Keeping dialogs on top
Chapter 8: QGIS Workflows
Introduction
Creating an NDVI
Geocoding addresses
Creating raster footprints
Performing network analysis
Routing along streets
Tracking a GPS
Creating a mapbook
Finding the least cost path
Performing nearest neighbor analysis
Creating a heat map
Creating a dot density map
Collecting field data
Computing road slope using elevation data
Geolocating photos on the map
Image change detection
Chapter 9: Other Tips and Tricks
Introduction
Creating tiles from a QGIS map
Adding a layer to geojson.io
Rendering map layers based on rules
Creating a layer style file
Using NULL values in PyQGIS
Using generators for layer queries
Using alpha values to show data density
Using the __geo_interface__ protocol
Generating points along a line
Using expression-based labels
Creating dynamic forms in QGIS
Calculating length for all selected lines
Using a different status bar CRS than the map
Creating HTML labels in QGIS
Using OpenStreetMap's points of interest in QGIS
Visualizing data in 3D with WebGL
Visualizing data on a globe

Book Details

ISBN 139781783984985
Paperback340 pages
Read More
From 6 reviews

Read More Reviews