Tutorial: Map Algebra with PCRaster Python

Site: Open Courseware for GIS
Course: Programming for Geospatial Hydrological Applications
Book: Tutorial: Map Algebra with PCRaster Python
Printed by: Guest user
Date: Thursday, 5 August 2021, 5:07 AM

1. Introduction

This tutorial will first present some theory on map algebra and will introduce the PCRaster Environmental Modelling Language.

The tutorial contains three Jupyter Notebook tutorials:

  • Map algebra for finding inaccessible wells in a study area
  • Interpolation of borehole data using Inverse Distance Weighting (IDW) and Thiessen methods
  • Stream and catchment delineation

The first one will be run online. The other two will be run locally. You'll learn how to install the Anaconda distribution, create an environment with the necessary libraries and run Jupyter Notebooks from the Anaconda prompt.

After this module you'll be able to:

  1. Explain the concepts of map algebra
  2. Convert rasters using GDAL in Python
  3. Use PCRaster in Python for map algebra
  4. Use PCRaster for interpolations
  5. Use PCRaster for stream and catchment delineation

2. Theory

The PCRaster Environmental Modelling Language

The video below will introduce you to the PCRaster environmental modelling language.

More information about PCRaster can be found at: http://www.pcraster.eu. You can subscribe there to the PCRaster mailing list to read and post questions and answers to the community.

Theory of Map Algebra

Watch this lecture to learn more about Map Algebra in GIS:


3. Spatial Analysis with Map Algebra

Use the button below to run the PCRaster Map Algebra Jupyter Notebook in Binder:

Open In Binder

If you are more experienced and want to run the Jupyter Notebook locally you can download it here:

https://github.com/jvdkwast/PCRasterTutorials (GitHub)

The data used in this tutorial is an artificial dataset.

This video shows the result of the Map Algebra tutorial:


4. Install Anaconda and libraries

For the next tutorials you need to install Anaconda and PCRaster on your computer and run the Jupyter Notebooks locally. You can use the instructions from the video above. Here are the steps:

1. Install Anaconda from here
2. Open Anaconda Prompt from the Windows Start Menu
3. Type:

conda create --name tutorials -c conda-forge pcraster spyder matplotlib jupyter pycrs numpy qgis

This will create a new environment with the name tutorials and will install the necessary libraries that we'll use.

4. Activate the new environment by typing

conda activate tutorials

5. Use the commands to go to the folder where you want to save the tutorials (note that the next step will create a subdirectory PCRasterTutorials there).

6. Go to https://github.com/jvdkwast/PCRasterTutorials

7. Click the Code button

8. Copy the HTTPS link

9. In the Anaconda prompt type:

conda install -c anaconda git

With this command we add the git library to our environment. With git we can interact with git repositories such as GitHub.

10. Now run the following command:

git clone https://github.com/jvdkwast/PCRasterTutorials.git

This will download all the tutorial materials of this and the next module to the subdirectory PCRasterTutorials.

To run the tutorials go to the subdirectory of the tutorial and run the command

jupyter notebook

This will open your browser and you can choose the notebook.

This video shows the procedure:


5. Spatial interpolation of borehole data

To run the Jupyter Notebook of this tutorial make sure you have followed the instructions for installing Anaconda and downloading the tutorials in the previous unit. We assume that you're in the Anaconda prompt and that you're in the tutorials environment.

  1. Go to the subdirectory \PCRasterTutorials\GroundwaterInterpolation
  2. Run the command
    jupyter notebook
  3. In the internet browser click on  Groundwater_level.ipynb and follow the tutorial.

The tutorial is based on this QGIS tutorial and uses data from the Borehole Database in the Stampriet Transboundary Aquifer from the Orange-Senqu River Basin GIS Server. If you want to visualise the data and results in QGIS, the projection is EPSG:32734.

This video shows the whole exercise in the Spyder IDE:

6. Stream and Catchment Delineation

To run the Jupyter Notebook of this tutorial make sure you have followed the instructions for installing Anaconda and downloading the tutorials as explained before. We assume that you're in the Anaconda prompt and that you're in the tutorials environment.

  1. Go to the subdirectory \PCRasterTutorials\PCRasterCatchmentDelineation
  2. Run the command
    jupyter notebook
  3. In the internet browser click on ContentsCatchmentDelineation.ipynb and follow the tutorial.

This tutorial uses data (included in the Data folder) from the Shuttle Radar Topography Mission: SRTM 1-Arc Second. If you want to apply it to other areas, you can download the data from USGS Earth Explorer.

This video shows the exercise using the Spyder IDE: