 Hello, this is Hans van der Kraas, senior lecturer at IHDALFT Institute for Water Education. In this video, I'm going to show you how to calculate the bifurcation ratio. The bifurcation ratio is defined as the number of streams of a certain strahler order divided by the number of streams of one higher order. And then for a whole river network, we can calculate the average bifurcation ratio, which can be used for morphometric analysis of rivers. There are other videos here on YouTube that explain more how to use it and interpret the results of the bifurcation ratio. Here I'll focus on the calculation in QGIS. What you need as an input is a stream layer with strahler orders. In other videos, I've demonstrated how to calculate the strahler order stream layer. So here I've added labels with the strahler orders, and you see there how the first order go into the second order, et cetera, into the third one. I chose a simple example here with only three orders, and in the attribute table those orders are in the order field. Now I need to know how many tributaries have a certain strahler order. I go to the processing toolbox, and I use there the tool statistics by categories. And I can choose there the order field, and it says that field to calculate statistics on this empty, it will only do the count, and I only need the count. And then I save it to a table, choose a CSV file, because the output is non-spatial. And then I can run it, and when I open the table, I see there the strahler orders, I can sort them, and then the number of tributaries found in each order. And then manually we can calculate the bifurcation ratio of each order. So for the first one that will be 26 divided by 9, for the second order it will be 9 divided by 16. And then I can take the average, but of course we would like to automate that, especially when we have a lot of strahler orders. Let's first also look at how to create a histogram, do a search for histogram, and then I use the vector layer histogram tool for the order attribute. You can change the number of bins, but here that's okay, close, and then when I click on the results viewer, I can see there the distribution of the strahler order tributaries. In order to automate this, we need to use PyQJS, so I'm going to open the Python console. Here we can type our code. Let's first make a variable that has the strahler order vector layer. Call it channels, and I'm going to add there is a string, the file name, and I can get that from the properties of the layer. I'll simply copy it, paste it here, I'll make it forward slashes, because that seems to work better. And I want to open this vector layer, and therefore I use the iface method with addVectorLayer, the channels file path goes in there, and then the name of the layer, how it will show up in the layers panel, and because it's a shapefile, we use OGR to open it. So that line tool will open the vector layer and add it to the layers panel. Now I also want to define the output file name of the statistics of the count. Let's put it in the same folder and call it statistics.csv. Now I want to run the statistics by categories tool, and the good thing is we don't need to program that, but we can simply use it from the history. So every tool from the toolbox that we run produces this entry in the history, which in fact is a Python dictionary that we can integrate in a Python script. So paste it here, processing.run, and then you see QGIS and then the name of the tool, statistics by categories, and I'm going to split the library to make it more readable. So you see all these different items from the dialogue, input where we define the input path, and output where we have the output path, and we have this values field name that we did not use because we only want the counts, and then categories field name is the order field. So I'm going to replace the input with the string of the input file name, which was stored in the channels variable, and for output I'm going to replace it with the stats variable, which has the output path. In this case, when we want to redo it with different file names, we only need to change it at lines 1 and 3. I also store the whole output in a variable called results, and I would like to open that result table, and I use again iface at vector layer, because the CSV file is also a vector layer in this concept, and we use the output of results, which is stored in the output key of the dictionary, and then that will be of course the string with the stats file name, and it should open in the layers panel with the name stats, and also for CSV files we use OGR to open it. So far, we have done exactly the same as we did in the manual way, but now the real power of using scripting is that we are going to automatically calculate the bifurcation ratio, and it starts with making a list with the count of channels per stroller order. I'm going to make an empty list with the counts, count list I call it, and then I'm going to fill it with the counts per feature, so for each order in the table, for each feature in the stats table, get features, so basically this loops over the rows of the attribute table, and then I want to add two count lists, so I use append, and I want only the counts, so if we go to the stats table, then the counts are in the count field, lower case, so this reads the value from the count field for each feature and stores it in our count list. The next step is to calculate the bifurcation ratio for each stroller order, except the last one, because we always need to use stroller order plus one, and that's not possible with the last one. So I'm creating another list, bifurcation ratio list, br list, and I need to know the amount of stroller orders, which is length of the count list, so the amount of items that we have in our count list, but I don't want the last order, so I subtract one, because now we can create our for loop for order in the range of 0 to orders, so which means for all orders except the last one, do the following, calculate the bifurcation ratio of that order, and the count field contains strings apparently, so therefore I use the float function to convert to floats, otherwise the count make the division, so I use the value of count list for the specific order in the loop, and I divide this by the float of count list, but then the item order plus one, so the count of the next order, which is needed for the equation. I want to print some user feedback, it's print of the screen, the bifurcation ratio of order, and then the order number, these accolads we can plant strings, variables to strings, and then I want to format it with two decimals, so I use this in the accolads, and then use dot format, and we want to order plus one, because those orders start with 0, because it's taken from the list, and then Br, so we get the order number in the string and the bifurcation ratio corresponding to that order, and I want to append it to the Br list, because in the end I want to have the average bifurcation ratio of the whole stream network, so I'm going to calculate the average bifurcation ratio of the stream network, and I do that by taking the sum of the list of the Br divided by the amount of items in that list with the length function, and I also want to print that result to the user, and we also format it with two decimals, and now we can save the file and run it, and there we see the result, and it adds the layers to the layers panel. So I said there are many videos explaining the theory of bifurcation ratio and what you can do with it, I focused on how to calculate it, I hope this was useful. In the description you can find a link to the script in the GitHub repository, and for more free materials I have a look at GISOpenCourseWare.org