<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.9.0">Jekyll</generator><link href="https://nunataryuk.github.io//feed.xml" rel="self" type="application/atom+xml" /><link href="https://nunataryuk.github.io//" rel="alternate" type="text/html" /><updated>2020-11-29T10:00:25+00:00</updated><id>https://nunataryuk.github.io//feed.xml</id><title type="html">Nunataryuk</title><subtitle>Codes and scripts</subtitle><entry><title type="html">Basin delineation: 4 GRASS basin delineation</title><link href="https://nunataryuk.github.io//basindelineate/basin-delineate-04/" rel="alternate" type="text/html" title="Basin delineation: 4 GRASS basin delineation" /><published>2020-10-28T00:00:00+00:00</published><updated>2020-10-28T00:00:00+00:00</updated><id>https://nunataryuk.github.io//basindelineate/basin-delineate-04</id><content type="html" xml:base="https://nunataryuk.github.io//basindelineate/basin-delineate-04/">&lt;script src=&quot;https://karttur.github.io/common/assets/js/karttur/togglediv.js&quot;&gt;&lt;/script&gt;

&lt;h1 id=&quot;introduction&quot;&gt;Introduction&lt;/h1&gt;

&lt;p&gt;In this part (4) of the of the Basin delineation tutorial series you will run a GRASS command line script generated in &lt;a href=&quot;../basin-delineate-03&quot;&gt;part 3&lt;/a&gt;. The script delineates a vector polygon for each distilled outlet point generated from &lt;a href=&quot;../basin-delineate-03&quot;&gt;part 3&lt;/a&gt;. Then another command line script will call GDAL for assembling all polygons into a single data source. You need to manually inspect this file to determine if and what editing is required.&lt;/p&gt;

&lt;p&gt;if you use the default ‘MOUTH’ process alternative, the script will first manipulate the input Digital Elevation Model (DEM) to better represented the flow path in river mouths and then use the manipulated DEM for identifying the extent of all larger river basins.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;You must have setup GRASS 7 and imported a DEM as described in &lt;a href=&quot;../../basindelineatesetup&quot;&gt;Installation &amp;amp; Setup&lt;/a&gt;. Then you must have completed the GRASS watershed processing as outlined in &lt;a href=&quot;../basin-delineate-02&quot;&gt;part 2&lt;/a&gt; and run the Python script &lt;em&gt;Basin_extractor&lt;/em&gt; as described in &lt;a href=&quot;../basin-delineate-03&quot;&gt;part 3&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;mouth-processing&quot;&gt;MOUTH processing&lt;/h3&gt;

&lt;p&gt;if you set the parameterization of &lt;em&gt;basin_extract&lt;/em&gt; in &lt;a href=&quot;../basin-delineate-03&quot;&gt;part 3&lt;/a&gt; to &lt;em&gt;outlet=’MOUTH’&lt;/em&gt;, the generated GRASS command line script will be called &lt;span class=&quot;file&quot;&gt;“region”_grass_basin_mouth2.sh&lt;/span&gt;, where &lt;em&gt;region&lt;/em&gt; is the study area. It will be saved in your project data directory where all other spatial datasets are exported. As defined in the xml file and following the general structure of KArttur’s GeoImagine Framework.&lt;/p&gt;

&lt;p&gt;The GRASS shell script file start with commands for creating a virtual DEM and then rerunning &lt;a href=&quot;&quot;&gt;r.watershed&lt;/a&gt;. Optionally the user can export intermediate raster layers by removing the ‘#’ sign. The generated commands for the Amazonia region used as example in this tutorial series are shown below. Your version will be similar, but with the regional identifier (&lt;em&gt;amazonia&lt;/em&gt; in the example) replaced by your study region identifier. Also other parts of the file names may differ.&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# Creating virtual DEM for directing hydrological flow in river basin mouths

# Import outlet points vector
v.in.ogr -o input=/Volumes/GRASS2020/GRASSproject/SRTM/region/basin/amazonia/0/basin-mouth2-outlet_drainage_amazonia_0_cgiar-250.shp output=basin_distil_mouth2_shorewall_pt --overwrite

# rasterize outlet points
v.to.rast input=basin_distil_mouth2_shorewall_pt output=basin_distil_mouth2_shorewall_pt type=point use=val value=1 --overwrite

# Add outlet points to river mouth DEM with all cells=1
r.mapcalc &quot;basin_distil_mouth2_lowlevel_DEM = if(isnull(basin_distil_mouth2_shorewall_pt),lowlevel_outlet_costgrow+1,basin_distil_mouth2_shorewall_pt)&quot; --overwrite

# cost grow analysis from new outlets over river mouth DEM
r.cost -n input=basin_distil_mouth2_lowlevel_DEM output=basin_distil_mouth2_mouth_dist start_points=basin_distil_mouth2_shorewall_pt max_cost=228 --overwrite

# export cost grow analysis (optional)
# r.out.gdal -f input=basin_distil_mouth2_mouth_dist format=GTiff type=Int16 output=/Volumes/GRASS2020/GRASSproject/SRTM/region/basin/amazonia/0/basin-distil-mouth2-mouth-dist_drainage_amazonia_0_cgiar-250.tif --overwrite

# Invert cost grow to create mouth flow route DEM directing flow towards outlet point
r.mapcalc &quot;basin_distil_mouth2_routing_dem = int(basin_distil_mouth2_mouth_dist-230)&quot; --overwrite

# export the mouth flow route DEM (optional)
# r.out.gdal -f input=basin_distil_mouth2_routing_dem format=GTiff type=Int16 output=/Volumes/GRASS2020/GRASSproject/SRTM/region/basin/amazonia/0/basin-distil-mouth2-routing-dem_drainage_amazonia_0_cgiar-250.tif --overwrite

# combine mouth flow route DEM with original DEM and add the shorewall
r.mapcalc &quot;basin_distil_mouth2_basin_dem = if(isnull(basin_distil_mouth2_routing_dem),(if(isnull(DEM@PERMANENT),shorewall,DEM@PERMANENT)),basin_distil_mouth2_routing_dem)&quot; --overwrite

# export the hydrological corrected DEM (optional)
# r.out.gdal -f input=basin_distil_mouth2_basin_dem format=GTiff type=Int16 output=/Volumes/GRASS2020/GRASSproject/SRTM/region/basin/amazonia/0/basin-distil-mouth2-basin-dem_drainage_amazonia_0_cgiar-250.tif --overwrite

# run r.watershed for the new outlet points and the virtual (hydrologically corrected) DEM
r.watershed -a convergence=5 elevation=basin_distil_mouth2_basin_dem accumulation=basin_distil_mouth2_wshed_acc drainage=basin_distil_mouth2_wshed_draindir_draindir threshold=2000

# convert updrain accumulation raster to byte format (optional)
# r.mapcalc &quot;basin_distil_mouth2_wshed_acc_ln = 10*log(basin_distil_mouth2_wshed_acc)&quot; --overwrite

# Set color ramp for upstream accumulation raster (optional)
# set color ramp for byte version of updrain accumulationr.colors map=basin_distil_mouth2_wshed_acc_ln color=ryb

# export the visualised upstream accumulation raster (optional)
# r.out.gdal -f input=basin_distil_mouth2_wshed_acc_ln format=GTiff type=Byte output=/Volumes/GRASS2020/GRASSproject/SRTM/region/basin/amazonia/0/basin-distil-mouth2-wshed-acc-ln_drainage_amazonia_0_cgiar-250.tif --overwrite

# add column &quot;updrain&quot; to the new outlet vector
v.db.addcolumn map=basin_distil_mouth2_shorewall_pt columns=&quot;updrain DOUBLE PRECISION&quot;

# extract data from r.watersehd updrain to the column &quot;updrain&quot; in the outlet point map
v.what.rast map=basin_distil_mouth2_shorewall_ptc column=updrain raster=basin_distil_mouth2_wshed_acc
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Run the shell script from your GRASS terminal with the active &lt;em&gt;mapset&lt;/em&gt; being the same as when you processed the data in &lt;a href=&quot;../basin-delineate-02&quot;&gt;part 2&lt;/a&gt;. Before you can run the commands you have to change the permission of the shell file (&lt;span class=&quot;file&quot;&gt;.sh&lt;/span&gt;) itself. To change the permissions to an executable script, type at the terminal:&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; chmod 777 “region”_grass_basin_mouth2.sh &lt;/span&gt;.&lt;/p&gt;

&lt;p&gt;Tu run the file you need to give either the relative or full path, where the relative path would be:&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; ./”region”_grass_basin_mouth2.sh &lt;/span&gt;.&lt;/p&gt;

&lt;p&gt;The full path depends on where you put the GRASS location.&lt;/p&gt;

&lt;p&gt;You can run the script now, as it will take several hours or even days to run. But that is not because of the commands above, but the commands following that create polygons of full river basins for each outlet point.&lt;/p&gt;

&lt;h3 id=&quot;grass-script-for-basin-polygon-delineation&quot;&gt;GRASS script for basin polygon delineation&lt;/h3&gt;

&lt;p&gt;For each identified outlet point from the Python script &lt;em&gt;basin_extract&lt;/em&gt; in &lt;a href=&quot;../basin-delineate-02&quot;&gt;part 2&lt;/a&gt; the delineation of the associated river basin contains the following steps:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;identify all upstream cells from the outlet coordinates&lt;/li&gt;
  &lt;li&gt;convert the identified cell region to a polygon&lt;/li&gt;
  &lt;li&gt;clean the polygon form errors&lt;/li&gt;
  &lt;li&gt;build the polygon attribute table&lt;/li&gt;
  &lt;li&gt;export the polygon vector to an ESRI shape file&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To save disk space, the raster version of the basin, created in the very first step, is deleted once the raster is vectorized in the second step. The actual GRASS commands for each outlet looks like the example below:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;r.water.outlet input=basin_distil_mouth2_wshed_draindir output=basin_mouth2_000001 coordinates=-7928554.691232,1295538.184247 --overwrite
r.to.vect input=basin_mouth2_000001 output=basin_mouth2_000001 type=area --overwrite
g.remove -f type=raster name=basin_mouth2_000001 --quiet
v.clean input=basin_mouth2_000001 output=basin_mouth2_000001c type=area tool=prune,rmdupl,rmbridge,rmline,rmdangle thresh=0,0,0,0,-1 --overwrite
v.db.addcolumn map=basin_mouth2_000001c columns=&quot;x_mouth DOUBLE PRECISION&quot;
v.db.addcolumn map=basin_mouth2_000001c columns=&quot;y_mouth DOUBLE PRECISION&quot;
v.db.update map=basin_mouth2_000001c column=x_mouth value=-7928554.691232
v.db.update map=basin_mouth2_000001c column=y_mouth  value=1295538.184247
v.db.addcolumn map=basin_mouth2_000001c columns=&quot;mouth_id INT&quot;
v.db.addcolumn map=basin_mouth2_000001c columns=&quot;basin_id INT&quot;
v.db.update map=basin_mouth2_000001c column=mouth_id  value=1
v.db.update map=basin_mouth2_000001c column=basin_id  value=1
v.to.db map=basin_mouth2_000001c type=centroid option=area columns=area_km2 units=kilometers
v.out.ogr input=basin_mouth2_000001c type=area format=ESRI_Shapefile output=/Volumes/GRASS2020/GRASSproject/SRTM/region/basin/amazoniax/0/basin_mouth2_000001c.shp --overwrite
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;As there can be thousands of outlet points the processing of the entire shell script might take a long time. But progress is reported int he &lt;span class=&quot;app&quot;&gt;Terminal&lt;/span&gt; window, indicating the number of the outlet under process. And as the outlets are sequential numbers you can always find out how far the processing has gone.&lt;/p&gt;

&lt;h3 id=&quot;assembling-basin-polygons-to-a-single-shape-file&quot;&gt;Assembling basin polygons to a single shape file&lt;/h3&gt;

&lt;p&gt;Looking at each polygon vector representing a river basin individually is not doable. To collect all the produced polygons into a single shape file, the Python package in &lt;a href=&quot;../basin-delineate-02&quot;&gt;part 2&lt;/a&gt; produced another shell script, &lt;span class=&quot;file&quot;&gt;“region”_ogr2ogr_basin_mouth2.sh&lt;/span&gt;. This script requires that you have installed GDAL (see &lt;a href=&quot;../basindelineatesetup&quot;&gt;Installation &amp;amp; Setup&lt;/a&gt;). The command &lt;em&gt;ogr2ogr&lt;/em&gt; is the vector processing tool of GDAL. The script simply assembles all the polygons into single vector file. In the first line, the first polygon is simply copied to a new file, all remaining commands then &lt;em&gt;append&lt;/em&gt; data to the first file.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ogr2ogr -skipfailures -nlt MULTIPOLYGON /Volumes/GRASS2020/GRASSproject/SRTM/region/basin/amazonia/0/basin-mouth2-outlet-area_drainage_amazonia_0_cgiar-250.shp /Volumes/GRASS2020/GRASSproject/SRTM/region/basin/amazoniax/0/basin_mouth2_000001c.shp
ogr2ogr  -append -skipfailures -nlt MULTIPOLYGON /Volumes/GRASS2020/GRASSproject/SRTM/region/basin/amazonia/0/basin-mouth2-outlet-area_drainage_amazonia_0_cgiar-250.shp /Volumes/GRASS2020/GRASSproject/SRTM/region/basin/amazoniax/0/basin_mouth2_000002c.shp
ogr2ogr  -append -skipfailures -nlt MULTIPOLYGON /Volumes/GRASS2020/GRASSproject/SRTM/region/basin/amazonia/0/basin-mouth2-outlet-area_drainage_amazonia_0_cgiar-250.shp /Volumes/GRASS2020/GRASSproject/SRTM/region/basin/amazoniax/0/basin_mouth2_000003c.shp
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;the script will only take a few minutes, and then you should have a new ESRI shape file with all the delineated basins.&lt;/p&gt;

&lt;h3 id=&quot;inspect&quot;&gt;Inspect&lt;/h3&gt;</content><author><name></name></author><category term="basindelineate" /><category term="GRASS7" /><category term="watershed" /><category term="basin" /><category term="delineation" /><category term="hydrology" /><summary type="html">Use GRASS7 for delineating river basins</summary></entry><entry><title type="html">Basin delineation: 3 Python distilling</title><link href="https://nunataryuk.github.io//basindelineate/basin-delineate-03/" rel="alternate" type="text/html" title="Basin delineation: 3 Python distilling" /><published>2020-10-27T00:00:00+00:00</published><updated>2020-10-27T00:00:00+00:00</updated><id>https://nunataryuk.github.io//basindelineate/basin-delineate-03</id><content type="html" xml:base="https://nunataryuk.github.io//basindelineate/basin-delineate-03/">&lt;script src=&quot;https://karttur.github.io/common/assets/js/karttur/togglediv.js&quot;&gt;&lt;/script&gt;

&lt;h1 id=&quot;introduction&quot;&gt;Introduction&lt;/h1&gt;

&lt;p&gt;Part 3 of the tutorial series on Basin delineation covers a custom written Python package, &lt;em&gt;basin_extract&lt;/em&gt;. The outcome of the package is a vector map with distilled outlet point, and two shell scripts. One for GRASS to actually delineate all the preliminary basins, and one for GDAL to assemble all basins into one data source (polygon vector file). The scripts are then used in &lt;a href=&quot;../basin-delineate-04&quot;&gt;part 4&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;You must have setup GRASS 7 and imported a DEM as described in &lt;a href=&quot;../../basindelineatesetup&quot;&gt;Installation &amp;amp; Setup&lt;/a&gt; of this blog. And then you must have completed the GRASS watershed processing as outlined in &lt;a href=&quot;../basin-delineate-02&quot;&gt;part 2&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;python-processing&quot;&gt;Python processing&lt;/h3&gt;

&lt;p&gt;If you followed the Installation &amp;amp; Setup and part 2 of this series, you should have candidate basin outlets available as vector point files in shape format. You can use &lt;span class=&quot;app&quot;&gt;QGIS&lt;/span&gt; (or another GIS software) to inspect the data source.&lt;/p&gt;

&lt;p&gt;The aim of this part of the basin delineation is threefold:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;to identify outlet points to include for generating preliminary basins,&lt;/li&gt;
  &lt;li&gt;to retain only a single outlet point per separate mouth - including one point per mouth for rivers (basins) that have multiple mouths (like e.g. a river delta),&lt;/li&gt;
  &lt;li&gt;to generate the GRASS command line code for basin delineation.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For these tasks I have created a Python script called &lt;a href=&quot;#&quot;&gt;basin_extractor&lt;/a&gt; available through GitHub. This script will also be used in &lt;a href=&quot;../basin-delineate-05&quot;&gt;part 5&lt;/a&gt; to join basins that belong to the same river and remove all basins that do no fall completely within the map region (as their complete basins are not represented in the map).&lt;/p&gt;

&lt;h4 id=&quot;default-distillation-method&quot;&gt;Default distillation method&lt;/h4&gt;

&lt;p&gt;Having experimented with different alternatives for creating a set of preliminary basins, the most resilient method is to create a virtual DEM directing flow out of the basin mouths towards a single outlet point (raster cell). Upstream basin delineation is rather problem free, given that the DEM is correct. As pointed out in &lt;a href=&quot;../basin-delineate-02&quot;&gt;part 2&lt;/a&gt; it is the flow in wide channels and at outlets that cause problems. In particular if the basins are to be used for estimating water balances of entire basins (i.e. at the basin outflows).&lt;/p&gt;

&lt;p&gt;The default method thus entails the following:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Candidate outlets defined from MFD and SFD combined,&lt;/li&gt;
  &lt;li&gt;virtual coastline walls enclosing the basin mouths,&lt;/li&gt;
  &lt;li&gt;a single cell punched hole in each wall for directing basin outflow,&lt;/li&gt;
  &lt;li&gt;changing outlet point from the “real” position to the punched hole,&lt;/li&gt;
  &lt;li&gt;virtually tilting basin mouths towards the punched hole.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This ensures that all basins above the user defined threshold size are identified. And that flow out of each river mouth of all identified basins is through a single cell (per mouth if the outlet is a delta with multiple mouths) and can be exactly accounted for.&lt;/p&gt;

&lt;p&gt;There are, however, other alternatives relying on using either the SFD or the MFD generated outlets. For some cases it might be necessary to separate or complementing basins, and then you might need to use the alternatives outlined below.&lt;/p&gt;

&lt;h4 id=&quot;alternative-basin-distillations&quot;&gt;Alternative basin distillations&lt;/h4&gt;

&lt;p&gt;There are 5 basic alternatives for how to start the distilling of your candidate basin outlet points:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Use the combined SFD-MFD (‘MOUTH’) default system with a virtual coastline wall,&lt;/li&gt;
  &lt;li&gt;Use all SFD identified outlets for generating preliminary basins,&lt;/li&gt;
  &lt;li&gt;Cluster SFD outlets based on adjacent MFD candidate outlets and distill to a single SFD outlet per cluster,&lt;/li&gt;
  &lt;li&gt;Use all MFD identified outlets for generating preliminary basins,&lt;/li&gt;
  &lt;li&gt;Cluster adjacent MFD outlets based on the MFD candidate outlets and distill to a single outlet per cluster.&lt;/li&gt;
&lt;/ol&gt;

&lt;h4 id=&quot;parameterisation&quot;&gt;Parameterisation&lt;/h4&gt;

&lt;p&gt;The Python script &lt;em&gt;basin_extractor&lt;/em&gt; is parameterised from an XML file that follows the general structure of &lt;a href=&quot;https://karttur.github.io/geoimagine/concept/concept-concepts/#xml-coded-parameterization&quot;&gt;Karttur´s GeoImagine Framework&lt;/a&gt;:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;basindelineate&amp;gt;
	&amp;lt;userproj userid = 'karttur' projectid = 'karttur' tractid= 'amazonia' siteid = '*' plotid = '*' system = 'MODIS'&amp;gt;&amp;lt;/userproj&amp;gt;

	&amp;lt;!-- Define processing --&amp;gt;
	&amp;lt;process processid ='BasinDelineate'&amp;gt;
		&amp;lt;overwrite&amp;gt;Y&amp;lt;/overwrite&amp;gt; # Y(es) or N(o)
		&amp;lt;delete&amp;gt;N&amp;lt;/delete&amp;gt; # Y(es) or N(o)
		&amp;lt;parameters
		stage = '1' # 1, 2 or 3
		adjacentdist = '330' # any number in map distance units
		outlet = 'SFD' # MOUTH, SFD or MFD
		distill = '' # MFD or None (nothing)
		clusteroutlet = 'central' # central or max
		basinthreshold='2000' # any number is cells
		watershed='MFD5' # SFD, MFD or MFD? (? in range 1..10)
		verbose = '1' # 0, 1 or 2
		proj4CRS = '+proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs'
		&amp;gt;

		&amp;lt;/parameters&amp;gt;
		&amp;lt;srcpath volume = &quot;GRASS2020&quot;&amp;gt;&amp;lt;/srcpath&amp;gt;
		&amp;lt;dstpath volume = &quot;GRASS2020&quot;&amp;gt;&amp;lt;/dstpath&amp;gt;

		&amp;lt;srccomp&amp;gt;
			&amp;lt;!-- the name &amp;lt;tag&amp;gt; and prefix must be the same and can not be altered. The script looks for which source data is included and compares that with the parameters, and then sets the processing method if these corresponds. Otherwise the script ends with an error message.
			--&amp;gt;
			&amp;lt;basin-mouth-outlet-pt source = &quot;SRTM&quot; product = &quot;drainage&quot; folder = &quot;basin&quot; band = &quot;basin-mouth-outlet-pt&quot; prefix = &quot;basin-mouth-outlet-pt&quot; suffix = &quot;cgiar-250&quot;&amp;gt;
			&amp;lt;/basin-mouth-outlet-pt&amp;gt;

			&amp;lt;basin-MFD-outlet-pt source = &quot;SRTM&quot; product = &quot;drainage&quot; folder = &quot;basin&quot; band = &quot;basin-MFD-outlet-pt&quot; prefix = &quot;basin-MFD-outlet-pt&quot; suffix = &quot;cgiar-250&quot;&amp;gt;
			&amp;lt;/basin-MFD-outlet-pt&amp;gt;

			&amp;lt;basin-SFD-outlet-pt source = &quot;SRTM&quot; product = &quot;drainage&quot; folder = &quot;basin&quot; band = &quot;basin-SFD-outlet-pt&quot; prefix = &quot;basin-SFD-outlet-pt&quot; suffix = &quot;cgiar-250&quot;&amp;gt;
			&amp;lt;/basin-SFD-outlet-pt&amp;gt;

			&amp;lt;shorewall-pt source = &quot;SRTM&quot; product = &quot;drainage&quot; folder = &quot;basin&quot; band = &quot;shorewall-pt&quot; prefix = &quot;shorewall-pt&quot; suffix = &quot;cgiar-250&quot;&amp;gt;
			&amp;lt;/shorewall-pt&amp;gt;
		&amp;lt;/srccomp&amp;gt;

		&amp;lt;dstcomp&amp;gt;
			&amp;lt;basin-outlet&amp;gt;
			&amp;lt;/basin-outlet&amp;gt;
			&amp;lt;!--
			To force a non-default destination path and name, you can enter all components explicitly, e.g.
			&amp;lt;basin-outlet band = &quot;basin-mfd+mfd-max&quot; prefix = &quot;basin-mfd+mfd-max&quot;&amp;gt;
			&amp;lt;/basin-outlet&amp;gt;
			--&amp;gt;
		&amp;lt;/dstcomp&amp;gt;
	&amp;lt;/process&amp;gt;

&amp;lt;/basindelineate&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The core parameters include:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;stage [1, 2 or 3] (this part is for stage = ‘1’)&lt;/li&gt;
  &lt;li&gt;adjacentdist [the maximum distance in map units used for clustering candidate outlet points] (only relevant if parameter &lt;em&gt;distill=’MFD’&lt;/em&gt;)&lt;/li&gt;
  &lt;li&gt;outlet [‘SFD’, ‘MFD’ or ‘MOUTH’]&lt;/li&gt;
  &lt;li&gt;distill [‘MFD’ or ‘None’]&lt;/li&gt;
  &lt;li&gt;clusteroutlet [‘central’ or ‘maximum’]&lt;/li&gt;
  &lt;li&gt;basinthreshold [r.watershed parameter &lt;em&gt;threshold&lt;/em&gt;, default= 2000]&lt;/li&gt;
  &lt;li&gt;watershed [flow direction setting for iterated run of r.watershed, only relevant if parameter &lt;em&gt;distill=’MOUTH’&lt;/em&gt;]&lt;/li&gt;
  &lt;li&gt;verbose [0, 1 or 2]&lt;/li&gt;
  &lt;li&gt;proj4CRS [‘proj4CRS definition for output data if input layers lack proper projection information’]&lt;/li&gt;
&lt;/ul&gt;

&lt;h5 id=&quot;mouth-processing&quot;&gt;MOUTH processing&lt;/h5&gt;

&lt;ul&gt;
  &lt;li&gt;outlet=’MOUTH’&lt;/li&gt;
  &lt;li&gt;distill is ignored&lt;/li&gt;
  &lt;li&gt;adjacentdist is ignored&lt;/li&gt;
  &lt;li&gt;watershed &lt;em&gt;must be set&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The following &lt;em&gt;srccomp&lt;/em&gt; tags must be set:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;basin-mouth-outlet-pt&lt;/li&gt;
  &lt;li&gt;shorewall-pt&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;em&gt;MOUTH&lt;/em&gt; processing is the most complex, but also generates the most consistent result. The processing includes the following steps:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;identify one candidate outlet point per river mouth (either central or with maximum upstream as determined by the &lt;em&gt;clusteroutlet&lt;/em&gt; parameter)&lt;/li&gt;
  &lt;li&gt;move candidate outlet point to the nearest cell in the shorewall&lt;/li&gt;
  &lt;li&gt;Create GRASS shell script for repeating the r.watershed analysis with the new outlet point and a hydrologically corrected DEM draining towards this point.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The ensuing processing (in &lt;a href=&quot;../basin-delineate-04&quot;&gt;part 4&lt;/a&gt;) thus includes creating a new, hydrologically adjusted virtual DEM and then redoing the &lt;a href=&quot;&quot;&gt;r.watershed&lt;/a&gt; analysis.&lt;/p&gt;

&lt;p&gt;If you want to run the &lt;em&gt;MOUTH&lt;/em&gt; alternative for other data source (ie. for and SFD analysis), you do that by renaming the SFD shape file to the MOUTH equivalent.&lt;/p&gt;

&lt;h5 id=&quot;non-clustered-sfd-processing&quot;&gt;Non-clustered SFD processing&lt;/h5&gt;

&lt;ul&gt;
  &lt;li&gt;outlet=’SFD’&lt;/li&gt;
  &lt;li&gt;distill=’None’&lt;/li&gt;
  &lt;li&gt;adjacentdist is ignored&lt;/li&gt;
  &lt;li&gt;watershed is ignored&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The following &lt;em&gt;srccomp&lt;/em&gt; tags must be set:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;basin-SFD-outlet-pt&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is the simplest delineation method. It just takes all SFD outlets and delineates basins from these.&lt;/p&gt;

&lt;h5 id=&quot;clustered-sfd-processing&quot;&gt;Clustered SFD processing&lt;/h5&gt;

&lt;ul&gt;
  &lt;li&gt;outlet=’SFD’&lt;/li&gt;
  &lt;li&gt;distill=’MFD’&lt;/li&gt;
  &lt;li&gt;adjacentdist &lt;em&gt;must be set&lt;/em&gt;&lt;/li&gt;
  &lt;li&gt;watershed is ignored&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The following &lt;em&gt;srccomp&lt;/em&gt; tags must be set:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;basin-SFD-outlet-pt&lt;/li&gt;
  &lt;li&gt;basin-MFD-outlet-pt&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With this method, all MFD outlets are clustered and any if two or more SFD outlets are found within the same cluster, only one is retained. Which one to retain is determined by the parameter &lt;em&gt;clusteroutlet&lt;/em&gt;. The parameter &lt;em&gt;adjacentdist&lt;/em&gt; determines the maximum distance between points for belonging to the same cluster.&lt;/p&gt;

&lt;h5 id=&quot;non-clustered-mfd-processing&quot;&gt;Non-clustered MFD processing&lt;/h5&gt;

&lt;ul&gt;
  &lt;li&gt;outlet=’MFD’&lt;/li&gt;
  &lt;li&gt;distill=’None’&lt;/li&gt;
  &lt;li&gt;adjacentdist is ignored&lt;/li&gt;
  &lt;li&gt;watershed is ignored&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The following &lt;em&gt;srccomp&lt;/em&gt; tags must be set:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;basin-MFD-outlet-pt&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With these parameter setting, &lt;em&gt;basin_extract&lt;/em&gt; delineates basins from all MFD candidates.&lt;/p&gt;

&lt;h5 id=&quot;clustered-mfd-processing&quot;&gt;Clustered MFD processing&lt;/h5&gt;

&lt;ul&gt;
  &lt;li&gt;outlet=’MFD’&lt;/li&gt;
  &lt;li&gt;distill=’MFD’&lt;/li&gt;
  &lt;li&gt;adjacentdist &lt;em&gt;must be set&lt;/em&gt;&lt;/li&gt;
  &lt;li&gt;watershed is ignored&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The following &lt;em&gt;srccomp&lt;/em&gt; tags must be set:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;basin-MFD-outlet-pt&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With both &lt;em&gt;outlet&lt;/em&gt; and &lt;em&gt;distill&lt;/em&gt; parameters set to &lt;em&gt;MFD&lt;/em&gt;, the MFD outlet candidates are first clustered. Within each cluster only one MFD outlet is retained. Which one to retain is determined by the parameter &lt;em&gt;clusteroutlet&lt;/em&gt;. The parameter &lt;em&gt;adjacentdist&lt;/em&gt; determines the maximum distance between points for belonging to the same cluster.&lt;/p&gt;

&lt;h5 id=&quot;editing-the-data-source&quot;&gt;Editing the data source&lt;/h5&gt;

&lt;p&gt;The processing is determined by the default naming convention of the source data sets. But you can manually edit any of the source data prior to processing. If you only want to process a few of the candidates, simply delete all other candidates. You can for instance use &lt;span class=&quot;app&quot;&gt;QGIS&lt;/span&gt; to open the point vector file (in ESRI shape format), change to edit mode and delete the candidates you are not interested in. Then run &lt;em&gt;basin_extract&lt;/em&gt; as usual.&lt;/p&gt;

&lt;p&gt;You can also change the name of, say an SFD generated point vector file and change to the default MOUTH name, and then run the MOUTH analysis instead of the SFD analysis.&lt;/p&gt;

&lt;h3 id=&quot;output&quot;&gt;Output&lt;/h3&gt;

&lt;p&gt;The script &lt;em&gt;basin_extractor&lt;/em&gt; will produce a point vector file in shape format containing the distilled outlet candidates points as requested in the parameterization. Alongside the point vector file, the script also produces a shell script (&lt;span class=&quot;file&quot;&gt;.sh&lt;/span&gt;) with the GRASS commands for delineating the basins from the selected candidate points. The name of the shell script file is defaulted to reflect which of the four main alternatives the parameterisation was set to.&lt;/p&gt;

&lt;h4 id=&quot;python-script-output&quot;&gt;Python script output&lt;/h4&gt;

&lt;p&gt;The main output of stage 1 of the Python script &lt;em&gt;basin_extract&lt;/em&gt; is a GRASS command line script. &lt;a href=&quot;../basin-delineate-04&quot;&gt;Part 4&lt;/a&gt; of this tutorial series will take you through he processing in that script.&lt;/p&gt;</content><author><name></name></author><category term="basindelineate" /><category term="Python" /><category term="GRASS script" /><category term="basin" /><category term="delineation" /><summary type="html">Python script for analysing and distilling basin candidates, and producing GRASS script for step 4</summary></entry><entry><title type="html">Basin delineation: 2 GRASS watershed analysis</title><link href="https://nunataryuk.github.io//basindelineate/basin-delineate-02/" rel="alternate" type="text/html" title="Basin delineation: 2 GRASS watershed analysis" /><published>2020-10-26T00:00:00+00:00</published><updated>2020-10-26T00:00:00+00:00</updated><id>https://nunataryuk.github.io//basindelineate/basin-delineate-02</id><content type="html" xml:base="https://nunataryuk.github.io//basindelineate/basin-delineate-02/">&lt;script src=&quot;https://karttur.github.io/common/assets/js/karttur/togglediv.js&quot;&gt;&lt;/script&gt;

&lt;h1 id=&quot;introduction&quot;&gt;Introduction&lt;/h1&gt;

&lt;p&gt;Part 2 of the Basin delineation tutorial series focuses on using GRASS7 for creating the initial watershed (or basin) files representing hydrological flow direction and upstream accumulation of each cell in the study region.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;You must have setup GRASS 7 and imported a DEM as described under &lt;a href=&quot;../../basindelineatesetup&quot;&gt;Installation &amp;amp; Setup&lt;/a&gt; in this blog.&lt;/p&gt;

&lt;h4 id=&quot;watershed-accumulation-and-basin-area&quot;&gt;Watershed accumulation and basin area&lt;/h4&gt;

&lt;p&gt;The GRASS raster command &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/r.watershed.html&quot;&gt;r.watershed&lt;/a&gt; calculates a variety of hydrological parameters, including the upstream (accumulation) area and the drainage direction of each cell. Note that the accumulation area is calculated as number of cells.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/r.watershed.html&quot;&gt;r.watershed&lt;/a&gt; can be run assigning a Single Flow Direction (SFD) out of each cell, or Multiple Flow Directions (MFD). MFD is in general preferred and also the default. MFD can be parameterised for determining the flow convergence/divergence, with the recommended default being an average (parameter &lt;em&gt;convergence=5&lt;/em&gt;). If you want to avoid negative numbers for upstream area, set the &lt;em&gt;-a&lt;/em&gt; flag. &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/r.watershed.html&quot;&gt;r.watershed&lt;/a&gt; can be set up to produce a range of hydrologically related layers. For delineating basins you must produce layers for &lt;em&gt;accumulation&lt;/em&gt; and &lt;em&gt;drainage&lt;/em&gt;.&lt;/p&gt;

&lt;h5 id=&quot;single-flow-direction&quot;&gt;Single Flow Direction&lt;/h5&gt;

&lt;p&gt;To apply an SFD flow routing, set the flag &lt;em&gt;-s&lt;/em&gt;. In the example below I have set both the &lt;em&gt;-a&lt;/em&gt; and &lt;em&gt;-s&lt;/em&gt; flags, and I have requested the production of layers for &lt;em&gt;accumulation&lt;/em&gt; and &lt;em&gt;drainage&lt;/em&gt;. I have set the cutoff threshold at 2000 cells:&lt;/p&gt;

&lt;p&gt;With the original DEM (in the mapset PERMANENT):&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; r.watershed -as elevation=DEM@PERMANENT accumulation=SFD_upstream drainage=SFD_drainage threshold=2000 –overwrite&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;With a patched up DEM from &lt;a href=&quot;../basin_delineate_01&quot;&gt;part 1&lt;/a&gt;:&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; r.watershed -as elevation=hydroptfill_DEM accumulation=SFD_upstream drainage=SFD_drainage threshold=2000 –overwrite&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;It takes a rather long time to run an &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/r.watershed.html&quot;&gt;r.watershed&lt;/a&gt; analysis. GRASS thus offers a faster alternative, &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/r.terraflow.html&quot;&gt;r.terraflow&lt;/a&gt; for massive grids. &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/r.terraflow.html&quot;&gt;r.terraflow&lt;/a&gt;, however, produces less accurate results and I strongly recommend against using it.&lt;/p&gt;

&lt;h5 id=&quot;multiple-flow-directions-mfd&quot;&gt;Multiple Flow Directions (MFD)&lt;/h5&gt;

&lt;p&gt;Omitting the &lt;em&gt;-s&lt;/em&gt; flag causes &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/r.watershed.html&quot;&gt;r.watershed&lt;/a&gt; to run in default mode, applying an MFD flow routing.&lt;/p&gt;

&lt;p&gt;With the original DEM (in the mapset PERMANENT):&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; r.watershed -a elevation=DEM@PERMANENT accumulation=MFD_upstream drainage=MFD_drainage threshold=2000 –overwrite&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;With a patched up DEM from &lt;a href=&quot;../basin_delineate_01&quot;&gt;part 1&lt;/a&gt;:&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; r.watershed -a elevation=hydroptfill_DEM accumulation=MFD_upstream drainage=MFD_drainage threshold=2000 –overwrite&lt;/span&gt;&lt;/p&gt;

&lt;h5 id=&quot;mfd-alternative-parameterizations&quot;&gt;MFD alternative parameterizations&lt;/h5&gt;

&lt;p&gt;If you want to better capture the full width of wide rivers, you should set a smaller &lt;em&gt;convergence&lt;/em&gt; factor for MFD:&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; r.watershed -a convergence=1 elevation=DEM@PERMANENT accumulation=MFD01_upstream drainage=MFD01_drainage threshold=2000 –overwrite&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Alternatively you can try to beautify flat areas (including wide channels) by setting the &lt;em&gt;-b&lt;/em&gt; flag.&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; r.watershed -ab convergence=1 elevation=DEM@PERMANENT accumulation=MFDab_upstream drainage=MFDab_drainage threshold=2000 –overwrite&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Or even combine a maximum divergence and beautification of flat areas:&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; r.watershed -ab convergence=1 elevation=DEM@PERMANENT accumulation=MFDab01_upstream drainage=MFDab01_drainage threshold=2000 –overwrite&lt;/span&gt;&lt;/p&gt;

&lt;h4 id=&quot;compare-upstream-accumulation&quot;&gt;Compare upstream accumulation&lt;/h4&gt;

&lt;p&gt;If you look at the accumulation layer, the major Rivers (e.g. the Amazon River) should have the largest accumulation values.&lt;/p&gt;

&lt;figure class=&quot;half&quot;&gt;
	&lt;a href=&quot;https://karttur.github.io/common/images/amazon_upstream_SFD.png&quot;&gt;&lt;img src=&quot;https://karttur.github.io/common/images/amazon_upstream_SFD.png&quot; alt=&quot;image&quot; /&gt;&lt;/a&gt;
  &lt;a href=&quot;https://karttur.github.io/common/images/amazon_upstream_MFD.png&quot;&gt;&lt;img src=&quot;https://karttur.github.io/common/images/amazon_upstream_MFD.png&quot; alt=&quot;image&quot; /&gt;&lt;/a&gt;

  &lt;a href=&quot;https://karttur.github.io/common/images/amazon_upstream_MFD01.png&quot;&gt;&lt;img src=&quot;https://karttur.github.io/common/images/amazon_upstream_MFD01.png&quot; alt=&quot;image&quot; /&gt;&lt;/a&gt;
  &lt;a href=&quot;https://karttur.github.io/common/images/amazon_upstream_MFD01ab.png&quot;&gt;&lt;img src=&quot;https://karttur.github.io/common/images/amazon_upstream_MFD01ab.png&quot; alt=&quot;image&quot; /&gt;&lt;/a&gt;

	&lt;figcaption&gt;Upstream area calculated using (clockwise) Single Flow Direction (SFD), Multi Flow Direction (MFD) with convergence=5 (default), MFD with convergence=1 and MFD with convergence=1 and -b (beautify flat area) flag.&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;After having compared the result of different Flow Direction parameterisations, I decided to stick with the default settings. In the remaining part of this tutorial series I will rely on default outputs from both SFD and MFD. The reason for using both MFD and SFD is that I want to find all mouths of bifurcating rivers (with MFD) but also all basins that are larger than my set threshhold (regardless of the number of cells though which the basin empties itself in the terminal drainage).&lt;/p&gt;

&lt;h4 id=&quot;export-upstream-layers&quot;&gt;Export upstream layers&lt;/h4&gt;

&lt;p&gt;You will need the raster layers showing the upstream accumulated areas for inspecting further processing. The upstream layer, however, contains very high numbers and is difficult to visualise. Thus it is better to first get the natural logarithm of the dataset and then export it in byte format. Apply the logarithmic conversion, including a multiplication with 10, with the GRASS command &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/r.mapcalc.html&quot;&gt;r.mapcalc&lt;/a&gt;. Set a colour ramp that is intuitive for interpreting water flow with the command &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/r.out.gdal.html&quot;&gt;r.colors&lt;/a&gt;. I choose red-yellow-blue (ryb). Then export the raster layer with the command &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/r.out.gdal.html&quot;&gt;r.out.gdal&lt;/a&gt;. That is how the 4 maps in the figure above were symbolised.&lt;/p&gt;

&lt;p&gt;Start by creating the target directory:&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; mkdir -p /Volumes/GRASS2020/GRASSproject/SRTM/region/basin/amazonia/0&lt;/span&gt;&lt;/p&gt;

&lt;h5 id=&quot;export-sfd&quot;&gt;Export SFD&lt;/h5&gt;

&lt;p&gt;The combined commands for exporting a nicely symbolised SFD accumulation layer:&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; r.mapcalc &quot;SFD_ln_upstream = 10*log(SFD_upstream)&quot; –overwrite&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; r.colors map=SFD_ln_upstream color=ryb&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; r.out.gdal -f input=SFD_ln_upstream format=GTiff type=Byte output=/Volumes/GRASS2020/GRASSproject/SRTM/region/basin/amazonia/0/upstream-ln-ryb-SFD_amazonia_0_cgiar-250.tif –overwrite&lt;/span&gt;&lt;/p&gt;

&lt;h5 id=&quot;export-mfd&quot;&gt;Export MFD&lt;/h5&gt;

&lt;p&gt;The combined commands for exporting a nicely symbolised MFD accumulation layer:&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; r.mapcalc &quot;MFD_ln_upstream = 10*log(MFD_upstream)&quot; –overwrite&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; r.colors map=MFD_ln_upstream color=ryb&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; r.out.gdal -f input=MFD_ln_upstream format=GTiff type=Byte output=/Volumes/GRASS2020/GRASSproject/SRTM/region/basin/amazonia/0/upstream-ln-ryb-MFD_amazonia_0_cgiar-250.tif –overwrite&lt;/span&gt;&lt;/p&gt;

&lt;h4 id=&quot;identify-terminal-water-body&quot;&gt;Identify terminal water body&lt;/h4&gt;

&lt;p&gt;Use &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/r.mapcalc.html&quot;&gt;r.mapcalc&lt;/a&gt; to create a layer where the terminal water body, into which the basins you want to delineate, drain. This is equal to setting all land areas (cells in your DEM layer with valid data) to NULL, and your terminal water body (e.g. the areas assigned with NULL in the DEM) to a value of 1.&lt;/p&gt;

&lt;p&gt;With the original DEM (in the mapset PERMANENT):&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; r.mapcalc &quot;drain_terminal = if(isnull(‘DEM@PERMANENT’), 1, null())&quot;&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;With a patched up DEM from &lt;a href=&quot;../basin_delineate_01&quot;&gt;part 1&lt;/a&gt;:&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; r.mapcalc &quot;drain_terminal = if(isnull(‘hydroptfill_DEM’), 1, null())&quot;&lt;/span&gt;&lt;/p&gt;

&lt;h4 id=&quot;get-shoreline&quot;&gt;Get shoreline&lt;/h4&gt;

&lt;p&gt;With the terminal water body identified, the shoreline with all the basin outlets will all be found in the neighbouring cells of the water body. In GRASS you can use the proximity analysis tool &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/r.buffer.html&quot;&gt;r.buffer&lt;/a&gt; to identify the shoreline from the terminal water body. To capture all shoreline cells set the distance to the length of the side of one cell * sqrt of 2 + plus a little extra. The spatial resolution of the DEM used in this example is 231.6 m. I thus set the distance threshold for the proximity analysis to 330 m.&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; r.buffer input=drain_terminal output=shoreline distances=330 units=meters –overwrite&lt;/span&gt;&lt;/p&gt;

&lt;h4 id=&quot;extract-accumulated-drainage-for-shoreline&quot;&gt;Extract accumulated drainage for shoreline&lt;/h4&gt;

&lt;p&gt;With both the shoreline and the flow accumulation identified on a per pixel level, you can once again apply &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/r.mapcalc.html&quot;&gt;r.mapcalc&lt;/a&gt;, but this time to mask out the layer with the accumulated upstream area to only include the shoreline (i.e. the potential basin outlets).&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; r.mapcalc &quot;shoreline_SFD_flowacc = if((shoreline &amp;gt; 1), SFD_upstream, null())&quot;&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; r.mapcalc &quot;shoreline_MFD_flowacc = if((shoreline &amp;gt; 1), MFD_upstream, null())&quot;&lt;/span&gt;&lt;/p&gt;

&lt;h4 id=&quot;threshold-minimum-drainage-area&quot;&gt;Threshold minimum drainage area&lt;/h4&gt;

&lt;p&gt;The map of the upstream (accumulated) cells masked to show only the shoreline contains the upstream area of every cell along the shoreline, regardless of the upstream area. But you only want the river basins, not the cells that empty directly into the terminal water body or via insignificant water courses. You need to &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/r.reclass.html&quot;&gt;r.reclass&lt;/a&gt; the raster layer showing the upstream area of the shoreline to retain only the cells above a given threshold. Remember that the accumulated upstream drainage is given in cells.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/r.reclass.html&quot;&gt;r.reclass&lt;/a&gt; can be parameterized in different ways, but to keep a memory of the processing it is best to do via a simple text file that is linked in the command. To retain all basins composed to 2000 cells or more, the file looks like this:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;2000 thru 99999999 = 1
* = NULL
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;As the map has a spatial resolution of 231.6 m, 2000 cells equals approximately 100 (107 to be exact) square kilometres. Then run &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/r.reclass.html&quot;&gt;r.reclass&lt;/a&gt; with the text file used for parameterisation.&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; r.reclass input=shoreline_SFD_flowacc output=basin_SFD_outlets rules=’/Volumes/GRASS2020/GRASSsupport/reclass/reclass_flow_acc_2000.txt’ –overwrite&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; r.reclass input=shoreline_MFD_flowacc output=basin_MFD_outlets rules=’/Volumes/GRASS2020/GRASSsupport/reclass/reclass_flow_acc_2000.txt’ –overwrite&lt;/span&gt;&lt;/p&gt;

&lt;h4 id=&quot;combine-the-two-basin-outlet-rasters&quot;&gt;Combine the two basin outlet rasters&lt;/h4&gt;

&lt;p&gt;Distilling the candidate basin outlets to a final set is rather complex - in essence the remaining of parts 2, and parts 3 and 4 are required to get there. To capture all outlets of all basins, the MFD and SFD results need to be joined. Easiest done using &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/r.mapcalc.htm&quot;&gt;r.mapcalc&lt;/a&gt;. Alternatively you can patch the vectorised outlets (shown further down) using &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/v.patch.htm&quot;&gt;v.patch&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; r.mapcalc &quot;shoreline_flowacc = if ((shoreline_SFD_flowacc &amp;gt; shoreline_MFD_flowacc), shoreline_SFD_flowacc, shoreline_MFD_flowacc)&quot; –overwrite&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; r.reclass input=shoreline_flowacc output=basin_outlets rules=’/Volumes/GRASS2020/GRASSsupport/reclass/reclass_flow_acc_2000.txt’ –overwrite&lt;/span&gt;&lt;/p&gt;

&lt;h4 id=&quot;vectorize-basin-outlet-candidates&quot;&gt;Vectorize basin outlet candidates&lt;/h4&gt;

&lt;p&gt;You should now have raster layers with candidate basin outlets. These points are required for the further processing and you need to convert them to vector points. The default suggested processing chain in this tutorial will, however, use the combined SFD and MFD outlets and not the SFD and MFD points. But you can go ahead and convert the latter to vectors anyway. In GRASS you convert raster cells to vector points with the command &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/r.to.vect.html&quot;&gt;r.to.vect&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; r.to.vect input=basin_outlets output=basin_outlet_pt type=point –overwrite&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; r.to.vect input=basin_SFD_outlets output=basin_SFD_outlet_pt type=point –overwrite&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; r.to.vect input=basin_MFD_outlets output=basin_MFD_outlet_pt type=point –overwrite&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;As mentioned above, you can use &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/v.patch.html&quot;&gt;v.patch&lt;/a&gt; to join the SFD and MFD outlet points. But if you followed the suggested path above you already have a vector layer called &lt;em&gt;basin_outlet_pt&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; v.patch input = basin_SFD_outlet_pt, basin_MFD_outlet_pt output = basin_outlet_pt&lt;/span&gt;&lt;/p&gt;

&lt;figure class=&quot;half&quot;&gt;
	&lt;a href=&quot;https://karttur.github.io/common/images/GRASS7_Amazonia-drainage-SFD.png&quot;&gt;&lt;img src=&quot;https://karttur.github.io/common/images/GRASS7_Amazonia-drainage-SFD.png&quot; alt=&quot;image&quot; /&gt;&lt;/a&gt;
  &lt;a href=&quot;https://karttur.github.io/common/images/GRASS7_Amazonia-drainage-MFD.png&quot;&gt;&lt;img src=&quot;https://karttur.github.io/common/images/GRASS7_Amazonia-drainage-MFD.png&quot; alt=&quot;image&quot; /&gt;&lt;/a&gt;
	&lt;figcaption&gt;Upstream area and identified candidate basin outlets using Single Flow Direction (SFD) and Multi Flow Direction (MFD). The wider spread of the accumulated flow with MFD (right) causes the wide river mouths to show up. The accumulated flow with SFD forces flow to a channel with a width of only 1 cell - and is not really seen in the map at the presented scale. If you click in one of the maps you will get popups with mire details. &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;h4 id=&quot;from-point-outlets-to-river-mouths&quot;&gt;From point outlets to river mouths&lt;/h4&gt;

&lt;p&gt;From the illustrations above six different problems are evident:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;The MFD analysis identifies multiple adjacent outlet points along wide river mouths of larger basins,&lt;/li&gt;
  &lt;li&gt;The MFD analysis only captures basins where the outflow over any single cell is above the threshold, regardless of the size of the basin,&lt;/li&gt;
  &lt;li&gt;The SFD analysis does not capture all outlet branches of large rivers that form multiple outlet channels (e.g. in river deltas),&lt;/li&gt;
  &lt;li&gt;Separate outlets belonging to the same river (as in 3 above), are not identified as being part of the same basin,&lt;/li&gt;
  &lt;li&gt;Both MFD, but in particular SFD, might identify multiple outlet points (or clusters of outlet points regarding MFD) in wide outlet mouths.&lt;/li&gt;
  &lt;li&gt;The flow path across flat surfaces differs greatly dependent on the parameterisation of r.watershed.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Problem 1 can be solved by clustering adjacent outlet points, problems 2 and 3 by joining MFD and SFD results. Problem 4 requires analysing the connectivity between adjacent and upstream connected, but along the shoreline topographically separated, river mouths. Problem 5 requires that the complete width of each separate mouth (regardless if part of a delta or not) be identified as a united entity. Issue 6 is only a problem if the identified basins are used for hydrological balancing or modeling (dealt with in parts 3 and 4 of this series).&lt;/p&gt;

&lt;p&gt;These problems need to be addressed by combining spatial and database analysis. The solution that I chose accomplishes this by using GRASS and Python scripting. The next section in this part deals with the GRASS spatial analysis for identifying contiguous basin mouths. The Python scripting is the topic of &lt;a href=&quot;../basin-delineate-03&quot;&gt;part 3&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;identify-contiguous-basin-mouths&quot;&gt;Identify contiguous basin mouths&lt;/h3&gt;

&lt;p&gt;Excluding the sea surface, you could simply reclass the DEM to only retain cells with an elevation of zero for identifying basin mouths. You will then, however, end up with cells along the shoreline that do not constitute a river mouth as well as landlocked regions at sea level. And then there are errors in the DEM and not all basin mouths have an elevation equal 0 (that will be fixed in parts 3 and 4). Instead you need to find all cells at sea level connected to at least one of your candidate outlet points. With the connection either being along the shoreline or via upstream bifurcations. The latter for finding rivers with multiple mouths, like in e.g. river deltas.&lt;/p&gt;

&lt;h4 id=&quot;cost-growth-analysis&quot;&gt;Cost growth analysis&lt;/h4&gt;

&lt;p&gt;Cost growth analysis calculates the accumulated travel cost over a friction surface from predefined starting points. You can use the GRASS cost grow tool &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/v.out.ogr.html&quot;&gt;r.cost&lt;/a&gt; as the first step for finding both the hydrological connectivity between adjacent river mouths and the complete width of the mouths of larger rivers. You have to use all outlet points (from MFD and SFD combined) as starting points and the DEM as a friction surface. Friction at sea surface level (DEM =0) will then be 0, and you let the cost grow analysis travel as long as it is frictionless, i.e. across all of the river mouths.&lt;/p&gt;

&lt;h5 id=&quot;identify-basin-river-mouths&quot;&gt;Identify basin river mouths&lt;/h5&gt;

&lt;p&gt;If you use the entire DEM for the cost grow analaysis, the result will connect all mouth cells (at sea level), even if not directly connected along the coast. Mouths across a a river delta will be linked (as long as the DEM shows that the water levels equals 0). The &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/v.out.ogr.html&quot;&gt;r.cost&lt;/a&gt; analysis for an entire basin can, however, take a rather long time.&lt;/p&gt;

&lt;p&gt;To speed up the processing, you can &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/r.mapcalc.html&quot;&gt;r.mapcalc&lt;/a&gt; the input DEM to only include lower elevations and set higher elevations to NULL. To avoid problems with negative DEM values you can also set these to 0.&lt;/p&gt;

&lt;p&gt;With the original DEM (in the mapset PERMANENT):&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; r.mapcalc &quot;lowlevel_DEM = if((DEM@PERMANENT &amp;lt;= 0), 0, null())&quot;&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;With a patched up DEM from &lt;a href=&quot;../basin_delineate_01&quot;&gt;part 1&lt;/a&gt;:&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; r.mapcalc &quot;lowlevel_DEM = if((hydroptfill_DEM &amp;lt;= 0), 0, null())&quot;&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;If you want to expand the connectivity you can set the “0” of &lt;em&gt;lowlevel_DEM&lt;/em&gt; to include also positive elevation (e.g. &lt;span class=&quot;terminal&quot;&gt;&amp;gt; r.mapcalc &quot;lowlevel_DEM = if((DEM@PERMANENT &amp;lt;= 2), 0, null())&quot;&lt;/span&gt;).&lt;/p&gt;

&lt;p&gt;Then run &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/r.cost.html&quot;&gt;r.cost&lt;/a&gt; on the more limited DEM. The &lt;em&gt;-n&lt;/em&gt; flag retains the null values in the output layer.&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; r.cost -n input=lowlevel_DEM output=lowlevel_outlet_costgrow start_points=basin_outlet_pt max_cost=0 –overwrite&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;To cluster the cells constituting basin mouths use the GRASS command &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/r.clumpl.html&quot;&gt;r.clump&lt;/a&gt;. Each separate cluster will be assigned a unique id, and you can use that id for identifying river mouths that are separated at the shoreline but joined upstream. Set the -d flag to clump diagonal cells, and then export the raster data source as you will need it to inspect your basins in later parts.&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; r.clump -d input=lowlevel_outlet_costgrow  output=lowlevel_outlet_clumps –overwrite&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; r.out.gdal -f input=lowlevel_outlet_clumps format=GTiff output=/Volumes/GRASS2020/GRASSproject/SRTM/region/basin/amazonia/0/lowlevel-outlet-clumps_amazonia_0_cgiar-250.tif –overwrite&lt;/span&gt;&lt;/p&gt;

&lt;h5 id=&quot;identify-beachfront-river-mouths&quot;&gt;Identify beachfront river mouths&lt;/h5&gt;

&lt;p&gt;To calculate cost growth for only the shoreline, apply &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/r.mapcalc.html&quot;&gt;r.mapcalc&lt;/a&gt; to extract the DEM only for the shoreline.&lt;/p&gt;

&lt;p&gt;With the original DEM (in the mapset PERMANENT):&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; r.mapcalc “shoreline_DEM = if((shoreline &amp;gt; 1 &amp;amp;&amp;amp; DEM@PERMANENT &amp;lt;= 0), 0, null())” –overwrite&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;With a patched up DEM from &lt;a href=&quot;../basin_delineate_01&quot;&gt;part 1&lt;/a&gt;:&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; r.mapcalc &quot;shoreline_DEM = if((shoreline &amp;gt; 1 &amp;amp;&amp;amp; hydroptfill_DEM &amp;lt;= 0), 0, null())&quot; –overwrite&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Then repeat the processing steps for identifying the basin mouths, but for the beachfront mouths.&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; r.cost -n input=shoreline_DEM output=shoreline_outlet_costgrow start_points=basin_outlet_pt max_cost=0 –overwrite&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; r.clump -d input=shoreline_outlet_costgrow output=shoreline_outlet_clumps –overwrite&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; r.out.gdal -f input=shoreline_outlet_clumps format=GTiff output=/Volumes/GRASS2020/GRASSproject/SRTM/region/basin/amazonia/0/shoreline-outlet-clumps_amazonia_0_cgiar-250.tif –overwrite&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;If you compare the number of clumps reported (at the GRASS commandline), there should be more clumps produced for the shoreline compared to the basin (lowlevel). For the Amazonia region used in the example there are 1065 shoreline clumps and 1037 basin clumps. The difference, 28 mouths, belong to rivers that have two or more separate mouths at the shoreline.&lt;/p&gt;

&lt;h4 id=&quot;vectorize-beachfront-mouths&quot;&gt;Vectorize beachfront mouths&lt;/h4&gt;

&lt;p&gt;You should now have clumps of rasters with unique id’s for each separate beachfront mouth. What you ultimately need is a single point in each clump. That is because you want to let all the water from your basin to flow out of that particular cell and none other. To achieve this you need to prepare point data sources in GRASS, and then analyse these in Python.&lt;/p&gt;

&lt;p&gt;Vectorize the beachfront mouths with  the command &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/r.to.vect.html&quot;&gt;r.to.vect&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; r.to.vect input=shoreline_outlet_clumps output=basin_mouth_outlet_pt type=point&lt;/span&gt;&lt;/p&gt;

&lt;h4 id=&quot;add-columns-to-vector-databases&quot;&gt;Add columns to vector databases&lt;/h4&gt;

&lt;p&gt;For the database processing in Python, you need to add several columns and fill them with data. You only need to add columns and data to the outlet or mouth vector(s) to use in the next step. The default is to use the &lt;em&gt;basin_mouths_pt&lt;/em&gt; point vector. But for convenience the processing of the SFD and MFD point vectors are also included.&lt;/p&gt;

&lt;p&gt;Use GRASS vector command &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/v.db.addcolumn.html&quot;&gt;v.db.addcolumn&lt;/a&gt; to add columns to the vector attribute tables.&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; v.db.addcolumn map=basin_mouth_outlet_pt columns=’upstream DOUBLE PRECISION’&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; v.db.addcolumn map=basin_mouth_outlet_pt columns=’elevation INT’&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; v.db.addcolumn map=basin_mouth_outlet_pt columns=’mouth_id INT’&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; v.db.addcolumn map=basin_mouth_outlet_pt columns=’basin_id INT’&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;or with multiple columns added in one command:&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; v.db.addcolumn map=basin_SFD_outlet_pt columns=’upstream DOUBLE PRECISION, elevation INT, mouth_id INT, basin_id INT’&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; v.db.addcolumn map=basin_MFD_outlet_pt columns=’upstream DOUBLE PRECISION, elevation INT, mouth_id INT, basin_id INT’&lt;/span&gt;&lt;/p&gt;

&lt;h4 id=&quot;add-basin-data-to-vector-databases&quot;&gt;Add basin data to vector databases&lt;/h4&gt;

&lt;p&gt;To transfer the cell data underlying each point to the vector attribute table, use the GRASS vector command &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/v.what.rast.html&quot;&gt;v.what.rast&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; v.what.rast map=basin_mouth_outlet_pt raster=SFD_upstream column=upstream&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; v.what.rast map=basin_mouth_outlet_pt raster=DEM@PERMANENT column=elevation&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; v.what.rast map=basin_mouth_outlet_pt raster=hydroptfill_DEM column=elevation&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; v.what.rast map=basin_mouth_outlet_pt raster=shoreline_outlet_clumps column=mouth_id&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; v.what.rast map=basin_mouth_outlet_pt raster=lowlevel_outlet_clumps column=basin_id&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; v.what.rast map=basin_SFD_outlet_pt raster=SFD_upstream column=upstream&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; v.what.rast map=basin_SFD_outlet_pt raster=DEM@PERMANENT column=elevation&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;or&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; v.what.rast map=basin_SFD_outlet_pt raster=hydroptfill_DEM column=elevation&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; v.what.rast map=basin_SFD_outlet_pt raster=shoreline_outlet_clumps column=mouth_id&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; v.what.rast map=basin_SFD_outlet_pt raster=lowlevel_outlet_clumps column=basin_id&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; v.what.rast map=basin_MFD_outlet_pt raster=MFD_upstream column=upstream&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; v.what.rast map=basin_MFD_outlet_pt raster=DEM@PERMANENT column=elevation&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;or&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; v.what.rast map=basin_MFD_outlet_pt raster=hydroptfill_DEM column=elevation&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; v.what.rast map=basin_MFD_outlet_pt raster=shoreline_outlet_clumps column=mouth_id&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; v.what.rast map=basin_MFD_outlet_pt raster=lowlevel_outlet_clumps column=basin_id&lt;/span&gt;&lt;/p&gt;

&lt;h4 id=&quot;upload-the-x-and-y-coordinates-to-the-vector-db&quot;&gt;Upload the x and y coordinates to the vector db&lt;/h4&gt;

&lt;p&gt;Some inherent vector properties can be added to the vector database without prior definition of the column. The command &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/v.to.db.html&quot;&gt;v.to.db&lt;/a&gt; creates the column(s) on the fly, for instance for adding point coordinates:&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; v.to.db map=basin_mouth_outlet_pt option=coor columns=xcoord,ycoord&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; v.to.db map=basin_SFD_outlet_pt option=coor columns=xcoord,ycoord&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; v.to.db map=basin_MFD_outlet_pt option=coor columns=xcoord,ycoord&lt;/span&gt;&lt;/p&gt;

&lt;h4 id=&quot;export-basin-outlet-candidates&quot;&gt;Export basin outlet candidates&lt;/h4&gt;

&lt;p&gt;Distillation of basin outlets from the candidates is done using a Python script and requires shape files as input. Export the outlet candidates with &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/v.out.ogr.html&quot;&gt;v.out.ogr&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; v.out.ogr type=point input=basin_mouth_outlet_pt format=ESRI_Shapefile output=/Volumes/GRASS2020/GRASSproject/SRTM/region/basin/amazonia/0/basin-mouth-outlet-pt_drainage_amazonia_0_cgiar-250.shp –overwrite&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; v.out.ogr type=point input=basin_SFD_outlet_pt format=ESRI_Shapefile output=/Volumes/GRASS2020/GRASSproject/SRTM/region/basin/amazonia/0/basin-SFD-outlet-pt_drainage_amazonia_0_cgiar-250.shp –overwrite&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; v.out.ogr type=point input=basin_MFD_outlet_pt format=ESRI_Shapefile output=/Volumes/GRASS2020/GRASSproject/SRTM/region/basin/amazonia/0/basin-MFD-outlet-pt_drainage_amazonia_0_cgiar-250.shp –overwrite&lt;/span&gt;&lt;/p&gt;

&lt;h3 id=&quot;virtual-direction-of-basin-outflow&quot;&gt;Virtual direction of basin outflow&lt;/h3&gt;

&lt;p&gt;The &lt;a href=&quot;&quot;&gt;r.watershed&lt;/a&gt; analysis is balancing the accumulated input and output through each cell. The results from &lt;a href=&quot;&quot;&gt;r.watershed&lt;/a&gt; can thus be used for calculating hydrological balances for every cell in a watershed. In wide rivers, including the wide mouths we have dealt with above, the total flow across a section is difficult to retrieve. Also because it depends on the flow direction algorithm. To get the accurate flow along a river, ideally you need a single cell across which all water in the river is forced to pass. To estimate the total outflow of a basin that has a mouth wider than a single cell thus requires a special solution. This section demonstrates how to create a virtual wall across the basin mouth. In the &lt;a href=&quot;../basin-delineate-03&quot;&gt;next part&lt;/a&gt; a virtual hole will be punched in the wall forcing all flow though a single cell.&lt;/p&gt;

&lt;h4 id=&quot;build-a-virtual-wall-across-the-basin-mouth&quot;&gt;Build a virtual wall across the basin mouth&lt;/h4&gt;

&lt;p&gt;The virtual wall is built by buffering the identified shoreline mouths (&lt;em&gt;shoreline_outlet_clumps&lt;/em&gt;) associated with river basins.&lt;/p&gt;

&lt;p&gt;Apply an &lt;a href=&quot;&quot;&gt;r.buffer&lt;/a&gt; analysis the width of 1 diagonal cell (330 m as caclualted above) and starting from the outlet mouths:&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; r.buffer input=shoreline_outlet_clumps output=mouth_buffer distances=330 units=meters –overwrite&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Overlay the buffer with the map of the terminal drainage, and only retain the buffer cells that fall in the terminal drainage (i.e. on the seaside of the shoreline):&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; r.mapcalc &quot;shorewall = if((drain_terminal == 1 &amp;amp;&amp;amp; mouth_buffer &amp;gt; 1), 1, null())&quot; –overwrite&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Convert the shoreline to vectors - this is the data source you need for the Python processing in the &lt;a href=&quot;../basin-delineate-03&quot;&gt;next part&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; r.to.vect input=shorewall output=shorewall_pt type=point&lt;/span&gt;&lt;/p&gt;

&lt;h4 id=&quot;export-the-shorewall&quot;&gt;Export the shorewall&lt;/h4&gt;

&lt;p&gt;Export the point vector version of the shorewall to an ESRI shape file.&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; v.out.ogr type=point input=shorewall_pt format=ESRI_Shapefile output=/Volumes/GRASS2020/GRASSproject/SRTM/region/basin/amazonia/0/shorewall-pt_drainage_amazonia_0_cgiar-250.shp –overwrite&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Optionally export the &lt;em&gt;shorewall&lt;/em&gt; raster map as a geotiff to inspect it in e.g. &lt;span class=&quot;app&quot;&gt;QGIS&lt;/span&gt;.&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; r.out.gdal -f format=GTiff type=Byte input=shorewall output=/Volumes/GRASS2020/GRASSproject/SRTM/region/basin/amazonia/0/shorewall_amazonia_0_cgiar-250.tif –overwrite&lt;/span&gt;&lt;/p&gt;

&lt;h3 id=&quot;grass-shell-script&quot;&gt;GRASS shell script&lt;/h3&gt;

&lt;p&gt;The complete GRASS processing chain in this post (after importing the DEM and creating a new mapset covered in &lt;a href=&quot;../basin-delineate-01&quot;&gt;part 1&lt;/a&gt;, can be run as a shell script. The shell script omits the trial of different MFD parameterisations discussed above.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# Basin delineation: GRASS watershed analysis

# Watershed accumulation and basin area

r.watershed -as elevation=DEM@PERMANENT accumulation=SFD_upstream drainage=SFD_drainage threshold=2000 --overwrite

# r.watershed -as elevation=hydroptfill_DEM accumulation=SFD_upstream drainage=SFD_drainage threshold=2000 --overwrite

r.watershed -a elevation=DEM@PERMANENT accumulation=MFD_upstream drainage=MFD_drainage threshold=2000 --overwrite

#r.watershed -a elevation=hydroptfill_DEM accumulation=MFD_upstream drainage=MFD_drainage threshold=2000 --overwrite

# Export upstream layers

mkdir -p /Volumes/GRASS2020/GRASSproject/SRTM/region/basin/amazonia/0

r.mapcalc &quot;SFD_ln_upstream = 10*log(SFD_upstream)&quot; --overwrite

r.colors map=SFD_ln_upstream color=ryb

r.out.gdal -f input=SFD_ln_upstream format=GTiff type=Byte output=/Volumes/GRASS2020/GRASSproject/SRTM/region/basin/amazonia/0/upstream-ln-ryb-SFD_amazonia_0_cgiar-250.tif --overwrite

r.mapcalc &quot;MFD_ln_upstream = 10*log(MFD_upstream)&quot; --overwrite

r.colors map=MFD_ln_upstream color=ryb

r.out.gdal -f input=MFD_ln_upstream format=GTiff type=Byte output=/Volumes/GRASS2020/GRASSproject/SRTM/region/basin/amazonia/0/upstream-ln-ryb-MFD_amazonia_0_cgiar-250.tif --overwrite

# Identify terminal water body

r.mapcalc &quot;drain_terminal = if(isnull('DEM@PERMANENT'), 1, null())&quot;

#r.mapcalc &quot;drain_terminal = if(isnull('hydroptfill_DEM'), 1, null())&quot;

# Get shoreline

r.buffer input=drain_terminal output=shoreline distances=330 units=meters --overwrite

# Extract accumulated drainage for shoreline

r.mapcalc &quot;shoreline_SFD_flowacc = if((shoreline &amp;gt; 1), SFD_upstream, null())&quot;

r.mapcalc &quot;shoreline_MFD_flowacc = if((shoreline &amp;gt; 1), MFD_upstream, null())&quot;

# Threshold minimum drainage area

# Create reclass parameterization file
### 2000 thru 99999999 = 1 ###
### * = NULL ###

r.reclass input=shoreline_SFD_flowacc output=basin_SFD_outlets rules='/Volumes/GRASS2020/GRASSsupport/reclass/reclass_flow_acc_2000.txt' --overwrite

r.reclass input=shoreline_MFD_flowacc output=basin_MFD_outlets rules='/Volumes/GRASS2020/GRASSsupport/reclass/reclass_flow_acc_2000.txt' --overwrite

# Combine the two basin outlet rasters

r.mapcalc &quot;shoreline_flowacc = if ((shoreline_SFD_flowacc &amp;gt; shoreline_MFD_flowacc), shoreline_SFD_flowacc, shoreline_MFD_flowacc)&quot; --overwrite

r.reclass input=shoreline_flowacc output=basin_shoreline_outlets rules='/Volumes/GRASS2020/GRASSsupport/reclass/reclass_flow_acc_2000.txt' --overwrite

# Vectorize basin shoreline outlet candidates

r.to.vect input=basin_shoreline_outlets output=basin_outlet_pt type=point --overwrite

r.to.vect input=basin_SFD_outlets output=basin_SFD_outlet_pt type=point  --overwrite

r.to.vect input=basin_MFD_outlets output=basin_MFD_outlet_pt type=point --overwrite

# Identify basin river mouths

### You can change the if conditions to include a wider range of values (e.g. 1 or 2).

r.mapcalc &quot;lowlevel_DEM = if((DEM@PERMANENT &amp;lt;= 0), 0, null())&quot;

#r.mapcalc &quot;lowlevel_DEM = if((hydroptfill_DEM &amp;lt;= 0), 0, null())&quot;

r.cost -n input=lowlevel_DEM output=lowlevel_outlet_costgrow start_points=basin_outlet_pt max_cost=0 --overwrite

r.clump -d input=lowlevel_outlet_costgrow  output=lowlevel_outlet_clumps --overwrite

r.out.gdal -f input=lowlevel_outlet_clumps format=GTiff output=/Volumes/GRASS2020/GRASSproject/SRTM/region/basin/amazonia/0/lowlevel-outlet-clumps_amazonia_0_cgiar-250.tif --overwrite

# Identify beachfront river mouths

r.mapcalc &quot;shoreline_DEM = if((shoreline &amp;gt; 1 &amp;amp;&amp;amp; DEM@PERMANENT &amp;lt;= 0), 0, null())&quot; --overwrite

#r.mapcalc &quot;shoreline_DEM = if((shoreline &amp;gt; 1 &amp;amp;&amp;amp; hydroptfill_DEM &amp;lt;= 0), 0, null())&quot; --overwrite

r.cost -n input=shoreline_DEM output=shoreline_outlet_costgrow start_points=basin_outlet_pt max_cost=0 --overwrite

r.clump -d input=shoreline_outlet_costgrow output=shoreline_outlet_clumps --overwrite

r.out.gdal -f input=shoreline_outlet_clumps format=GTiff output=/Volumes/GRASS2020/GRASSproject/SRTM/region/basin/amazonia/0/shoreline-outlet-clumps_amazonia_0_cgiar-250.tif --overwrite

### From this point the separate SFD and MFD data sources are not needed for a combined solutions. The coding is however included for convenience. ###

# Vectorize shoreline mouths

r.to.vect input=shoreline_outlet_clumps output=basin_mouth_outlet_pt type=point

# Add columns to vector databases

v.db.addcolumn map=basin_mouth_outlet_pt columns='upstream DOUBLE PRECISION'

v.db.addcolumn map=basin_mouth_outlet_pt columns='elevation INT'

v.db.addcolumn map=basin_mouth_outlet_pt columns='mouth_id INT'

v.db.addcolumn map=basin_mouth_outlet_pt columns='basin_id INT'

# or with multiple columns added in one command

v.db.addcolumn map=basin_SFD_outlet_pt columns='upstream DOUBLE PRECISION, elevation INT, mouth_id INT, basin_id INT'

v.db.addcolumn map=basin_MFD_outlet_pt columns='upstream DOUBLE PRECISION, elevation INT, mouth_id INT, basin_id INT'

# Add basin data to vector databases

v.what.rast map=basin_mouth_outlet_pt raster=SFD_upstream column=upstream

v.what.rast map=basin_mouth_outlet_pt raster=DEM@PERMANENT column=elevation

#v.what.rast map=basin_mouth_outlet_pt raster=hydroptfill_DEM column=elevation

v.what.rast map=basin_mouth_outlet_pt raster=shoreline_outlet_clumps column=mouth_id

v.what.rast map=basin_mouth_outlet_pt raster=lowlevel_outlet_clumps column=basin_id

v.what.rast map=basin_SFD_outlet_pt raster=SFD_upstream column=upstream

v.what.rast map=basin_SFD_outlet_pt raster=DEM@PERMANENT column=elevation

#v.what.rast map=basin_SFD_outlet_pt raster=hydroptfill_DEM column=elevation

v.what.rast map=basin_SFD_outlet_pt raster=shoreline_outlet_clumps column=mouth_id

v.what.rast map=basin_SFD_outlet_pt raster=lowlevel_outlet_clumps column=basin_id

v.what.rast map=basin_MFD_outlet_pt raster=MFD_upstream column=upstream

v.what.rast map=basin_MFD_outlet_pt raster=DEM@PERMANENT column=elevation

#v.what.rast map=basin_MFD_outlet_pt raster=hydroptfill_DEM column=elevation

v.what.rast map=basin_MFD_outlet_pt raster=shoreline_outlet_clumps column=mouth_id

v.what.rast map=basin_MFD_outlet_pt raster=lowlevel_outlet_clumps column=basin_id

# Upload the x and y coordinates to the vector db

v.to.db map=basin_mouth_outlet_pt option=coor columns=xcoord,ycoord

v.to.db map=basin_SFD_outlet_pt option=coor columns=xcoord,ycoord

v.to.db map=basin_MFD_outlet_pt option=coor columns=xcoord,ycoord

# Export basin outlet candidates

v.out.ogr type=point input=basin_mouth_outlet_pt format=ESRI_Shapefile output=/Volumes/GRASS2020/GRASSproject/SRTM/region/basin/amazonia/0/basin-mouth-outlet-pt_drainage_amazonia_0_cgiar-250.shp --overwrite

v.out.ogr type=point input=basin_SFD_outlet_pt format=ESRI_Shapefile output=/Volumes/GRASS2020/GRASSproject/SRTM/region/basin/amazonia/0/basin-SFD-outlet-pt_drainage_amazonia_0_cgiar-250.shp --overwrite

v.out.ogr type=point input=basin_MFD_outlet_pt format=ESRI_Shapefile output=/Volumes/GRASS2020/GRASSproject/SRTM/region/basin/amazonia/0/basin-MFD-outlet-pt_drainage_amazonia_0_cgiar-250.shp --overwrite

# Build a virtual wall across the basin mouths

r.buffer input=shoreline_outlet_clumps output=mouth_buffer distances=330 units=meters --overwrite

r.mapcalc &quot;thickwall = if((drain_terminal == 1 &amp;amp;&amp;amp; mouth_buffer &amp;gt; 1), 1, null())&quot; --overwrite

r.out.gdal -f format=GTiff type=Byte input=thickwall output=/Volumes/GRASS2020/GRASSproject/SRTM/region/basin/amazonia/0/thickwall_amazonia_0_cgiar-250.tif --overwrite

# Shrink the thickwall to 1 cell width

r.mapcalc &quot;drain_terminal_remain = if((drain_terminal == 1),  if ((isnull(thickwall)),1, null() ),null() ) &quot; --overwrite  

r.out.gdal -f format=GTiff type=Byte input=drain_terminal_remain output=/Volumes/GRASS2020/GRASSproject/SRTM/region/basin/amazonia/0/drain-terminal-remain_amazonia_0_cgiar-250.tif --overwrite

r.buffer input=drain_terminal_remain output=mouthshoreline distances=330 units=meters --overwrite

r.out.gdal -f format=GTiff type=Byte input=mouthshoreline output=/Volumes/GRASS2020/GRASSproject/SRTM/region/basin/amazonia/0/mouthshoreline_amazonia_0_cgiar-250.tif --overwrite

r.mapcalc &quot;shorewall = if((mouthshoreline &amp;gt; 1 &amp;amp;&amp;amp; thickwall == 1), 1, null())&quot; --overwrite

r.out.gdal -f format=GTiff type=Byte input=shorewall output=/Volumes/GRASS2020/GRASSproject/SRTM/region/basin/amazonia/0/shorewall_amazonia_0_cgiar-250.tif --overwrite

r.to.vect input=shorewall output=shorewall_pt type=point --overwrite

v.out.ogr type=point input=shorewall_pt format=ESRI_Shapefile output=/Volumes/GRASS2020/GRASSproject/SRTM/region/basin/amazonia/0/shorewall-pt_drainage_amazonia_0_cgiar-250.shp --overwrite

r.out.gdal -f format=GTiff type=Byte input=shorewall output=/Volumes/GRASS2020/GRASSproject/SRTM/region/basin/amazonia/0/shorewall_amazonia_0_cgiar-250.tif --overwrite

# Fill up the original DEM with the holes created from the difference between thickwall and shorewall

r.mapcalc &quot;fillholeDEM = if ((thickwall==1 &amp;amp;&amp;amp; isnull(mouthwall)),1, null() ) &quot; --overwrite

r.out.gdal format=GTiff type=Byte input=fillholeDEM output=/Volumes/GRASS2020/GRASSproject/SRTM/region/basin/amazonia/0/fillholeDEM_amazonia_0_cgiar-250.tif --overwrite

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;next-step&quot;&gt;Next step&lt;/h3&gt;

&lt;p&gt;The &lt;a href=&quot;../basin-delineate-03&quot;&gt;next step&lt;/a&gt; requires in depth analysis of database records and spatial relations. I do not know how to accomplish that using standard GIS commands. The next step thus requires Python scripting.&lt;/p&gt;</content><author><name></name></author><category term="basindelineate" /><category term="GRASS7" /><category term="watershed" /><category term="basin" /><category term="delineation" /><category term="upstream accumulation" /><category term="hydrology" /><summary type="html">Use GRASS7 r.watershed module for analysing drainage direction and accumulated upstream area</summary></entry><entry><title type="html">Basin delineation: 1 patch up DEM</title><link href="https://nunataryuk.github.io//basindelineate/basin-delineate-01/" rel="alternate" type="text/html" title="Basin delineation: 1 patch up DEM" /><published>2020-10-24T00:00:00+00:00</published><updated>2020-10-24T00:00:00+00:00</updated><id>https://nunataryuk.github.io//basindelineate/basin-delineate-01</id><content type="html" xml:base="https://nunataryuk.github.io//basindelineate/basin-delineate-01/">&lt;script src=&quot;https://karttur.github.io/common/assets/js/karttur/togglediv.js&quot;&gt;&lt;/script&gt;

&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;

&lt;p&gt;The basin delineation system outlined in this blog only requires a Digital Elevation Model (DEM) as input. The quality of the DEM is, however, critical. This first part of the series of instructions deals with hydrological correction of DEMs using GRASS GIS.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;You must have setup GRASS 7 and imported a DEM as described under &lt;a href=&quot;../../basindelineatesetup&quot;&gt;Installation &amp;amp; Setup&lt;/a&gt; in this blog. The DEM must have null (“no data”) for the water body into which the basins to delineate drain.&lt;/p&gt;

&lt;h2 id=&quot;dem-errors-affecting-basin-delineation&quot;&gt;DEM errors affecting basin delineation&lt;/h2&gt;

&lt;p&gt;Basin delineation is grounded in the fact that water tends to flow downhill (from a higher to a lower potential, but strictly that does not necessarily mean downhill). This routing of water across the hillside and within water courses is the key for connecting any geographic point in the landscape to an outlet point.&lt;/p&gt;

&lt;p&gt;DEM issues that can cause problems for flow routing include:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;regions lacking elevation data,&lt;/li&gt;
  &lt;li&gt;artificial pits,&lt;/li&gt;
  &lt;li&gt;artificial barriers,&lt;/li&gt;
  &lt;li&gt;flat regions,&lt;/li&gt;
  &lt;li&gt;wide river mouths.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Regions lacking data are generally difficult to mend. There are, however, special cases where it is necessary. In this tutorial you will mend small no data regions (a few cells) by first assigning a low elevation (typically 0) and then, optionally, apply a pit filling. It is crucial to remove land locked no data cells as these regions will otherwise gorge all the (virtual) water flow entering.&lt;/p&gt;

&lt;p&gt;Artificial (virtual) pits are the most common problem, and most flow routing algorithms, including GRASS &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/r.watershed.html&quot;&gt;r.watershed&lt;/a&gt; can handle pits. For hydrological modeling it is often, but not always, better to mend artificial depressions. Also that will be done in the this manual.&lt;/p&gt;

&lt;p&gt;Artificial barriers are more difficult to handle. And is not approached in this manual. You could, however, invert the DEM and solve problems with barriers as if they were pits.&lt;/p&gt;

&lt;p&gt;Flat regions and wide river mouths cause similar flow routing problems. Different algorithms can be applied (i.e. Single flow Direction, or SFD, versus Multiple Flow Direction, MFD) for handling flat area. But a more ideal solution is to increase the vertical resolution and properly steer the routing using elevation data. This post includes a test of different flow directions algorithms, and in &lt;a href=&quot;../basin-delineate-03&quot;&gt;part 3&lt;/a&gt; you will create an artificial slope directing outflow from wide river mouths.&lt;/p&gt;

&lt;h3 id=&quot;coastal-dem-errors&quot;&gt;Coastal DEM errors&lt;/h3&gt;

&lt;p&gt;Narrow bays, estuaries, lagoons etc. can cause problems in near shore regions. Both because they can come out as pits, but a worse problem is if such features become defined as land locked no data cells. That will cause problems both for identifying the basin outlets and later for modeling water flow out of the basin. To get rid of small “no data” regions along the coast you need to identify them and then assign an elevation that allows water to pass. The main objective of this post is to present a GRASS based scripting sequence that removes land locked no data holes in Digital Elevation Models.&lt;/p&gt;

&lt;h2 id=&quot;grass-preparations&quot;&gt;GRASS preparations&lt;/h2&gt;

&lt;p&gt;There are several GRASS commands that can be used for mending and adjusting DEMs. The easiest is to use ordinary gap filling (&lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/r.fill.stats.html&quot;&gt;r.fill.stats&lt;/a&gt; or &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/r.fillnulls.html&quot;&gt;r.fillnulls&lt;/a&gt;) but these methods do not consider the flow routing. Instead GRASS offers the &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/r.fill.dir.html&quot;&gt;r.fill.dir&lt;/a&gt; algorithm. &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/r.fill.dir.html&quot;&gt;r.fill.dir&lt;/a&gt; identifies pits along flow paths and then fills them. You can also use &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/r.fill.dir.html&quot;&gt;r.fill.dir&lt;/a&gt; for mending small no data holes, by first filling all the holes with a low elevation (typically DEM = 0).&lt;/p&gt;

&lt;p&gt;The command &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/addons/r.flowfill.html&quot;&gt;r.flowfill&lt;/a&gt; is an alternative, available as an &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/addons/&quot;&gt;addon&lt;/a&gt;. To install GRASS addons you must have prepared the GRASS c-compiler as described in the post &lt;a href=&quot;https://karttur.github.io/setup-ide/setup-ide/install-gis/#grass&quot;&gt;Install GDAL, QGIS and GRASS&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you want to try &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/addons/r.flowfill.html&quot;&gt;r.flowfill&lt;/a&gt; , the command for installing it is:&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; g.extension  extension=r.fill.gaps&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;The rest of this manual however, uses &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/r.fill.dir.html&quot;&gt;r.fill.dir&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;grass-processing&quot;&gt;GRASS processing&lt;/h2&gt;

&lt;p&gt;The text sections below explains how to use GRASS for identifying and filling land locked no data regions. If you only want to run the process, all the commands are summarized further down.&lt;/p&gt;

&lt;p&gt;Once you have filled the land locked no data holes you can use &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/r.fill.dir.html&quot;&gt;r.fill.dir&lt;/a&gt; fill them up with more realistic elevation values. Fur running the watershed analysis in parts 2 and 3, it is, however, enough to assign a low elevation (i.e. 0) to the no data holes. The GRASS process [r.watershed] manages pits on its own.&lt;/p&gt;

&lt;p&gt;If you want to fill up pits in your DEM in a hydrologically sound manner then you should run &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/r.fill.dir.html&quot;&gt;r.fill.dir&lt;/a&gt;. For large grids that will, however, take a very (very) long time. For a DEM of 10 M pixels it can take 24 hours or longer. Thus you need to tile the DEM, with some overlap, before running &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/r.fill.dir.html&quot;&gt;r.fill.dir&lt;/a&gt; separately on each tile. To write such a command structure would also take a day. But with the help of the Python package &lt;em&gt;basin_extract&lt;/em&gt; the GRASS commands will be setup in a second. Below both the principles for the pit filling, and how to use &lt;em&gt;basin_extract&lt;/em&gt;, are covered. But first you need to remove the land locked no data regions.&lt;/p&gt;

&lt;h3 id=&quot;make-the-target-directory&quot;&gt;Make the target directory&lt;/h3&gt;

&lt;p&gt;If you want to follow the standard of Karttur’s GeoImagine Framework, your target folder for the exported layers should be (where &lt;em&gt;amazonia&lt;/em&gt; is the region used in this example):&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; mkdir -p /Volumes/GRASS2020/GRASSproject/SRTM/region/basin/amazonia/0/stage0/script&lt;/span&gt;&lt;/p&gt;

&lt;h3 id=&quot;create-a-map-of-the-terminal-drainage&quot;&gt;Create a map of the terminal drainage&lt;/h3&gt;

&lt;p&gt;Create a map of the terminal drainage using the GRASS command &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/r.mapcalc.html&quot;&gt;r.mapcalc&lt;/a&gt;. For this to work, your original DEM (as described in the post on &lt;a href=&quot;../../basin_delineate_setup/basin-delineate-grass7/&quot;&gt;GRASS 7 setup&lt;/a&gt; ) must have the terminal drainage defined as “null” in the original DEM.&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt;r.mapcalc &quot;drain_terminal = if(isnull('DEM@PERMANENT'), 1, null())&quot;&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Clump (&lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/r.clump.html&quot;&gt;r.clump&lt;/a&gt;) all contiguous cells together with a unique id to allow identifying large and small bodies representing the terminal drainage (null in the original DEM).&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; r.clump input=drain_terminal output=terminal_clumps –overwrite&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Convert the clumps to polygons with the command &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/r.to.vect.html&quot;&gt;r.to.vect&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; r.to.vect input=terminal_clumps output=terminal_clumps type=area –overwrite&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Add the area of each polygon to the the vector database with the command &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/v.to.db.html&quot;&gt;v.to.db&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; v.to.db map=terminal_clumps type=centroid option=area columns=area_km2 units=kilometers&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Optionally export the vector with the polygons representing “no data” in the original DEM with &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/v.out.ogr.html&quot;&gt;v.out.ogr&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; v.out.ogr input=terminal_clumps type=area format=ESRI_Shapefile output=/Volumes/GRASS2020/GRASSproject/SRTM/region/basin/amazoniax/0/terminal-clumps_SRTM_hydro-amazonia_0_cgiar-250.shp –overwrite&lt;/span&gt;&lt;/p&gt;

&lt;h3 id=&quot;fill-in-dem-over-land&quot;&gt;Fill in DEM over land&lt;/h3&gt;

&lt;p&gt;With the command &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/v.to.rast.html&quot;&gt;v.to.rast&lt;/a&gt; it is possible to apply an SQL while converting vectors to a raster. Use that function to assign polygons below an area threshold to be the “no data” to fill, while polygons above the threshold should then represent the true terminal drainage. Set the pixel value to 0 - the preliminary elevation you will assign these cells. In the example I have set the threshold at 2.0 square kilometers. You should inspect the result and change the threshold if required.&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; v.to.rast input=terminal_clumps type=area where=&quot;area_km2&amp;lt; 2.0&quot; output=drain_terminal_small use=val value=0 –overwrite&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; v.to.rast input=terminal_clumps type=area where=&quot;area_km2 &amp;gt;= 2.0&quot; output=drain_terminal_large use=val value=0 –overwrite&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;if you can not identify a threshold that works you might have to consider manually editing the polygons. For example by deleting individual polygons in &lt;span class=&quot;app&quot;&gt;QGIS&lt;/span&gt;.&lt;/p&gt;

&lt;p&gt;Superimpose the small “no data” clumps over the original DEM to create a DEM that is complete over land.&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; r.mapcalc &quot;inland_comp_DEM = if(( isnull(DEM@PERMANENT)), drain_terminal_small,DEM@PERMANENT )&quot; –overwrite&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Optionally export the completed DEM for land areas:&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; r.out.gdal format=GTiff  input=inland_comp_DEM output=/Volumes/GRASS2020/GRASSproject/SRTM/region/basin/amazoniax/0/inland-comp-DEM_amazonia_0_cgiar-250.tif –overwrite&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;The DEM that you just created, &lt;em&gt;inland_comp_DEM&lt;/em&gt; is sufficient for use with &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/r.watershed.html&quot;&gt;r.watershed&lt;/a&gt; in &lt;a href=&quot;../basin-delineate-02&quot;&gt;part 2&lt;/a&gt;. The remaining part of this post deals with filling up pits. But that is strictly not needed for running &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/r.watershed.html&quot;&gt;r.watershed&lt;/a&gt; and might even have detrimental consequences.&lt;/p&gt;

&lt;h3 id=&quot;grass-shell-script&quot;&gt;GRASS shell script&lt;/h3&gt;

&lt;p&gt;The shell script below contains all the GRASS commands explained above. You can run the commands below by copying them to a file with the extension &lt;span class=&quot;file&quot;&gt;.sh&lt;/span&gt;. Remember that you have to make the script file executable before you can run it (&lt;span class=&quot;terminal&quot;&gt;chmod u+x /path/to/file.sh&lt;/span&gt; or &lt;span class=&quot;terminal&quot;&gt;chmod 755 /path/to/file.sh&lt;/span&gt;). The run it from the &lt;span class=&quot;app&quot;&gt;Terminal&lt;/span&gt; window where GRASS is running.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# Create a map of the terminal drainage

mkdir -p /Volumes/GRASS2020/GRASSproject/SRTM/region/basin/amazonia/0/stage0/script

r.mapcalc &quot;drain_terminal = if(isnull('DEM@PERMANENT'), 1, null())&quot;

r.clump input=drain_terminal output=terminal_clumps --overwrite

r.to.vect input=terminal_clumps output=terminal_clumps type=area --overwrite

v.to.db map=terminal_clumps type=centroid option=area columns=area_km2 units=kilometers

# v.out.ogr input=terminal_clumps type=area format=ESRI_Shapefile output=/Volumes/GRASS2020/GRASSproject/SRTM/region/basin/amazoniax/0/terminal-clumps_SRTM_hydro-amazonia_0_cgiar-250.shp --overwrite

# Fill in DEM over land

v.to.rast input=terminal_clumps type=area where=&quot;area_km2&amp;lt; 2.0&quot; output=drain_terminal_small use=val value=0 --overwrite

v.to.rast input=terminal_clumps type=area where=&quot;area_km2 &amp;gt;= 2.0&quot; output=drain_terminal_large use=val value=0 --overwrite

r.mapcalc &quot;inland_comp_DEM = if(( isnull(DEM@PERMANENT)), drain_terminal_small,DEM@PERMANENT )&quot; --overwrite

# r.out.gdal format=GTiff  input=inland_comp_DEM output=/Volumes/GRASS2020/GRASSproject/SRTM/region/basin/amazoniax/0/inland-comp-DEM_amazonia_0_cgiar-250.tif --overwrite
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;restrict-dem-filling-to-coastal-strip&quot;&gt;Restrict DEM filling to coastal strip&lt;/h3&gt;

&lt;p&gt;If you want to restrict the pit filling to the coastal strip you need to create a mask (&lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/r.mask.html&quot;&gt;r.mask&lt;/a&gt;). A mask limits the region for raster operations and will reduce the processing time.&lt;/p&gt;

&lt;p&gt;To set a mask restricting the processing to the coastal strip you have to create a new layer for the terminal drainage - this time excluding the cells identified in &lt;em&gt;drain_terminal_small&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; r.mapcalc &quot;drain_terminal_v2 = if(isnull('inland_comp_DEM'), 1, null())&quot; –overwrite&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Then do an &lt;a href=&quot;#&quot;&gt;r.buffer&lt;/a&gt; starting from version 2 (&lt;em&gt;v2&lt;/em&gt;) of the terminal drainage, set the maximum distance to the width of the coastal strip you want to analyse (e.g. 3 kilometers in the example).&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; r.buffer input=drain_terminal_v2 output=coastal_strip distances=3000 units=meters –overwrite&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Apply an &lt;a href=&quot;#&quot;&gt;r.mapcalc&lt;/a&gt; calculation to create a boolean layer of the coastal strip.&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; r.mapcalc &quot;coastal_mask = if((coastal_strip &amp;gt; 1), 1, null())&quot;&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;To apply the mask by &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/r.mask.html&quot;&gt;r.mask&lt;/a&gt; you just set it and it will remain in place until explicitly removed.&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; r.mask raster=coastal_mask&lt;/span&gt;&lt;/p&gt;

&lt;h3 id=&quot;filling-pits-in-the-dem&quot;&gt;Filling pits in the DEM&lt;/h3&gt;

&lt;p&gt;With all land areas having an assigned elevation, the next task is to fill up pits. If you want to fill up pits large than a single cell, you need to set a threshold for the maximum pit size to fill. This is done in one or two steps. First you have to decide whether to only fill up single cell pits (by setting the the parameter &lt;em&gt;filldirsingle&lt;/em&gt; to &lt;em&gt;1&lt;/em&gt; [= True]). Filling up single cell pits is more straight forward and is the recommended practice. If you set &lt;em&gt;filldirsingle&lt;/em&gt; &lt;em&gt;0&lt;/em&gt; [= False] you also need to set the maximum pit are to fill up (the parameter &lt;em&gt;pitfillmaxarea&lt;/em&gt;), in the area units set to the vector database (square kilometers in the example below).&lt;/p&gt;

&lt;p&gt;As noted above, you can not run &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/r.fill.dir.html&quot;&gt;r.fill.dir&lt;/a&gt; on large files. Here I only list the principal steps. To actually run the pit filling make use of the Python package &lt;em&gt;basin_extract&lt;/em&gt; to create the required GRASS commands as explained further down.&lt;/p&gt;

&lt;p&gt;The principal steps for filling up single cell pits (set the &lt;em&gt;-f&lt;/em&gt; flag) in a DEM in GRASS:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;r.fill.dir -f originaldem -&amp;gt; filldem [the f flag forces filling of single cells only]&lt;/li&gt;
  &lt;li&gt;r.mapcalc difference between filled and orignal dem&lt;/li&gt;
  &lt;li&gt;r.to.vect for cells (points) with difference&lt;/li&gt;
  &lt;li&gt;v.db.addcolumn for storing filled DEM value&lt;/li&gt;
  &lt;li&gt;v.what.rast extract fill DEM to vector points&lt;/li&gt;
  &lt;li&gt;v.patch to collect all vector points to fill in a single data source&lt;/li&gt;
  &lt;li&gt;v.to.rast rasterize the DEM values&lt;/li&gt;
  &lt;li&gt;r.mapcalc superimpose the filled DEM values over the original DEM&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Overlaying the vector points on top of the original DEM can be done using different approaches. Apart from the GRASS comamnds used above (v.patch, v.to.rast and r.mapcalc) you can also make use of GDAL. This is discussed further down.&lt;/p&gt;

&lt;p&gt;The principal steps for filling up also pits larger than a single cell in a DEM in GRASS:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;r.fill.dir originaldem -&amp;gt; filldem2 [all pit cells filled regardless of area]&lt;/li&gt;
  &lt;li&gt;r.mapcalc difference between filled and orignal dem&lt;/li&gt;
  &lt;li&gt;r.to.vect for regions (areas) with difference&lt;/li&gt;
  &lt;li&gt;v.db.addcolumn for storing filled DEM value&lt;/li&gt;
  &lt;li&gt;v.what.rast extract fill DEM to polygon centroids&lt;/li&gt;
  &lt;li&gt;
    &lt;ul&gt;
      &lt;li&gt;v.patch to collect all vector areas to fill in a single data source&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;v.to.rast rasterize the DEM values&lt;/li&gt;
  &lt;li&gt;r.mapcalc superimpose the filled DEM values over the original DEM&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Overlaying the vector points on top of the original DEM can be done using different approaches. Apart from the GRASS comamnds used above (v.patch, v.to.rast and r.mapcalc) you can also make use of GDAL. This is discussed further down.&lt;/p&gt;

&lt;p&gt;The process steps above can not be run for very large grids - it takes too long time. But pits are usually small. Larger pits (hundreds of square kilometers or larger) usually represent endorheic basins and should not be filled. Thus you must, and can, partition large DEMs into smaller tiles and then run each tile separately. With an overlap that allows you to capture pits up to the size you deem relevant.&lt;/p&gt;

&lt;h3 id=&quot;create-tiled-process-loop&quot;&gt;Create tiled process loop&lt;/h3&gt;

&lt;p&gt;The example DEM used in this tutorial of central South America is approximately 22700 × 22500 cells. This is far too large to process in one go. The Python package developed for assisting the basin delineation, &lt;em&gt;basin_extract&lt;/em&gt; can generate a tiled process loop for GRASS.&lt;/p&gt;

&lt;h4 id=&quot;parameterisation&quot;&gt;Parameterisation&lt;/h4&gt;

&lt;p&gt;The Python script &lt;em&gt;basin_extractor&lt;/em&gt; is parameterised from an XML file that follows the general structure of &lt;a href=&quot;https://karttur.github.io/geoimagine/concept/concept-concepts/#xml-coded-parameterization&quot;&gt;Karttur´s GeoImagine Framework&lt;/a&gt;. The structure below shows the setup for generating the GRASS shell script for a tiled processing of pit filling of the DEM over central South America (“Amazonia”).&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;basindelineate&amp;gt;
	&amp;lt;userproj userid = 'karttur' projectid = 'karttur' tractid= 'amazonia' siteid = '*' plotid = '*' system = 'MODIS'&amp;gt;&amp;lt;/userproj&amp;gt;

	&amp;lt;!-- Define processing --&amp;gt;
	&amp;lt;process processid ='BasinDelineate'&amp;gt;
		&amp;lt;overwrite&amp;gt;Y&amp;lt;/overwrite&amp;gt; # Y(es) or N(o)
		&amp;lt;delete&amp;gt;N&amp;lt;/delete&amp;gt; # Y(es) or N(o)
		&amp;lt;parameters
		stage = '0' # 0, 1, 2 or 3
		adjacentdist = '330' # any number in map distance units
		outlet = 'SFD' # MOUTH, SFD or MFD
		distill = '' # MFD or None (nothing)
		clusteroutlet = 'central' # central or max
		basinthreshold='2000' # any number is cells
		watershed='MFD5' # SFD, MFD or MFD? (? in range 1..10)
		verbose = '1' # 0, 1 or 2
		proj4CRS = '+proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs'
		&amp;gt;

		&amp;lt;/parameters&amp;gt;
		&amp;lt;srcpath volume = &quot;GRASS2020&quot;&amp;gt;&amp;lt;/srcpath&amp;gt;
		&amp;lt;dstpath volume = &quot;GRASS2020&quot;&amp;gt;&amp;lt;/dstpath&amp;gt;

		&amp;lt;srccomp&amp;gt;
			&amp;lt;!-- the name &amp;lt;tag&amp;gt; and prefix must be the same and can not be altered. The script looks for which source data is included and compares that with the parameters, and then sets the processing method if these corresponds. Otherwise the script ends with an error message.
			--&amp;gt;
			&amp;lt;basin-mouth-outlet-pt source = &quot;SRTM&quot; product = &quot;drainage&quot; folder = &quot;basin&quot; band = &quot;basin-mouth-outlet-pt&quot; prefix = &quot;basin-mouth-outlet-pt&quot; suffix = &quot;cgiar-250&quot;&amp;gt;
			&amp;lt;/basin-mouth-outlet-pt&amp;gt;

			&amp;lt;basin-MFD-outlet-pt source = &quot;SRTM&quot; product = &quot;drainage&quot; folder = &quot;basin&quot; band = &quot;basin-MFD-outlet-pt&quot; prefix = &quot;basin-MFD-outlet-pt&quot; suffix = &quot;cgiar-250&quot;&amp;gt;
			&amp;lt;/basin-MFD-outlet-pt&amp;gt;

			&amp;lt;basin-SFD-outlet-pt source = &quot;SRTM&quot; product = &quot;drainage&quot; folder = &quot;basin&quot; band = &quot;basin-SFD-outlet-pt&quot; prefix = &quot;basin-SFD-outlet-pt&quot; suffix = &quot;cgiar-250&quot;&amp;gt;
			&amp;lt;/basin-SFD-outlet-pt&amp;gt;

			&amp;lt;shorewall-pt source = &quot;SRTM&quot; product = &quot;drainage&quot; folder = &quot;basin&quot; band = &quot;shorewall-pt&quot; prefix = &quot;shorewall-pt&quot; suffix = &quot;cgiar-250&quot;&amp;gt;
			&amp;lt;/shorewall-pt&amp;gt;
		&amp;lt;/srccomp&amp;gt;

		&amp;lt;dstcomp&amp;gt;
			&amp;lt;basin-outlet&amp;gt;
			&amp;lt;/basin-outlet&amp;gt;
			&amp;lt;!--
			To force a non-default destination path and name, you can enter all components explicitly, e.g.
			&amp;lt;basin-outlet band = &quot;basin-mfd+mfd-max&quot; prefix = &quot;basin-mfd+mfd-max&quot;&amp;gt;
			&amp;lt;/basin-outlet&amp;gt;
			--&amp;gt;
		&amp;lt;/dstcomp&amp;gt;
	&amp;lt;/process&amp;gt;

&amp;lt;/basindelineate&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The core parameters of relevance for this stage (0) include:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;stage [0, 1, 2 or 3] (this part is for stage = ‘0’)&lt;/li&gt;
  &lt;li&gt;adjacentdist [not relevant for stage 0]&lt;/li&gt;
  &lt;li&gt;outlet [not relevant for stage 0]&lt;/li&gt;
  &lt;li&gt;distill [not relevant for stage 0]&lt;/li&gt;
  &lt;li&gt;clusteroutlet [not relevant for stage 0]&lt;/li&gt;
  &lt;li&gt;basinthreshold [not relevant for stage 0]&lt;/li&gt;
  &lt;li&gt;watershed [not relevant for stage 0]&lt;/li&gt;
  &lt;li&gt;pitfill&lt;/li&gt;
  &lt;li&gt;tilesize&lt;/li&gt;
  &lt;li&gt;tileoverlap&lt;/li&gt;
  &lt;li&gt;single [Boolean]&lt;/li&gt;
  &lt;li&gt;verbose [0, 1 or 2]&lt;/li&gt;
  &lt;li&gt;proj4CRS [‘proj4CRS definition for output data if input layers lack proper projection information’]&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;setup-xml-file-and-run-basin_extract&quot;&gt;Setup xml file and run &lt;em&gt;basin_extract&lt;/em&gt;&lt;/h4&gt;

&lt;p&gt;Use the xml template file above to setup the processing for your own xml. Set the parameter &lt;em&gt;stage&lt;/em&gt; to &lt;em&gt;0&lt;/em&gt; and run &lt;em&gt;basin_extract&lt;/em&gt; pointing to the xml.&lt;/p&gt;

&lt;p&gt;Running &lt;em&gt;basin_extract&lt;/em&gt; with &lt;em&gt;stage = 0&lt;/em&gt; generates two shell script files, one for GRASS and one for OGR2OGR. You do not need to use the OGR2OGR file, it is just supplied in case you prefer that.&lt;/p&gt;

&lt;p&gt;For each tile that is processed the GRASS shell script includes the following set of commands (excluding comments):&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# This example shows the script for single cell filling (with the -f flag set):

# region is automatically set from the tiling process
g.region -a n=-3660170.456407 s=-3914992.450317 e=-8826107.250690 w=-9080929.244600

# the -f flag restricts the filling to single, isolated cells
r.fill.dir -f input=inland_comp_DEM output=hydro_cellfill_DEM_0_0 direction=hydro_cellfill_draindir_0_0 areas=hydro_cellfill_problems_0_0 --overwrite

# get the difference between the filled and the original DEM
r.mapcalc &quot;DEM_cellfill_diff_0_0 = hydro_cellfill_DEM_0_0 - inland_comp_DEM&quot; --overwrite

# create boolean mask of changed cells
r.mapcalc &quot;inland_fill_cell_0_0 = if(DEM_cellfill_diff_0_0 != 0, 1, null() )&quot; --overwrite

# convert the cells with altered elevation to a point vector
r.to.vect input=inland_fill_cell_0_0 output=inland_fill_pt_0_0 type=point --overwrite

# add column to vector file with altered elevations
v.db.addcolumn map=inland_fill_pt_0_0 columns=&quot;filldem DOUBLE PRECISION&quot;

# read the altered DEM values to the point vector
v.what.rast map=inland_fill_pt_0_0 column=filldem raster=hydro_cellfill_DEM_0_0

# export the point vector to an ESRI shape file
v.out.ogr input=inland_fill_pt_0_0 type=point format=ESRI_Shapefile output=/Volumes/GRASS2020/GRASSproject/SRTM/region/basin/amazonia/0/stage0/inland-fill-pt-0-0.shp --overwrite
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;superimpose-filled-dem-over-original-dem&quot;&gt;Superimpose filled DEM over original DEM&lt;/h4&gt;

&lt;p&gt;All areas identified as pits are captured in the sequence of tiles. You now have several alternatives for superimposing these values over the original DEM. All alternatives can be applied using a tile by tile approach, or applied for the whole region after patching the tiles.&lt;/p&gt;

&lt;p&gt;Filling of pits affects flow routing and hydrological modeling. When the flow routing enters a flat area, an algorithm traversing the flat area towards the lowest drainage(s) out of the flat must be identified. Dependent on the parameterization of the flow dividsion (single or multiples and the spread if multiple) the flat area will be wetted differently. This will later also affect the estimation of soil moisture, evapotranspiration and flooding tendencies of the cells in the flat area (in general all tend to increase). The most widely spread recommendation is to allow filling of single cell pits, but be more careful with filling larger and larger pits.&lt;/p&gt;

&lt;p&gt;On the other hand, DEMs generated by SAR data have a tendency to underestimate the elevation of water surface (due to radar signal double bouncing on water surface and shoreline structures). Thus no general recommendation can be given regarding how to handle larger depressions in DEMs.&lt;/p&gt;

&lt;p&gt;You have different alternatives for superimposing
The alternatives for filling pits that I have tested include:&lt;/p&gt;

&lt;p&gt;-&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;superimpose tile by tile with GDAL&lt;/li&gt;
  &lt;li&gt;patch up vectors in GDAL, superimpose&lt;/li&gt;
  &lt;li&gt;patch up vectors in GRASS, rasterize, superimpose&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;GDAL is quicker and can be used for &lt;em&gt;burning&lt;/em&gt; vector data on a raster. The burning can be done on a tile by tile basis, or after patching up the tiles to a single vector data source. With GRASS you have to first patch the vector tiles, then rasterize them, and then superimpose. This takes longer and demands more processing capacity.&lt;/p&gt;

&lt;h5 id=&quot;patching-tiles-optional&quot;&gt;Patching tiles (optional)&lt;/h5&gt;

&lt;p&gt;If you use GDAL (GDAL_rasterize) for burning the filled DEM on the original DEM you do not need to patch the tiles. Just continue to the section on &lt;em&gt;Rasterize using GDAL&lt;/em&gt; below. The shell script for the burning is already produced by the Python package &lt;em&gt;basin_extract&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;You can use two different methods for patching together the tiles. The first alternative is to use the GRASS command &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/v.patch.html&quot;&gt;v.patch&lt;/a&gt;. The commands for patching all the times are given at the end of the GRASS script file:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;g.region raster=DEM@PERMANENT

MAPS=$(g.list type=vector separator=comma pat=&quot;inland_fill_pt_*&quot;)
v.patch -e input=$MAPS output=inland_fill_pt

MAPS=$(g.list type=vector separator=comma pat=&quot;inland_fill_area_*&quot;)
v.patch -e input=$MAPS output=inland_fill_area
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The other alternative is to use the GDAL vector command &lt;a href=&quot;&quot;&gt;ogr2ogr&lt;/a&gt;. The command line process for patching together all vectors are prepared in the script file &lt;em&gt;“region”_ogr_part0.sh&lt;/em&gt;, produced and reported by the Python package &lt;em&gt;basin_extractor&lt;/em&gt;. The first lines of the shell script looks like this:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ogr2ogr -skipfailures -nlt /Volumes/GRASS2020/GRASSproject/SRTM/region/basin/amazonia/0/flowdir-pt-dem_drainage_amazonia_0_cgiar-250.shp /Volumes/GRASS2020/GRASSproject/SRTM/region/basin/amazonia/0/stage0/inland-fill-pt-0-0.shp

ogr2ogr -skipfailures -nlt /Volumes/GRASS2020/GRASSproject/SRTM/region/basin/amazonia/0/flowdir-pt-dem_drainage_amazonia_0_cgiar-250.shp /Volumes/GRASS2020/GRASSproject/SRTM/region/basin/amazonia/0/stage0/inland-fill-area-0-0.shp

ogr2ogr  -append -skipfailures -nlt /Volumes/GRASS2020/GRASSproject/SRTM/region/basin/amazonia/0/flowdir-pt-dem_drainage_amazonia_0_cgiar-250.shp /Volumes/GRASS2020/GRASSproject/SRTM/region/basin/amazonia/0/stage0/inland-fill-pt-0-1.shp

ogr2ogr -append  -skipfailures -nlt /Volumes/GRASS2020/GRASSproject/SRTM/region/basin/amazonia/0/flowdir-pt-dem_drainage_amazonia_0_cgiar-250.shp /Volumes/GRASS2020/GRASSproject/SRTM/region/basin/amazonia/0/stage0/inland-fill-area-0-1.shp
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The two first lines coopies the input vectors to a new file, all following lines appends the input vectors to the initial output file.&lt;/p&gt;

&lt;h5 id=&quot;patch-up-vectors-in-grass-rasterize-superimpose&quot;&gt;Patch up vectors in GRASS, rasterize, superimpose&lt;/h5&gt;

&lt;p&gt;It is more complicated to superimpose the filled elevation values using GRASS compare to GDAL. For GRASS you first have to patch up the vectors, then rasterize and finally superimpose. The required sequence of commands is included in the GRASS shell script file that was produced when running &lt;em&gt;basin_extract&lt;/em&gt; for stage0. The very last lines of the that shell script file should be something like:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&lt;/span&gt;
v.to.rast input=inland_fill_area where=”area_km2&amp;lt;2.0” output=inland_fill_accept use=attr attribute_column=filldem –overwrite&amp;lt;/span&amp;gt;&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;r.mapcalc inland_fill_dem = if( isnull(inland_fill_accept), inland_comp_DEM, inland_fill_accept)&lt;/span&gt;&lt;/p&gt;

&lt;h4 id=&quot;rasterize-using-gdal&quot;&gt;Rasterize using GDAL&lt;/h4&gt;

&lt;p&gt;With GDAL you can instead use &lt;a href=&quot;https://gdal.org/programs/gdal_rasterize.html&quot;&gt;GDAL_rasterize&lt;/a&gt; to burn the DEM values from the vectors with the filled DEM value directly over the original DEM. The burning can be done using either the vector tiles or a patched vector. By using vector tiles you bypass the need for patching tiles.&lt;/p&gt;

&lt;p&gt;That requires fewer step and is already prepared for running in the two shell script files:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;amazonia_GDAL_pt_part0.sh&lt;/li&gt;
  &lt;li&gt;amazonia_GDAL_area_part0.sh&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here are the first lines from the latter:&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;GDAL_rasterize -a filldem /Volumes/GRASS2020/GRASSproject/SRTM/region/basin/amazonia/0/stage0/inland-fill-area-0-0.shp /Volumes/GRASS2020/GRASSproject/SRTM/region/basin/amazonia/0/inland-comp-demm_drainage_amazonia_0_cgiar-250.shp
GDAL_rasterize -a filldem /Volumes/GRASS2020/GRASSproject/SRTM/region/basin/amazonia/0/stage0/inland-fill-area-0-1.shp /Volumes/GRASS2020/GRASSproject/SRTM/region/basin/amazonia/0/inland-comp-demm_drainage_amazonia_0_cgiar-250.shp
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;visualizing-the-dem-errors-and-fixes&quot;&gt;Visualizing the DEM errors and fixes&lt;/h3&gt;

&lt;p&gt;If you want to try the hydrological fixing of DEM using &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/r.fill.dir.html&quot;&gt;r.fill.dir&lt;/a&gt;, you need to start by setting a subregion to process with &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/g.region.html&quot;&gt;g.region&lt;/a&gt;. About 1 million cells is a suitable region.&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; g.region -a n=”float” s=”float” e=”float” w=”float”&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;If you do not set a subregion, the process will take several hours, or even days. With a small subregion set , you can run &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/r.fill.dir.html&quot;&gt;r.fill.dir&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;for single cell pits only:&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; r.fill.dir -f input=inland_comp_DEM output=hydro_cellfill_DEM_ direction=hydro_cellfill_draindir areas=hydro_cellfill_problems –overwrite&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;or for all depressions:&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; r.fill.dir input=inland_comp_DEM output=hydro_areafill_DEM_ direction=hydro_areafill_draindir areas=hydro_areafill_problems –overwrite&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Calculate the difference between the pit filled and the original DEM using &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/r.mapcalc.html&quot;&gt;r.mapcalc&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; r.mapcalc “DEM_fill_diff = hydro_areafill_DEM - inland_comp_DEM” –overwrite&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Set a color ramp to the difference layer.&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; r.colors DEM_fill_diff color=differences&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Export the difference layer to a goetiff.&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; r.out.gdal format=GTiff input=DEM_fill_diff output=/Volumes/GRASS2020/GRASSproject/SRTM/region/basin/amazoniax/0/DEM-fill-diff_amazonia_0_cgiar-250.tif –overwrite&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Calculate a boolean map of the difference area.&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; r.mapcalc “inland_fill_areas = if(DEM_fill_diff != 0, 1, null() )” –overwrite&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Convert the boolean map of identified pits to a vector.&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; r.to.vect input=inland_fill_areas output=inland_fill_areas type=area –overwrite&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Export the vector with identified pits to an ESRI shape file.&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; v.out.ogr input=inland_fill_areas type=area format=ESRI_Shapefile output=/Volumes/GRASS2020/GRASSproject/SRTM/region/basin/amazoniax/0/inland-fill-areas_SRTM_hydro-amazonia_0_cgiar-250.shp –overwrite&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;You can inspect the exported files in for instance &lt;span class=&quot;app&quot;&gt;QGIS&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;You can also open a monitor for GRASS directly from the command line and then add the layers from the command line.&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; d.mon wx0&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; d.shade shade=inland_comp_DEM&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; color=inland_comp_DEM&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; d.vect inland_fill_areas type=boundary color=red&lt;/span&gt;&lt;/p&gt;</content><author><name></name></author><category term="basindelineate" /><category term="GRASS7" /><category term="GDAL" /><category term="QGIS" /><category term="DEM" /><category term="fix" /><summary type="html">Patch up the DEM using GRASS</summary></entry><entry><title type="html">git clone basin_delineate</title><link href="https://nunataryuk.github.io//basin_delineate_setup/basin-delineate-pydev-git-clone/" rel="alternate" type="text/html" title="git clone basin_delineate" /><published>2020-10-17T11:27:00+00:00</published><updated>2020-10-17T11:27:00+00:00</updated><id>https://nunataryuk.github.io//basin_delineate_setup/basin-delineate-pydev-git-clone</id><content type="html" xml:base="https://nunataryuk.github.io//basin_delineate_setup/basin-delineate-pydev-git-clone/">&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;

&lt;p&gt;The python package &lt;em&gt;basin_extract&lt;/em&gt; is freely available as a &lt;a href=&quot;https://github.com/karttur/basin_extract/&quot;&gt;repository on GitHub&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you just want to download &lt;em&gt;basin_extract&lt;/em&gt; from GitHub, just go ahead and you are done with this post. The rest of this post deals cloning either a stable version or a development version. The letter is intended for those who want to participate in the code development.&lt;/p&gt;

&lt;h2 id=&quot;prerequisits&quot;&gt;Prerequisits&lt;/h2&gt;

&lt;p&gt;If you want to use &lt;em&gt;basin_extract&lt;/em&gt; with the IDE &lt;span class=&quot;app&quot;&gt;Eclipse&lt;/span&gt; you must have setup &lt;span class=&quot;app&quot;&gt;Eclipse&lt;/span&gt; as outlined in the &lt;a href=&quot;../basin-delineate-eclipse-setup&quot;&gt;previous&lt;/a&gt; post. It you want to participate in the development you should also have setup the &lt;a href=&quot;https://karttur.github.io/git-vcs/&quot;&gt;git version control system&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;cloning-alternatives&quot;&gt;Cloning alternatives&lt;/h2&gt;

&lt;p&gt;First you need to decide if you want to participate in the development, or even develop your own code. If you want to do either of those you should start by &lt;a href=&quot;https://karttur.github.io/git-vcs/git/git-forks/&quot;&gt;forking the original repo to your own GitHub account&lt;/a&gt;. If you d not need your won development version you can skip the forking and just clone the &lt;em&gt;basin_extract&lt;/em&gt; from Karttur’s repo.&lt;/p&gt;

&lt;h3 id=&quot;fork-project-to-your-own-repo&quot;&gt;Fork project to your own repo&lt;/h3&gt;

&lt;p&gt;Login to your account on &lt;a href=&quot;https://github.com/&quot;&gt;GitHub&lt;/a&gt;, or create a free account if you do not have one. While being logged in navigate to &lt;a href=&quot;https://github.com/karttur/basin_extract&quot;&gt;Karttur’s &lt;em&gt;basin_extract&lt;/em&gt; repo&lt;/a&gt;. In the upper right corner, below the top menu row, there is a button for &lt;span class=&quot;textbox&quot;&gt;Fork&lt;/span&gt;. Click on it, and GitHub creates a fork (copy), then opens it in your own account.&lt;/p&gt;

&lt;figure&gt;
&lt;img src=&quot;../../images/github_fork_stulturum_basin_extract.png&quot; /&gt;
&lt;figcaption&gt; GitHub forked repo.
&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;&lt;img src=&quot;../../images/github-switch-branch.png&quot; alt=&quot;swithc_branch&quot; class=&quot;pull-right&quot; /&gt;
The forked repo has two branches. Change to the branch &lt;em&gt;dev&lt;/em&gt; by clicking the branch button, as illustrated to the right. The page for the &lt;em&gt;dev&lt;/em&gt; branch (below) tells you that &lt;em&gt;This branch is 1 commit ahead of main.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;With your own fork of the project &lt;em&gt;basin_extract&lt;/em&gt; you are ready to get a local clone and starting coding. You can then push your improved/customized code to your won fork, and then push&lt;/p&gt;

&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;h3 id=&quot;local-clone&quot;&gt;Local clone&lt;/h3&gt;

&lt;p&gt;Regardless if you want to clone the stable (&lt;em&gt;main&lt;/em&gt;) or development (&lt;em&gt;dev&lt;/em&gt;) version, you have some different options.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Clone using &lt;span class=&quot;app&quot;&gt;Eclipse&lt;/span&gt; at startup&lt;/li&gt;
  &lt;li&gt;Clone from inside &lt;span class=&quot;app&quot;&gt;Eclipse&lt;/span&gt;&lt;/li&gt;
  &lt;li&gt;Clone using git command line and then import separately into &lt;span class=&quot;app&quot;&gt;Eclipse&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If you do not intend to use &lt;span class=&quot;app&quot;&gt;Eclipse&lt;/span&gt; you can use the third laternative, but you can also just download the package directly from &lt;a href=&quot;https://github.com/karttur/basin_extract/&quot;&gt;GitHub&lt;/a&gt;.&lt;/p&gt;

&lt;h4 id=&quot;eclipse-startup-clone&quot;&gt;Eclipse startup clone&lt;/h4&gt;

&lt;p&gt;&lt;img src=&quot;../../images/eclispe_create_new_folder_in_workspace.png&quot; alt=&quot;new-workspace-folder&quot; class=&quot;pull-right&quot; /&gt;
This is probably the easiest way to get the whole project setup in &lt;span class=&quot;app&quot;&gt;Eclipse&lt;/span&gt;. Start &lt;span class=&quot;app&quot;&gt;Eclipse&lt;/span&gt; and when the IDE launcher page open, create a new folder in your workspace directory. When the welcome pages opens, select the alternative &lt;em&gt;Checkout projects from Git&lt;/em&gt;.&lt;/p&gt;

&lt;figure&gt;
&lt;img src=&quot;../../images/eclipse_welcome_select_git.png&quot; /&gt;
&lt;figcaption&gt; From Eclipse Welcome page select _Checkout projects from Git_.
&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;The next window that opens, &lt;span class=&quot;window&quot;&gt;Select Repository Source&lt;/span&gt; just highlight [Clone URI] and click &lt;span class=&quot;button&quot;&gt;Next&lt;/span&gt;.&lt;/p&gt;

&lt;figure&gt;
&lt;img src=&quot;../../images/eclipse_select_repo_source.png&quot; /&gt;
&lt;figcaption&gt; Eclipse Select Repository Source - highlight [Clone URI] and click Next.
&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;In the &lt;span class=&quot;window&quot;&gt;Source Git Repository&lt;/span&gt; window, paste the URI to the GitHub repo of &lt;em&gt;basin_extract&lt;/em&gt;. Either Karttur’s original repo (if you are not pushing any changes back online) or your own fork (to allow staging and committing changes to the online repo). Then click &lt;span class=&quot;button&quot;&gt;Next&lt;/span&gt;.&lt;/p&gt;

&lt;figure&gt;
&lt;img src=&quot;../../images/eclipse_source_git_repo.png&quot; /&gt;
&lt;figcaption&gt; Eclipse Source Git repo.
&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;In the Branch selection window you should have two alternatives, &lt;em&gt;dev&lt;/em&gt; and &lt;em&gt;main&lt;/em&gt;. Choose the one that suit your needs (&lt;em&gt;main&lt;/em&gt; if you are user, &lt;em&gt;dev&lt;/em&gt; if you are a developer). Click &lt;span class=&quot;button&quot;&gt;Next&lt;/span&gt;.&lt;/p&gt;

&lt;figure&gt;
&lt;img src=&quot;../../images/eclipse_git_branch_selection.png&quot; /&gt;
&lt;figcaption&gt; Eclipse Git Branch Selection.
&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;Set the path to the directory you just created as the target &lt;span class=&quot;textbox&quot;&gt;Directory&lt;/span&gt;. &lt;em&gt;DO NOT&lt;/em&gt; accept the default target path suggested by &lt;spa class=&quot;app&quot;&gt;Eclipse&amp;lt;/span&amp;gt;, under your user and then &lt;span class=&quot;file&quot;&gt;git&lt;/span&gt;. This will lead to conflicts when updating git itself. You can also change the &lt;span class=&quot;textbox&quot;&gt;Initial branch&lt;/span&gt; to _dev_ (or another branch). But keep the standard _origin_ for the &lt;span class=&quot;textbox&quot;&gt;Remote name&lt;/span&gt;.&lt;/spa&gt;&lt;/p&gt;

&lt;figure&gt;
&lt;img src=&quot;../../images/eclipse_git_clone_local-dir.png&quot; /&gt;
&lt;figcaption&gt; Eclipse Git Local Destination.
&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;In the next window (&lt;span class=&quot;window&quot;&gt;Select a wizard to use for importing projects&lt;/span&gt;) click the radio button for Importing Existing Eclipse Projects. Then &lt;span class=&quot;button&quot;&gt;Next&lt;/span&gt;.&lt;/p&gt;

&lt;figure&gt;
&lt;img src=&quot;../../images/eclipse_select_import_wizard.png&quot; /&gt;
&lt;figcaption&gt; Eclipse Select a wizard to use for importing projects.
&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;And you should have reached the last window, &lt;span class=&quot;window&quot;&gt;Import Projects&lt;/span&gt; and this time you can happily click &lt;span class=&quot;button&quot;&gt;Finish&lt;/span&gt;.&lt;/p&gt;

&lt;figure&gt;
&lt;img src=&quot;../../images/eclipse_import_project_finish.png&quot; /&gt;
&lt;figcaption&gt; Eclipse Finish Import projects.
&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;When the project is imported, &lt;span class=&quot;app&quot;&gt;Eclipse&lt;/span&gt; looks for a Python interpreter.&lt;/p&gt;

&lt;figure&gt;
&lt;img src=&quot;../../images/eclipse_python_interpreter_missing.png&quot; /&gt;
&lt;figcaption&gt; Eclipse python projects requires a Python interpreter.
&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;Click the &lt;span class=&quot;button&quot;&gt;Manual config&lt;/span&gt; and you will get to to the Preferences window and the menu set to identifying Python interpreters.&lt;/p&gt;

&lt;figure&gt;
&lt;img src=&quot;../../images/eclipse_python_interpreter.png&quot; /&gt;
&lt;figcaption&gt; Eclipse Preferences, identifying python interpreters.
&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;The Python interpreter you need is described in the parallel post on &lt;a href=&quot;../basin-delineate-seclipse-setup/&quot;&gt;Eclipse for Basin extraction&lt;/a&gt;.&lt;/p&gt;

&lt;h4 id=&quot;clone-from-inside-eclipse&quot;&gt;Clone from inside Eclipse&lt;/h4&gt;

&lt;p&gt;You can clone a git repo from within an existing &lt;span class=&quot;app&quot;&gt;Eclipse&lt;/span&gt; project. Start an empty &lt;span class=&quot;app&quot;&gt;Eclipse&lt;/span&gt; project. Get the GIT perspective. Either from the menu system:&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;menu&quot;&gt;Window -&amp;gt; Perspective -&amp;gt; Open Perspective -&amp;gt; Other … &lt;/span&gt;.&lt;/p&gt;

&lt;figure&gt;
&lt;img src=&quot;../../images/eclipse_window_perspecive_other.png&quot; /&gt;
&lt;figcaption&gt; Eclipse Menu path to different perspectives.
&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;figure&gt;
&lt;img src=&quot;../../images/eclipse_perspectives_other.png&quot; /&gt;
&lt;figcaption&gt; Eclipse menu of perspectives.
&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;You can also use the search tool and start writing “git” and locate &lt;em&gt;git perspective&lt;/em&gt;.&lt;/p&gt;

&lt;figure&gt;
&lt;img src=&quot;../../images/eclipse_search_git.png&quot; /&gt;
&lt;figcaption&gt; Eclipse search.
&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;In the &lt;span class=&quot;tab&quot;&gt;Git repositories&lt;/span&gt; view you should click the alternative &lt;em&gt;Clone a Git repository&lt;/em&gt; (if no text appears slide the cursor over the icons to get the alternative &lt;em&gt;Clone a Git repository and add the clone to this view&lt;/em&gt;).&lt;/p&gt;

&lt;figure&gt;
&lt;img src=&quot;../../images/github-framework_karttur_03_Git-repo-alternatives.jpg&quot; /&gt;
&lt;figcaption&gt; Eclipse git perspective.
&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;figure&gt;
&lt;img src=&quot;../../images/github-framework_karttur_0301_Git-repo-alternatives.jpg&quot; /&gt;
&lt;figcaption&gt; Eclipse git perspective.
&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;This will open the same &lt;span class=&quot;window&quot;&gt;Source Git Repository&lt;/span&gt; window as in the previous section and the steps that then follows are the same. But &lt;span class=&quot;app&quot;&gt;Eclipse&lt;/span&gt; does not recognise the imported repo as a PyDev project. You have to define the PyDev project and then team up the git repo packages. How to do that is covered in the post &lt;a href=&quot;https://karttur.github.io/geoimagine/develop/develop-github-eclipse/&quot;&gt;Setup Eclipse teamed with GitHub repository&lt;/a&gt;.&lt;/p&gt;</content><author><name></name></author><category term="basin_delineate_setup" /><category term="Eclipse" /><category term="Basin extract" /><category term="git" /><category term="clone" /><summary type="html">git clone basin_delineate from GitHub for use in Eclipse</summary></entry><entry><title type="html">Make a git repo</title><link href="https://nunataryuk.github.io//basin_delineate_setup/basin-delineate-pydev-git-make/" rel="alternate" type="text/html" title="Make a git repo" /><published>2020-10-17T11:27:00+00:00</published><updated>2020-10-17T11:27:00+00:00</updated><id>https://nunataryuk.github.io//basin_delineate_setup/basin-delineate-pydev-git-make</id><content type="html" xml:base="https://nunataryuk.github.io//basin_delineate_setup/basin-delineate-pydev-git-make/">&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;

&lt;p&gt;The python package &lt;em&gt;basin_extract&lt;/em&gt; is freely available as a &lt;a href=&quot;https://github.com/karttur/basin_extract/&quot;&gt;repository on GitHub&lt;/a&gt;. This post is a manual for how it was constructed. The post is thus only of interest if you want to create a similar repo.&lt;/p&gt;

&lt;h2 id=&quot;prerequisits&quot;&gt;Prerequisits&lt;/h2&gt;

&lt;p&gt;You need to have a &lt;a href=&quot;https://github.com&quot;&gt;GitHub&lt;/a&gt; account and you must have &lt;a href=&quot;https://karttur.github.io/git-vcs/git/git-commandline-install/&quot;&gt;Installed git for command line&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;basin_extract-at-github&quot;&gt;&lt;em&gt;basin_extract&lt;/em&gt; at GitHub&lt;/h2&gt;

&lt;p&gt;This post outlines how to create a &lt;a href=&quot;https://github.com&quot;&gt;GitHub&lt;/a&gt; repo with a default &lt;em&gt;main&lt;/em&gt; branch and a &lt;em&gt;dev&lt;/em&gt;(elopment) branch using &lt;a href=&quot;https://karttur.github.io/git-vcs/git/git-commandline-install/&quot;&gt;git command line tool&lt;/a&gt;. You have to register a free account with &lt;a href=&quot;https://github.com&quot;&gt;GitHub&lt;/a&gt; and then &lt;a href=&quot;https://karttur.github.io/git-vcs/git/git-github/&quot;&gt;create a repository&lt;/a&gt;. The name of the repo with the &lt;em&gt;basin_extract&lt;/em&gt; code is called &lt;em&gt;basin_extract&lt;/em&gt;. For a more detailed instruction on creating a new repo, see the post &lt;a href=&quot;https://karttur.github.io/git-vcs/git/git-github/&quot;&gt;Remote repositories with GitHub&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../../images/github-code-alts.png&quot; alt=&quot;github-settings-menu-SSH-keys&quot; class=&quot;pull-right&quot; /&gt;
When the new repo is created, click on the green button &lt;span class=&quot;button&quot;&gt;Code&lt;/span&gt;. There are (in November 2020) 4 alternatives for cloning, plus the additional options [Open with GitHub Desktop] and [Download ZIP]. Copy the text for the clone alternative [SSH], as shown in the figure to the right.&lt;/p&gt;

&lt;h2 id=&quot;clone-to-local-repo&quot;&gt;Clone to local repo&lt;/h2&gt;

&lt;p&gt;Open a &lt;span class=&quot;app&quot;&gt;Terminal&lt;/span&gt; window on your local machine and &lt;span class=&quot;terminalapp&quot;&gt;cd&lt;/span&gt; to the parent directory where you want to create the &lt;em&gt;clone&lt;/em&gt; of your remote (GitHub) repository. Then type (but do &lt;strong&gt;not&lt;/strong&gt; execute):&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;$ git clone &lt;/span&gt;&lt;/p&gt;

&lt;p&gt;If you followed the instructions in the previous section, you can now just paste the SSH link from your clipboard to the command line:&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;$ git clone git@github.com:karttur/basin_extract.git&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Execute the command and the remote repo (“basin_extract”) should clone to your local machine. If this is the first time you use the SSH key, you will be prompted the following statement and question:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Cloning into 'basin_extract'...
The authenticity of host 'github.com (140.82.118.4)' can't be established.
RSA key fingerprint is [.....].
Are you sure you want to continue connecting (yes/no)?
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Answer with a full &lt;span class=&quot;terminal&quot;&gt;yes&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;The cloning will only take a few seconds, and the result is reported at the terminal prompt:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Cloning into 'basin_extract'...
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (4/4), 12.54 KiB | 2.51 MiB/s, done.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Change direction (&lt;span class=&quot;terminalapp&quot;&gt;cd&lt;/span&gt;) to the newly cloned repo and list (&lt;span class=&quot;terminalapp&quot;&gt;ls&lt;/span&gt;) the content:&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;$ cd basin_extract&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;$ ls&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;if you instead type (&lt;span class=&quot;terminalapp&quot;&gt;ls -a&lt;/span&gt;) also hidden files will be listed, including &lt;span class=&quot;file&quot;&gt;.git&lt;/span&gt;.&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;$ ls -a&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;The post on &lt;a href=&quot;https://karttur.github.io/git-vcs/git/git-local-use&quot;&gt;Local git control&lt;/a&gt; explains how to &lt;a href=&quot;https://karttur.github.io/git-vcs/git/git-local-use&quot;&gt;Check and setup user name&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You can check the remote repo associated with your local clone:&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;git remote -v&lt;/span&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;origin	git@github.com:karttur/basin_extract.git (fetch)
origin	git@github.com:karttur/basin_extract.git (push)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Check out the branches and history:&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;$ git log --all --decorate --oneline --graph&lt;/span&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;* a5a7152 (HEAD -&amp;gt; main, origin/main, origin/HEAD) Initial commit
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;For the &lt;em&gt;clone&lt;/em&gt; we are working with the &lt;em&gt;HEAD&lt;/em&gt; pointer points towards two new items: &lt;em&gt;origin/main&lt;/em&gt; and &lt;em&gt;origin/HEAD&lt;/em&gt;. As &lt;em&gt;origin&lt;/em&gt; can be seen as an alias for the primary remote repo, the pointing is towards the remote (GitHub) repo. As &lt;em&gt;HEAD&lt;/em&gt; is pointing both towards the local &lt;em&gt;main&lt;/em&gt; and the remote &lt;em&gt;main&lt;/em&gt;, our local repo is in sync with the GitHub repo.&lt;/p&gt;

&lt;h2 id=&quot;createedit-gitignore-file&quot;&gt;Create/edit .gitignore file&lt;/h2&gt;

&lt;p&gt;Open/create the file &lt;span class=&quot;file&quot;&gt;.gitignore&lt;/span&gt; using for example the terminal editor &lt;span class=&quot;terminalapp&quot;&gt;pico&lt;/span&gt;.&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;$ pico .gitignore&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Add the following items to ignore:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;.DS_Store
*.pyc
__pycache__/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The first item (.DS_Store) is an internal Mac OSX file, the second (*.pyc) is a wildcard for ignoring all python cache files and the last item (__pycache__/) is the directory holding the python cache files.&lt;/p&gt;

&lt;p&gt;Hit [ctrl]+[X] to exit &lt;span class=&quot;terminalapp&quot;&gt;pico&lt;/span&gt; and save the edits by pressing &lt;span class=&quot;terminal&quot;&gt;Y&lt;/span&gt; when asked.&lt;/p&gt;

&lt;h3 id=&quot;stage-and-commit-i&quot;&gt;Stage and commit I&lt;/h3&gt;

&lt;p&gt;With edits done to the &lt;span class=&quot;file&quot;&gt;.gitignore&lt;/span&gt; you can &lt;em&gt;stage&lt;/em&gt; (add) and &lt;em&gt;commit&lt;/em&gt; (lock in and save) the changes to your local git repository:&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;git add .&lt;br /&gt;
git commit -m 'created .gitignore'&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;As this is the first time you &lt;em&gt;stage&lt;/em&gt; something you have to use the separate &lt;span class=&quot;terminal&quot;&gt;git add .&lt;/span&gt; command.&lt;/p&gt;

&lt;p&gt;If all the files that you want to commit have already been staged before, you can shorten the two commands to:&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;git commit -am 'created .gitignore'&lt;/span&gt;&lt;/p&gt;

&lt;h2 id=&quot;create-or-copy-pydev-project&quot;&gt;Create or copy PyDev project&lt;/h2&gt;

&lt;p&gt;The repo (&lt;em&gt;basin_extract&lt;/em&gt;) only contains the &lt;span class=&quot;file&quot;&gt;README, LICENSE&lt;/span&gt; and &lt;span class=&quot;file&quot;&gt;.gitignore&lt;/span&gt; files (plus &lt;span class=&quot;file&quot;&gt;.git&lt;/span&gt;). The Python package that you want to develop with the help of git must be either created inside the repo or, if you have already started the package, copied into it. As I have already built a first version of the Python package &lt;em&gt;basin_extract&lt;/em&gt;, I copy the complete structure into the repo. The initial structure of &lt;em&gt;basin_extract&lt;/em&gt; thus becomes (leaving out the content of &lt;span class=&quot;file&quot;&gt;.git&lt;/span&gt; and adding comments):&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;.
|____README.md # initial README markdown (md) text file
|____.gitignore
|____.git
|____basin_delineate # This is the Eclipse project (containing the package)
| |____.project # definition of the Eclipse project
| |____.pydevproject # definition of the Eclipse PyDev project
| |____basin_extract # The main python package
| | |______init__.py # Python default initiation script file
| | |____basin_extract.py # The main python module
| |____params # Python package for setting parameters
| | |____be_params.py # python module for setting parameters
| | |______init__.py # Python default initiation script file
| | |____be_xml.py # python package for reading xml parameters
| |____ds_manage # python package for spatial data source (ds) management
| | |______init__.py # Python default initiation script file
| | |____datasource.py # python module for patial data source (ds) management
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;stage-and-commit-ii&quot;&gt;Stage and commit II&lt;/h3&gt;

&lt;p&gt;Stage and commit the complete project in your local clone of the repo &lt;em&gt;basin_extract&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;git add .&lt;br /&gt;
git commit -m 'initial commit of PyDev project'&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;The check the status of your repo:&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;$ git status&lt;/span&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;On branch main
Your branch is ahead of 'origin/main' by 1 commit.
  (use &quot;git push&quot; to publish your local commits)

nothing to commit, working tree clean
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Or with the more decorated option:&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;$ git log --all --decorate --oneline --graph&lt;/span&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;* 1430b14 (HEAD -&amp;gt; main) initial commit of PyDev project
* a5a7152 (origin/main, origin/HEAD) Initial commit
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;push-to-origin&quot;&gt;Push to origin&lt;/h3&gt;

&lt;p&gt;Push the committed changes to your remote repo on GitHub:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;git push  &amp;lt;REMOTENAME&amp;gt; &amp;lt;BRANCHNAME&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;$ git push origin main&lt;/span&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Enumerating objects: 1139, done.
Counting objects: 100% (1139/1139), done.
Delta compression using up to 4 threads
Compressing objects: 100% (1104/1104), done.
Writing objects: 100% (1138/1138), 8.10 MiB | 3.79 MiB/s, done.
Total 1138 (delta 229), reused 0 (delta 0)
remote: Resolving deltas: 100% (229/229), done.
To github.com:karttur/basin_extract.git
   a5a7152..1430b14  main -&amp;gt; main
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;$ git status&lt;/span&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;On branch main
Your branch is up to date with 'origin/main'.

nothing to commit, working tree clean
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;create-local-dev-branch&quot;&gt;Create local dev branch&lt;/h3&gt;

&lt;p&gt;Create a local development (&lt;em&gt;dev&lt;/em&gt;) branch.&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;$ git checkout -b &quot;dev&quot;&lt;/span&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Switched to a new branch 'dev'
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;$ git branch [-r] [-a]&lt;/span&gt; [-r] to see only remote branches, [-a] to see all branches.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;* dev
  main
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;a-minute-edit&quot;&gt;A minute edit&lt;/h4&gt;

&lt;p&gt;Do some small editing to any of the files in the repo, just for illustration really. As all files are already staged, you can stage and commit in one command:&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;$ git commit -a -m &quot;update be_params.py&quot;&lt;/span&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;[dev 4cc0223] update be_params.py
 1 file changed, 96 insertions(+), 97 deletions(-)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;$ git log --all --decorate --oneline --graph&lt;/span&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;* 4cc0223 (HEAD -&amp;gt; dev) update be_params.py
* 1430b14 (origin/main, origin/HEAD, main) initial commit of PyDev project
* a5a7152 Initial commit
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;HEAD&lt;/em&gt; is now pointing at the branch &lt;em&gt;dev&lt;/em&gt; and the remote repo (with the alias &lt;em&gt;origin&lt;/em&gt;) is out of sync still pointing at &lt;em&gt;masin&lt;/em&gt;.&lt;/p&gt;

&lt;h3 id=&quot;upload-branch&quot;&gt;Upload branch&lt;/h3&gt;

&lt;p&gt;Upload the branch &lt;em&gt;dev&lt;/em&gt; to the repo&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;$ git push origin dev&lt;/span&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Enumerating objects: 9, done.
Counting objects: 100% (9/9), done.
Delta compression using up to 4 threads
Compressing objects: 100% (5/5), done.
Writing objects: 100% (5/5), 975 bytes | 975.00 KiB/s, done.
Total 5 (delta 3), reused 0 (delta 0)
remote: Resolving deltas: 100% (3/3), completed with 3 local objects.
remote:
remote: Create a pull request for 'dev' on GitHub by visiting:
remote:      https://github.com/karttur/basin_extract/pull/new/dev
remote:
To github.com:karttur/basin_extract.git
 * [new branch]      dev -&amp;gt; dev
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;$ git log --all --decorate --oneline --graph&lt;/span&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;* 4cc0223 (HEAD -&amp;gt; dev, origin/dev) update be_params.py
* 1430b14 (origin/main, origin/HEAD, main) initial commit of PyDev project
* a5a7152 Initial commit
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;HEAD&lt;/em&gt; is pointing towards the branch &lt;em&gt;dev&lt;/em&gt;, also in the remote repo (&lt;em&gt;origin/dev&lt;/em&gt;).&lt;/p&gt;

&lt;p&gt;Return to the online (remote) &lt;a href=&quot;https://github.com/karttur/basin_extract&quot;&gt;GitHub repo &lt;em&gt;basin_extract&lt;/em&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;figure&gt;
&lt;img src=&quot;../../images/github_basin_extract_two_branches.png&quot; /&gt;
&lt;figcaption&gt; GitHub repo after pushing local branch &quot;dev&quot;.
&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;&lt;img src=&quot;../../images/github-switch-branch.png&quot; alt=&quot;swithc_branch&quot; class=&quot;pull-right&quot; /&gt;
Change to the branch &lt;em&gt;dev&lt;/em&gt; by clicking the branch button, as illustrated to the right. The page for the &lt;em&gt;dev&lt;/em&gt; branch (below) tells you that &lt;em&gt;This branch is 1 commit ahead of main.&lt;/em&gt;. Thus we know that all has worked out as we intended.&lt;/p&gt;

&lt;p&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/p&gt;

&lt;figure&gt;
&lt;img src=&quot;../../images/github_basin_delineate_dev-branch.png&quot; /&gt;
&lt;figcaption&gt; GitHub repo showing the &quot;dev&quot; branch.
&lt;/figcaption&gt;
&lt;/figure&gt;</content><author><name></name></author><category term="basin_delineate_setup" /><category term="Eclipse" /><category term="Basin extract" /><category term="git" /><category term="push" /><summary type="html">Make a GitHub repo for _basin_delineate_, clone it locally and add the PyDev project and push it back to GitHub</summary></entry><entry><title type="html">Eclipse for Basin extraction</title><link href="https://nunataryuk.github.io//basin_delineate_setup/basin-delineate-eclipse-setup/" rel="alternate" type="text/html" title="Eclipse for Basin extraction" /><published>2020-10-15T11:27:00+00:00</published><updated>2020-10-15T11:27:00+00:00</updated><id>https://nunataryuk.github.io//basin_delineate_setup/basin-delineate-eclipse-setup</id><content type="html" xml:base="https://nunataryuk.github.io//basin_delineate_setup/basin-delineate-eclipse-setup/">&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;basin_extract&lt;/em&gt; is a Python package that belongs to the semi automated system for delineating river basins. This post is a manual on how to setup a Python environment for the &lt;em&gt;basin_extract&lt;/em&gt; package in the &lt;span class=&quot;app&quot;&gt;Eclipse&lt;/span&gt; Integrated Development Environment (IDE).&lt;/p&gt;

&lt;h3 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h3&gt;

&lt;p&gt;You must have installed &lt;a href=&quot;https://karttur.github.io/setup-ide/setup-ide/install-anaconda/&quot;&gt;Anaconda&lt;/a&gt; for handling virtual Python environments and &lt;a href=&quot;https://karttur.github.io/setup-ide/setup-ide/install-eclipse/&quot;&gt;Eclipse IDE for PyDev&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;setup-virtual-python-environment&quot;&gt;Setup virtual Python environment&lt;/h3&gt;

&lt;p&gt;The &lt;em&gt;basin_extract&lt;/em&gt; package requires the standard (default) Python packages that come with Eclipse, plus some additional packages:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;numpy&lt;/li&gt;
  &lt;li&gt;pandas&lt;/li&gt;
  &lt;li&gt;shapely&lt;/li&gt;
  &lt;li&gt;scipy&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Trying to setup a working Python environment for &lt;em&gt;basin_extract&lt;/em&gt; in October 2020 I found that the listed packages are incompatible in Python 3.8. The virtual environment herein is thus setup using the older Python 3.6 interpreter.&lt;/p&gt;

&lt;p&gt;The basics of &lt;a href=&quot;https://karttur.github.io/setup-ide/setup-ide/conda-environ/&quot;&gt;Conda virtual environments&lt;/a&gt; is covered in another post. More extensive information is available on the &lt;a href=&quot;https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html&quot;&gt;official conda document site&lt;/a&gt;. Here I will only step over the barebones of setting up a virtual environment for Ortho.&lt;/p&gt;

&lt;p&gt;When you open a &lt;span class=&quot;app&quot;&gt;Terminal&lt;/span&gt; window on a machine with Conda installed, the first entry on the cursor line should be the active conda environment. &lt;span class=&quot;terminal&quot;&gt;(base)&lt;/span&gt; is the default (overall) environment. If the cursor line of your Terminal window does not say &lt;span class=&quot;terminal&quot;&gt;(base)&lt;/span&gt; you have activated another Conda environment. To return to base enter the command:&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;$ conda activate&lt;/span&gt;&lt;/p&gt;

&lt;h4 id=&quot;update-conda-and-anaconda&quot;&gt;Update conda and anaconda&lt;/h4&gt;

&lt;p&gt;Before setting up the virtual environment, update Conda and Anaconda at the terminal:&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;(base) …$ conda update conda&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;(base) …$ conda update anaconda&lt;/span&gt;&lt;/p&gt;

&lt;h4 id=&quot;create-a-python-36-virtual-environment&quot;&gt;Create a Python 3.6 virtual environment&lt;/h4&gt;

&lt;p&gt;The basic command for creating a virtual Python environment with a pre-defined version of Python is (do not run it yet, read a bit further first):&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;(base) …$ conda create -n basin_extract_py36 python=3.6&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;If your Conda environment is defined to include a set of additional packages (if you have no idea about what I am writing, never mind and just continue), then these will be installed in your virtual environment on top of the default Python installation. To avoid that instead type:&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;(base) …$ conda create --no-default-packages -n basin_extract_py36 python=3.6&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;This will install a barebone Python 3.6 virtual environment even if you have defined additional packages to be installed by default. Now you can go ahead and create the barebone virtual environment with one of the command above.&lt;/p&gt;

&lt;h4 id=&quot;install-additional-packages&quot;&gt;Install additional packages&lt;/h4&gt;

&lt;p&gt;Your newly created environment does not include the additional packages needed (the ones listed above)for the Python package &lt;em&gt;basin-extract&lt;/em&gt;. When adding these packages you have to make sure that they are installed within your new environment. That can be achieved by either destining the installation from &lt;span class=&quot;terminal&quot;&gt;(base)&lt;/span&gt; or first activate your new environment. Here I will only show how to change the active environment and then install. Thus activate your virtual environment:&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;(base) …$ conda activate basin_extract_py36&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;The cursor line will then change to reflect that you are now in another environment.&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;(basin_extract_py36) …$&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;With your new Python 3.6 environment active you can install the required packages:&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;(basin_extract_py36) …$ conda install -c conda-forge gdal&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;(basin_extract_py36) …$ conda install -c anaconda numpy&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;(basin_extract_py36) …$ conda install -c anaconda scipy&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;(basin_extract_py36) …$ conda install -c conda-forge shapely&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Setting this up in October 2020, all installations went through without problems. However, setting up a virtual environment with more recent versions of Python can lead to conflicts between packages. For that reason it is better to install all required packages with the Python initial installation, as explained in the next section.&lt;/p&gt;

&lt;h4 id=&quot;install-packages-with-the-python-version&quot;&gt;Install packages with the python version&lt;/h4&gt;

&lt;p&gt;Setting up a virtual environment with all the packages listed has the advantage that Conda checks the consistencies between all packages before installing. To try that out for the &lt;em&gt;basin_extract&lt;/em&gt; environment, return to base:&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;(basin_extract_py36) …$ conda activate&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;And then extend the Conda &lt;em&gt;create&lt;/em&gt; command with a list of all the packages to install:&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;(base) …$ conda create --no-default-packages -n basin_extract_py36 python=3.6 numpy scipy gdal shapely&lt;/span&gt;&lt;/p&gt;

&lt;h3 id=&quot;set-your-virtual-environment-as-your-python-interpreter-in-eclipse&quot;&gt;Set your virtual environment as your Python interpreter in Eclipse&lt;/h3&gt;

&lt;p&gt;Start &lt;span class=&quot;app&quot;&gt;Eclipse&lt;/span&gt;. In the &lt;em&gt;Eclipse IDE Launcher&lt;/em&gt; window, specify a new location and click &lt;span class=&quot;button&quot;&gt;Launch&lt;/span&gt;.&lt;/p&gt;

&lt;figure&gt;
&lt;img src=&quot;https://karttur.github.io/common/images/eclipse_ide_launcher.png&quot; /&gt;
&lt;figcaption&gt; Eclipse select a new directory as workspace. &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;In the &lt;em&gt;Welcome&lt;/em&gt; page that opens, just go to Workbench (in the upper right corner).&lt;/p&gt;

&lt;figure&gt;
&lt;img src=&quot;https://karttur.github.io/common/images/eclipse_welcome_page.png&quot; /&gt;
&lt;figcaption&gt; Eclipse welcome page, just click the Workbench in the upper right corner. &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;The default IDE that opens is for Java projects, but you want it to be a &lt;em&gt;PyDev&lt;/em&gt; project page. To change to a PyDev perspective, go via the menu:&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;menu&quot;&gt;Window -&amp;gt; Perspective -&amp;gt; Open Perspective -&amp;gt; Other… &lt;/span&gt;&lt;/p&gt;

&lt;figure&gt;
&lt;img src=&quot;https://karttur.github.io/common/images/eclipse_menu_open-other-perspective.png&quot; /&gt;
&lt;figcaption&gt; Eclipse menu route for opening a new perspective. &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;In the window that opens, select &lt;em&gt;PyDev&lt;/em&gt;. If &lt;em&gt;PyDev&lt;/em&gt; is not available as an alternative, you need to go back to post on &lt;a href=&quot;https://karttur.github.io/setup-ide/setup-ide/install-eclipse/&quot;&gt;Eclipse IDE for PyDev&lt;/a&gt; and use the Eclipse Marketplace to install &lt;em&gt;PyDev&lt;/em&gt; for Eclipse.&lt;/p&gt;

&lt;figure&gt;
&lt;img src=&quot;https://karttur.github.io/common/images/eclipse_window_select_perspective.png&quot; /&gt;
&lt;figcaption&gt; Eclipse pop up widnow for selecting perspective. &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;To set up your virtual Python environment as the Python interpreter, go via the main menu:&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;menu&quot;&gt;Eclipse -&amp;gt; Preferences&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;And then in the &lt;em&gt;Preference&lt;/em&gt; window that opens, go via the left side navigation:&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;menu&quot;&gt;PyDev -&amp;gt; Interpreters -&amp;gt; Python Interpreter&lt;/span&gt;,&lt;/p&gt;

&lt;p&gt;and the right side of the window expands. Click the &lt;span class=&quot;button&quot;&gt;Browse for python/pypy exe&lt;/span&gt; and navigate to the directory &lt;span class=&quot;file&quot;&gt;envs&lt;/span&gt; under your &lt;span class=&quot;file&quot;&gt;Anaconda&lt;/span&gt; installation. There you will find the virtual environment that you just created, &lt;span class=&quot;file&quot;&gt;basin_extract_py36&lt;/span&gt;. The Python executable file is then under the path &lt;span class=&quot;file&quot;&gt;bin/python&lt;/span&gt;. The full path on my machine is shown in the figure below. Click &lt;span class=&quot;button&quot;&gt;OK&lt;/span&gt; to select the python interpreter.&lt;/p&gt;

&lt;figure&gt;
&lt;img src=&quot;https://karttur.github.io/common/images/eclipse_select_interpreter_ortho-py38.png&quot; /&gt;
&lt;figcaption&gt; Eclipse window for selecting interpreter. &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;In the &lt;a href=&quot;../basin-delineate-pydev-setup&quot;&gt;next post&lt;/a&gt; you will setup the projects, packages and modules that constitute the &lt;em&gt;basin_extract&lt;/em&gt; Python package.&lt;/p&gt;</content><author><name></name></author><category term="basin_delineate_setup" /><category term="Eclipse" /><category term="Basin extract" /><category term="setup" /><summary type="html">Setup Eclipse for the Python packages Basin_extraction</summary></entry><entry><title type="html">GRASS 7 setup</title><link href="https://nunataryuk.github.io//basin_delineate_setup/basin-delineate-grass7/" rel="alternate" type="text/html" title="GRASS 7 setup" /><published>2020-10-03T06:17:25+00:00</published><updated>2020-10-03T06:17:25+00:00</updated><id>https://nunataryuk.github.io//basin_delineate_setup/basin-delineate-grass7</id><content type="html" xml:base="https://nunataryuk.github.io//basin_delineate_setup/basin-delineate-grass7/">&lt;script src=&quot;https://karttur.github.io/common/assets/js/karttur/togglediv.js&quot;&gt;&lt;/script&gt;

&lt;h1 id=&quot;introduction&quot;&gt;Introduction&lt;/h1&gt;

&lt;p&gt;This post covers how to start GRASS 7 for the first time. Including defining a GRASS database and a location with a projection. The manual is for GRASS 7 on Mac OSX and is part of a series on how to delineate river basins using GRASS GIS and other tools.&lt;/p&gt;

&lt;p&gt;The manual only covers the command line GRASS tools for processing. But all the processing can also be done using the GRASS Graphical User Interface (GUI).&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;You must have GRASS 7 installed as described in the post on &lt;a href=&quot;https://karttur.github.io/setup-ide/setup-ide/install-gis/#grass&quot;&gt;Install GDAL, QGIS and GRASS&lt;/a&gt;. You also need a projected raster file (e.g. a GeoTIFF) for defining both the projection and the location.&lt;/p&gt;

&lt;h2 id=&quot;start-grass&quot;&gt;Start GRASS&lt;/h2&gt;

&lt;p&gt;Before you actually start GRASS, you need to start up a &lt;span class=&quot;app&quot;&gt;Terminal&lt;/span&gt; session. Otherwise GRASS might not start properly. Once you have the &lt;span class=&quot;app&quot;&gt;Terminal&lt;/span&gt; up and running locate your installation of GRASS7 and start it. Alternatively you can also start GRASS7 directly from the command line, just type in the full path to the &lt;span class=&quot;file&quot;&gt;grass.sh&lt;/span&gt; executable script:&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;/Applications/GRASS-7.X.X.app/Contents/MacOS/Grass.sh
&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;The startup might take a while, and GRASS should then show the window &lt;em&gt;GRASS GIS 7.x.x Startup&lt;/em&gt;.&lt;/p&gt;

&lt;figure&gt;
&lt;img src=&quot;https://karttur.github.io/common/images/GRASS7_1stStartup_welcome01.png&quot; /&gt;
&lt;figcaption&gt; GRASS7 initial startup page &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;If the location you want to work with is already defined in your GRASS database, it is listed as a GRASS location (the location Amazonia in the example below). You can then just skip to the last section in this post &lt;em&gt;Create a new mapset&lt;/em&gt;.&lt;/p&gt;

&lt;figure&gt;
&lt;img src=&quot;https://karttur.github.io/common/images/GRASS7_Amazonia-Startup_welcome01.png&quot; /&gt;
&lt;figcaption&gt; GRASS7 startup page with existing location &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;h3 id=&quot;define-new-grass-location&quot;&gt;Define new GRASS Location&lt;/h3&gt;

&lt;p&gt;The first time you start GRASS7 you have to set the database directory, and a first Location. As noted on the startup page, in red even, &lt;em&gt;GRASS needs a directory (GRASS database) in which to store its data …&lt;/em&gt;. The default database directory is the users home directory. The basin delineation will, however, require a rather large storage space and I recommend using an external disk or server for the data storage.&lt;/p&gt;

&lt;p&gt;Use the &lt;span class=&quot;button&quot;&gt;Browse&lt;/span&gt; button to select the location for your GRASS database. Then use the &lt;span class=&quot;button&quot;&gt;New&lt;/span&gt; button between the smaller windows for &lt;em&gt;2 Select GRASS Location&lt;/em&gt; and &lt;em&gt;3. Select GRASS Mapset&lt;/em&gt;. GRASS then presents you with a new window, &lt;em&gt;Define new GRASS Location&lt;/em&gt;.&lt;/p&gt;

&lt;figure&gt;
&lt;img src=&quot;https://karttur.github.io/common/images/GRASS7_1stStartup_database.png&quot; /&gt;
&lt;figcaption&gt; Define new GRASS Location &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;In the example figure above, I created a local disk &lt;span class=&quot;file&quot;&gt;GRASS2020&lt;/span&gt; and then the directory &lt;span class=&quot;file&quot;&gt;GRASSDATA&lt;/span&gt; for my GRASS database. I then also defined a &lt;em&gt;Location&lt;/em&gt; called &lt;em&gt;Amazonia&lt;/em&gt;. All GRASS locations will get a default &lt;em&gt;MapSet&lt;/em&gt; called &lt;em&gt;PERMANENT&lt;/em&gt;; let that be your initial mapset for the &lt;em&gt;Location&lt;/em&gt; you define.&lt;/p&gt;

&lt;p&gt;Do not tick the boxes for &lt;em&gt;Set default region extent and resolution&lt;/em&gt; or &lt;em&gt;Create user mapset&lt;/em&gt;, we will fix that in the next step. Click &lt;span class=&quot;button&quot;&gt;Next &amp;gt;&lt;/span&gt; to get to the next step.&lt;/p&gt;

&lt;h4 id=&quot;choose-method-for-creating-a-new-location&quot;&gt;Choose method for creating a new location&lt;/h4&gt;

&lt;p&gt;A new window, but still belonging to the &lt;em&gt;Define new GRASS Location&lt;/em&gt; set, opens. The best option here is to have a projected raster file that you can point at for defining the location. Assuming that you have one, click the radiobutton for that option.&lt;/p&gt;

&lt;figure&gt;
&lt;img src=&quot;https://karttur.github.io/common/images/GRASS7_1stStartup_create-location.png&quot; /&gt;
&lt;figcaption&gt; Define new GRASS Location &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;Click &lt;span class=&quot;button&quot;&gt;Next &amp;gt;&lt;/span&gt; and browse to the raster file to use for creating the new location. If you did not select the option to use an existing file for creating the new location, you need to manually add the projection and location.&lt;/p&gt;

&lt;p&gt;When you have added all the information required, GRASS will summarise the definition of the new location.&lt;/p&gt;

&lt;figure&gt;
&lt;img src=&quot;https://karttur.github.io/common/images/GRASS7_1stStartup_define-location.png&quot; /&gt;
&lt;figcaption&gt; Define new GRASS Location &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;Click &lt;span class=&quot;button&quot;&gt;Finish&lt;/span&gt; and you are done with the definition of database, location and projection of your first GRASS7 project.&lt;/p&gt;

&lt;h3 id=&quot;import-raster-defining-the-location&quot;&gt;Import raster defining the location&lt;/h3&gt;

&lt;p&gt;If you choose to define your location using a raster file, like illustrated above, GRASS7 will automatically ask if you want to import this data. In most cases the answer is most likely &lt;span class=&quot;button&quot;&gt;yes&lt;/span&gt;. Whereupon the file should be successfully imported into the MapSet &lt;em&gt;PERMANENT&lt;/em&gt;. Click &lt;span class=&quot;button&quot;&gt;OK&lt;/span&gt; and GRASS7 should start its Graphical User interface.&lt;/p&gt;

&lt;figure class=&quot;half&quot;&gt;
&lt;img src=&quot;https://karttur.github.io/common/images/GRASS7_1stStartup_import-data.png&quot; /&gt;
&lt;img src=&quot;https://karttur.github.io/common/images/GRASS7_1stStartup_import-success.png&quot; /&gt;
&lt;figcaption&gt; Import data used for defining projection and region &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;h3 id=&quot;grass-graphical-user-interface-gui&quot;&gt;GRASS Graphical User Interface (GUI)&lt;/h3&gt;

&lt;p&gt;GRASS7 opens an empty GUI with two windows, a &lt;em&gt;Map Display&lt;/em&gt; and a &lt;em&gt;Layer Manager&lt;/em&gt;.&lt;/p&gt;

&lt;figure&gt;
&lt;img src=&quot;https://karttur.github.io/common/images/GRASS7_empty-GUI.png&quot; /&gt;
&lt;figcaption&gt; GRASS 7 Graphical User Interface (GUI) &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;In the &lt;em&gt;Layer Manager&lt;/em&gt; find the button for &lt;em&gt;Add multiple raster or vector map layers&lt;/em&gt; and click it. Add the raster file you used for defining the location and projection.&lt;/p&gt;

&lt;figure&gt;
&lt;img src=&quot;https://karttur.github.io/common/images/GRASS7_map-display-amazonia-DEM.png&quot; /&gt;
&lt;figcaption&gt; GRASS 7 Map Display &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;h3 id=&quot;grass-commandline&quot;&gt;GRASS commandline&lt;/h3&gt;

&lt;p&gt;You can either user GRASS via the GUI, or using the GRASS commandline. If you started GRASS with the &lt;span class=&quot;app&quot;&gt;Terminal&lt;/span&gt; up and running, you should now have a terminal windown where GRASS is running.&lt;/p&gt;

&lt;figure&gt;
&lt;img src=&quot;https://karttur.github.io/common/images/GRASS7_empty-terminal.png&quot; /&gt;
&lt;figcaption&gt; GRASS 7 terminal commandline &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;h4 id=&quot;commandline-tools&quot;&gt;Commandline tools&lt;/h4&gt;

&lt;p&gt;Try the commandline tool by checking out the projection:&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;GRASS 7.2.2 (Amazonia):~ &amp;gt; g.proj -p&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;The projection information will be returned at the prompt:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;-PROJ_INFO-------------------------------------------------
name       : Sinusoidal (Sanson-Flamsteed)
a          : 6371007.181
es         : 0
proj       : sinu
lon_0      : 0
x_0        : 0
y_0        : 0
no_defs    : defined
-PROJ_UNITS------------------------------------------------
unit       : meter
units      : meters
meters     : 1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Then also check the region:&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;GRASS 7.2.2 (Amazonia):~ &amp;gt; g.region -p&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Note how the returned information specifies the boundaries of the region as well as the spatial resolution and the number of rows and columns (cols) that span the region.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;projection: 99 (Sinusoidal (Sanson-Flamsteed))
zone:       0
datum:      ** unknown (default: WGS84) **
ellipsoid:  a=6371007.181 es=0
north:      1297275.606933
south:      -3914992.450317
west:       -9080929.2446
east:       -3822329.91573
nsres:      231.6563581
ewres:      231.6563581
rows:       22500
cols:       22700
cells:      510750000
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;rename-layer&quot;&gt;Rename layer&lt;/h4&gt;

&lt;p&gt;In the following parts of this series on Basin delineation, the name of the DEM layer to use for delineating basins will be assumed to be &lt;em&gt;DEM&lt;/em&gt;. If you imported the DEM as part of the definition of your location, it will be given the same name as the original map. If you forgot the name, use the GRASS general command &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/g.list.html&quot;&gt;g.list&lt;/a&gt; to list the layers in the current mapset:&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; g.list type=raster&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;If your DEM is not listed, make sure you are in the correct mapset with the command &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/g.mapset.html&quot;&gt;g.mapset&lt;/a&gt;:&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; g.mapset -p&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; g.mapset mapset=&lt;em&gt;name&lt;/em&gt;&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Rename any layer in your current mapset with the GRASS general command &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/g.rename.html&quot;&gt;g.rename&lt;/a&gt;:&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; g.rename raster=DEM_SRTM_amazonia_0_cgiar-250,DEM&lt;/span&gt;&lt;/p&gt;

&lt;h4 id=&quot;import-dem&quot;&gt;Import DEM&lt;/h4&gt;

&lt;p&gt;To manually import the DEM, and at the same time set a custom name you can use the GRASS command &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/r.in.gdal.html&quot;&gt;r.in.gdal&lt;/a&gt;. Add the &lt;em&gt;-e&lt;/em&gt; flag to set the region to the extent of the imported DEM if not already set to fit this dataset.&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; r.in.gdal -e input=/Volumes/GRASS2020/MODIS/SRTM/region/DEM/amazonia/DEM_SRTM_amazonia_0_cgiar-250.tif output=DEM –overwrite
&lt;/span&gt;&lt;/p&gt;

&lt;h3 id=&quot;create-a-new-mapset&quot;&gt;Create a new mapset&lt;/h3&gt;

&lt;p&gt;The DEM raster file that you imported, either when you defined the projection and location, or as a separate raster layer, should have ended up in the mapset &lt;em&gt;PERMANENT&lt;/em&gt;. For the delineation work, deriving a range of intermediate layers, and finally getting a map of all the basin outlets, it is better to change to a user defined mapset. Use the GRASS command &lt;a href=&quot;https://grass.osgeo.org/grass78/manuals/g.mapset.html&quot;&gt;g.mapset&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;&amp;gt; g.mapset -c drainage&lt;/span&gt;&lt;/p&gt;

&lt;h3 id=&quot;exit-grass&quot;&gt;Exit GRASS&lt;/h3&gt;

&lt;p&gt;To end the session you need to to exit from the GRASS commandline:&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;terminal&quot;&gt;GRASS 7.2.2 (Amazonia):~ &amp;gt; exit&lt;/span&gt;&lt;/p&gt;</content><author><name></name></author><category term="basin_delineate_setup" /><category term="GRASS7" /><category term="start" /><category term="Mac OSX" /><summary type="html">Introduction</summary></entry></feed>