Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
podcast
Filter by Categories
ArcGIS Pro
GDAL
GeoJson
Map
postgis
Python
QGIS
Uncategorized

Mastering QGIS Raster Calculator

Mastering QGIS Raster Calculator for Enhanced Geospatial Analysis

Throughout this post, we will cover essential concepts such as the syntax used in the QGIS Raster Calculator, how to perform calculations on multiple raster layers, and the use of logical and mathematical operators. Additionally, we’ll discuss how to set NoData values, work with conditional statements, and combine GeoTIFFs to create new raster layers.

By the end of our discussion, you’ll have a solid foundation for leveraging the QGIS Raster Calculator to perform complex geospatial analysis tasks and transform your raster data into meaningful insights.

Want to stay ahead of the geospatial curve? Listen to our podcast!

How to use the QGIS Raster Calculator

The QGIS Raster Calculator is a powerful tool within the QGIS software that allows you to perform mathematical operations on raster layers. You can use the raster calculator to create new raster layers from existing ones, combine multiple raster layers, or perform various types of calculations based on the pixel values of the raster layers.

To use the QGIS Raster Calculator, follow these steps:

  1. Open QGIS: Launch QGIS on your computer.
  2. Load raster layers: Add the raster layers you want to work with by clicking on “Layer” > “Add Layer” > “Add Raster Layer” or by dragging and dropping the files into the Layers panel.
  3. Open Raster Calculator: Go to the main menu and click on “Raster” > “Raster Calculator” to open the Raster Calculator dialog window.
  4. Create expression: In the Raster Calculator dialog, you’ll see a list of your loaded raster layers on the left. Create your desired expression using the raster layers and mathematical operators, functions, or conditional statements. Double-click on a raster layer to add it to the expression box or click on the appropriate buttons to insert mathematical operations.
  5. Set output settings: In the “Output layer” section, specify the file path where you want to save the result by clicking the “…” button. Define the output file format (e.g., GeoTIFF) from the “Output format” dropdown menu. Optionally, you can set the output layer’s nodata value and choose the output format type (e.g., Float32, Int16, etc.).
  6. Check the expression: Before running the calculation, make sure your expression is correct and free of errors. QGIS will display an error message in the Raster Calculator dialog window if there are any issues with your expression, allowing you to correct the problems before running the calculation.
  7. Run the calculation: Once you’ve set up your expression and output settings, click “OK” to run the raster calculation. QGIS will process the calculation and create a new raster layer based on your expression.
  8. Check the results: After the calculation is complete, the new raster layer will be added to the Layers panel. You can inspect the results by using the Identify tool or by checking the layer’s properties and statistics. If necessary, adjust your expression and re-run the calculation to fine-tune the output.
  9. Save your project: To keep the new raster layer and your work, save your QGIS project by clicking “Project” > “Save” or “Save As” in the main menu.

By following these steps, you can efficiently use the QGIS Raster Calculator to perform various calculations and analyses on your raster data. As you become more familiar with the tool and its syntax, you can create increasingly complex expressions and unlock the full potential of the QGIS Raster Calculator for your geospatial analysis needs.

QGIS Raster Calculator Syntax and Examples

The QGIS Raster Calculator uses a simple syntax to perform calculations on raster layers. It allows users to apply mathematical operations, functions, and logical operators to create new raster layers. The syntax is composed of the following elements:

Raster Layers:

To use a raster layer in the expression, double-click on it in the left pane of the Raster Calculator dialog or type the layer’s name in the expression box. Raster layer names must be wrapped in double quotes, e.g., “raster_layer_name”.

Mathematical Operators:

You can use basic arithmetic operators such as addition (+), subtraction (-), multiplication (*), and division (/) to perform calculations with raster layers. For example, to add two raster layers, you can write: “raster_layer1” + “raster_layer2”

Parentheses:

You can use parentheses to control the order of operations in your expression. For example: (“raster_layer1” * 2) – “raster_layer2”

