Basic Raster Processing & Analysis

BEN HUR S. PINTOR

Basic Raster Processing & Analysis


Credits & License

Basic Raster Processing & Analysis

by Ben Hur S. Pintor

version 2020.05

This work and its contents by Ben Hur S. Pintor is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

You are free to:

Share — copy and redistribute the material in any medium or format

Adapt — remix, transform, and build upon the material

Under the following terms:

Attribution — You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.

NonCommercial — You may not use the material for commercial purposes.

ShareAlike — If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original.

Other works (software, source code, etc.) referenced in this work are under their own respective licenses.

About this workbook

About the Author

Geospatial Generalist. Open Stuff Advocate. Datactivist + Maptivist.

Ben Hur is a free and open stuff advocate based in The Philippines who likes to work at the intersections of the geospatial, data, development, and openness fields. He received his Bachelor’s degree in Geodetic Engineering from the University of the Philippines and, at the time of this writing, is working towards his Master’s degree in Geomatics Engineering with a specialization in GeoInformatics. His first experience with QGIS was with version 1.5 Tethys in 2010 but he started using it consistently in 2012 with version 1.8 Lisboa.

He is an alumnus of the School of Data Fellowship and Data Expert Programme and has provided training, support, and consulting services on data literacy and free and open sources software for geospatial (FOSS4G) applications such as QGIS, GRASS, and GeoNode to individuals, private corporations, and organizations.

