# Difference between revisions of "R.basin"

## Introduction

The module r.basin has been designed to perform the delineation and the morphometric characterization of a given basin, on the basis of an elevation raster map and the coordinates of the outlet. Please note that it is designed to work only in projected coordinates. Here a tutorial based on NC sample dataset is presented. Please note that the tutorial refers to GRASS 6. In GRASS 7, a few improvements have been introduced to r.basin. See the section below for details.

## Preparation

As a first step, we set the computational region to match the elevation raster map:

```g.region rast=elevation@PERMANENT -ap
projection: 99 (Lambert Conformal Conic)
zone:       0
ellipsoid:  a=6378137 es=0.006694380022900787
north:      228500
south:      215000
west:       630000
east:       645000
nsres:      10
ewres:      10
rows:       1350
cols:       1500
cells:      2025000
```

For the basin's delineation, a pair of coordinates is required. Usually coordinates belonging to the natural river network don't exactly match with the calculated stream network. What we should do is to calculate the stream network first, and then to find the coordinates on the calculated stream network closest to the coordinates belonging to the natural stream network.

```# Calculate flow accumulation map (MFD)
r.watershed -af elevation=elevation@PERMANENT accumulation=accum

# Extract the stream network
r.stream.extract elevation=elevation@PERMANENT accumulation=accum threshold=20 stream_rast=stream_network stream_vect=streams
```

We no longer need the accumulation map:

```g.remove rast=accum
```

Now that we have the calculated stream network, we should choose a pair of coordinates for the outlet belonging to it. Let's choose for example:

```easting=636654.791181 northing=218824.126649
```

We no longer need the stream network map:

```g.remove rast=stream_network
```

## Preparation for multiple basins' analysis

If there are a lot of basins to be analysed, the coordinate pairs of the intersections between the calculated stream network and basins (outlet points) can be found by:

```# Convert basins polygon vector boundaries to lines
v.type input=basinspoly output=basinsline type=boundary,line

# Patch basins lines with stream network
v.patch input=basinsline,streams output=basins_streams

# Find cross points at intersections
v.clean input=basins_streams output=basins_streams_clean error=crosspoints_err tool=break

# Add categories to cross points vector
v.category input=crosspoints_err output=crosspoints

# Add table to cross points vector with columns for category, x and y coordinates
v.db.addtable map=crosspoints columns="cat integer,xcoor double precision, ycoor double precision"

# Upload category to cross points attribute table
v.to.db map=crosspoints option=cat columns=cat

# Upload x and y coordinates to cross points attribute table
v.to.db map=crosspoints option=coor columns=xcoor,ycoor
```

At this point we can create a text file called crosspoints with the coordinates in this form:

```xcoor1 ycoor1
xcoor2 ycoor2
xcoor3 ycoor3
...
```

Now we can parse this coordinates file in order to create a new file called commands in which we'll have the r.basin commands for each sub-watershed, ready to be run in a GRASS session. Let's create a script in python for parsing:

```#!/usr/bin/env python

# Open the ''crosspoints'' text file to read the coordinates
fin = open("crosspoints","r")

# Create the ''commands'' text file
fout = open("commands","w")

# Read the ''crosspoints'' lines
linesout = (line.rstrip().split() for line in fin)

# Build the r.basin command
# Put your threshold parameter instead of threshold=10000 and the name of your DEM instead of map=elevation
cmd = 'r.basin map=elevation prefix=bas%s easting=%s northing=%s threshold=10000\n'

# Write the file ''commands''
fout.writelines(cmd % (n, easting, northing) for n,(easting,northing) in enumerate(linesout))
```

This is a Python script, it is supposed to run externally from GRASS GIS. Save this script as parsing.py in a separate folder on your Desktop, and put the file crosspoints in such folder. To run it, open a terminal and type:

```cd yourfolder
python parsing.py
```

Now in the same folder you will find the file commands, in which we have the r.basin commands to run in GRASS.

## Usage

We can run r.basin:

```r.basin map=elevation@PERMANENT prefix=out easting=636654.791181 northing=218824.126649 threshold=20
```

Prefix parameter is a string given by the user in order to distinguish all the maps produced by every run of the program, i.e. every set of outlet's coordinates. Prefix must start with a letter.

Threshold is the same parameter given in r.watershed and r.stream.extract. Physically, it means the number of cells that determine "where the river begins". (This is an open issue for the hydrological science and a wide literature has been produced on the topic). Threshold value is usually determined by trials and errors. r.basin has a flag -a, which uses an authomatic value of threshold, corresponding to 1km^2. Such value comes from literature and is usually suitable for Italian territory, but could be definitely wrong for other territories. In order to determine the threshold, see also r.threshold and r.stream.preview.

The output of r.basin consists of:

• Several morphometric parameters, which are printed in terminal and also stored in a csv file called out_elevation_parameters.csv, in the working directory;
• Maps;
• Plots.

## Morphometric parameters

The main parameters are:

• The coordinates of the vertices of the rectangle containing the basin.
• The center of gravity of the basin: the coordinates of the pixel nearest to the center of gravity of the geometric figure resulting from the projection of the basin on the horizontal plane.
• The area of the basin: is the area of a single cell multiplied by the number of cells belonging to the basin.
• The perimeter: is the length of the contour of the figure resulting by the projection of the basin on the horizontal plane.

[As a side note, a paper namely "How Long Is the Coast of Britain? Statistical Self-Similarity and Fractional Dimension" points out that perimeter is affected by a fractal problem and the number is mostly meaningless taken by itself. Hence, perimeter should never be mixed with anything from another dataset or even the same data set at a different cell resolution; it is only good as a percentage comparison to its neighbors in the same dataset].

• Characteristic values of elevation: the highest and the lowest altitude, the difference between them and the mean elevation calculated as the sum of the values of the cells divided by the number of the cells.
• The mean slope, calculated averaging the slope map.
• The length of the directing vector: the length of the vector linking the outlet to basin's barycenter.
• The prevalent orientation: is the orientation of the directing vector.
• The length of main channel: is the length of the longest succession of segments that connect a source to the outlet of the basin.
• The mean slope of main channel: it is calculated as follows:

where N is the topological diameter, i.e. the number of links in which the main channel can be divided on the basis of the junctions.

• The circularity ratio: is the ratio between the area of the basin and the area of the circle having the same perimeter of the basin.
• The elongation ratio: is the ratio between the diameter of the circle having the same area of the basin and the length of the main channel.
• The compactness coefficient: is the ratio between the perimeter of the basin and the diameter of the circle having the same area of the basin.
• The shape factor: is the ratio between the area of the basin and the square of the length of the main channel.
• The concentration time (Giandotti, 1934):

Where A is the area, L the length of the main channel and H the difference between the highest and the lowest elevation of the basin.

• The mean hillslope length: is the mean of the distances calculated along the flow direction of each point non belonging to the river network from the point in which flows into the network.
• The magnitudo: is the number of the branches of order 1 following the Strahler hierarchy.
• The max order: is the order of the basin, following the Strahler hierarchy.
• The number of streams: is the number of the branches of the river network.
• The total stream length: is the sum of the length of every branches.
• The first order stream frequency: is the ratio between the magnitudo and the area of the basin.
• The drainage density: is the ratio between the total length of the river network and the area.
• The Horton ratios (Horton, 1945; Strahler, 1957).
```##################################
Morphometric parameters of basin :
##################################
Easting Centroid of basin : 635195.00
Northing Centroid of Basin : 220715.00
Rectangle containing basin N-W : 632870 , 222890
Rectangle containing basin S-E : 637000 , 218720
Area of basin [km^2] : 7.8559625
Perimeter of basin [km] : 16.8287990515
Max Elevation [m s.l.m.] : 151.7396
Min Elevation [m s.l.m.]: 94.82206
Elevation Difference [m]: 56.91754
Mean Elevation [m s.l.m.]: 127.6772
Mean Slope : 2.93
Length of Directing Vector [km] : 2.38880562659
Prevalent Orientation [degree from north, counterclockwise] :
0.913351005341
Compactness Coefficient : 5.32106255399
Circularity Ratio : 0.348580442132
Topological Diameter : 127.0
Elongation Ratio : 0.470961456397
Shape Factor : 1.16984953656
Concentration Time (Giandotti, 1934) [hr] : 3.52654267443
Length of Mainchannel [km] : 6.715361467
Mean slope of mainchannel [percent] : 0.957339
Mean hillslope length [m] : 8145.381
Magnitudo : 621.0
Max order (Strahler) : 5
Number of streams : 884
Total Stream Length [km] : 96.3436
First order stream frequency : 79.0482388377
Drainage Density [km/km^2] : 12.2637550778
Bifurcation Ratio (Horton) : 5.7374
Length Ratio (Horton) : 2.6902
Area ratio (Horton) : 5.6815
Slope ratio (Horton): 1.5533
```

The hypsographic curve provides the distribution of the areas at different altitudes. Each point on the hypsographic curve has on the y-axis the altitude and on the x-axis the percentage of basin surface placed above that altitude. The hypsometric curve has the same shape but is dimensionless. Quantiles of the hypsometric curve are displayed:

```Tot. cells 78560.0
===========================
Ipsometric | quantiles
===========================
145 | 0.025
143 | 0.05
141 | 0.1
137 | 0.25
129 | 0.5
119 | 0.75
121 | 0.7
110 | 0.9
100 | 0.975
```

The width function W(x) gives the area of the cells in the basin at a certain flow distance x from the outlet (distance-area function). Note that the distance is not intended in the euclidean sense, but it's calculated considering the hydrological path of the water.

```Tot. cells 78560.0
Tot. area 7856000.0
Max distance 6809.546521
===========================
Whidth Function | quantiles
===========================
784 | 0.05
1477 | 0.15
2137 | 0.3
2543 | 0.4
2933 | 0.5
3606 | 0.6
4169 | 0.7
5144 | 0.85
6105 | 0.95
```

The output of r.stream.stats is also displayed:

```
Summary:
Max order | Tot.N.str. | Tot.str.len. | Tot.area. | Dr.dens. | Str.freq.
(num)   |    (num)   |     (km)     |   (km2)   | (km/km2) | (num/km2)
5 |        884 |      96.3436 |    7.8339 |  12.2983 | 112.8429

Stream ratios with standard deviations:
Bif.rt. | Len.rt. | Area.rt. | Slo.rt. | Grd.rt.
5.7374 |  2.6902 |   5.6815 |  1.5533 |  1.7962
3.1961 |  1.9514 |   5.2294 |  0.1250 |  0.7482

Order | Avg.len |  Avg.ar  |  Avg.sl |  Avg.grad. | Avg.el.dif
num  |   (km)  |  (km2)   |  (m/m)  |    (m/m)   |     (m)
1 |  0.0750 |   0.0069 |  0.0523 |     0.0384 |  3.1062
2 |  0.1428 |   0.0311 |  0.0342 |     0.0277 |  4.1701
3 |  0.4834 |   0.1732 |  0.0243 |     0.0205 |  9.0641
4 |  2.4205 |   2.1885 |  0.0155 |     0.0134 | 24.1708
5 |  1.1247 |   7.8339 |  0.0091 |     0.0046 |  5.1594

Order | Std.len |  Std.ar  |  Std.sl |  Std.grad. | Std.el.dif
num  |   (km)  |  (km2)   |  (m/m)  |    (m/m)   |     (m)
1 |  0.0600 |   0.0052 |  0.0398 |     0.0287 |  3.2301
2 |  0.1244 |   0.0230 |  0.0250 |     0.0196 |  4.4835
3 |  0.4554 |   0.1353 |  0.0132 |     0.0122 |  7.1065
4 |  2.2874 |   2.3843 |  0.0046 |     0.0061 | 12.9175
5 |  0.0000 |   0.0000 |  0.0000 |     0.0000 |  0.0000

Order | N.streams | Tot.len (km) | Tot.area (km2)
1 |       712 |      53.4067 |  4.8998
2 |       137 |      19.5657 |  4.2562
3 |        31 |      14.9849 |  5.3684
4 |         3 |       7.2616 |  6.5655
5 |         1 |       1.1247 |  7.8339

Order | Bif.rt. | Len.rt. | Area.rt. | Slo.rt. | Grd.rt. | d.dens. | str.freq.
1 |  5.1971 |  1.9040 |   0.0000 |  1.5311 |  1.3892 | 10.8998 | 145.3120
2 |  4.4194 |  3.3847 |   4.5144 |  1.4046 |  1.3472 |  4.5970 | 32.1883
3 | 10.3333 |  5.0075 |   5.5742 |  1.5691 |  1.5365 |  2.7913 |  5.7745
4 |  3.0000 |  0.4646 |  12.6376 |  1.7083 |  2.9120 |  1.1060 |  0.4569
5 |  0.0000 |  0.0000 |   3.5796 |  0.0000 |  0.0000 |  0.1436 |  0.1277
```

## Maps

Since r.basin performs the delimitation of the river basin, all the maps produced are cropped over the basin. They are:

• Raster maps
```g.list rast

out_elevation_accumulation              out_elevation_hillslope_distance
out_elevation_aspect                    out_elevation_horton
out_elevation_dist2out                  out_elevation_shreve
out_elevation_drainage                  out_elevation_slope
out_elevation_hack                      out_elevation_strahler

```
• Vector maps
```g.list vect

out_elevation_basin       out_elevation_network
out_elevation_mainchannel out_elevation_outlet
```

## Graphics

Graphics are stored in the working directory:

• out_elevation_ipsographic.png
• out_elevation_ipsometric.png
• out_elevation_width_function.png

## Known issues

r.basin hasn't been designed for working in lat/long coordinates. This means that if you are working in lat/long coordinates, you need to reproject your map first in order to apply the tool.
Furthermore, r.basin is designed to work in meters unit. The values that you get using feet are nonsense.

## r.basin for GRASS 7

r.basin has a slightly different usage in GRASS 7.

```r.basin [-ac] map=name prefix=prefix coordinates=east,north dir=name [threshold=threshold]
```

It takes in input the coordinates as a pair, separated by a comma. Furthermore, a directory should be indicated, in which the output will be found (the graphics as png and the csv file).

In G7, r.basin has been improved to take in input coordinates not exactly belonging to the river network (but not too far from it). It basically snaps to the closest point belonging to the network. This feature is experimental and might not produce the expected result. To check if the snapped outlet is acceptable, at the end of the computation, two outlet vector maps are produced: the one with the coordinates inserted by the user and the snapped one. If the user is not happy with this latter, should tweak the coordinates to match with the river network.

The outlet_snap vector map has a table associated, called summary, in which all the calculated parameters are stored.