Logical Operators:

The Raster Calculator supports logical operators like greater than (>), less than (<), equal to (=), greater than or equal to (>=), less than or equal to (<=), and not equal to (<>). For example: “raster_layer1” > “raster_layer2”

Conditional Statements:

You can use the “IF” statement to create conditional expressions. The syntax is:

IF(condition, value_if_true, value_if_false)

For example, to create a raster layer where each pixel has a value of 1 if “raster_layer1” is greater than “raster_layer2”, and 0 otherwise, you would write: IF(“raster_layer1” > “raster_layer2”, 1, 0)

Here’s how you can create conditional statements in QGIS Raster Calculator:

Open QGIS and load your raster layers into the Layers panel.

Go to the main menu and click on “Raster” > “Raster Calculator” to open the Raster Calculator dialog window.

In the Raster Calculator dialog, create a condition using raster layers and comparison operators, such as greater than (>), less than (<), equal to (=), greater than or equal to (>=), less than or equal to (<=), or not equal to (<>).

For example, if you want to create an output raster layer where each pixel has a value of 1 if “raster_layer1” is greater than 10, and 0 otherwise, you would write:

IF("raster_layer1" > 10, 1, 0)

If you need to combine multiple conditions, use the AND, OR, and NOT operators. For example, to create an output raster layer where each pixel has a value of 1 if “raster_layer 1” is greater than 10 and “raster_layer2” is less than 20, and 0 otherwise, you would write:

IF(("raster_layer1" > 10) AND ("raster_layer2" < 20), 1, 0)

You can also use nested IF statements to handle multiple conditions with different outcomes. For example, to create an output raster layer with the following conditions:

  • Value of 1 if “raster_layer1” is greater than 10
  • Value of 2 if “raster_layer2” is less than 20
  • Value of 3 if both conditions are met
  • Value of 0 if none of the conditions are met

You would write:

IF(("raster_layer1" > 10) AND ("raster_layer2" < 20), 3, IF("raster_layer1" > 10, 1, IF("raster_layer2" < 20, 2, 0)))

Once you’ve written your conditional statement, specify the output file format, the file path, and the output format type in the “Output layer” section of the Raster Calculator dialog window.

Click “OK” to run the calculation and create the new raster layer based on your conditional statement.

Remember to check your expressions for errors before executing the raster calculation. QGIS will display an error message in the Raster Calculator dialog window if there are any issues with your expression, allowing you to correct the problems before running the calculation.

Mathematical Functions:

The Raster Calculator supports various mathematical functions, such as:

  • sin(): Sine of a raster’s pixel value (in radians).
  • cos(): Cosine of a raster’s pixel value (in radians).
  • tan(): Tangent of a raster’s pixel value (in radians).
  • sqrt(): Square root of a raster’s pixel value.
  • log(): Natural logarithm of a raster’s pixel value.
  • exp(): Exponential of a raster’s pixel value.

For example, to calculate the square root of a raster layer’s pixel values, you would write: sqrt(“raster_layer1”)

Cell Statistics Functions:

These functions allow you to calculate statistics on a per-cell basis across multiple raster layers:

  • stat_min(): Minimum value of the specified raster layers on a per-cell basis.
  • stat_max(): Maximum value of the specified raster layers on a per-cell basis.
  • stat_mean(): Mean value of the specified raster layers on a per-cell basis.
  • stat_sum(): Sum of the specified raster layers on a per-cell basis.

To use these functions, you need to specify the raster layers separated by a comma within the parentheses. For example, to calculate the mean value of three raster layers on a per-cell basis, you would write:

stat_mean("raster_layer1", "raster_layer2", "raster_layer3")

Boolean Functions:

You can use boolean functions like AND, OR, and NOT to combine multiple conditions. For example, to create a raster layer where each pixel has a value of 1 if “raster_layer1” is greater than 10 AND “raster_layer2” is less than 20, and 0 otherwise, you would write:

