Sampling a raster dataset using a regular grid

Sometimes, you need to sample a raster dataset at regular intervals in order to provide summary statistics or for quality assurance purposes on the raster data. A common way to accomplish this regular sampling is to create a point grid over the dataset, query the grid at each point, and assign the results as attributes to those points. In this recipe, we will perform this type of sampling over a satellite image. QGIS has a tool to perform this operation called regular points, which is in the Vector menu under Research Tools. However, there is no tool in the QGIS API to perform this operation programmatically. Instead, we can implement this algorithm directly using Python's numpy module.

Getting ready ...

Get QGIS Python Programming Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.