 Hello, this is Hans van der Kwas senior lecturer at IHC Delft Institute for Water Education. In this video I've done to show you how you can use PCRuster within QGIS and even make tools that use the operations that you can do with PCRuster. PCRuster is great software to make our own environmental models. It comes with many building blocks that are based on map algebra that we can use to make spatial and spatial dynamic models. Here you see an overview of all the operations that are available and wouldn't it be great to have those also available in the processing toolbox of QGIS? Well, in this video I'm going to show a way to make that possible. Each of these operations are very well documented and come with examples. And because it is in Python, we can also integrate it in QGIS and QGIS is also now available in the KONDA repository so we can use it in Anaconda as well as PCRuster and that gives a lot of possibilities. So here you see that we can install PCRuster using KONDA and that it is cross-platform. And there's a little instruction here that you can follow. It's for mini-KONDA, which is a bit lighter. Here I show how to do it for Anaconda. You just go to anaconda.com, click Get Started, install the individual edition and then click on Download and then choose your operating system and if you need 64 or 32-bit. Here we use 64-bit. After installing you can find the Anaconda prompt where you can type these commands. And the first thing that we are going to do is to create the environment and install PCRuster. Finding these instructions we can type KONDA create and then we give a name to the new environment and I'm going to call this one QGIS PCRuster and then from which channel it has to be downloaded, which repository, so from KONDAforge and then I have to give the Python version and here I'm going to use 3.7 for PCRuster and that's also the version that the QGIS version uses that I want to install. I also installed a few other libraries so you can add more libraries here and they will install in one go as well as create the environment. So you run it and it will download all the necessary packages and start to install it taking into account the dependencies. That's a great thing about using KONDA for installing libraries. We can go to this new environment by typing KONDA activate and then the name of the environment QGIS PCRuster and now we are in the environment. Now we are going to install QGIS in this environment and on the GitHub page which is also in the description of this video you can find the command to do that. We also get it from the KONDAforge and we can specify the specific version and here I'm going to install the latest LTR version that's 3.10.10 that also takes a while I have increased the speed. It has some issues with solving the environment but KONDA then tries it in another way and then it succeeds. There we are and now we can just type QGIS to start the software. There we see the long term release has started and there we are. Now let's open the Python console from the plugins menu and see if we can access PCRuster. So let's see if PCRuster works. So we can type here from PCRuster import and then it doesn't give an error which means it imports the library and let's read a DEM from disk, give the full path and that also works. Now we want to see it so I can use the Agila tool and there it is. So we can access PCRuster and visualize with Agila from QGIS and I can also use functions of PCRuster. So here I calculate the slope from the DEM and then I can visualize it in Agila. So basically we have now a much easier roster calculator with lots of extra tools that we can access from PCRuster. So we can also write these calculations to disk using report. Let's open it now in the map canvas instead of in Agila. So I'm going to use the path, let's use the one from the DEM and put it in a variable and then I use this PyQGIS function to visualize the DEM in the map canvas. So with the string DEM it gives the name of the layer and then ifase addRusterLayer loads it. And let's do the same for the slope map, I'll directly put the string in there and let's call the layer slope, GDOL is added because then it knows that's a GDOL compatible format so it knows how to open it and that works. So we can open PCRuster layers in that way from scripts. You can also simply drag and drop the map files to the map canvas. As said PCRuster doesn't store the projections so we need to identify them. Let's put our project on the correct projection. Now because I can make scripts using PCRuster and QGIS I can also make processing scripts and on the GitHub page there are some examples that I'm going to demonstrate here. First I show you how to make it. If you download them from the GitHub site you can go to your profile and there under scripts you can paste them. So all these scripts can be found in the processing toolbox and I can edit the scripts so you can see how I make them. It uses a template and you have to fill in certain parts to make it run from the processing toolbox. Now the best way to do that is to just go to the Python button and say create new script from template. And there you see the template. And then basically you fill in the parts that you need to use PCRuster and define the interface. Now if you want to build these operations into QGIS then you need to know what they need. So let's look at the catchment function. It needs the local drain direction map and points where the points are the outlets. Local drain direction map is the flow direction and so we can make the script. Here I show what I've put in the script. I have to of course import PCRuster to get access to the PCRuster tools and some from QGIS that I need. Then you define the different inputs and outputs. You just fill it in in the template and then here also you make some changes to the template so it points to the inputs and outputs that you want. And then here under process algorithm is basically what you write. So input LDD defines the path that the user gives and the outlet and the catchment also. And then we do the reading from the map of the local drain direction map using that path that the user defined. Then the outlets are read and the catchments are calculated from the outlets and then we report it to the disk and we open it in QGIS in a map canvas by using these lines. So I have a good look at it from the repository and see how it works and in a similar way you can implement all of these PCRuster operations. Now let's apply these tools to the root catchment. So we're going to follow the catchment delineation procedure. I drag all the layers to the map canvas. There's a little bug so I have to settle the projections and the M is in 4326 and we have the bounding box. So the first step is to mosaic the tiles. We'll do that with a virtual raster. So we're going to use QGIS tools and PCRuster tools together and this is of course just from QGIS where we can easily mosaic the tiles and remove the ones that we don't need. I set the projection and now I'm going to export this to a new projection and click to the bounding box to change the projection, make sure the bounding box is selected and I can customize here the spatial resolution to 30 meters because we re-project the no data can be useful to fill up the no data pixels. Remove the bounding box. We see that our DM is nicely clipped. We set the projection and now it's in the UTM32 North and then we need to convert this to the PCRuster format in order to use the PCRuster tools. So I made a tool for the conversion which uses some GDAL Python scripts which are you have .map files and output. We go and I run it and the file is loaded. When you hover your mouse over the layer name you can see which one is which so I'm going to remove the TIFF file and set the projection of the layer. The PCRuster layer don't have projections but in QDS we can indicate what we want to use and then I want to calculate the local drain direction map which is the flow direction map in PCRuster terminology. I need to specify the output. Let's call it flow direction. The DM is of course the input and then I run it. That takes some time. Here I've speeded up a bit and there's the output. Opens nicely. We can set the projection. PCRuster uses the values of the numeric path on your keyboard for the flow directions. But Aguila has also a nice way of visualizing the PCRuster flow direction and as we've seen before we can just open the PCRuster layers with Aguila using the Python console. There it is. It looks strange but when we zoom in we can see the flow direction of each pixel with arrows and we can query the directions. After calculating the flow direction we can calculate the stream order and the stream order function in PCRuster calculates the strahler order and the input is the LDD, the local drain direction map or the flow direction map. Let's call the output strahler and I run it and there it is. Set the projection and let's style it. You can use the palette of unique values and use different shades of blue and there we see that the darker the blue, the bigger the river. That looks great and then in order to find the rivers we do the strahler threshold calibration, as you can see in other videos, so we can simply use the raster calculator on this PCRuster map. The only thing is we cannot write the output to PCRuster maps but we can use the geotiff format and we run it and we use the palette of unique values again to highlight the two pixels here which are strahler, larger equal to 8. Make them blue, remove the background, maybe not remove it because I want to compare also with the calculation using PCRuster a bit. With the strahler calculation you're going to compare it with the background reference map to see if the rivers match so you can try it for strahler 7, 8, 9 etc. to see which one gives the best match, this one looks quite okay and these are all explained in other videos and tutorials. Set the projection and I'm going to show you how to calculate this strahler using PCRuster in the Python console because that's also a quick way of doing raster calculations. First I'm going to read the strahler map from disk and then I can use map algebra. I'll make a variable strahler 8 which then equals strahler, larger equal to 8 and that's a condition similar to the raster calculator of QGIS which gives a Boolean map with true for larger equal than 8 and false for less than 8. I write it to the disk, let's give it a different name to give the full path and let's visualize it with the same command that we used previously. There we are, now we can compare both, I'm copying the style and I set the projection and this is a verification if they completely overlap and you see that they are exactly the same as can be expected. So there's no difference between the raster calculator from PCRuster or from QGIS. Now let's assume that with this strahler threshold we have the river, the rule river then we need to define the outlet and there is approximately where the rule gets into the mouse. I can use the coordinate capture tool, pick a coordinate, let's pick this one and then in the command prompt I can make a file with the coordinates because PCRuster needs a raster layer with that pixel which identifies the outlet, you can also have multiple pixels with different ID numbers, here I'm going to create a file outlet.txt and I'm copying the coordinates, so the format is x space, y space and then an ID number, non-zero ID number and then it will create in the end a catchment with that ID number, so if we have multiple points for each of these points it will create a catchment. But first we need to make it a map, we use the call to map tool, you can find all these tools also on the PCRuster website, it's a nominal map that we need therefore I use minus M and we need to fit it to the same dimensions as our DEM, so therefore I define as a clone DEM clip top map and then this one point is converted to a raster that fits the dimensions, I can simply drag it to the map canvas, there it is, the black pixel and I can style it, so the red pixel is the outlet that we're going to use to delineate the full catchment, there I use calculate catchment to see that I didn't set the projections yet, it's just good practice, it doesn't do much with it PCRuster because it doesn't need it, but in order to avoid mistakes it's always good to set it, so it uses the flow direction map and the outlet to define the catchment of that point in this case, then I run it, there it is, let's zoom out and first set the projection, there we see our catchment, looks slightly different than the one that we delineate with the other procedures because this one takes a bit better care of the mine which becomes its own catchment and the LDD function has more possibilities that I would like to later integrate in the scripting tool interface so we can play around with the fill sinks option, the flow direction algorithm combines the fill sinks with calculating the flow direction, so why is this so useful to integrate PCRuster here while we also have the other QGIS tools, in my opinion it's always great to have access to many tools, there is some overlap but there are also tools available in PCRuster that are not available in QGIS and also the raster calculator of PCRuster is much more advanced if you want to do conditions and boolean logic and much easier to write, so I hope you like this and follow me on the YouTube channel and keep an eye on tutorials coming up on gisopencourseware.org