IF(("raster_layer1" > 10) AND ("raster_layer2" < 20), 1, 0)

Remember that the syntax is case-sensitive, so make sure to use the correct case for layer names, functions, and operators. Always test your expressions for errors before executing the raster calculation. If there are any errors in your expression, QGIS will display a message in the Raster Calculator dialog window, so you can correct the issues before running the calculation.

Some practical examples

Set nodata values using the raster calculator in QGIS

In QGIS Raster Calculator, you can set NoData values for the output raster layer by using conditional statements in your expression. NoData values represent missing or invalid data in a raster dataset. Here’s how to set NoData values using the raster calculator:

  1. Open the Raster Calculator by clicking on “Raster” in the main menu, then selecting “Raster Calculator” from the dropdown menu.
  2. Create the expression for your calculation. For example, if you want to add two raster layers, your expression would be: “raster_layer1” + “raster_layer2”
  3. Identify the NoData value(s) in your input raster layers. This information can be found in the raster layer’s properties, under the “Information” or “Source” tab.
  4. Modify your expression to use the “IF” statement to set NoData values. For example, if the NoData value in both input raster layers is -9999, you would write: IF(“raster_layer1” = -9999 OR “raster_layer2” = -9999, -9999, “raster_layer1” + “raster_layer2”) This expression checks if either of the input raster layers has a NoData value, and if so, sets the output pixel value to -9999 (or any other NoData value you choose). Otherwise, it performs the calculation and assigns the result to the output pixel value.
  5. Specify the output file format, the file path, and the output format type in the “Output layer” section of the Raster Calculator dialog window.
  6. Click “OK” to run the calculation and create the new raster layer with the specified NoData values.

By using the IF function and conditional statements, you can effectively set NoData values in the output raster layer, ensuring that missing or invalid data is properly accounted for in your geospatial analysis.

How to combine multiple geotiffs using the QGIS raster calculator

To combine multiple GeoTIFF raster layers into a single layer using QGIS Raster Calculator, you can use cell statistics functions like stat_mean(), stat_sum(), stat_min(), or stat_max(). The choice of function depends on the desired outcome of the combination.

For example, to calculate the mean value of multiple GeoTIFF raster layers on a per-cell basis, use the stat_mean() function:

stat_mean("raster_layer1", "raster_layer2", "raster_layer3", ...)

Follow these steps to perform the combination in QGIS:

Open QGIS and load your GeoTIFF raster layers into the Layers panel.

Go to the main menu and click on “Raster” > “Raster Calculator” to open the Raster Calculator dialog window.

In the Raster Calculator dialog, create your expression using one of the cell statistics functions and the names of your raster layers. For example:

stat_mean("raster_layer1", "raster_layer2", "raster_layer3")

In the “Output layer” section, specify the file path for the output raster by clicking the “…” button. Also, select the output file format (e.g., GeoTIFF) from the “Output format” dropdown menu.

Optionally, you can set the output layer’s nodata value and choose the output format type (e.g., Float32, Int16, etc.).

Click “OK” to run the calculation and create the new combined raster layer based on your expression.

This process will create a new GeoTIFF raster layer that combines the information from the input raster layers using the specified cell statistics function.

Summary

In this blog post, we explored the powerful capabilities of the QGIS Raster Calculator for geospatial analysis. We delved into essential concepts, such as the syntax used in the Raster Calculator, performing calculations on multiple raster layers, and working with mathematical and logical operators. We discussed how to set NoData values, use conditional statements, and combine multiple GeoTIFFs to create new raster layers.

By following our step-by-step guide to using the QGIS Raster Calculator, you’ll be well-equipped to perform complex geospatial analysis tasks and transform your raster data into valuable insights.

About the Author
I'm Daniel O'Donohue, the voice and creator behind The MapScaping Podcast ( A podcast for the geospatial community ). With a professional background as a geospatial specialist, I've spent years harnessing the power of spatial to unravel the complexities of our world, one layer at a time.

Leave a Reply