In 2019, he established BNHR (https://bnhr.xyz) as a way to advocate for open data and open source while helping others create sustainable and beneficial solutions to their spatial and data problems using open technologies like QGIS. He is a QGIS Certified Instructor and currently the only QGIS Sustaining Member in the Philippines.

He is an active member of the open mapping community in the Philippines and regularly organizes, supports, and volunteers for activities such as Pista ng Mapa, MaptimeDiliman, and local Open Data Day events.

Learn more about him at https://bnhr.xyz.

Acknowledgements

Standing on the shoulders of giants.

This workbook is an ever evolving piece of work that reflects my learnings and realizations over the years. The information provided here consists of my own experiences and those that I’ve gathered from the experiences of others from whom I have taken inspiration.

The names of everyone I’ve learned from over the years may be too many to mention but I would like to acknowledge the QGIS Project and its volunteers for the Official QGIS Documentation and User Guide and the local and global FOSS4G and QGIS communities among the many inspirations and resources that I had in creating this workbook.

Contents

Basic Raster Processing & Analysis        0

Credits & License        2

About this workbook        3

About the Author        3

Acknowledgements        3

Contents        4

Introduction        5

What you should already know        5

Other learning resources        5

Downloading data using the SRTM Downloader plugin        7

Exercise 1: Downloading SRTM Digital Elevation Models into QGIS        7

Merging raster layers        9

Exercise 2: Merging the SRTM DEMs into one raster        9

Clipping rasters        12

Exercise 3: Using a polygon/mask layer to clip a raster        12

Reprojecting rasters        15

Exercise 4: Reprojecting a raster        15

Working with topography        17

Exercise 5: Generating contours        17

Exercise 6: Computing for the slope and aspect        18

Exercise 7: Computing for the solar radiation/insolation        20

Working with Vectors        23

Sampling rasters at points        23

Exercise 8: Getting solar radiation values at points using the Point Sampling Tool        23

Getting zonal statistics        25

Exercise 9: Getting the average solar radiation values per city/municipality using Zonal Statistics        25

Reclassification        27

Exercise 9: Reclassifying the GHI raster        27

Raster calculator        29

Weighted overlay analysis        29

Exercise 10: Using the raster calculator        29


Introduction

This workbook is designed to introduce you with processing algorithms for working with rasters in QGIS.

By the end of the workbook, you should know: merge and clip rasters; work with topography; sampling rasters; reclassifying rasters; and using the raster calculator.

What you should already know

Since this is a beginner workbook, no previous knowledge of GIS or QGIS is required.

Other learning resources

Official QGIS Documentation (QGIS 3.10)

Official QGIS Documentation (latest, QGIS 3.16)

BNHR blog

BNHR Facebook & Twitter & YouTube

Free and Open Source GIS Ramblings by Anita Graser

Klas Karlsson’s Youtube channel

QGIS User Group Philippines Facebook Page

Geographic Information Science Philippines Facebook Page

Downloading data using the SRTM Downloader plugin

Exercise 1: Downloading SRTM Digital Elevation Models into QGIS

SRTM (Shuttle Radar Topography Mission) is a global research endeavor that yielded nearly-global digital elevation models (DEM). QGIS has a plugin called the SRTM Downloader that allows for SRTM data to easily be downloaded straight from QGIS. SRTM data downloaded by the plugin is in the form of .hgt files.

In order to download SRTM data using the plugin, you should have a working NASA Earth Data account available, if not you may create one at https://urs.earthdata.nasa.gov/users/new.

For this exercise, let’s try to download SRTM data for the province of Albay.

  1. Load the PHL_municities_Albay layer and zoom to the aforementioned layer.
  2. Open the SRTM Downloader plugin by clicking  in the Plugins toolbar.
  1. Click Set Canvas Extent.
  2. Check Load images in QGIS.
  1. Click Download.
  2. The plugin should prompt you for your Earth Data username and password.
  3. Afterwards, the SRTM data should be loaded in the Layer Panel.

Notice that the loaded SRTM layers are memory layers and if you open their properties, they are in SRTMHGT File Format.

BONUS: What is the data type and no data value for the loaded SRTM layers?

Merging raster layers

If you noticed, we were able to download four SRTM layers but for our next analyses, we only need one that covers the entire province of Albay.

Just like vectors, QGIS also allows for rasters to be merged. In this exercise, we will merge the four SRTM layers into one using the Merge algorithm.

Exercise 2: Merging the SRTM DEMs into one raster
  1. Open the Merge algorithm from the Locator bar, via Raster -> Miscellaneous -> Merge in the Menu bar, or under GDAL -> Raster miscellaneous -> Merge in the Processing Panel.
  1. Input layers: the four SRTM layers
  2. Output data type: Int16
  3. “Nodata”: -32768
  1. Click Run.
  2. A new Merged layer should be added to the Layer Panel.

BONUS: Notice that at the bottom of the GDAL algorithm there is a part labelled GDAL/OGR console call. This is the GDAL/OGR command that is run when running the algorithm. If you know how to use GDAL/OGR, you can customize this command by using the Additional command-line parameters.

Clipping rasters

Now we should have one raster layer covering the entire Albay province. However, notice that the raster also includes areas outside of Albay. If we want to create a raster that includes only the extent or area of Albay, we can use the Clip by extent or Clip by mask layer algorithms in QGIS.

As with other algorithms available in QGIS, there are several clip functions that we can use -- the default QGIS/GDAL function, the one in GRASS, the one in SAGA, etc. For this exercise, we’ll use the default QGIS/GDAL functions to clip the Merged layer using the Albay polygon.

Exercise 3: Using a polygon/mask layer to clip a raster
  1. Open the Clip raster by mask layer algorithm from the Locator bar, via Raster -> Extraction -> Clip Raster by Mask Layer in the Menu bar, or under GDAL -> Raster extraction -> Clip raster by mask layer in the Processing Panel.
  1. Input layer: Merged
  2. Mask layer: PHL_municities_Albay
  1. Click Run.
  2. There should be a Clipped (mask) layer added to the Layer Panel.

BONUS: Try to use the other algorithms. What are their differences, if any?

Reprojecting rasters

As with vectors, there are several ways to reproject a raster. The easiest is by exporting the layer and setting the new CRS there. Another is by using the Reproject algorithm.

Exercise 4: Reprojecting a raster
  1. Open the Warp (reproject)  algorithm from the Locator bar, via Raster -> Projections -> Reproject in the Menu bar, or under GDAL -> Raster projections -> Warp (reproject) in the Processing Panel.
  1. Input layer: Clipped (mask)
  2. Source CRS: EPSG:4326
  3. Target CRS: EPSG:32651

  1. Click Run.
  2. There should be a Reprojected layer added to the Layer Panel.

BONUS: Try to reproject the raster by exporting it. Which one is easier?

Working with topography

Working with topography and topographic data (e.g. Digital Elevation Models) is one of the most common things we do with GIS.

Exercise 5: Generating contours

Generating contours is one of the most common ways to work with elevation or topography data. QGIS easily generates contours from DEMs using several Contour algorithms available to it.

  1.  Open the Contour algorithm from the Locator bar, via Raster -> Extraction -> Contour… in the Menu bar, or under GDAL -> Raster extraction ->  Contour in the Processing Panel.
  1. Input layer: reprojected_SRTM_Albay
  2. Interval between contour lines: 100
  1. Click Run.
  2. A Contours layer should be added to the Layer Panel.

Exercise 6: Computing for the slope and aspect

In the next exercises, we’ll use the DEM of Albay we generated from earlier exercises to compute for the slope, aspect, and solar radiation received in the area.

There are several algorithms that can be used to compute the slope and aspect in QGIS. For this exercise, we’ll use the default GDAL computations.

  1. Open the Slope algorithm from the Locator bar, via Raster -> Analysis -> Slope in the Menu bar, or under GDAL -> Raster analysis -> Slope or Raster terrain analysis -> Slope in the Processing Panel. Using Raster terrain analysis -> Slope.
  1. Elevation layer: reprojected_SRTM
  2. Z factor: 1
  1. Click Run.
  2. A Slope layer should be added to the Layer Panel.

  1. Open the Aspect algorithm from the Locator bar, via Raster -> Analysis -> Slope in the Menu bar, or under GDAL -> Raster analysis -> Aspect or Raster terrain analysis -> Slope in the Processing Panel. Using Raster terrain analysis -> Aspect.
  1. Elevation layer: reprojected_SRTM
  2. Z factor: 1
  1. Click Run.
  2. An Aspect layer should be added to the Layer Panel.

BONUS: Try r.slope.aspect to compute the slope and aspect from the DEM. Are there any differences?

Exercise 7: Computing for the solar radiation/insolation

QGIS has access to GRASS and SAGA algorithms such as the solar radiation algorithms of GRASS - r.sun.*.

In this exercise, we’ll generate the clear-sky Global Horizontal Irradiance (GHI) for the province of Albay for June 5, 3PM using the elevation, slope, and aspect rasters with the r.sun.incidout algorithm.

  1. Open r.sun.incidout from the Locator bar or under GRASS -> Raster -> r.sun.incidout in the Processing Panel.
  1. Elevation layer: reprojected_SRTM_Albay
  2. Aspect layer: Aspect
  3. Slope layer: Slope
  4. No. of day of the year: 156
  5. Local (solar) time: 15.00
  6. Check Global (total) irradiance/irradiation

  1. Click Run.
  2. A Global (total) irradiance/irradiation [W.m-2] layer should be added to the Layer Panel. You can rename this layer to GHI.

Working with Vectors

Sampling rasters at points

Exercise 8: Getting solar radiation values at points using the Point Sampling Tool

In this exercise, we’ll use the Point Sampling Tool plugin to get the solar radiation values at each of the sample points.

  1. Load the sampling_points layer.
  2. Open the Point Sampling Tool  from the Plugins toolbar or via Plugins -> Analyses -> Point Sampling Tool. The Point Sampling Tool is able to sample all visible (checked) layers in the Layer Panel so make sure first that the layer you want to sample is visible. In this case, the GHI layer.
  1. General:
  1. Layer containing sampling points: sampling_points
  2. Layers with fields/bands: GHI
  3. Output point vector layer: a file
  1. Fields:
  1. Source: GHI: Band 1, Name: GHI
  1. Click Run.
  2. The output file should be loaded in the Layer Panel.

If you check the attribute table of the created point vector, it should have a field for GHI values.

BONUS: The Point Sampling Tool works for both Vector and Raster Layers. Just make sure that the layers are visible (checked) in the Layer Panel.

Try to use the v.what.rast and/or v.what.vect GRASS algorithms.

Getting zonal statistics

Exercise 9: Getting the average solar radiation values per city/municipality using Zonal Statistics

Polygons can be used to compute zonal statistics based on raster values. For this exercise, we’ll use the Zonal Statistics algorithm to compute the sum, mean, min, and max GHI for the different cities/municipalities.

  1. Open the Zonal Statistics algorithm from the Locator bar or under Raster Analysis -> Zonal statistics in the Processing Panel.
  1. Raster layer: GHI
  2. Vector layer: PHL_municities_Albay
  3. Output column prefix: GHI_
  4. Statistics to compute: sum, mean, minimum, maximum
  1. Click Run.
  2. New attributes/fields should be added to the PHL_municities_Albay layer.

BONUS: Try to use the other zonal statistics algorithms available in QGIS (e.g. v.rast.stats in GRASS)

Reclassification

Reclassification is the process of changing the value of a raster pixel based on its existing value. This is useful when creating categorized rasters. In this exercise, we will reclassify the GHI raster into low, medium, and high values (1, 2, 3) based on the current GHI value of the pixel.

Exercise 9: Reclassifying the GHI raster
  1. Open the Reclassify by table algorithm from the Locator bar or under Raster analysis -> Reclassify by table in the Processing Panel.
  1. Raster layer: GHI
  2. Reclassification table: 0 - 499: 1, 500-999: 2, 1000+: 3
  1. Click Run.
  2. There should be a Reclassified raster layer in the Layer Panel.

BONUS: Reclassify the Slope raster as well. Have values < 20 be equal to 1 and values > 20 be equal to 0.

BONUS: Try the other reclassification algorithms in QGIS.

Raster calculator

The Raster Calculator is one of the most useful tools in QGIS when working with rasters. Aside from raster arithmetic, it also allows for boolean operations, comparisons, etc. In fact, you can use the Raster Calculator to reclassify rasters if you use the proper expressions.

Weighted overlay analysis

Exercise 10: Using the raster calculator
  1. Open the Raster Calculator in the Locator bar, via Raster -> Raster calculator… in the Menu bar, or under Raster analysis -> Raster calculator in the Processing Panel. Several other raster calculators should be available to you depending on the Processing providers activated in your QGIS instance.

  1. Reclassify the slope raster using the raster calculator.
  2. Compute for the suitability score using the following weights: 0.25 for the slope, 0.75 for the GHI.

This work and its contents by Ben Hur S. Pintor is licensed under a

Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.