Tutorial: Map Algebra with PCRaster Python

Site: OpenCourseWare for GIS
Course: Programming for Geospatial Hydrological Applications
Book: Tutorial: Map Algebra with PCRaster Python
Printed by: Guest user
Date: Wednesday, 8 May 2024, 8:02 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 Miniconda distribution, create an environment with the necessary libraries and run Jupyter Notebooks in Jupyter Labs from the Miniconda 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. Setting up a local Python environment with Mamba

For the next tutorials you need to install the Mamba package manager and PCRaster on your computer and run the Jupyter Notebooks locally.

In the next subchapters you'll learn to:

  • Install Mamba
  • Access the Miniforge prompt
  • Create a Python environment
  • Clone a GitHub repository
  • Use Jupyter Lab

This video also shows the procedure:

4.1. Install Mamba

A package manager is a tool that allows you to manage the dependencies for your project that are not part of the Python standard library. Dependencies are code that is required for your program to function properly, often in the form of packages.

Pip is the most popular package manager for Python, which can install packages from many sources. The Python Package Index, PyPI, is the primary source. 

Although Pip is fine for general Python computing, it is preferred to use package managers that can create isolated environments that can be used to manage dependencies for different projects. Conda is the most popular one. A newer package manager is Mamba, which is faster than conda.

1. Download the Mambaforge installer for Windows from the official Miniforge GitHub repository.

2. Install Mambaforge. Follow these instructions from the excellent tutorial by Christian Mills.

4.2. Accessing the Miniforge prompt

On Windows we use Mamba through the Miniforge prompt, which is a command-line interface for interacting with conda environments.

Follow these steps in the tutorial by Christian Mills.


4.3. Create a Python environment

Before setting up a new Python environment, let's review the advantages of virtual environments and why they are crucial for Python projects (from Christian Mills tutorial).

  • Isolation of Dependencies: Virtual environments create isolated spaces for each project, allowing developers to install and manage different package versions without conflicts. This isolation ensures that the specific dependencies required for one project do not interfere with or break the dependencies of another project.
  • Easier Project Management: Virtual environments help streamline project management by maintaining separate configurations for different projects. Developers can replicate or share project environments with team members, ensuring consistent behavior across various machines. Shareable project environments help developers collaborate, troubleshoot, and deploy projects.
  • Simplified System Maintenance: Using virtual environments helps developers avoid cluttering their system-wide Python installation with numerous packages and varying versions. System maintenance is cleaner as developers can easily add, update, or remove packages within individual project environments without affecting other projects or the system as a whole.

Let's create an environment with the name tutorials and install the packages pcraster, qgis, matplotlib, jupyter lab, pycrs, numpy and git that we need for the tutorials.

1. At the Miniforge Prompt type the following command and press <Enter>:

mamba create --name tutorials pcraster qgis matplotlib jupyterlab pycrs numpy git 
Mamba will look for the packages.


2. Confirm the changes by typing Y.


When the packages are downloaded and installed Mamba will tell you how to activate the environment:


3. Activate the tutorials environment by typing:

mamba activate tutorials
You'll use this command in the future every time you start a new Miniforge prompt in order to use the tutorials environment.

In the next subchapter you'll learn how to clone the repository with tutorials from the GitHub repository.

4.4. Clone a GitHub repository

Now we've setup the environment, we need to get the Jupyter Notebooks and data that we'll use in this course.

The Jupyter Notebooks and related data are stored in a GitHub repository. We can easily clone the GitHub repository, which means that we'll create a local copy of the contents of the repository.

Git is a distributed version control system that tracks changes in any set of computer files and is used for coordinating work among programmers collaboratively developing source code during software development.

With git commands we can interact with git repositories such as GitHub. A nice way to learn git commands is the serious game Oh My Git.

In the next steps we're going to use git commands to clone the GitHub repository with the Jupyter Notebooks and related data.

1. 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).

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

3. Click the Code button.


4. Copy the HTTPS link.

5. Now go back to the Miniforge Prompt and use the following git command:

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

Note that the https link was pasted.

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

In the next subchapter you'll learn how to launch a Jupyter Notebook locally.


4.5. Use Jupyter Notebooks with Jupyter Lab

Now we have the Jupyter Notebooks and related data cloned from the GitHub repository, we can use them.

1. Go to the PCRasterTutorials subdirectory

2. Launch Jupyter Lab with the following command:

jupyter lab

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

If your browser doesn't open automatically or if you would like to use another browser, check the links that are shown on the screen.

5. Spatial interpolation of borehole data

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

  1. Run the command from the PCRasterTutorials directory:
    jupyter lab

  2. Go to the subdirectory \PCRasterTutorials\GroundwaterInterpolation
  3. Click on  Groundwater_level.ipynb so the Jupyter Notebook will open in a tab.
  4. Click right in the Jupyter Notebook and choose New Console for Notebook to add a Python Console that is linked to the notebook.
  5. Click the + next to the Groundwater_level.ipynb tab to add a Terminal, which you can use to navigate your file system with the CLI command that you've learned in Module 1.
  6. Arrange the tabs in such away (drag and drop) that your screen looks like this:
  7. Follow the tutorial in the Jupyter Notebook, use the Python Console to test code and use the Terminal to navigate the file system.

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 Jupyter Lab interface:

6. Stream and Catchment Delineation

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

  1. Go to the subdirectory \PCRasterTutorials
  2. Run the command
    jupyter lab
  3. In Jupyter Lab browse to the PCRasterCatchmentDelineation folder, 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.

You can write the code also in the QGIS Python Console. In that way it's easier to visualise the results. You can use QGIS from your miniconda environment or the standard QGIS on Windows if you have installed PCRaster through the OSGeo4W installer.

This video shows the exercise applied in the QGIS Python Console: