<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://grasswiki.osgeo.org/w/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=%E2%9A%A0%EF%B8%8FPmav99</id>
	<title>GRASS-Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://grasswiki.osgeo.org/w/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=%E2%9A%A0%EF%B8%8FPmav99"/>
	<link rel="alternate" type="text/html" href="https://grasswiki.osgeo.org/wiki/Special:Contributions/%E2%9A%A0%EF%B8%8FPmav99"/>
	<updated>2026-04-18T12:21:07Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://grasswiki.osgeo.org/w/index.php?title=Python/pygrass&amp;diff=22145</id>
		<title>Python/pygrass</title>
		<link rel="alternate" type="text/html" href="https://grasswiki.osgeo.org/w/index.php?title=Python/pygrass&amp;diff=22145"/>
		<updated>2015-12-19T17:07:59Z</updated>

		<summary type="html">&lt;p&gt;⚠️Pmav99: /* Interface to listing maps (g.list/g.mlist) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''PyGRASS'' is a library that extends the GRASS GIS 7 capabilities to allow users to access the low-level GRASS API.&lt;br /&gt;
&lt;br /&gt;
== Documentation ==&lt;br /&gt;
&lt;br /&gt;
Detailed PyGRASS documentation: http://grass.osgeo.org/grass71/manuals/libpython/&lt;br /&gt;
&lt;br /&gt;
== How to test library ==&lt;br /&gt;
&lt;br /&gt;
''pygrass'' has 'doctest' included in its code. You can run doctest in the [http://grass.osgeo.org/sampledata/north_carolina/nc_basic_spm_grass7.tar.gz North Carolina basic location], using the mapset '''user1'''. &lt;br /&gt;
&lt;br /&gt;
To test a single module (file) you have to change into the source code directory of pygrass and run the following code (this is an example of functions.py):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python -m doctest functions.py&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
* Zambelli, P., Gebbert, S., Ciolli, M., 2013. ''Pygrass: An Object Oriented Python Application Programming Interface (API) for Geographic Resources Analysis Support System (GRASS) Geographic Information System (GIS)''. ISPRS International Journal of Geo-Information 2, 201–219. ([http://dx.doi.org/10.3390/ijgi2010201 DOI] | [http://www.mdpi.com/2220-9964/2/1/201/pdf PDF])&lt;br /&gt;
&lt;br /&gt;
* [http://grass.osgeo.org/grass71/manuals/libpython/ pyGrass documentation] (updated weekly from SVN)&lt;br /&gt;
&lt;br /&gt;
== Sample PyGRASS scripts ==&lt;br /&gt;
&lt;br /&gt;
=== Training material ===&lt;br /&gt;
&lt;br /&gt;
Two workshops were presented during the [http://geomorfolab.arch.unige.it/genova2013/ XIV Italian meeting of GRASS] at Genova.&lt;br /&gt;
The workshops use '''ipython notebook''' to show the python and pygrass API, all the material are available on github ([https://github.com/zarch/workshop-python python], [https://github.com/zarch/workshop-pygrass pygrass]). All the execute examples are reported here:&lt;br /&gt;
&lt;br /&gt;
* python training (Genova)&lt;br /&gt;
** [http://nbviewer.ipython.org/urls/raw.github.com/zarch/workshop-python/master/00_intro.ipynb      Introduction]&lt;br /&gt;
** [http://nbviewer.ipython.org/urls/raw.github.com/zarch/workshop-python/master/01_data_types.ipynb Data Types]&lt;br /&gt;
** [http://nbviewer.ipython.org/urls/raw.github.com/zarch/workshop-python/master/02_syntax.ipynb     Syntax]&lt;br /&gt;
** [http://nbviewer.ipython.org/urls/raw.github.com/zarch/workshop-python/master/03_function.ipynb   Function]&lt;br /&gt;
** [http://nbviewer.ipython.org/urls/raw.github.com/zarch/workshop-python/master/04_class.ipynb      Class]&lt;br /&gt;
** [http://nbviewer.ipython.org/urls/raw.github.com/zarch/workshop-python/master/05_other.ipynb      Other]&lt;br /&gt;
&lt;br /&gt;
* python training (EURAC)&lt;br /&gt;
** [http://nbviewer.ipython.org/urls/bitbucket.org/zarch/python-course/raw/teacher/00_intro.ipynb Introduction]&lt;br /&gt;
** [http://nbviewer.ipython.org/urls/bitbucket.org/zarch/python-course/raw/teacher/01_data_types.ipynb Data Types]&lt;br /&gt;
** [http://nbviewer.ipython.org/urls/bitbucket.org/zarch/python-course/raw/teacher/02_syntax.ipynb Syntax]&lt;br /&gt;
** [http://nbviewer.ipython.org/urls/bitbucket.org/zarch/python-course/raw/teacher/03_version_control_system.ipynb Version Control System (hg)]&lt;br /&gt;
** [http://nbviewer.ipython.org/urls/bitbucket.org/zarch/python-course/raw/teacher/04_objects.ipynb Objects]&lt;br /&gt;
** [http://nbviewer.ipython.org/urls/bitbucket.org/zarch/python-course/raw/teacher/05_python_debugger.ipynb Debugger]&lt;br /&gt;
** [http://nbviewer.ipython.org/urls/bitbucket.org/zarch/python-course/raw/teacher/06_functions.ipynb Functions]&lt;br /&gt;
** [http://nbviewer.ipython.org/urls/bitbucket.org/zarch/python-course/raw/teacher/07_numpy.ipynb Numpy]&lt;br /&gt;
** [http://nbviewer.ipython.org/urls/bitbucket.org/zarch/python-course/raw/teacher/08_matplotlib.ipynb Matplotlib]&lt;br /&gt;
** [http://nbviewer.ipython.org/urls/bitbucket.org/zarch/python-course/raw/teacher/09_scipy.ipynb Scipy]&lt;br /&gt;
** [http://nbviewer.ipython.org/urls/bitbucket.org/zarch/python-course/raw/teacher/10_pandas.ipynb Pandas]&lt;br /&gt;
** [http://nbviewer.ipython.org/urls/bitbucket.org/zarch/python-course/raw/teacher/11_sympy.ipynb Sympy]&lt;br /&gt;
** [http://nbviewer.ipython.org/urls/bitbucket.org/zarch/python-course/raw/teacher/12_user_interfaces.ipynb GUI]&lt;br /&gt;
&lt;br /&gt;
* pygrass training&lt;br /&gt;
** [http://nbviewer.ipython.org/urls/raw.github.com/zarch/workshop-pygrass/master/00_Modules.ipynb Modules]&lt;br /&gt;
** [http://nbviewer.ipython.org/urls/raw.github.com/zarch/workshop-pygrass/master/01_GIS_objects.ipynb GIS]&lt;br /&gt;
** [http://nbviewer.ipython.org/urls/raw.github.com/zarch/workshop-pygrass/master/02_Vector.ipynb Vector]&lt;br /&gt;
** [http://nbviewer.ipython.org/urls/raw.github.com/zarch/workshop-pygrass/master/03_Raster.ipynb Raster]&lt;br /&gt;
&lt;br /&gt;
=== Interface to listing maps (g.list/g.mlist) ===&lt;br /&gt;
&lt;br /&gt;
Here we call a GRASS module that writes to stdout and do not call a Python function that returns a Python object, therefore we can save stdout and then parse it with:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from grass.pygrass.modules.shortcuts import general as g&lt;br /&gt;
import subprocess as sub&lt;br /&gt;
gl = g.mlist  # GRASS 6&lt;br /&gt;
gl = g.list   # GRASS 7&lt;br /&gt;
gl(type='rast', pattern='elev-*', stdout=sub.PIPE)&lt;br /&gt;
gl.outputs.stdout.split()&lt;br /&gt;
['elev-000-000', 'elev-000-001', 'elev-000-002', 'elev-001-000',&lt;br /&gt;
'elev-001-001', 'elev-001-002', 'elev-002-000', 'elev-002-001',&lt;br /&gt;
'elev-002-002']&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or you can use the &amp;quot;glist&amp;quot; method of the Mapset class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from grass.pygrass.gis import Mapset&lt;br /&gt;
m = Mapset()&lt;br /&gt;
m.glist('rast', pattern='elev-*')&lt;br /&gt;
['elev-002-000',  'elev-000-000',  'elev-000-002',  'elev-000-001',&lt;br /&gt;
'elev-001-001',  'elev-002-002',  'elev-002-001',  'elev-001-000',&lt;br /&gt;
'elev-001-002']&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
... that uses the &amp;quot;G_list&amp;quot; function through ctypes and therefore is faster.&lt;br /&gt;
&lt;br /&gt;
Note: If you choose the first solution it is better if you use directly the&lt;br /&gt;
function in [http://grass.osgeo.org/programming7/namespacepython_1_1script_1_1core.html python.script.core.mlist_grouped() etc.].&lt;br /&gt;
&lt;br /&gt;
=== Interface to copying maps (g.copy) ===&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from grass.pygrass.modules.shortcuts import general as g&lt;br /&gt;
&lt;br /&gt;
vectinmap = 'zipcodes_wake'&lt;br /&gt;
vectoutmap = 'zip_elev_zonal'&lt;br /&gt;
g.copy(vect=&amp;quot;%s,%s&amp;quot; % (vectinmap,vectoutmap) )&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sample script for opening, query and closing of a vector map ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
#!/usr/bin/env python&lt;br /&gt;
 &lt;br /&gt;
# Example for pyGRASS usage - vector API&lt;br /&gt;
&lt;br /&gt;
from grass.pygrass.modules.shortcuts import general as g&lt;br /&gt;
from grass.pygrass.vector import VectorTopo&lt;br /&gt;
 &lt;br /&gt;
g.message(&amp;quot;Assessing vector topology...&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
vectmap = 'zipcodes_wake'&lt;br /&gt;
zipcodes = VectorTopo(vectmap)&lt;br /&gt;
&lt;br /&gt;
# Open the map with topology:&lt;br /&gt;
zipcodes.open()&lt;br /&gt;
&lt;br /&gt;
# query number of topological features&lt;br /&gt;
areas   = zipcodes.number_of(&amp;quot;areas&amp;quot;)&lt;br /&gt;
islands = zipcodes.number_of(&amp;quot;islands&amp;quot;)&lt;br /&gt;
print 'Map: &amp;lt;' + vectmap + '&amp;gt; with %d areas and %d islands' % (areas, islands)&lt;br /&gt;
&lt;br /&gt;
# http://www.ing.unitn.it/~zambelli/projects/pygrass/attributes.html&lt;br /&gt;
# (note that above documentation is slightly outdated)&lt;br /&gt;
dblink = zipcodes.dblinks[0]&lt;br /&gt;
print 'DB name:'&lt;br /&gt;
print dblink.database&lt;br /&gt;
table = dblink.table()&lt;br /&gt;
print 'Column names:'&lt;br /&gt;
print table.columns.names()&lt;br /&gt;
print 'Column types:'&lt;br /&gt;
print table.columns.types()&lt;br /&gt;
&lt;br /&gt;
zipcodes.close()&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sample script for Landsat 7 processing ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
#!/usr/bin/env python&lt;br /&gt;
#Date: 7th February, 2013&lt;br /&gt;
#Public domain, GRASS GIS&lt;br /&gt;
&lt;br /&gt;
#Run this script in the terminal as:&lt;br /&gt;
#python python-grass.py&lt;br /&gt;
#For debugging, run as:&lt;br /&gt;
#python -i python-grass.py&lt;br /&gt;
#PURPOSE&lt;br /&gt;
#This script processes LANDSAT 7 ETM+ images&lt;br /&gt;
#1 - unzip *.gz files&lt;br /&gt;
#2 - import files in GRASS GIS Location of your choice (r.in.gdal)&lt;br /&gt;
#3 - DN to Top of Atmosphere reflectance (i.landsat.toar)&lt;br /&gt;
#4 - TOA reflectance to Surface reflectance (i.atcorr)&lt;br /&gt;
#5 - NDVI (i.vi), Albedo (i.albedo), Emissivity (i.emissivity)&lt;br /&gt;
&lt;br /&gt;
#USER HAS TO SET THOSE&lt;br /&gt;
#QUIET REPORTS&lt;br /&gt;
QIET=True&lt;br /&gt;
#OVERWRITE EXISTING FILES&lt;br /&gt;
OVR=False&lt;br /&gt;
#Define Landsat 7 sensor for i.landsat.toar&lt;br /&gt;
LSENSOR=&amp;quot;tm7&amp;quot;&lt;br /&gt;
#Setup the path to the Landsat 7 Directories&lt;br /&gt;
rsdatapath=&amp;quot;/home/yann/RSDATA/Myanmar/L7&amp;quot;&lt;br /&gt;
#Setup your GRASS GIS working directory&lt;br /&gt;
gisdb=&amp;quot;/home/yann/GRASSDATA&amp;quot;&lt;br /&gt;
location=&amp;quot;L7_Myanmar&amp;quot;&lt;br /&gt;
print location&lt;br /&gt;
mapset=&amp;quot;PERMANENT&amp;quot;&lt;br /&gt;
#DEM input to atmospheric correction&lt;br /&gt;
inDEM=rsdatapath+&amp;quot;/dem.tif&amp;quot;&lt;br /&gt;
#set L7 Metadata wildcards&lt;br /&gt;
wldc_mtl=&amp;quot;*_MTL.txt&amp;quot;&lt;br /&gt;
#wldc_met=&amp;quot;*.met&amp;quot;&lt;br /&gt;
#Visibility distance [Km]&lt;br /&gt;
vis=18&lt;br /&gt;
&lt;br /&gt;
#Set python path to enable finding of grass.script lib&lt;br /&gt;
&lt;br /&gt;
#END OF USER CHANGES&lt;br /&gt;
###DO NOT CHANGE ANYTHING AFTER THIS LINE !&lt;br /&gt;
###&lt;br /&gt;
#Load necessary libraries&lt;br /&gt;
import os, glob, time, re&lt;br /&gt;
from grass import script as g&lt;br /&gt;
from grass.script import setup as gsetup&lt;br /&gt;
gisbase=os.environ['GISBASE']&lt;br /&gt;
gsetup.init(gisbase,gisdb,location,mapset)&lt;br /&gt;
from grass.pygrass.modules.shortcuts import raster as r&lt;br /&gt;
from grass.pygrass.modules.shortcuts import imagery as i&lt;br /&gt;
from grass.pygrass.modules.shortcuts import display as d&lt;br /&gt;
#Needed for floor()&lt;br /&gt;
from math import *&lt;br /&gt;
#env = os.environ.copy()&lt;br /&gt;
#env['GRASS_MESSAGE_FORMAT'] = 'gui'&lt;br /&gt;
#Function to get a list of L7 Directories in the rsdatapath&lt;br /&gt;
def fn(path):&lt;br /&gt;
	for top, dirs, files in os.walk(path):&lt;br /&gt;
		return [os.path.join(top, dir) for dir in dirs]&lt;br /&gt;
&lt;br /&gt;
#START PROCESS&lt;br /&gt;
### PART 0: PRE-PROCESSING STUFF ###&lt;br /&gt;
#import DEM for atmospheric correction&lt;br /&gt;
#r.in.gdal(input=inDEM,output=&amp;quot;dem&amp;quot;,overwrite=OVR)&lt;br /&gt;
#r.mapcalc(expression=&amp;quot;dem=25&amp;quot;,overwrite=OVR)&lt;br /&gt;
#create a visibility map&lt;br /&gt;
r.mapcalc(expression=&amp;quot;vis=18&amp;quot;,overwrite=OVR)&lt;br /&gt;
#Find the central location of the Landsat file from metadata&lt;br /&gt;
metadata=[]&lt;br /&gt;
fileList=[]&lt;br /&gt;
L7Dirs=fn(rsdatapath)&lt;br /&gt;
for L7Dir in L7Dirs:&lt;br /&gt;
	#Ungzip all of your Landsat7 images in all your directories&lt;br /&gt;
#	print &amp;quot;Ungzip Landsat files in\t&amp;quot;,L7Dir&lt;br /&gt;
#	p=os.system(&amp;quot;gzip -d -q &amp;quot;+L7Dir+&amp;quot;/*.gz&amp;quot;)&lt;br /&gt;
	#Using pthreads on multi-core machines&lt;br /&gt;
	#p=os.system(&amp;quot;pigz -d &amp;quot;+L7Dir+&amp;quot;/*.gz&amp;quot;)&lt;br /&gt;
	#Wait ten seconds for gzip to create the tif images&lt;br /&gt;
#	time.sleep(10)&lt;br /&gt;
	print &amp;quot;Import in GRASS GIS&amp;quot;&lt;br /&gt;
	for L7f in glob.glob(os.path.join(L7Dir,&amp;quot;*.[tT][iI][fF]&amp;quot;)):&lt;br /&gt;
		f1=L7f.replace(L7Dir+&amp;quot;/&amp;quot;,&amp;quot;&amp;quot;)&lt;br /&gt;
		f2=f1.replace(&amp;quot;.TIF&amp;quot;,&amp;quot;&amp;quot;)&lt;br /&gt;
		f3=f2.replace(&amp;quot;_B10&amp;quot;,&amp;quot;.1&amp;quot;)&lt;br /&gt;
		f4=f3.replace(&amp;quot;_B20&amp;quot;,&amp;quot;.2&amp;quot;)&lt;br /&gt;
		f5=f4.replace(&amp;quot;_B30&amp;quot;,&amp;quot;.3&amp;quot;)&lt;br /&gt;
		f6=f5.replace(&amp;quot;_B40&amp;quot;,&amp;quot;.4&amp;quot;)&lt;br /&gt;
		f7=f6.replace(&amp;quot;_B50&amp;quot;,&amp;quot;.5&amp;quot;)&lt;br /&gt;
		f8=f7.replace(&amp;quot;_B61&amp;quot;,&amp;quot;.61&amp;quot;)&lt;br /&gt;
		f9=f8.replace(&amp;quot;_B62&amp;quot;,&amp;quot;.62&amp;quot;)&lt;br /&gt;
		f10=f9.replace(&amp;quot;_B70&amp;quot;,&amp;quot;.7&amp;quot;)&lt;br /&gt;
		f11=f10.replace(&amp;quot;_B80&amp;quot;,&amp;quot;.8&amp;quot;)&lt;br /&gt;
		f12=f11.replace(&amp;quot;L72&amp;quot;,&amp;quot;L71&amp;quot;)&lt;br /&gt;
		L7r=f12.replace(&amp;quot;_VCID_&amp;quot;,&amp;quot;&amp;quot;)&lt;br /&gt;
		print &amp;quot;\t&amp;gt; &amp;quot;,L7r&lt;br /&gt;
		r.in_gdal(input=L7f,output=L7r,flags=&amp;quot;e&amp;quot;,overwrite=OVR)&lt;br /&gt;
		fileList.append(L7r)&lt;br /&gt;
&lt;br /&gt;
	#reproject the DEM World map for the new PERMANENT location extents&lt;br /&gt;
	r.proj(input=&amp;quot;dem&amp;quot;,location=&amp;quot;Myanmar&amp;quot;,memory=10000,resolution=90.0,overwrite=OVR)&lt;br /&gt;
	#Get list of metadata files&lt;br /&gt;
	for metaf in glob.glob(os.path.join(L7Dir,wldc_mtl)):&lt;br /&gt;
		metadata.append(metaf)&lt;br /&gt;
	print &amp;quot;Metadata in:\n&amp;quot;,metadata[0]&lt;br /&gt;
	with open(metadata[0],&amp;quot;r&amp;quot;) as f:&lt;br /&gt;
		data=f.read()&lt;br /&gt;
&lt;br /&gt;
	f.close()&lt;br /&gt;
	dt=data.split(&amp;quot;\n&amp;quot;)&lt;br /&gt;
	for idx in range(len(dt)):&lt;br /&gt;
		found=dt[idx].find(&amp;quot;CORNER_UL_LON_PRODUCT = &amp;quot;)&lt;br /&gt;
		if found &amp;gt; 0:&lt;br /&gt;
			ulx=float(dt[idx].replace(&amp;quot;CORNER_UL_LON_PRODUCT = &amp;quot;,&amp;quot;&amp;quot;))&lt;br /&gt;
		found=dt[idx].find(&amp;quot;CORNER_UL_LAT_PRODUCT = &amp;quot;)&lt;br /&gt;
		if found &amp;gt; 0:&lt;br /&gt;
			uly=float(dt[idx].replace(&amp;quot;CORNER_UL_LAT_PRODUCT = &amp;quot;,&amp;quot;&amp;quot;))&lt;br /&gt;
		found=dt[idx].find(&amp;quot;CORNER_LR_LON_PRODUCT = &amp;quot;)&lt;br /&gt;
		if found &amp;gt; 0:&lt;br /&gt;
			lrx=float(dt[idx].replace(&amp;quot;CORNER_LR_LON_PRODUCT = &amp;quot;,&amp;quot;&amp;quot;))&lt;br /&gt;
		found=dt[idx].find(&amp;quot;CORNER_LR_LAT_PRODUCT = &amp;quot;)&lt;br /&gt;
		if found &amp;gt; 0:&lt;br /&gt;
			lry=float(dt[idx].replace(&amp;quot;CORNER_LR_LAT_PRODUCT = &amp;quot;,&amp;quot;&amp;quot;))&lt;br /&gt;
		#Two cases, because a string starting by 0 is not converted well&lt;br /&gt;
		found=dt[idx].find(&amp;quot;SCENE_CENTER_TIME = &amp;quot;)&lt;br /&gt;
		if found &amp;gt; 0:&lt;br /&gt;
			tim=dt[idx].replace(&amp;quot;SCENE_CENTER_TIME = &amp;quot;,&amp;quot;&amp;quot;)&lt;br /&gt;
			print &amp;quot;timestamp=&amp;quot;,str(tim)&lt;br /&gt;
			gmth=float(tim.split(&amp;quot;:&amp;quot;)[0])&lt;br /&gt;
			gmtm=float(tim.split(&amp;quot;:&amp;quot;)[1])&lt;br /&gt;
			gmtdec=float(tim.split(&amp;quot;:&amp;quot;)[2].replace(&amp;quot;Z&amp;quot;,&amp;quot;&amp;quot;))&lt;br /&gt;
			print gmth,gmtm,gmtdec&lt;br /&gt;
		found=dt[idx].find(&amp;quot;DATE_ACQUIRED = &amp;quot;)&lt;br /&gt;
		if found &amp;gt; 0:&lt;br /&gt;
			dat=dt[idx].replace(&amp;quot;DATE_ACQUIRED = &amp;quot;,&amp;quot;&amp;quot;)&lt;br /&gt;
			date=dat.split(&amp;quot;-&amp;quot;)&lt;br /&gt;
		found=dt[idx].find(&amp;quot;SUN_ELEVATION = &amp;quot;)&lt;br /&gt;
                if found &amp;gt; 0:&lt;br /&gt;
                        sunza=90-float(dt[idx].replace(&amp;quot;SUN_ELEVATION = &amp;quot;,&amp;quot;&amp;quot;))&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
	#L7 DN-&amp;gt;Rad-&amp;gt;Ref&lt;br /&gt;
	print &amp;quot;Convert DN to Rad to TOARef&amp;quot;&lt;br /&gt;
	f1=sorted(fileList)[0]&lt;br /&gt;
	f2=f1.replace(L7Dir+&amp;quot;/&amp;quot;,&amp;quot;&amp;quot;)&lt;br /&gt;
	pref=f2[:-1]&lt;br /&gt;
	outpref=pref[:-2]+&amp;quot;.toar.&amp;quot;&lt;br /&gt;
	print &amp;quot;Prefix IN:\t&amp;quot;,pref&lt;br /&gt;
	print &amp;quot;Prefix OUT:\t&amp;quot;, outpref&lt;br /&gt;
	i.landsat_toar(input_prefix=pref,output_prefix=outpref,metfile=metadata[0],sensor=LSENSOR,quiet=QIET,overwrite=OVR)&lt;br /&gt;
	#Atmospheric Correction&lt;br /&gt;
	print &amp;quot;Atmospheric Correction&amp;quot;&lt;br /&gt;
	# Basic script for i.atcorr for L 7 ETM+&lt;br /&gt;
	#Geometrical conditions (L7ETM+)&lt;br /&gt;
	geom=7&lt;br /&gt;
	#Sensor height (satellite is -1000)&lt;br /&gt;
	sens_height=-1000&lt;br /&gt;
	#Here we suppose you have altitude (DEM) and Visibility (VIS) maps ready&lt;br /&gt;
	#---------------------------------------------&lt;br /&gt;
	#Visibility dummy value (overwritten by VIS raster input)&lt;br /&gt;
	vis=15&lt;br /&gt;
	#Altitude dummy value (in Km should be negative in this param file)&lt;br /&gt;
	#(overwritten by DEM raster input)&lt;br /&gt;
	alt=-1.200&lt;br /&gt;
	#datetime of satellite overpass (month, day, GMT decimal hour)&lt;br /&gt;
	mdh=str(int(date[1]))+&amp;quot; &amp;quot;+str(int(date[2]))+&amp;quot; &amp;quot;+str(&amp;quot;%.2f&amp;quot; % (gmth+gmtm/60.0+gmtdec/3600.0))&lt;br /&gt;
	print &amp;quot;MM DD hh.ddd:\t&amp;quot;,mdh&lt;br /&gt;
	# Central Lat/Long&lt;br /&gt;
	Long=ulx+(lrx-ulx)/2.0&lt;br /&gt;
	Lat=lry+(uly-lry)/2.0&lt;br /&gt;
	print &amp;quot;Center:\t(&amp;quot;,Long, &amp;quot;,&amp;quot;, Lat,&amp;quot;)&amp;quot;&lt;br /&gt;
	#Atmospheric mode&lt;br /&gt;
	atm_mode=1 #Tropical&lt;br /&gt;
	#Aerosol model&lt;br /&gt;
	aerosol_mode=2 #Sri Lanka is a small island (maritime)&lt;br /&gt;
	#satellite band number (L5TM [25,26,27,28,29,30], L7ETM+ [61,62,63,64,65,66,67])&lt;br /&gt;
	satbandno=61 #Band 1 of L7ETM+ is first to undergo atmospheric correction&lt;br /&gt;
	#make time stamp for use in time series analysis&lt;br /&gt;
	dat1=str(date[2])+&amp;quot;-&amp;quot;+str(date[1])+&amp;quot;-&amp;quot;+str(date[0])&lt;br /&gt;
	dat=dat1.replace(&amp;quot; &amp;quot;,&amp;quot;&amp;quot;)&lt;br /&gt;
	timestamp=time.strftime(&amp;quot;%d %b %Y&amp;quot;,time.strptime(dat,&amp;quot;%d-%m-%Y&amp;quot;))&lt;br /&gt;
	print &amp;quot;Timestamp:\t&amp;quot;,timestamp&lt;br /&gt;
	for idx in [1,2,3,4,5,7]:&lt;br /&gt;
		b=pref[:-2]+&amp;quot;.toar.&amp;quot;+str(idx)&lt;br /&gt;
		b_out=b.replace(&amp;quot;.toar.&amp;quot;,&amp;quot;.surf.&amp;quot;)&lt;br /&gt;
		param=[]&lt;br /&gt;
		param.append(str(geom)+&amp;quot; - geometrical conditions=Landsat 7 ETM+\n&amp;quot;)&lt;br /&gt;
		param.append(str(mdh)+&amp;quot; &amp;quot;+str(&amp;quot;%.2f&amp;quot; % Long)+&amp;quot; &amp;quot;+str(&amp;quot;%.2f&amp;quot; % Lat)+&amp;quot; - month day hh.ddd longitude latitude (hh.ddd is in decimal hours GMT)\n&amp;quot;)&lt;br /&gt;
		param.append(str(atm_mode)+&amp;quot; - atmospheric mode=tropical\n&amp;quot;)&lt;br /&gt;
		param.append(str(aerosol_mode)+&amp;quot; - aerosols model=maritime\n&amp;quot;)&lt;br /&gt;
		param.append(str(vis)+&amp;quot; - visibility [km] (aerosol model concentration), not used as there is raster input\n&amp;quot;)&lt;br /&gt;
		param.append(str(alt)+&amp;quot; - mean target elevation above sea level [km] (here 600m asl), not used as there is raster input\n&amp;quot;)&lt;br /&gt;
		param.append(str(sens_height)+&amp;quot; - sensor height (here, sensor on board a satellite)\n&amp;quot;)&lt;br /&gt;
		param.append(str(satbandno)+&amp;quot; - i th band of ETM+ Landsat 7 (atcorr internal no)\n&amp;quot;)&lt;br /&gt;
		f=open(os.path.join(L7Dir,&amp;quot;param_L7.txt&amp;quot;),&amp;quot;w&amp;quot;)&lt;br /&gt;
		f.writelines(param)&lt;br /&gt;
		f.close()&lt;br /&gt;
		prm=os.path.join(L7Dir,&amp;quot;param_L7.txt&amp;quot;)&lt;br /&gt;
		print &amp;quot;\t&amp;gt; &amp;quot;,b&lt;br /&gt;
		print &amp;quot;\t&amp;gt; &amp;quot;,b_out&lt;br /&gt;
		i.atcorr(input=b, elevation=&amp;quot;dem&amp;quot;, visibility=&amp;quot;vis&amp;quot;, parameters=prm, output=b_out, flags=&amp;quot;ra&amp;quot;, range=[0,1],quiet=QIET,overwrite=OVR)&lt;br /&gt;
		r.timestamp(map=b_out,date=timestamp,finish_=False)&lt;br /&gt;
		satbandno = satbandno + 1&lt;br /&gt;
&lt;br /&gt;
	#Allocate surface reflectance names&lt;br /&gt;
	b1=pref[:-2]+&amp;quot;.surf.1&amp;quot;&lt;br /&gt;
	b2=pref[:-2]+&amp;quot;.surf.2&amp;quot;&lt;br /&gt;
	b3=pref[:-2]+&amp;quot;.surf.3&amp;quot;&lt;br /&gt;
	b4=pref[:-2]+&amp;quot;.surf.4&amp;quot;&lt;br /&gt;
	b5=pref[:-2]+&amp;quot;.surf.5&amp;quot;&lt;br /&gt;
	b61=pref[:-2]+&amp;quot;.toar.61&amp;quot;&lt;br /&gt;
	b62=pref[:-2]+&amp;quot;.toar.62&amp;quot;&lt;br /&gt;
	b7=pref[:-2]+&amp;quot;.surf.7&amp;quot;&lt;br /&gt;
	b8=pref[:-2]+&amp;quot;.surf.8&amp;quot;&lt;br /&gt;
&lt;br /&gt;
	### PART 1: BASIC STUFF ###&lt;br /&gt;
	b_in=pref[:-2]+&amp;quot;.toar.&amp;quot;&lt;br /&gt;
	b_clouds=pref[:-2]+&amp;quot;.toar.acca&amp;quot;&lt;br /&gt;
	print &amp;quot;Clouds:\t\t&amp;gt;&amp;quot;,b_clouds&lt;br /&gt;
	i.landsat_acca(input_prefix=b_in,output=b_clouds,overwrite=OVR)&lt;br /&gt;
	#png_clouds=L7Dir+&amp;quot;/&amp;quot;+pref[:-2]+&amp;quot;.clouds.png&amp;quot;&lt;br /&gt;
	#d.mon(start='png',output=png_clouds,width=800,height=800)&lt;br /&gt;
	print &amp;quot;MASK:\t\tON&amp;quot;&lt;br /&gt;
	#Should always be rewritten!&lt;br /&gt;
	r.mask(raster=b_clouds,flags=&amp;quot;i&amp;quot;,overwrite=True)&lt;br /&gt;
&lt;br /&gt;
	b_ndvi=pref[:-2]+&amp;quot;.surf.ndvi&amp;quot;&lt;br /&gt;
	print &amp;quot;NDVI:\t&amp;quot;,b_ndvi&lt;br /&gt;
	i.vi(red=b3,nir=b4,output=b_ndvi,viname=&amp;quot;ndvi&amp;quot;,quiet=QIET,overwrite=OVR,finish_=False)&lt;br /&gt;
&lt;br /&gt;
	b_in=[b1,b2,b3,b4,b5,b7]&lt;br /&gt;
	b_albedo=pref[:-2]+&amp;quot;.surf.albedo&amp;quot;&lt;br /&gt;
	print &amp;quot;Albedo:\t&amp;quot;,b_albedo&lt;br /&gt;
	i.albedo(input=b_in,output=b_albedo,flags=&amp;quot;lc&amp;quot;,quiet=QIET,overwrite=OVR,finish_=False)&lt;br /&gt;
	&lt;br /&gt;
	b_emissivity=pref[:-2]+&amp;quot;.surf.emissivity&amp;quot;&lt;br /&gt;
	print &amp;quot;Emissivity:\t&amp;quot;,b_emissivity&lt;br /&gt;
	i.emissivity(input=b_ndvi, output=b_emissivity,quiet=QIET,overwrite=OVR,finish_=False)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Multiprocessing example: parallelized SHAPE file import ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
#!/usr/bin/env python&lt;br /&gt;
# -*- coding: utf-8 -*-&lt;br /&gt;
&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
Multiprocessing example: parallelized SHAPE file import&lt;br /&gt;
&lt;br /&gt;
Created on Fri, Oct 18, 2013, posted to grass-user@lists.osgeo.org&lt;br /&gt;
&lt;br /&gt;
@author: Pietro Zambelli, freely inspired by: http://stackoverflow.com/a/16071616&lt;br /&gt;
         http://lists.osgeo.org/pipermail//grass-user/2013-October/069130.html&lt;br /&gt;
&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Directory containing SHAPE files to import&lt;br /&gt;
DIR = '/data/shp'&lt;br /&gt;
&lt;br /&gt;
###&lt;br /&gt;
from multiprocessing import Queue, Process, cpu_count&lt;br /&gt;
from os.path import split&lt;br /&gt;
from subprocess import Popen&lt;br /&gt;
&lt;br /&gt;
from grass.pygrass.functions import findfiles&lt;br /&gt;
&lt;br /&gt;
def spawn(func):&lt;br /&gt;
    def fun(q_in, q_out):&lt;br /&gt;
        while True:&lt;br /&gt;
            path, cmdstr = q_in.get()&lt;br /&gt;
            if path is None:&lt;br /&gt;
                break&lt;br /&gt;
            q_out.put(func(path, cmdstr))&lt;br /&gt;
    return fun&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def mltp_importer(dirpath, match, cmdstr, func, nprocs=cpu_count()):&lt;br /&gt;
    q_in = Queue(1)&lt;br /&gt;
    q_out = Queue()&lt;br /&gt;
    procs = [Process(target=spawn(func), args=(q_in, q_out))&lt;br /&gt;
             for _ in range(nprocs)]&lt;br /&gt;
    for proc in procs:&lt;br /&gt;
        proc.daemon = True&lt;br /&gt;
        proc.start()&lt;br /&gt;
&lt;br /&gt;
    # set the parameters&lt;br /&gt;
    sent = [q_in.put((path, cmdstr)) for path in findfiles(dirpath, match)]&lt;br /&gt;
    # set the end of the cycle&lt;br /&gt;
    [q_in.put((None, None)) for proc in procs]&lt;br /&gt;
    [proc.join() for proc in procs]&lt;br /&gt;
    return [q_out.get() for _ in range(len(sent))]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def importer(path, cmdstr):&lt;br /&gt;
    name = split(path)[-1][:-4]&lt;br /&gt;
    print name&lt;br /&gt;
    popen = Popen(cmdstr.format(path=path, name=name), shell=True)&lt;br /&gt;
    popen.wait()&lt;br /&gt;
    return path, name, False if popen.returncode else True&lt;br /&gt;
&lt;br /&gt;
CMD = 'v.in.ogr dsn={path} layer={name} output={name}'&lt;br /&gt;
&lt;br /&gt;
processed = mltp_importer(DIR, '*.shp', CMD, importer)&lt;br /&gt;
# check for errors&lt;br /&gt;
errors = [p for p in processed if not p[2]]&lt;br /&gt;
if errors:&lt;br /&gt;
    # do something (print list of failed SHP files at end)&lt;br /&gt;
    pass&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Python}}&lt;/div&gt;</summary>
		<author><name>⚠️Pmav99</name></author>
	</entry>
	<entry>
		<id>https://grasswiki.osgeo.org/w/index.php?title=Creating_watersheds&amp;diff=22134</id>
		<title>Creating watersheds</title>
		<link rel="alternate" type="text/html" href="https://grasswiki.osgeo.org/w/index.php?title=Creating_watersheds&amp;diff=22134"/>
		<updated>2015-12-07T21:13:28Z</updated>

		<summary type="html">&lt;p&gt;⚠️Pmav99: /* Combining watersheds into one patched vector */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Arcview users, needing to delineate watersheds and stream networks, choose the extension called  &amp;quot;Arc Hydro&amp;quot; (requires at least Spatial Analyst). This extension introduces the concepts of &amp;quot;Batch Points&amp;quot; and &amp;quot;Adjoint Catchments&amp;quot;.  Batch points are locations that the user defines as drainage outlets. An adjoint catchment is the collection of all raster cells that drain into one of the batch points. Here we'll demonstrate how to get similar results with GRASS GIS 6. &lt;br /&gt;
&lt;br /&gt;
== Creating watersheds with specific drainage outlets==&lt;br /&gt;
As an example, we'll create a set of catchments with their drainage outlets exactly at the points where the streams cross a road. We'll assume our starting data includes an elevation raster called &amp;quot;dem&amp;quot; and a line vector called &amp;quot;roads&amp;quot;.  We first create the regular hydrology layers.&lt;br /&gt;
&lt;br /&gt;
=== Preparation ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#set the region to the dem raster, and run the r.watershed module.&lt;br /&gt;
g.region -p rast=dem&lt;br /&gt;
&lt;br /&gt;
# threshold in map cells (try 10000 as a start). Note: DEM Sink-filling not needed:&lt;br /&gt;
r.watershed elev=dem drain=fdir basin=catch stream=str thresh=&amp;lt;your-threshold&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So far, pretty straightforward. There's abundant information on {{cmd|r.watershed}}. I'll just mention that the threshold value is the number of &amp;lt;u&amp;gt;cells&amp;lt;/u&amp;gt; that will be the minimum catchment size. So if the resolution of our dem raster is, for example, 10x10 meters (each cell=100 sq. meters), then a threshold of 20,000 (=2,000,000 sq. meters) would create catchments of at least 2 sq. kilometers.&lt;br /&gt;
&lt;br /&gt;
=== Display first results ===&lt;br /&gt;
When the process finishes we'll have three new raster maps: the flow direction map, the streams and the catchments. Let's see what we've got so far:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#Convert the steams and catchments to vectors&lt;br /&gt;
# r.to.vect in=catch out=catchments feature=area        # Grass 6&lt;br /&gt;
r.to.vect in=catch out=catchments type=area             # Grass 7&lt;br /&gt;
&lt;br /&gt;
# the stream raster usually requires thinning&lt;br /&gt;
r.thin in=str out=str_thin&lt;br /&gt;
# r.to.vect in=str_thin out=streams feature=line        # Grass 6&lt;br /&gt;
r.to.vect in=str_thin out=streams type=line             # Grass 7&lt;br /&gt;
r.colors dem col=elevation&lt;br /&gt;
&lt;br /&gt;
# Make a hillshade raster for displaying &amp;quot;3D&amp;quot; &lt;br /&gt;
# r.shaded.relief map=dem shade=dem_shade zmult=1.5     # Grass 6&lt;br /&gt;
r.relief input=dem output=dem_shade zscale=1.5          # Grass 7&lt;br /&gt;
&lt;br /&gt;
# Now display layers&lt;br /&gt;
# d.mon x0          # Grass 6&lt;br /&gt;
d.mon start=wx0     # Grass 7&lt;br /&gt;
d.his h=dem i=dem_shade&lt;br /&gt;
d.vect map=streams color=blue width=3&lt;br /&gt;
d.vect map=catchments type=boundary color=red&lt;br /&gt;
d.vect roads color=black width=2&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Determine drainage points ===&lt;br /&gt;
Now we need to find all the points where streams cross roads. The {{cmd|v.overlay}} module does not deal with point vectors (hint: {{cmd|v.select}} does). Instead we use a trick in v.clean. When cleaning a line vector, all points where lines cross and no node exists are considered topological &amp;quot;errors&amp;quot; and can be saved to a new point vector. So by merging the roads and streams vectors, we create a vector with lines (streams) crossing other lines (roads) without a node. Then we run {{cmd|v.clean}}, and we get all those intersection points in a new vector.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Patch the streams and roads vectors together&lt;br /&gt;
v.patch in=streams,roads out=streams_roads&lt;br /&gt;
v.clean in=streams_roads out=streams_roads_clean tool=break error=cross_points&lt;br /&gt;
&lt;br /&gt;
#View cross points on display&lt;br /&gt;
d.vect cross_points icon=basic/circle color=green size=12&lt;br /&gt;
&lt;br /&gt;
# Save crossing points to a text file&lt;br /&gt;
# v.out.ascii in=cross_points out=cross_points.txt format=point fs=space                   # Grass 6&lt;br /&gt;
v.out.ascii in=cross_points out=cross_points.txt format=point layer=-1 separator=space     # Grass 7&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Looping thru drainage outlet points ===&lt;br /&gt;
Once we have the crossing points in a file, we simply run {{cmd|r.water.outlet}} in a loop to create a watershed for each cross point. However the raster result of r.water.outlet has value '1' in each cell that is upstream of the drainage point, and '0' everywhere else. For our purposes, we want to patch the rasters together after running the loop, so we need to have '''null values''' outside of the watersheds, and each watershed must use a '''different value''' in the upstream cells for its drainage point. To achieve these results, we use the r.null module to set '0' value cells to null. Then, we take advantage of the {{cmd|r.reclass}} function to make a reclassed raster with different values for each watershed.  Here's how it works for GRASS 6:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
i=0   #an iterator to give consecutive names to the new watersheds&lt;br /&gt;
while read X Y; do \&lt;br /&gt;
    i=$(( ${i} + 1 ))  \&lt;br /&gt;
    r.water.outlet drain=fdir east=$X north=$Y basin=tmp_$i  \&lt;br /&gt;
    r.null tmp_$i setnull=0 \&lt;br /&gt;
    echo 1=$i | r.reclass in=tmp_$i out=tmp_reclass_$i&lt;br /&gt;
done &amp;lt; cross_points.txt&lt;br /&gt;
echo &amp;quot;Created $i watersheds&amp;quot;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And here's how it works for GRASS 7:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
i=0   #an iterator to give consecutive names to the new watersheds&lt;br /&gt;
while read X Y; do&lt;br /&gt;
    i=$(( ${i} + 1 ))&lt;br /&gt;
    r.water.outlet --overwrite input=$drain coordinates=$X,$Y output=tmp_$i &lt;br /&gt;
    r.null tmp_$i setnull=0&lt;br /&gt;
    echo 1=$i | r.reclass --overwrite in=tmp_$i out=tmp_reclass_$i rules=- &lt;br /&gt;
done &amp;lt; cross_points.txt&lt;br /&gt;
echo &amp;quot;Created $i watersheds&amp;quot;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Combining watersheds into one patched vector ===&lt;br /&gt;
Next we patch together all the reclassed rasters (watersheds), convert to vector and clean the merged watersheds vector.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# r.patch in=`g.mlist rast pattern=tmp_reclass* separator=,` out=wshed_patch     # Grass 6&lt;br /&gt;
# r.to.vect in=wshed_patch out=wshed_patch feature=area                          # Grass 6&lt;br /&gt;
r.patch in=`g.list raster pattern=tmp_reclass* separator=,` out=wshed_patch      # Grass 7&lt;br /&gt;
r.to.vect in=wshed_patch out=wshed_patch type=area&lt;br /&gt;
# Use v.clean to remove tiny areas (that were a string of single cells in the raster)&lt;br /&gt;
v.clean wshed_patch out=wshed_final tool=rmarea thresh=150&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Choose an appropriate threshold value based on your region resolution.  With a region resolution of 10, each individual cell will be 100 sqm, so choosing 150 as the threshold for v.clean allows removing these small areas. Additional manual cleaning may be required. &lt;br /&gt;
&lt;br /&gt;
Clean up tmp rasters:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# g.remove rast=`g.mlist pattern=tmp* sep=,`                          # Grass 6&lt;br /&gt;
g.remove -f type=raster name=`g.list raster pattern=tmp* sep=,  `     # Grass 7&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Most likely we'll want to calculate the area for each watershed.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Add a table with a column for area in sq.km.&lt;br /&gt;
# v.db.addcol map=wshed_final col=&amp;quot;area_sqkm double&amp;quot;      # Grass 6&lt;br /&gt;
v.db.addcolumn map=wshed_final col=&amp;quot;area_sqkm double&amp;quot;     # Grass 7&lt;br /&gt;
&lt;br /&gt;
# Use unit=k(ilometers) to get area in sq. km.&lt;br /&gt;
v.to.db map=wshed_final option=area col=area_sqkm unit=k&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And finally, we can view the catchments, and their area values (you may use the [[wxGUI]]):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
d.vect wshed_final type=boundary,centroid display=shape,attr attrcol=area_sqkm size=0 width=3 color=orange&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category: FAQ]]&lt;br /&gt;
[[Category: Documentation]]&lt;br /&gt;
[[Category: Hydrology]]&lt;/div&gt;</summary>
		<author><name>⚠️Pmav99</name></author>
	</entry>
	<entry>
		<id>https://grasswiki.osgeo.org/w/index.php?title=Creating_watersheds&amp;diff=22133</id>
		<title>Creating watersheds</title>
		<link rel="alternate" type="text/html" href="https://grasswiki.osgeo.org/w/index.php?title=Creating_watersheds&amp;diff=22133"/>
		<updated>2015-12-07T21:11:14Z</updated>

		<summary type="html">&lt;p&gt;⚠️Pmav99: /* Combining watersheds into one patched vector */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Arcview users, needing to delineate watersheds and stream networks, choose the extension called  &amp;quot;Arc Hydro&amp;quot; (requires at least Spatial Analyst). This extension introduces the concepts of &amp;quot;Batch Points&amp;quot; and &amp;quot;Adjoint Catchments&amp;quot;.  Batch points are locations that the user defines as drainage outlets. An adjoint catchment is the collection of all raster cells that drain into one of the batch points. Here we'll demonstrate how to get similar results with GRASS GIS 6. &lt;br /&gt;
&lt;br /&gt;
== Creating watersheds with specific drainage outlets==&lt;br /&gt;
As an example, we'll create a set of catchments with their drainage outlets exactly at the points where the streams cross a road. We'll assume our starting data includes an elevation raster called &amp;quot;dem&amp;quot; and a line vector called &amp;quot;roads&amp;quot;.  We first create the regular hydrology layers.&lt;br /&gt;
&lt;br /&gt;
=== Preparation ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#set the region to the dem raster, and run the r.watershed module.&lt;br /&gt;
g.region -p rast=dem&lt;br /&gt;
&lt;br /&gt;
# threshold in map cells (try 10000 as a start). Note: DEM Sink-filling not needed:&lt;br /&gt;
r.watershed elev=dem drain=fdir basin=catch stream=str thresh=&amp;lt;your-threshold&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So far, pretty straightforward. There's abundant information on {{cmd|r.watershed}}. I'll just mention that the threshold value is the number of &amp;lt;u&amp;gt;cells&amp;lt;/u&amp;gt; that will be the minimum catchment size. So if the resolution of our dem raster is, for example, 10x10 meters (each cell=100 sq. meters), then a threshold of 20,000 (=2,000,000 sq. meters) would create catchments of at least 2 sq. kilometers.&lt;br /&gt;
&lt;br /&gt;
=== Display first results ===&lt;br /&gt;
When the process finishes we'll have three new raster maps: the flow direction map, the streams and the catchments. Let's see what we've got so far:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#Convert the steams and catchments to vectors&lt;br /&gt;
# r.to.vect in=catch out=catchments feature=area        # Grass 6&lt;br /&gt;
r.to.vect in=catch out=catchments type=area             # Grass 7&lt;br /&gt;
&lt;br /&gt;
# the stream raster usually requires thinning&lt;br /&gt;
r.thin in=str out=str_thin&lt;br /&gt;
# r.to.vect in=str_thin out=streams feature=line        # Grass 6&lt;br /&gt;
r.to.vect in=str_thin out=streams type=line             # Grass 7&lt;br /&gt;
r.colors dem col=elevation&lt;br /&gt;
&lt;br /&gt;
# Make a hillshade raster for displaying &amp;quot;3D&amp;quot; &lt;br /&gt;
# r.shaded.relief map=dem shade=dem_shade zmult=1.5     # Grass 6&lt;br /&gt;
r.relief input=dem output=dem_shade zscale=1.5          # Grass 7&lt;br /&gt;
&lt;br /&gt;
# Now display layers&lt;br /&gt;
# d.mon x0          # Grass 6&lt;br /&gt;
d.mon start=wx0     # Grass 7&lt;br /&gt;
d.his h=dem i=dem_shade&lt;br /&gt;
d.vect map=streams color=blue width=3&lt;br /&gt;
d.vect map=catchments type=boundary color=red&lt;br /&gt;
d.vect roads color=black width=2&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Determine drainage points ===&lt;br /&gt;
Now we need to find all the points where streams cross roads. The {{cmd|v.overlay}} module does not deal with point vectors (hint: {{cmd|v.select}} does). Instead we use a trick in v.clean. When cleaning a line vector, all points where lines cross and no node exists are considered topological &amp;quot;errors&amp;quot; and can be saved to a new point vector. So by merging the roads and streams vectors, we create a vector with lines (streams) crossing other lines (roads) without a node. Then we run {{cmd|v.clean}}, and we get all those intersection points in a new vector.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Patch the streams and roads vectors together&lt;br /&gt;
v.patch in=streams,roads out=streams_roads&lt;br /&gt;
v.clean in=streams_roads out=streams_roads_clean tool=break error=cross_points&lt;br /&gt;
&lt;br /&gt;
#View cross points on display&lt;br /&gt;
d.vect cross_points icon=basic/circle color=green size=12&lt;br /&gt;
&lt;br /&gt;
# Save crossing points to a text file&lt;br /&gt;
# v.out.ascii in=cross_points out=cross_points.txt format=point fs=space                   # Grass 6&lt;br /&gt;
v.out.ascii in=cross_points out=cross_points.txt format=point layer=-1 separator=space     # Grass 7&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Looping thru drainage outlet points ===&lt;br /&gt;
Once we have the crossing points in a file, we simply run {{cmd|r.water.outlet}} in a loop to create a watershed for each cross point. However the raster result of r.water.outlet has value '1' in each cell that is upstream of the drainage point, and '0' everywhere else. For our purposes, we want to patch the rasters together after running the loop, so we need to have '''null values''' outside of the watersheds, and each watershed must use a '''different value''' in the upstream cells for its drainage point. To achieve these results, we use the r.null module to set '0' value cells to null. Then, we take advantage of the {{cmd|r.reclass}} function to make a reclassed raster with different values for each watershed.  Here's how it works for GRASS 6:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
i=0   #an iterator to give consecutive names to the new watersheds&lt;br /&gt;
while read X Y; do \&lt;br /&gt;
    i=$(( ${i} + 1 ))  \&lt;br /&gt;
    r.water.outlet drain=fdir east=$X north=$Y basin=tmp_$i  \&lt;br /&gt;
    r.null tmp_$i setnull=0 \&lt;br /&gt;
    echo 1=$i | r.reclass in=tmp_$i out=tmp_reclass_$i&lt;br /&gt;
done &amp;lt; cross_points.txt&lt;br /&gt;
echo &amp;quot;Created $i watersheds&amp;quot;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And here's how it works for GRASS 7:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
i=0   #an iterator to give consecutive names to the new watersheds&lt;br /&gt;
while read X Y; do&lt;br /&gt;
    i=$(( ${i} + 1 ))&lt;br /&gt;
    r.water.outlet --overwrite input=$drain coordinates=$X,$Y output=tmp_$i &lt;br /&gt;
    r.null tmp_$i setnull=0&lt;br /&gt;
    echo 1=$i | r.reclass --overwrite in=tmp_$i out=tmp_reclass_$i rules=- &lt;br /&gt;
done &amp;lt; cross_points.txt&lt;br /&gt;
echo &amp;quot;Created $i watersheds&amp;quot;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Combining watersheds into one patched vector ===&lt;br /&gt;
Next we patch together all the reclassed rasters (watersheds), convert to vector and clean the merged watersheds vector.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# r.patch in=`g.mlist rast pattern=tmp_reclass* separator=,` out=wshed_patch     # Grass 6&lt;br /&gt;
# r.to.vect in=wshed_patch out=wshed_patch feature=area                          # Grass 6&lt;br /&gt;
r.patch in=`g.list raster pattern=tmp_reclass* separator=,` out=wshed_patch      # Grass 7&lt;br /&gt;
r.to.vect in=wshed_patch out=wshed_patch type=area&lt;br /&gt;
# Use v.clean to remove tiny areas (that were a string of single cells in the raster)&lt;br /&gt;
v.clean wshed_patch out=wshed_final tool=rmarea thresh=150&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Choose an appropriate threshold value based on your region resolution.  With a region resolution of 10, each individual cell will be 100 sqm, so choosing 150 as the threshold for v.clean allows removing these small areas. Additional manual cleaning may be required. &lt;br /&gt;
&lt;br /&gt;
Clean up tmp rasters:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# g.remove rast=`g.mlist pattern=tmp* sep=,`                          # Grass 6&lt;br /&gt;
g.remove -f type=raster name=`g.list raster pattern=tmp* sep=,  `     # Grass 7&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Most likely we'll want to calculate the area for each watershed.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Add a table with a column for area in sq.km.&lt;br /&gt;
v.db.addcol map=wshed_final col=&amp;quot;area_sqkm double&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Use unit=k(ilometers) to get area in sq. km.&lt;br /&gt;
v.to.db map=wshed_final option=area col=area_sqkm unit=k&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And finally, we can view the catchments, and their area values (you may use the [[wxGUI]]):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
d.vect wshed_final type=boundary,centroid display=shape,attr attrcol=area_sqkm size=0 width=3 color=orange&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category: FAQ]]&lt;br /&gt;
[[Category: Documentation]]&lt;br /&gt;
[[Category: Hydrology]]&lt;/div&gt;</summary>
		<author><name>⚠️Pmav99</name></author>
	</entry>
	<entry>
		<id>https://grasswiki.osgeo.org/w/index.php?title=Creating_watersheds&amp;diff=22132</id>
		<title>Creating watersheds</title>
		<link rel="alternate" type="text/html" href="https://grasswiki.osgeo.org/w/index.php?title=Creating_watersheds&amp;diff=22132"/>
		<updated>2015-12-07T21:09:14Z</updated>

		<summary type="html">&lt;p&gt;⚠️Pmav99: /* Combining watersheds into one patched vector */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Arcview users, needing to delineate watersheds and stream networks, choose the extension called  &amp;quot;Arc Hydro&amp;quot; (requires at least Spatial Analyst). This extension introduces the concepts of &amp;quot;Batch Points&amp;quot; and &amp;quot;Adjoint Catchments&amp;quot;.  Batch points are locations that the user defines as drainage outlets. An adjoint catchment is the collection of all raster cells that drain into one of the batch points. Here we'll demonstrate how to get similar results with GRASS GIS 6. &lt;br /&gt;
&lt;br /&gt;
== Creating watersheds with specific drainage outlets==&lt;br /&gt;
As an example, we'll create a set of catchments with their drainage outlets exactly at the points where the streams cross a road. We'll assume our starting data includes an elevation raster called &amp;quot;dem&amp;quot; and a line vector called &amp;quot;roads&amp;quot;.  We first create the regular hydrology layers.&lt;br /&gt;
&lt;br /&gt;
=== Preparation ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#set the region to the dem raster, and run the r.watershed module.&lt;br /&gt;
g.region -p rast=dem&lt;br /&gt;
&lt;br /&gt;
# threshold in map cells (try 10000 as a start). Note: DEM Sink-filling not needed:&lt;br /&gt;
r.watershed elev=dem drain=fdir basin=catch stream=str thresh=&amp;lt;your-threshold&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So far, pretty straightforward. There's abundant information on {{cmd|r.watershed}}. I'll just mention that the threshold value is the number of &amp;lt;u&amp;gt;cells&amp;lt;/u&amp;gt; that will be the minimum catchment size. So if the resolution of our dem raster is, for example, 10x10 meters (each cell=100 sq. meters), then a threshold of 20,000 (=2,000,000 sq. meters) would create catchments of at least 2 sq. kilometers.&lt;br /&gt;
&lt;br /&gt;
=== Display first results ===&lt;br /&gt;
When the process finishes we'll have three new raster maps: the flow direction map, the streams and the catchments. Let's see what we've got so far:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#Convert the steams and catchments to vectors&lt;br /&gt;
# r.to.vect in=catch out=catchments feature=area        # Grass 6&lt;br /&gt;
r.to.vect in=catch out=catchments type=area             # Grass 7&lt;br /&gt;
&lt;br /&gt;
# the stream raster usually requires thinning&lt;br /&gt;
r.thin in=str out=str_thin&lt;br /&gt;
# r.to.vect in=str_thin out=streams feature=line        # Grass 6&lt;br /&gt;
r.to.vect in=str_thin out=streams type=line             # Grass 7&lt;br /&gt;
r.colors dem col=elevation&lt;br /&gt;
&lt;br /&gt;
# Make a hillshade raster for displaying &amp;quot;3D&amp;quot; &lt;br /&gt;
# r.shaded.relief map=dem shade=dem_shade zmult=1.5     # Grass 6&lt;br /&gt;
r.relief input=dem output=dem_shade zscale=1.5          # Grass 7&lt;br /&gt;
&lt;br /&gt;
# Now display layers&lt;br /&gt;
# d.mon x0          # Grass 6&lt;br /&gt;
d.mon start=wx0     # Grass 7&lt;br /&gt;
d.his h=dem i=dem_shade&lt;br /&gt;
d.vect map=streams color=blue width=3&lt;br /&gt;
d.vect map=catchments type=boundary color=red&lt;br /&gt;
d.vect roads color=black width=2&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Determine drainage points ===&lt;br /&gt;
Now we need to find all the points where streams cross roads. The {{cmd|v.overlay}} module does not deal with point vectors (hint: {{cmd|v.select}} does). Instead we use a trick in v.clean. When cleaning a line vector, all points where lines cross and no node exists are considered topological &amp;quot;errors&amp;quot; and can be saved to a new point vector. So by merging the roads and streams vectors, we create a vector with lines (streams) crossing other lines (roads) without a node. Then we run {{cmd|v.clean}}, and we get all those intersection points in a new vector.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Patch the streams and roads vectors together&lt;br /&gt;
v.patch in=streams,roads out=streams_roads&lt;br /&gt;
v.clean in=streams_roads out=streams_roads_clean tool=break error=cross_points&lt;br /&gt;
&lt;br /&gt;
#View cross points on display&lt;br /&gt;
d.vect cross_points icon=basic/circle color=green size=12&lt;br /&gt;
&lt;br /&gt;
# Save crossing points to a text file&lt;br /&gt;
# v.out.ascii in=cross_points out=cross_points.txt format=point fs=space                   # Grass 6&lt;br /&gt;
v.out.ascii in=cross_points out=cross_points.txt format=point layer=-1 separator=space     # Grass 7&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Looping thru drainage outlet points ===&lt;br /&gt;
Once we have the crossing points in a file, we simply run {{cmd|r.water.outlet}} in a loop to create a watershed for each cross point. However the raster result of r.water.outlet has value '1' in each cell that is upstream of the drainage point, and '0' everywhere else. For our purposes, we want to patch the rasters together after running the loop, so we need to have '''null values''' outside of the watersheds, and each watershed must use a '''different value''' in the upstream cells for its drainage point. To achieve these results, we use the r.null module to set '0' value cells to null. Then, we take advantage of the {{cmd|r.reclass}} function to make a reclassed raster with different values for each watershed.  Here's how it works for GRASS 6:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
i=0   #an iterator to give consecutive names to the new watersheds&lt;br /&gt;
while read X Y; do \&lt;br /&gt;
    i=$(( ${i} + 1 ))  \&lt;br /&gt;
    r.water.outlet drain=fdir east=$X north=$Y basin=tmp_$i  \&lt;br /&gt;
    r.null tmp_$i setnull=0 \&lt;br /&gt;
    echo 1=$i | r.reclass in=tmp_$i out=tmp_reclass_$i&lt;br /&gt;
done &amp;lt; cross_points.txt&lt;br /&gt;
echo &amp;quot;Created $i watersheds&amp;quot;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And here's how it works for GRASS 7:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
i=0   #an iterator to give consecutive names to the new watersheds&lt;br /&gt;
while read X Y; do&lt;br /&gt;
    i=$(( ${i} + 1 ))&lt;br /&gt;
    r.water.outlet --overwrite input=$drain coordinates=$X,$Y output=tmp_$i &lt;br /&gt;
    r.null tmp_$i setnull=0&lt;br /&gt;
    echo 1=$i | r.reclass --overwrite in=tmp_$i out=tmp_reclass_$i rules=- &lt;br /&gt;
done &amp;lt; cross_points.txt&lt;br /&gt;
echo &amp;quot;Created $i watersheds&amp;quot;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Combining watersheds into one patched vector ===&lt;br /&gt;
Next we patch together all the reclassed rasters (watersheds), convert to vector and clean the merged watersheds vector.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# r.patch in=`g.mlist rast pattern=tmp_reclass* separator=,` out=wshed_patch     # Grass 6&lt;br /&gt;
# r.to.vect in=wshed_patch out=wshed_patch feature=area                          # Grass 6&lt;br /&gt;
r.patch in=`g.list raster pattern=tmp_reclass* separator=,` out=wshed_patch      # Grass 7&lt;br /&gt;
r.to.vect in=wshed_patch out=wshed_patch type=area&lt;br /&gt;
# Use v.clean to remove tiny areas (that were a string of single cells in the raster)&lt;br /&gt;
v.clean wshed_patch out=wshed_final tool=rmarea thresh=150&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Choose an appropriate threshold value based on your region resolution.  With a region resolution of 10, each individual cell will be 100 sqm, so choosing 150 as the threshold for v.clean allows removing these small areas. Additional manual cleaning may be required. &lt;br /&gt;
&lt;br /&gt;
Clean up tmp rasters:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# g.remove rast=`g.mlist pattern=tmp* sep=,`           # Grass 6&lt;br /&gt;
g.remove rast=`g.list raster pattern=tmp* sep=,  `     # Grass 7&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Most likely we'll want to calculate the area for each watershed.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Add a table with a column for area in sq.km.&lt;br /&gt;
v.db.addcol map=wshed_final col=&amp;quot;area_sqkm double&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Use unit=k(ilometers) to get area in sq. km.&lt;br /&gt;
v.to.db map=wshed_final option=area col=area_sqkm unit=k&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And finally, we can view the catchments, and their area values (you may use the [[wxGUI]]):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
d.vect wshed_final type=boundary,centroid display=shape,attr attrcol=area_sqkm size=0 width=3 color=orange&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category: FAQ]]&lt;br /&gt;
[[Category: Documentation]]&lt;br /&gt;
[[Category: Hydrology]]&lt;/div&gt;</summary>
		<author><name>⚠️Pmav99</name></author>
	</entry>
	<entry>
		<id>https://grasswiki.osgeo.org/w/index.php?title=Creating_watersheds&amp;diff=22131</id>
		<title>Creating watersheds</title>
		<link rel="alternate" type="text/html" href="https://grasswiki.osgeo.org/w/index.php?title=Creating_watersheds&amp;diff=22131"/>
		<updated>2015-12-07T21:08:10Z</updated>

		<summary type="html">&lt;p&gt;⚠️Pmav99: /* Combining watersheds into one patched vector */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Arcview users, needing to delineate watersheds and stream networks, choose the extension called  &amp;quot;Arc Hydro&amp;quot; (requires at least Spatial Analyst). This extension introduces the concepts of &amp;quot;Batch Points&amp;quot; and &amp;quot;Adjoint Catchments&amp;quot;.  Batch points are locations that the user defines as drainage outlets. An adjoint catchment is the collection of all raster cells that drain into one of the batch points. Here we'll demonstrate how to get similar results with GRASS GIS 6. &lt;br /&gt;
&lt;br /&gt;
== Creating watersheds with specific drainage outlets==&lt;br /&gt;
As an example, we'll create a set of catchments with their drainage outlets exactly at the points where the streams cross a road. We'll assume our starting data includes an elevation raster called &amp;quot;dem&amp;quot; and a line vector called &amp;quot;roads&amp;quot;.  We first create the regular hydrology layers.&lt;br /&gt;
&lt;br /&gt;
=== Preparation ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#set the region to the dem raster, and run the r.watershed module.&lt;br /&gt;
g.region -p rast=dem&lt;br /&gt;
&lt;br /&gt;
# threshold in map cells (try 10000 as a start). Note: DEM Sink-filling not needed:&lt;br /&gt;
r.watershed elev=dem drain=fdir basin=catch stream=str thresh=&amp;lt;your-threshold&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So far, pretty straightforward. There's abundant information on {{cmd|r.watershed}}. I'll just mention that the threshold value is the number of &amp;lt;u&amp;gt;cells&amp;lt;/u&amp;gt; that will be the minimum catchment size. So if the resolution of our dem raster is, for example, 10x10 meters (each cell=100 sq. meters), then a threshold of 20,000 (=2,000,000 sq. meters) would create catchments of at least 2 sq. kilometers.&lt;br /&gt;
&lt;br /&gt;
=== Display first results ===&lt;br /&gt;
When the process finishes we'll have three new raster maps: the flow direction map, the streams and the catchments. Let's see what we've got so far:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#Convert the steams and catchments to vectors&lt;br /&gt;
# r.to.vect in=catch out=catchments feature=area        # Grass 6&lt;br /&gt;
r.to.vect in=catch out=catchments type=area             # Grass 7&lt;br /&gt;
&lt;br /&gt;
# the stream raster usually requires thinning&lt;br /&gt;
r.thin in=str out=str_thin&lt;br /&gt;
# r.to.vect in=str_thin out=streams feature=line        # Grass 6&lt;br /&gt;
r.to.vect in=str_thin out=streams type=line             # Grass 7&lt;br /&gt;
r.colors dem col=elevation&lt;br /&gt;
&lt;br /&gt;
# Make a hillshade raster for displaying &amp;quot;3D&amp;quot; &lt;br /&gt;
# r.shaded.relief map=dem shade=dem_shade zmult=1.5     # Grass 6&lt;br /&gt;
r.relief input=dem output=dem_shade zscale=1.5          # Grass 7&lt;br /&gt;
&lt;br /&gt;
# Now display layers&lt;br /&gt;
# d.mon x0          # Grass 6&lt;br /&gt;
d.mon start=wx0     # Grass 7&lt;br /&gt;
d.his h=dem i=dem_shade&lt;br /&gt;
d.vect map=streams color=blue width=3&lt;br /&gt;
d.vect map=catchments type=boundary color=red&lt;br /&gt;
d.vect roads color=black width=2&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Determine drainage points ===&lt;br /&gt;
Now we need to find all the points where streams cross roads. The {{cmd|v.overlay}} module does not deal with point vectors (hint: {{cmd|v.select}} does). Instead we use a trick in v.clean. When cleaning a line vector, all points where lines cross and no node exists are considered topological &amp;quot;errors&amp;quot; and can be saved to a new point vector. So by merging the roads and streams vectors, we create a vector with lines (streams) crossing other lines (roads) without a node. Then we run {{cmd|v.clean}}, and we get all those intersection points in a new vector.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Patch the streams and roads vectors together&lt;br /&gt;
v.patch in=streams,roads out=streams_roads&lt;br /&gt;
v.clean in=streams_roads out=streams_roads_clean tool=break error=cross_points&lt;br /&gt;
&lt;br /&gt;
#View cross points on display&lt;br /&gt;
d.vect cross_points icon=basic/circle color=green size=12&lt;br /&gt;
&lt;br /&gt;
# Save crossing points to a text file&lt;br /&gt;
# v.out.ascii in=cross_points out=cross_points.txt format=point fs=space                   # Grass 6&lt;br /&gt;
v.out.ascii in=cross_points out=cross_points.txt format=point layer=-1 separator=space     # Grass 7&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Looping thru drainage outlet points ===&lt;br /&gt;
Once we have the crossing points in a file, we simply run {{cmd|r.water.outlet}} in a loop to create a watershed for each cross point. However the raster result of r.water.outlet has value '1' in each cell that is upstream of the drainage point, and '0' everywhere else. For our purposes, we want to patch the rasters together after running the loop, so we need to have '''null values''' outside of the watersheds, and each watershed must use a '''different value''' in the upstream cells for its drainage point. To achieve these results, we use the r.null module to set '0' value cells to null. Then, we take advantage of the {{cmd|r.reclass}} function to make a reclassed raster with different values for each watershed.  Here's how it works for GRASS 6:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
i=0   #an iterator to give consecutive names to the new watersheds&lt;br /&gt;
while read X Y; do \&lt;br /&gt;
    i=$(( ${i} + 1 ))  \&lt;br /&gt;
    r.water.outlet drain=fdir east=$X north=$Y basin=tmp_$i  \&lt;br /&gt;
    r.null tmp_$i setnull=0 \&lt;br /&gt;
    echo 1=$i | r.reclass in=tmp_$i out=tmp_reclass_$i&lt;br /&gt;
done &amp;lt; cross_points.txt&lt;br /&gt;
echo &amp;quot;Created $i watersheds&amp;quot;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And here's how it works for GRASS 7:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
i=0   #an iterator to give consecutive names to the new watersheds&lt;br /&gt;
while read X Y; do&lt;br /&gt;
    i=$(( ${i} + 1 ))&lt;br /&gt;
    r.water.outlet --overwrite input=$drain coordinates=$X,$Y output=tmp_$i &lt;br /&gt;
    r.null tmp_$i setnull=0&lt;br /&gt;
    echo 1=$i | r.reclass --overwrite in=tmp_$i out=tmp_reclass_$i rules=- &lt;br /&gt;
done &amp;lt; cross_points.txt&lt;br /&gt;
echo &amp;quot;Created $i watersheds&amp;quot;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Combining watersheds into one patched vector ===&lt;br /&gt;
Next we patch together all the reclassed rasters (watersheds), convert to vector and clean the merged watersheds vector.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# r.patch in=`g.mlist rast pattern=tmp_reclass* separator=,` out=wshed_patch     # Grass 6&lt;br /&gt;
# r.to.vect in=wshed_patch out=wshed_patch feature=area                          # Grass 6&lt;br /&gt;
r.patch in=`g.list raster pattern=tmp_reclass* separator=,` out=wshed_patch      # Grass 7&lt;br /&gt;
r.to.vect in=wshed_patch out=wshed_patch type=area&lt;br /&gt;
# Use v.clean to remove tiny areas (that were a string of single cells in the raster)&lt;br /&gt;
v.clean wshed_patch out=wshed_final tool=rmarea thresh=150&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Choose an appropriate threshold value based on your region resolution.  With a region resolution of 10, each individual cell will be 100 sqm, so choosing 150 as the threshold for v.clean allows removing these small areas. Additional manual cleaning may be required. &lt;br /&gt;
&lt;br /&gt;
Clean up tmp rasters:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
g.remove rast=`g.mlist pattern=tmp* sep=,`&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Most likely we'll want to calculate the area for each watershed.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Add a table with a column for area in sq.km.&lt;br /&gt;
v.db.addcol map=wshed_final col=&amp;quot;area_sqkm double&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Use unit=k(ilometers) to get area in sq. km.&lt;br /&gt;
v.to.db map=wshed_final option=area col=area_sqkm unit=k&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And finally, we can view the catchments, and their area values (you may use the [[wxGUI]]):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
d.vect wshed_final type=boundary,centroid display=shape,attr attrcol=area_sqkm size=0 width=3 color=orange&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category: FAQ]]&lt;br /&gt;
[[Category: Documentation]]&lt;br /&gt;
[[Category: Hydrology]]&lt;/div&gt;</summary>
		<author><name>⚠️Pmav99</name></author>
	</entry>
	<entry>
		<id>https://grasswiki.osgeo.org/w/index.php?title=Creating_watersheds&amp;diff=22130</id>
		<title>Creating watersheds</title>
		<link rel="alternate" type="text/html" href="https://grasswiki.osgeo.org/w/index.php?title=Creating_watersheds&amp;diff=22130"/>
		<updated>2015-12-07T21:01:44Z</updated>

		<summary type="html">&lt;p&gt;⚠️Pmav99: /* Looping thru drainage outlet points */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Arcview users, needing to delineate watersheds and stream networks, choose the extension called  &amp;quot;Arc Hydro&amp;quot; (requires at least Spatial Analyst). This extension introduces the concepts of &amp;quot;Batch Points&amp;quot; and &amp;quot;Adjoint Catchments&amp;quot;.  Batch points are locations that the user defines as drainage outlets. An adjoint catchment is the collection of all raster cells that drain into one of the batch points. Here we'll demonstrate how to get similar results with GRASS GIS 6. &lt;br /&gt;
&lt;br /&gt;
== Creating watersheds with specific drainage outlets==&lt;br /&gt;
As an example, we'll create a set of catchments with their drainage outlets exactly at the points where the streams cross a road. We'll assume our starting data includes an elevation raster called &amp;quot;dem&amp;quot; and a line vector called &amp;quot;roads&amp;quot;.  We first create the regular hydrology layers.&lt;br /&gt;
&lt;br /&gt;
=== Preparation ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#set the region to the dem raster, and run the r.watershed module.&lt;br /&gt;
g.region -p rast=dem&lt;br /&gt;
&lt;br /&gt;
# threshold in map cells (try 10000 as a start). Note: DEM Sink-filling not needed:&lt;br /&gt;
r.watershed elev=dem drain=fdir basin=catch stream=str thresh=&amp;lt;your-threshold&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So far, pretty straightforward. There's abundant information on {{cmd|r.watershed}}. I'll just mention that the threshold value is the number of &amp;lt;u&amp;gt;cells&amp;lt;/u&amp;gt; that will be the minimum catchment size. So if the resolution of our dem raster is, for example, 10x10 meters (each cell=100 sq. meters), then a threshold of 20,000 (=2,000,000 sq. meters) would create catchments of at least 2 sq. kilometers.&lt;br /&gt;
&lt;br /&gt;
=== Display first results ===&lt;br /&gt;
When the process finishes we'll have three new raster maps: the flow direction map, the streams and the catchments. Let's see what we've got so far:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#Convert the steams and catchments to vectors&lt;br /&gt;
# r.to.vect in=catch out=catchments feature=area        # Grass 6&lt;br /&gt;
r.to.vect in=catch out=catchments type=area             # Grass 7&lt;br /&gt;
&lt;br /&gt;
# the stream raster usually requires thinning&lt;br /&gt;
r.thin in=str out=str_thin&lt;br /&gt;
# r.to.vect in=str_thin out=streams feature=line        # Grass 6&lt;br /&gt;
r.to.vect in=str_thin out=streams type=line             # Grass 7&lt;br /&gt;
r.colors dem col=elevation&lt;br /&gt;
&lt;br /&gt;
# Make a hillshade raster for displaying &amp;quot;3D&amp;quot; &lt;br /&gt;
# r.shaded.relief map=dem shade=dem_shade zmult=1.5     # Grass 6&lt;br /&gt;
r.relief input=dem output=dem_shade zscale=1.5          # Grass 7&lt;br /&gt;
&lt;br /&gt;
# Now display layers&lt;br /&gt;
# d.mon x0          # Grass 6&lt;br /&gt;
d.mon start=wx0     # Grass 7&lt;br /&gt;
d.his h=dem i=dem_shade&lt;br /&gt;
d.vect map=streams color=blue width=3&lt;br /&gt;
d.vect map=catchments type=boundary color=red&lt;br /&gt;
d.vect roads color=black width=2&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Determine drainage points ===&lt;br /&gt;
Now we need to find all the points where streams cross roads. The {{cmd|v.overlay}} module does not deal with point vectors (hint: {{cmd|v.select}} does). Instead we use a trick in v.clean. When cleaning a line vector, all points where lines cross and no node exists are considered topological &amp;quot;errors&amp;quot; and can be saved to a new point vector. So by merging the roads and streams vectors, we create a vector with lines (streams) crossing other lines (roads) without a node. Then we run {{cmd|v.clean}}, and we get all those intersection points in a new vector.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Patch the streams and roads vectors together&lt;br /&gt;
v.patch in=streams,roads out=streams_roads&lt;br /&gt;
v.clean in=streams_roads out=streams_roads_clean tool=break error=cross_points&lt;br /&gt;
&lt;br /&gt;
#View cross points on display&lt;br /&gt;
d.vect cross_points icon=basic/circle color=green size=12&lt;br /&gt;
&lt;br /&gt;
# Save crossing points to a text file&lt;br /&gt;
# v.out.ascii in=cross_points out=cross_points.txt format=point fs=space                   # Grass 6&lt;br /&gt;
v.out.ascii in=cross_points out=cross_points.txt format=point layer=-1 separator=space     # Grass 7&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Looping thru drainage outlet points ===&lt;br /&gt;
Once we have the crossing points in a file, we simply run {{cmd|r.water.outlet}} in a loop to create a watershed for each cross point. However the raster result of r.water.outlet has value '1' in each cell that is upstream of the drainage point, and '0' everywhere else. For our purposes, we want to patch the rasters together after running the loop, so we need to have '''null values''' outside of the watersheds, and each watershed must use a '''different value''' in the upstream cells for its drainage point. To achieve these results, we use the r.null module to set '0' value cells to null. Then, we take advantage of the {{cmd|r.reclass}} function to make a reclassed raster with different values for each watershed.  Here's how it works for GRASS 6:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
i=0   #an iterator to give consecutive names to the new watersheds&lt;br /&gt;
while read X Y; do \&lt;br /&gt;
    i=$(( ${i} + 1 ))  \&lt;br /&gt;
    r.water.outlet drain=fdir east=$X north=$Y basin=tmp_$i  \&lt;br /&gt;
    r.null tmp_$i setnull=0 \&lt;br /&gt;
    echo 1=$i | r.reclass in=tmp_$i out=tmp_reclass_$i&lt;br /&gt;
done &amp;lt; cross_points.txt&lt;br /&gt;
echo &amp;quot;Created $i watersheds&amp;quot;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And here's how it works for GRASS 7:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
i=0   #an iterator to give consecutive names to the new watersheds&lt;br /&gt;
while read X Y; do&lt;br /&gt;
    i=$(( ${i} + 1 ))&lt;br /&gt;
    r.water.outlet --overwrite input=$drain coordinates=$X,$Y output=tmp_$i &lt;br /&gt;
    r.null tmp_$i setnull=0&lt;br /&gt;
    echo 1=$i | r.reclass --overwrite in=tmp_$i out=tmp_reclass_$i rules=- &lt;br /&gt;
done &amp;lt; cross_points.txt&lt;br /&gt;
echo &amp;quot;Created $i watersheds&amp;quot;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Combining watersheds into one patched vector ===&lt;br /&gt;
Next we patch together all the reclassed rasters (watersheds), convert to vector and clean the merged watersheds vector.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
r.patch in=`g.mlist rast pattern=tmp_reclass* separator=,` out=wshed_patch&lt;br /&gt;
r.to.vect in=wshed_patch out=wshed_patch feature=area&lt;br /&gt;
# Use v.clean to remove tiny areas (that were a string of single cells in the raster)&lt;br /&gt;
v.clean wshed_patch out=wshed_final tool=rmarea thresh=150&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Choose an appropriate threshold value based on your region resolution.  With a region resolution of 10, each individual cell will be 100 sqm, so choosing 150 as the threshold for v.clean allows removing these small areas. Additional manual cleaning may be required. &lt;br /&gt;
&lt;br /&gt;
Clean up tmp rasters:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
g.remove rast=`g.mlist pattern=tmp* sep=,`&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Most likely we'll want to calculate the area for each watershed.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Add a table with a column for area in sq.km.&lt;br /&gt;
v.db.addcol map=wshed_final col=&amp;quot;area_sqkm double&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Use unit=k(ilometers) to get area in sq. km.&lt;br /&gt;
v.to.db map=wshed_final option=area col=area_sqkm unit=k&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And finally, we can view the catchments, and their area values (you may use the [[wxGUI]]):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
d.vect wshed_final type=boundary,centroid display=shape,attr attrcol=area_sqkm size=0 width=3 color=orange&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category: FAQ]]&lt;br /&gt;
[[Category: Documentation]]&lt;br /&gt;
[[Category: Hydrology]]&lt;/div&gt;</summary>
		<author><name>⚠️Pmav99</name></author>
	</entry>
	<entry>
		<id>https://grasswiki.osgeo.org/w/index.php?title=Creating_watersheds&amp;diff=22129</id>
		<title>Creating watersheds</title>
		<link rel="alternate" type="text/html" href="https://grasswiki.osgeo.org/w/index.php?title=Creating_watersheds&amp;diff=22129"/>
		<updated>2015-12-07T20:55:29Z</updated>

		<summary type="html">&lt;p&gt;⚠️Pmav99: /* Determine drainage points */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Arcview users, needing to delineate watersheds and stream networks, choose the extension called  &amp;quot;Arc Hydro&amp;quot; (requires at least Spatial Analyst). This extension introduces the concepts of &amp;quot;Batch Points&amp;quot; and &amp;quot;Adjoint Catchments&amp;quot;.  Batch points are locations that the user defines as drainage outlets. An adjoint catchment is the collection of all raster cells that drain into one of the batch points. Here we'll demonstrate how to get similar results with GRASS GIS 6. &lt;br /&gt;
&lt;br /&gt;
== Creating watersheds with specific drainage outlets==&lt;br /&gt;
As an example, we'll create a set of catchments with their drainage outlets exactly at the points where the streams cross a road. We'll assume our starting data includes an elevation raster called &amp;quot;dem&amp;quot; and a line vector called &amp;quot;roads&amp;quot;.  We first create the regular hydrology layers.&lt;br /&gt;
&lt;br /&gt;
=== Preparation ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#set the region to the dem raster, and run the r.watershed module.&lt;br /&gt;
g.region -p rast=dem&lt;br /&gt;
&lt;br /&gt;
# threshold in map cells (try 10000 as a start). Note: DEM Sink-filling not needed:&lt;br /&gt;
r.watershed elev=dem drain=fdir basin=catch stream=str thresh=&amp;lt;your-threshold&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So far, pretty straightforward. There's abundant information on {{cmd|r.watershed}}. I'll just mention that the threshold value is the number of &amp;lt;u&amp;gt;cells&amp;lt;/u&amp;gt; that will be the minimum catchment size. So if the resolution of our dem raster is, for example, 10x10 meters (each cell=100 sq. meters), then a threshold of 20,000 (=2,000,000 sq. meters) would create catchments of at least 2 sq. kilometers.&lt;br /&gt;
&lt;br /&gt;
=== Display first results ===&lt;br /&gt;
When the process finishes we'll have three new raster maps: the flow direction map, the streams and the catchments. Let's see what we've got so far:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#Convert the steams and catchments to vectors&lt;br /&gt;
# r.to.vect in=catch out=catchments feature=area        # Grass 6&lt;br /&gt;
r.to.vect in=catch out=catchments type=area             # Grass 7&lt;br /&gt;
&lt;br /&gt;
# the stream raster usually requires thinning&lt;br /&gt;
r.thin in=str out=str_thin&lt;br /&gt;
# r.to.vect in=str_thin out=streams feature=line        # Grass 6&lt;br /&gt;
r.to.vect in=str_thin out=streams type=line             # Grass 7&lt;br /&gt;
r.colors dem col=elevation&lt;br /&gt;
&lt;br /&gt;
# Make a hillshade raster for displaying &amp;quot;3D&amp;quot; &lt;br /&gt;
# r.shaded.relief map=dem shade=dem_shade zmult=1.5     # Grass 6&lt;br /&gt;
r.relief input=dem output=dem_shade zscale=1.5          # Grass 7&lt;br /&gt;
&lt;br /&gt;
# Now display layers&lt;br /&gt;
# d.mon x0          # Grass 6&lt;br /&gt;
d.mon start=wx0     # Grass 7&lt;br /&gt;
d.his h=dem i=dem_shade&lt;br /&gt;
d.vect map=streams color=blue width=3&lt;br /&gt;
d.vect map=catchments type=boundary color=red&lt;br /&gt;
d.vect roads color=black width=2&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Determine drainage points ===&lt;br /&gt;
Now we need to find all the points where streams cross roads. The {{cmd|v.overlay}} module does not deal with point vectors (hint: {{cmd|v.select}} does). Instead we use a trick in v.clean. When cleaning a line vector, all points where lines cross and no node exists are considered topological &amp;quot;errors&amp;quot; and can be saved to a new point vector. So by merging the roads and streams vectors, we create a vector with lines (streams) crossing other lines (roads) without a node. Then we run {{cmd|v.clean}}, and we get all those intersection points in a new vector.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Patch the streams and roads vectors together&lt;br /&gt;
v.patch in=streams,roads out=streams_roads&lt;br /&gt;
v.clean in=streams_roads out=streams_roads_clean tool=break error=cross_points&lt;br /&gt;
&lt;br /&gt;
#View cross points on display&lt;br /&gt;
d.vect cross_points icon=basic/circle color=green size=12&lt;br /&gt;
&lt;br /&gt;
# Save crossing points to a text file&lt;br /&gt;
# v.out.ascii in=cross_points out=cross_points.txt format=point fs=space                   # Grass 6&lt;br /&gt;
v.out.ascii in=cross_points out=cross_points.txt format=point layer=-1 separator=space     # Grass 7&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Looping thru drainage outlet points ===&lt;br /&gt;
Once we have the crossing points in a file, we simply run {{cmd|r.water.outlet}} in a loop to create a watershed for each cross point. However the raster result of r.water.outlet has value '1' in each cell that is upstream of the drainage point, and '0' everywhere else. For our purposes, we want to patch the rasters together after running the loop, so we need to have '''null values''' outside of the watersheds, and each watershed must use a '''different value''' in the upstream cells for its drainage point. To achieve these results, we use the r.null module to set '0' value cells to null. Then, we take advantage of the {{cmd|r.reclass}} function to make a reclassed raster with different values for each watershed.  Here's how it works:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
i=0   #an iterator to give consecutive names to the new watersheds&lt;br /&gt;
while read X Y; do \&lt;br /&gt;
    i=$(( ${i} + 1 ))  \&lt;br /&gt;
    r.water.outlet drain=fdir east=$X north=$Y basin=tmp_$i  \&lt;br /&gt;
    r.null tmp_$i setnull=0 \&lt;br /&gt;
    echo 1=$i | r.reclass in=tmp_$i out=tmp_reclass_$i&lt;br /&gt;
done &amp;lt; cross_points.txt&lt;br /&gt;
echo &amp;quot;Created $i watersheds&amp;quot;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Combining watersheds into one patched vector ===&lt;br /&gt;
Next we patch together all the reclassed rasters (watersheds), convert to vector and clean the merged watersheds vector.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
r.patch in=`g.mlist rast pattern=tmp_reclass* separator=,` out=wshed_patch&lt;br /&gt;
r.to.vect in=wshed_patch out=wshed_patch feature=area&lt;br /&gt;
# Use v.clean to remove tiny areas (that were a string of single cells in the raster)&lt;br /&gt;
v.clean wshed_patch out=wshed_final tool=rmarea thresh=150&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Choose an appropriate threshold value based on your region resolution.  With a region resolution of 10, each individual cell will be 100 sqm, so choosing 150 as the threshold for v.clean allows removing these small areas. Additional manual cleaning may be required. &lt;br /&gt;
&lt;br /&gt;
Clean up tmp rasters:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
g.remove rast=`g.mlist pattern=tmp* sep=,`&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Most likely we'll want to calculate the area for each watershed.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Add a table with a column for area in sq.km.&lt;br /&gt;
v.db.addcol map=wshed_final col=&amp;quot;area_sqkm double&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Use unit=k(ilometers) to get area in sq. km.&lt;br /&gt;
v.to.db map=wshed_final option=area col=area_sqkm unit=k&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And finally, we can view the catchments, and their area values (you may use the [[wxGUI]]):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
d.vect wshed_final type=boundary,centroid display=shape,attr attrcol=area_sqkm size=0 width=3 color=orange&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category: FAQ]]&lt;br /&gt;
[[Category: Documentation]]&lt;br /&gt;
[[Category: Hydrology]]&lt;/div&gt;</summary>
		<author><name>⚠️Pmav99</name></author>
	</entry>
	<entry>
		<id>https://grasswiki.osgeo.org/w/index.php?title=Creating_watersheds&amp;diff=22128</id>
		<title>Creating watersheds</title>
		<link rel="alternate" type="text/html" href="https://grasswiki.osgeo.org/w/index.php?title=Creating_watersheds&amp;diff=22128"/>
		<updated>2015-12-07T20:52:19Z</updated>

		<summary type="html">&lt;p&gt;⚠️Pmav99: /* Determine drainage points */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Arcview users, needing to delineate watersheds and stream networks, choose the extension called  &amp;quot;Arc Hydro&amp;quot; (requires at least Spatial Analyst). This extension introduces the concepts of &amp;quot;Batch Points&amp;quot; and &amp;quot;Adjoint Catchments&amp;quot;.  Batch points are locations that the user defines as drainage outlets. An adjoint catchment is the collection of all raster cells that drain into one of the batch points. Here we'll demonstrate how to get similar results with GRASS GIS 6. &lt;br /&gt;
&lt;br /&gt;
== Creating watersheds with specific drainage outlets==&lt;br /&gt;
As an example, we'll create a set of catchments with their drainage outlets exactly at the points where the streams cross a road. We'll assume our starting data includes an elevation raster called &amp;quot;dem&amp;quot; and a line vector called &amp;quot;roads&amp;quot;.  We first create the regular hydrology layers.&lt;br /&gt;
&lt;br /&gt;
=== Preparation ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#set the region to the dem raster, and run the r.watershed module.&lt;br /&gt;
g.region -p rast=dem&lt;br /&gt;
&lt;br /&gt;
# threshold in map cells (try 10000 as a start). Note: DEM Sink-filling not needed:&lt;br /&gt;
r.watershed elev=dem drain=fdir basin=catch stream=str thresh=&amp;lt;your-threshold&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So far, pretty straightforward. There's abundant information on {{cmd|r.watershed}}. I'll just mention that the threshold value is the number of &amp;lt;u&amp;gt;cells&amp;lt;/u&amp;gt; that will be the minimum catchment size. So if the resolution of our dem raster is, for example, 10x10 meters (each cell=100 sq. meters), then a threshold of 20,000 (=2,000,000 sq. meters) would create catchments of at least 2 sq. kilometers.&lt;br /&gt;
&lt;br /&gt;
=== Display first results ===&lt;br /&gt;
When the process finishes we'll have three new raster maps: the flow direction map, the streams and the catchments. Let's see what we've got so far:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#Convert the steams and catchments to vectors&lt;br /&gt;
# r.to.vect in=catch out=catchments feature=area        # Grass 6&lt;br /&gt;
r.to.vect in=catch out=catchments type=area             # Grass 7&lt;br /&gt;
&lt;br /&gt;
# the stream raster usually requires thinning&lt;br /&gt;
r.thin in=str out=str_thin&lt;br /&gt;
# r.to.vect in=str_thin out=streams feature=line        # Grass 6&lt;br /&gt;
r.to.vect in=str_thin out=streams type=line             # Grass 7&lt;br /&gt;
r.colors dem col=elevation&lt;br /&gt;
&lt;br /&gt;
# Make a hillshade raster for displaying &amp;quot;3D&amp;quot; &lt;br /&gt;
# r.shaded.relief map=dem shade=dem_shade zmult=1.5     # Grass 6&lt;br /&gt;
r.relief input=dem output=dem_shade zscale=1.5          # Grass 7&lt;br /&gt;
&lt;br /&gt;
# Now display layers&lt;br /&gt;
# d.mon x0          # Grass 6&lt;br /&gt;
d.mon start=wx0     # Grass 7&lt;br /&gt;
d.his h=dem i=dem_shade&lt;br /&gt;
d.vect map=streams color=blue width=3&lt;br /&gt;
d.vect map=catchments type=boundary color=red&lt;br /&gt;
d.vect roads color=black width=2&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Determine drainage points ===&lt;br /&gt;
Now we need to find all the points where streams cross roads. The {{cmd|v.overlay}} module does not deal with point vectors (hint: {{cmd|v.select}} does). Instead we use a trick in v.clean. When cleaning a line vector, all points where lines cross and no node exists are considered topological &amp;quot;errors&amp;quot; and can be saved to a new point vector. So by merging the roads and streams vectors, we create a vector with lines (streams) crossing other lines (roads) without a node. Then we run {{cmd|v.clean}}, and we get all those intersection points in a new vector.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Patch the streams and roads vectors together&lt;br /&gt;
v.patch in=streams,roads out=streams_roads&lt;br /&gt;
v.clean in=streams_roads out=streams_roads_clean tool=break error=cross_points&lt;br /&gt;
&lt;br /&gt;
#View cross points on display&lt;br /&gt;
d.vect cross_points icon=basic/circle color=green size=12&lt;br /&gt;
&lt;br /&gt;
# Save crossing points to a text file&lt;br /&gt;
# v.out.ascii in=cross_points out=cross_points.txt format=point fs=space          # Grass 6&lt;br /&gt;
v.out.ascii in=cross_points out=cross_points.txt format=point separator=space     # Grass 7&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Looping thru drainage outlet points ===&lt;br /&gt;
Once we have the crossing points in a file, we simply run {{cmd|r.water.outlet}} in a loop to create a watershed for each cross point. However the raster result of r.water.outlet has value '1' in each cell that is upstream of the drainage point, and '0' everywhere else. For our purposes, we want to patch the rasters together after running the loop, so we need to have '''null values''' outside of the watersheds, and each watershed must use a '''different value''' in the upstream cells for its drainage point. To achieve these results, we use the r.null module to set '0' value cells to null. Then, we take advantage of the {{cmd|r.reclass}} function to make a reclassed raster with different values for each watershed.  Here's how it works:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
i=0   #an iterator to give consecutive names to the new watersheds&lt;br /&gt;
while read X Y; do \&lt;br /&gt;
    i=$(( ${i} + 1 ))  \&lt;br /&gt;
    r.water.outlet drain=fdir east=$X north=$Y basin=tmp_$i  \&lt;br /&gt;
    r.null tmp_$i setnull=0 \&lt;br /&gt;
    echo 1=$i | r.reclass in=tmp_$i out=tmp_reclass_$i&lt;br /&gt;
done &amp;lt; cross_points.txt&lt;br /&gt;
echo &amp;quot;Created $i watersheds&amp;quot;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Combining watersheds into one patched vector ===&lt;br /&gt;
Next we patch together all the reclassed rasters (watersheds), convert to vector and clean the merged watersheds vector.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
r.patch in=`g.mlist rast pattern=tmp_reclass* separator=,` out=wshed_patch&lt;br /&gt;
r.to.vect in=wshed_patch out=wshed_patch feature=area&lt;br /&gt;
# Use v.clean to remove tiny areas (that were a string of single cells in the raster)&lt;br /&gt;
v.clean wshed_patch out=wshed_final tool=rmarea thresh=150&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Choose an appropriate threshold value based on your region resolution.  With a region resolution of 10, each individual cell will be 100 sqm, so choosing 150 as the threshold for v.clean allows removing these small areas. Additional manual cleaning may be required. &lt;br /&gt;
&lt;br /&gt;
Clean up tmp rasters:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
g.remove rast=`g.mlist pattern=tmp* sep=,`&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Most likely we'll want to calculate the area for each watershed.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Add a table with a column for area in sq.km.&lt;br /&gt;
v.db.addcol map=wshed_final col=&amp;quot;area_sqkm double&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Use unit=k(ilometers) to get area in sq. km.&lt;br /&gt;
v.to.db map=wshed_final option=area col=area_sqkm unit=k&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And finally, we can view the catchments, and their area values (you may use the [[wxGUI]]):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
d.vect wshed_final type=boundary,centroid display=shape,attr attrcol=area_sqkm size=0 width=3 color=orange&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category: FAQ]]&lt;br /&gt;
[[Category: Documentation]]&lt;br /&gt;
[[Category: Hydrology]]&lt;/div&gt;</summary>
		<author><name>⚠️Pmav99</name></author>
	</entry>
	<entry>
		<id>https://grasswiki.osgeo.org/w/index.php?title=Creating_watersheds&amp;diff=22127</id>
		<title>Creating watersheds</title>
		<link rel="alternate" type="text/html" href="https://grasswiki.osgeo.org/w/index.php?title=Creating_watersheds&amp;diff=22127"/>
		<updated>2015-12-07T20:43:44Z</updated>

		<summary type="html">&lt;p&gt;⚠️Pmav99: /* Display first results */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Arcview users, needing to delineate watersheds and stream networks, choose the extension called  &amp;quot;Arc Hydro&amp;quot; (requires at least Spatial Analyst). This extension introduces the concepts of &amp;quot;Batch Points&amp;quot; and &amp;quot;Adjoint Catchments&amp;quot;.  Batch points are locations that the user defines as drainage outlets. An adjoint catchment is the collection of all raster cells that drain into one of the batch points. Here we'll demonstrate how to get similar results with GRASS GIS 6. &lt;br /&gt;
&lt;br /&gt;
== Creating watersheds with specific drainage outlets==&lt;br /&gt;
As an example, we'll create a set of catchments with their drainage outlets exactly at the points where the streams cross a road. We'll assume our starting data includes an elevation raster called &amp;quot;dem&amp;quot; and a line vector called &amp;quot;roads&amp;quot;.  We first create the regular hydrology layers.&lt;br /&gt;
&lt;br /&gt;
=== Preparation ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#set the region to the dem raster, and run the r.watershed module.&lt;br /&gt;
g.region -p rast=dem&lt;br /&gt;
&lt;br /&gt;
# threshold in map cells (try 10000 as a start). Note: DEM Sink-filling not needed:&lt;br /&gt;
r.watershed elev=dem drain=fdir basin=catch stream=str thresh=&amp;lt;your-threshold&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So far, pretty straightforward. There's abundant information on {{cmd|r.watershed}}. I'll just mention that the threshold value is the number of &amp;lt;u&amp;gt;cells&amp;lt;/u&amp;gt; that will be the minimum catchment size. So if the resolution of our dem raster is, for example, 10x10 meters (each cell=100 sq. meters), then a threshold of 20,000 (=2,000,000 sq. meters) would create catchments of at least 2 sq. kilometers.&lt;br /&gt;
&lt;br /&gt;
=== Display first results ===&lt;br /&gt;
When the process finishes we'll have three new raster maps: the flow direction map, the streams and the catchments. Let's see what we've got so far:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#Convert the steams and catchments to vectors&lt;br /&gt;
# r.to.vect in=catch out=catchments feature=area        # Grass 6&lt;br /&gt;
r.to.vect in=catch out=catchments type=area             # Grass 7&lt;br /&gt;
&lt;br /&gt;
# the stream raster usually requires thinning&lt;br /&gt;
r.thin in=str out=str_thin&lt;br /&gt;
# r.to.vect in=str_thin out=streams feature=line        # Grass 6&lt;br /&gt;
r.to.vect in=str_thin out=streams type=line             # Grass 7&lt;br /&gt;
r.colors dem col=elevation&lt;br /&gt;
&lt;br /&gt;
# Make a hillshade raster for displaying &amp;quot;3D&amp;quot; &lt;br /&gt;
# r.shaded.relief map=dem shade=dem_shade zmult=1.5     # Grass 6&lt;br /&gt;
r.relief input=dem output=dem_shade zscale=1.5          # Grass 7&lt;br /&gt;
&lt;br /&gt;
# Now display layers&lt;br /&gt;
# d.mon x0          # Grass 6&lt;br /&gt;
d.mon start=wx0     # Grass 7&lt;br /&gt;
d.his h=dem i=dem_shade&lt;br /&gt;
d.vect map=streams color=blue width=3&lt;br /&gt;
d.vect map=catchments type=boundary color=red&lt;br /&gt;
d.vect roads color=black width=2&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Determine drainage points ===&lt;br /&gt;
Now we need to find all the points where streams cross roads. The {{cmd|v.overlay}} module does not deal with point vectors (hint: {{cmd|v.select}} does). Instead we use a trick in v.clean. When cleaning a line vector, all points where lines cross and no node exists are considered topological &amp;quot;errors&amp;quot; and can be saved to a new point vector. So by merging the roads and streams vectors, we create a vector with lines (streams) crossing other lines (roads) without a node. Then we run {{cmd|v.clean}}, and we get all those intersection points in a new vector.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Patch the streams and roads vectors together&lt;br /&gt;
v.patch in=streams,roads out=streams_roads&lt;br /&gt;
v.clean in=streams_roads out=streams_roads_clean tool=break error=cross_points&lt;br /&gt;
&lt;br /&gt;
#View cross points on display&lt;br /&gt;
d.vect cross_points icon=basic/circle color=green size=12&lt;br /&gt;
&lt;br /&gt;
# Save crossing points to a text file&lt;br /&gt;
v.out.ascii in=cross_points out=cross_points.txt format=point fs=space&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Looping thru drainage outlet points ===&lt;br /&gt;
Once we have the crossing points in a file, we simply run {{cmd|r.water.outlet}} in a loop to create a watershed for each cross point. However the raster result of r.water.outlet has value '1' in each cell that is upstream of the drainage point, and '0' everywhere else. For our purposes, we want to patch the rasters together after running the loop, so we need to have '''null values''' outside of the watersheds, and each watershed must use a '''different value''' in the upstream cells for its drainage point. To achieve these results, we use the r.null module to set '0' value cells to null. Then, we take advantage of the {{cmd|r.reclass}} function to make a reclassed raster with different values for each watershed.  Here's how it works:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
i=0   #an iterator to give consecutive names to the new watersheds&lt;br /&gt;
while read X Y; do \&lt;br /&gt;
    i=$(( ${i} + 1 ))  \&lt;br /&gt;
    r.water.outlet drain=fdir east=$X north=$Y basin=tmp_$i  \&lt;br /&gt;
    r.null tmp_$i setnull=0 \&lt;br /&gt;
    echo 1=$i | r.reclass in=tmp_$i out=tmp_reclass_$i&lt;br /&gt;
done &amp;lt; cross_points.txt&lt;br /&gt;
echo &amp;quot;Created $i watersheds&amp;quot;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Combining watersheds into one patched vector ===&lt;br /&gt;
Next we patch together all the reclassed rasters (watersheds), convert to vector and clean the merged watersheds vector.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
r.patch in=`g.mlist rast pattern=tmp_reclass* separator=,` out=wshed_patch&lt;br /&gt;
r.to.vect in=wshed_patch out=wshed_patch feature=area&lt;br /&gt;
# Use v.clean to remove tiny areas (that were a string of single cells in the raster)&lt;br /&gt;
v.clean wshed_patch out=wshed_final tool=rmarea thresh=150&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Choose an appropriate threshold value based on your region resolution.  With a region resolution of 10, each individual cell will be 100 sqm, so choosing 150 as the threshold for v.clean allows removing these small areas. Additional manual cleaning may be required. &lt;br /&gt;
&lt;br /&gt;
Clean up tmp rasters:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
g.remove rast=`g.mlist pattern=tmp* sep=,`&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Most likely we'll want to calculate the area for each watershed.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Add a table with a column for area in sq.km.&lt;br /&gt;
v.db.addcol map=wshed_final col=&amp;quot;area_sqkm double&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Use unit=k(ilometers) to get area in sq. km.&lt;br /&gt;
v.to.db map=wshed_final option=area col=area_sqkm unit=k&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And finally, we can view the catchments, and their area values (you may use the [[wxGUI]]):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
d.vect wshed_final type=boundary,centroid display=shape,attr attrcol=area_sqkm size=0 width=3 color=orange&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category: FAQ]]&lt;br /&gt;
[[Category: Documentation]]&lt;br /&gt;
[[Category: Hydrology]]&lt;/div&gt;</summary>
		<author><name>⚠️Pmav99</name></author>
	</entry>
	<entry>
		<id>https://grasswiki.osgeo.org/w/index.php?title=Creating_watersheds&amp;diff=22126</id>
		<title>Creating watersheds</title>
		<link rel="alternate" type="text/html" href="https://grasswiki.osgeo.org/w/index.php?title=Creating_watersheds&amp;diff=22126"/>
		<updated>2015-12-07T20:43:29Z</updated>

		<summary type="html">&lt;p&gt;⚠️Pmav99: /* Display first results */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Arcview users, needing to delineate watersheds and stream networks, choose the extension called  &amp;quot;Arc Hydro&amp;quot; (requires at least Spatial Analyst). This extension introduces the concepts of &amp;quot;Batch Points&amp;quot; and &amp;quot;Adjoint Catchments&amp;quot;.  Batch points are locations that the user defines as drainage outlets. An adjoint catchment is the collection of all raster cells that drain into one of the batch points. Here we'll demonstrate how to get similar results with GRASS GIS 6. &lt;br /&gt;
&lt;br /&gt;
== Creating watersheds with specific drainage outlets==&lt;br /&gt;
As an example, we'll create a set of catchments with their drainage outlets exactly at the points where the streams cross a road. We'll assume our starting data includes an elevation raster called &amp;quot;dem&amp;quot; and a line vector called &amp;quot;roads&amp;quot;.  We first create the regular hydrology layers.&lt;br /&gt;
&lt;br /&gt;
=== Preparation ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#set the region to the dem raster, and run the r.watershed module.&lt;br /&gt;
g.region -p rast=dem&lt;br /&gt;
&lt;br /&gt;
# threshold in map cells (try 10000 as a start). Note: DEM Sink-filling not needed:&lt;br /&gt;
r.watershed elev=dem drain=fdir basin=catch stream=str thresh=&amp;lt;your-threshold&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So far, pretty straightforward. There's abundant information on {{cmd|r.watershed}}. I'll just mention that the threshold value is the number of &amp;lt;u&amp;gt;cells&amp;lt;/u&amp;gt; that will be the minimum catchment size. So if the resolution of our dem raster is, for example, 10x10 meters (each cell=100 sq. meters), then a threshold of 20,000 (=2,000,000 sq. meters) would create catchments of at least 2 sq. kilometers.&lt;br /&gt;
&lt;br /&gt;
=== Display first results ===&lt;br /&gt;
When the process finishes we'll have three new raster maps: the flow direction map, the streams and the catchments. Let's see what we've got so far:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#Convert the steams and catchments to vectors&lt;br /&gt;
# r.to.vect in=catch out=catchments feature=area        # Grass 6&lt;br /&gt;
r.to.vect in=catch out=catchments type=area             # Grass 7&lt;br /&gt;
&lt;br /&gt;
# the stream raster usually requires thinning&lt;br /&gt;
r.thin in=str out=str_thin&lt;br /&gt;
# If you are using GRASS 6, then you should use the following line&lt;br /&gt;
# r.to.vect in=str_thin out=streams feature=line        # Grass 6&lt;br /&gt;
r.to.vect in=str_thin out=streams type=line             # Grass 7&lt;br /&gt;
r.colors dem col=elevation&lt;br /&gt;
&lt;br /&gt;
# Make a hillshade raster for displaying &amp;quot;3D&amp;quot; &lt;br /&gt;
# r.shaded.relief map=dem shade=dem_shade zmult=1.5     # Grass 6&lt;br /&gt;
r.relief input=dem output=dem_shade zscale=1.5          # Grass 7&lt;br /&gt;
&lt;br /&gt;
# Now display layers&lt;br /&gt;
# d.mon x0          # Grass 6&lt;br /&gt;
d.mon start=wx0     # Grass 7&lt;br /&gt;
d.his h=dem i=dem_shade&lt;br /&gt;
d.vect map=streams color=blue width=3&lt;br /&gt;
d.vect map=catchments type=boundary color=red&lt;br /&gt;
d.vect roads color=black width=2&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Determine drainage points ===&lt;br /&gt;
Now we need to find all the points where streams cross roads. The {{cmd|v.overlay}} module does not deal with point vectors (hint: {{cmd|v.select}} does). Instead we use a trick in v.clean. When cleaning a line vector, all points where lines cross and no node exists are considered topological &amp;quot;errors&amp;quot; and can be saved to a new point vector. So by merging the roads and streams vectors, we create a vector with lines (streams) crossing other lines (roads) without a node. Then we run {{cmd|v.clean}}, and we get all those intersection points in a new vector.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Patch the streams and roads vectors together&lt;br /&gt;
v.patch in=streams,roads out=streams_roads&lt;br /&gt;
v.clean in=streams_roads out=streams_roads_clean tool=break error=cross_points&lt;br /&gt;
&lt;br /&gt;
#View cross points on display&lt;br /&gt;
d.vect cross_points icon=basic/circle color=green size=12&lt;br /&gt;
&lt;br /&gt;
# Save crossing points to a text file&lt;br /&gt;
v.out.ascii in=cross_points out=cross_points.txt format=point fs=space&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Looping thru drainage outlet points ===&lt;br /&gt;
Once we have the crossing points in a file, we simply run {{cmd|r.water.outlet}} in a loop to create a watershed for each cross point. However the raster result of r.water.outlet has value '1' in each cell that is upstream of the drainage point, and '0' everywhere else. For our purposes, we want to patch the rasters together after running the loop, so we need to have '''null values''' outside of the watersheds, and each watershed must use a '''different value''' in the upstream cells for its drainage point. To achieve these results, we use the r.null module to set '0' value cells to null. Then, we take advantage of the {{cmd|r.reclass}} function to make a reclassed raster with different values for each watershed.  Here's how it works:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
i=0   #an iterator to give consecutive names to the new watersheds&lt;br /&gt;
while read X Y; do \&lt;br /&gt;
    i=$(( ${i} + 1 ))  \&lt;br /&gt;
    r.water.outlet drain=fdir east=$X north=$Y basin=tmp_$i  \&lt;br /&gt;
    r.null tmp_$i setnull=0 \&lt;br /&gt;
    echo 1=$i | r.reclass in=tmp_$i out=tmp_reclass_$i&lt;br /&gt;
done &amp;lt; cross_points.txt&lt;br /&gt;
echo &amp;quot;Created $i watersheds&amp;quot;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Combining watersheds into one patched vector ===&lt;br /&gt;
Next we patch together all the reclassed rasters (watersheds), convert to vector and clean the merged watersheds vector.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
r.patch in=`g.mlist rast pattern=tmp_reclass* separator=,` out=wshed_patch&lt;br /&gt;
r.to.vect in=wshed_patch out=wshed_patch feature=area&lt;br /&gt;
# Use v.clean to remove tiny areas (that were a string of single cells in the raster)&lt;br /&gt;
v.clean wshed_patch out=wshed_final tool=rmarea thresh=150&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Choose an appropriate threshold value based on your region resolution.  With a region resolution of 10, each individual cell will be 100 sqm, so choosing 150 as the threshold for v.clean allows removing these small areas. Additional manual cleaning may be required. &lt;br /&gt;
&lt;br /&gt;
Clean up tmp rasters:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
g.remove rast=`g.mlist pattern=tmp* sep=,`&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Most likely we'll want to calculate the area for each watershed.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Add a table with a column for area in sq.km.&lt;br /&gt;
v.db.addcol map=wshed_final col=&amp;quot;area_sqkm double&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Use unit=k(ilometers) to get area in sq. km.&lt;br /&gt;
v.to.db map=wshed_final option=area col=area_sqkm unit=k&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And finally, we can view the catchments, and their area values (you may use the [[wxGUI]]):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
d.vect wshed_final type=boundary,centroid display=shape,attr attrcol=area_sqkm size=0 width=3 color=orange&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category: FAQ]]&lt;br /&gt;
[[Category: Documentation]]&lt;br /&gt;
[[Category: Hydrology]]&lt;/div&gt;</summary>
		<author><name>⚠️Pmav99</name></author>
	</entry>
	<entry>
		<id>https://grasswiki.osgeo.org/w/index.php?title=Creating_watersheds&amp;diff=22125</id>
		<title>Creating watersheds</title>
		<link rel="alternate" type="text/html" href="https://grasswiki.osgeo.org/w/index.php?title=Creating_watersheds&amp;diff=22125"/>
		<updated>2015-12-07T20:39:23Z</updated>

		<summary type="html">&lt;p&gt;⚠️Pmav99: /* Display first results */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Arcview users, needing to delineate watersheds and stream networks, choose the extension called  &amp;quot;Arc Hydro&amp;quot; (requires at least Spatial Analyst). This extension introduces the concepts of &amp;quot;Batch Points&amp;quot; and &amp;quot;Adjoint Catchments&amp;quot;.  Batch points are locations that the user defines as drainage outlets. An adjoint catchment is the collection of all raster cells that drain into one of the batch points. Here we'll demonstrate how to get similar results with GRASS GIS 6. &lt;br /&gt;
&lt;br /&gt;
== Creating watersheds with specific drainage outlets==&lt;br /&gt;
As an example, we'll create a set of catchments with their drainage outlets exactly at the points where the streams cross a road. We'll assume our starting data includes an elevation raster called &amp;quot;dem&amp;quot; and a line vector called &amp;quot;roads&amp;quot;.  We first create the regular hydrology layers.&lt;br /&gt;
&lt;br /&gt;
=== Preparation ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#set the region to the dem raster, and run the r.watershed module.&lt;br /&gt;
g.region -p rast=dem&lt;br /&gt;
&lt;br /&gt;
# threshold in map cells (try 10000 as a start). Note: DEM Sink-filling not needed:&lt;br /&gt;
r.watershed elev=dem drain=fdir basin=catch stream=str thresh=&amp;lt;your-threshold&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So far, pretty straightforward. There's abundant information on {{cmd|r.watershed}}. I'll just mention that the threshold value is the number of &amp;lt;u&amp;gt;cells&amp;lt;/u&amp;gt; that will be the minimum catchment size. So if the resolution of our dem raster is, for example, 10x10 meters (each cell=100 sq. meters), then a threshold of 20,000 (=2,000,000 sq. meters) would create catchments of at least 2 sq. kilometers.&lt;br /&gt;
&lt;br /&gt;
=== Display first results ===&lt;br /&gt;
When the process finishes we'll have three new raster maps: the flow direction map, the streams and the catchments. Let's see what we've got so far:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#Convert the steams and catchments to vectors&lt;br /&gt;
# If you are using GRASS 6, then you should use the following line&lt;br /&gt;
#     r.to.vect in=catch out=catchments feature=area&lt;br /&gt;
r.to.vect in=catch out=catchments type=area&lt;br /&gt;
&lt;br /&gt;
# the stream raster usually requires thinning&lt;br /&gt;
r.thin in=str out=str_thin&lt;br /&gt;
# If you are using GRASS 6, then you should use the following line&lt;br /&gt;
#     r.to.vect in=str_thin out=streams feature=line&lt;br /&gt;
r.to.vect in=str_thin out=streams type=line&lt;br /&gt;
r.colors dem col=elevation&lt;br /&gt;
&lt;br /&gt;
# Make a hillshade raster for displaying &amp;quot;3D&amp;quot; &lt;br /&gt;
# If you are using GRASS 6, then you should use the following line&lt;br /&gt;
#     r.shaded.relief map=dem shade=dem_shade zmult=1.5&lt;br /&gt;
r.relief input=dem output=dem_shade zscale=1.5&lt;br /&gt;
&lt;br /&gt;
# Now display layers&lt;br /&gt;
d.mon x0&lt;br /&gt;
d.his h=dem i=dem_shade&lt;br /&gt;
d.vect map=streams color=blue width=3&lt;br /&gt;
d.vect map=catchments type=boundary color=red&lt;br /&gt;
d.vect roads color=black width=2&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Determine drainage points ===&lt;br /&gt;
Now we need to find all the points where streams cross roads. The {{cmd|v.overlay}} module does not deal with point vectors (hint: {{cmd|v.select}} does). Instead we use a trick in v.clean. When cleaning a line vector, all points where lines cross and no node exists are considered topological &amp;quot;errors&amp;quot; and can be saved to a new point vector. So by merging the roads and streams vectors, we create a vector with lines (streams) crossing other lines (roads) without a node. Then we run {{cmd|v.clean}}, and we get all those intersection points in a new vector.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Patch the streams and roads vectors together&lt;br /&gt;
v.patch in=streams,roads out=streams_roads&lt;br /&gt;
v.clean in=streams_roads out=streams_roads_clean tool=break error=cross_points&lt;br /&gt;
&lt;br /&gt;
#View cross points on display&lt;br /&gt;
d.vect cross_points icon=basic/circle color=green size=12&lt;br /&gt;
&lt;br /&gt;
# Save crossing points to a text file&lt;br /&gt;
v.out.ascii in=cross_points out=cross_points.txt format=point fs=space&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Looping thru drainage outlet points ===&lt;br /&gt;
Once we have the crossing points in a file, we simply run {{cmd|r.water.outlet}} in a loop to create a watershed for each cross point. However the raster result of r.water.outlet has value '1' in each cell that is upstream of the drainage point, and '0' everywhere else. For our purposes, we want to patch the rasters together after running the loop, so we need to have '''null values''' outside of the watersheds, and each watershed must use a '''different value''' in the upstream cells for its drainage point. To achieve these results, we use the r.null module to set '0' value cells to null. Then, we take advantage of the {{cmd|r.reclass}} function to make a reclassed raster with different values for each watershed.  Here's how it works:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
i=0   #an iterator to give consecutive names to the new watersheds&lt;br /&gt;
while read X Y; do \&lt;br /&gt;
    i=$(( ${i} + 1 ))  \&lt;br /&gt;
    r.water.outlet drain=fdir east=$X north=$Y basin=tmp_$i  \&lt;br /&gt;
    r.null tmp_$i setnull=0 \&lt;br /&gt;
    echo 1=$i | r.reclass in=tmp_$i out=tmp_reclass_$i&lt;br /&gt;
done &amp;lt; cross_points.txt&lt;br /&gt;
echo &amp;quot;Created $i watersheds&amp;quot;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Combining watersheds into one patched vector ===&lt;br /&gt;
Next we patch together all the reclassed rasters (watersheds), convert to vector and clean the merged watersheds vector.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
r.patch in=`g.mlist rast pattern=tmp_reclass* separator=,` out=wshed_patch&lt;br /&gt;
r.to.vect in=wshed_patch out=wshed_patch feature=area&lt;br /&gt;
# Use v.clean to remove tiny areas (that were a string of single cells in the raster)&lt;br /&gt;
v.clean wshed_patch out=wshed_final tool=rmarea thresh=150&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Choose an appropriate threshold value based on your region resolution.  With a region resolution of 10, each individual cell will be 100 sqm, so choosing 150 as the threshold for v.clean allows removing these small areas. Additional manual cleaning may be required. &lt;br /&gt;
&lt;br /&gt;
Clean up tmp rasters:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
g.remove rast=`g.mlist pattern=tmp* sep=,`&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Most likely we'll want to calculate the area for each watershed.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Add a table with a column for area in sq.km.&lt;br /&gt;
v.db.addcol map=wshed_final col=&amp;quot;area_sqkm double&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Use unit=k(ilometers) to get area in sq. km.&lt;br /&gt;
v.to.db map=wshed_final option=area col=area_sqkm unit=k&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And finally, we can view the catchments, and their area values (you may use the [[wxGUI]]):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
d.vect wshed_final type=boundary,centroid display=shape,attr attrcol=area_sqkm size=0 width=3 color=orange&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category: FAQ]]&lt;br /&gt;
[[Category: Documentation]]&lt;br /&gt;
[[Category: Hydrology]]&lt;/div&gt;</summary>
		<author><name>⚠️Pmav99</name></author>
	</entry>
	<entry>
		<id>https://grasswiki.osgeo.org/w/index.php?title=Creating_watersheds&amp;diff=22124</id>
		<title>Creating watersheds</title>
		<link rel="alternate" type="text/html" href="https://grasswiki.osgeo.org/w/index.php?title=Creating_watersheds&amp;diff=22124"/>
		<updated>2015-12-07T20:36:56Z</updated>

		<summary type="html">&lt;p&gt;⚠️Pmav99: /* Display first results */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Arcview users, needing to delineate watersheds and stream networks, choose the extension called  &amp;quot;Arc Hydro&amp;quot; (requires at least Spatial Analyst). This extension introduces the concepts of &amp;quot;Batch Points&amp;quot; and &amp;quot;Adjoint Catchments&amp;quot;.  Batch points are locations that the user defines as drainage outlets. An adjoint catchment is the collection of all raster cells that drain into one of the batch points. Here we'll demonstrate how to get similar results with GRASS GIS 6. &lt;br /&gt;
&lt;br /&gt;
== Creating watersheds with specific drainage outlets==&lt;br /&gt;
As an example, we'll create a set of catchments with their drainage outlets exactly at the points where the streams cross a road. We'll assume our starting data includes an elevation raster called &amp;quot;dem&amp;quot; and a line vector called &amp;quot;roads&amp;quot;.  We first create the regular hydrology layers.&lt;br /&gt;
&lt;br /&gt;
=== Preparation ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#set the region to the dem raster, and run the r.watershed module.&lt;br /&gt;
g.region -p rast=dem&lt;br /&gt;
&lt;br /&gt;
# threshold in map cells (try 10000 as a start). Note: DEM Sink-filling not needed:&lt;br /&gt;
r.watershed elev=dem drain=fdir basin=catch stream=str thresh=&amp;lt;your-threshold&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So far, pretty straightforward. There's abundant information on {{cmd|r.watershed}}. I'll just mention that the threshold value is the number of &amp;lt;u&amp;gt;cells&amp;lt;/u&amp;gt; that will be the minimum catchment size. So if the resolution of our dem raster is, for example, 10x10 meters (each cell=100 sq. meters), then a threshold of 20,000 (=2,000,000 sq. meters) would create catchments of at least 2 sq. kilometers.&lt;br /&gt;
&lt;br /&gt;
=== Display first results ===&lt;br /&gt;
When the process finishes we'll have three new raster maps: the flow direction map, the streams and the catchments. Let's see what we've got so far:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#Convert the steams and catchments to vectors&lt;br /&gt;
r.to.vect in=catch out=catchments feature=area&lt;br /&gt;
&lt;br /&gt;
# the stream raster usually requires thinning&lt;br /&gt;
r.thin in=str out=str_thin&lt;br /&gt;
r.to.vect in=str_thin out=streams feature=line&lt;br /&gt;
r.colors dem col=elevation&lt;br /&gt;
&lt;br /&gt;
# Make a hillshade raster for displaying &amp;quot;3D&amp;quot; &lt;br /&gt;
# If you are using GRASS 6, then uncomment the following line&lt;br /&gt;
#r.shaded.relief map=dem shade=dem_shade zmult=1.5&lt;br /&gt;
r.relief input=dem output=dem_shade zscale=1.5&lt;br /&gt;
&lt;br /&gt;
# Now display layers&lt;br /&gt;
d.mon x0&lt;br /&gt;
d.his h=dem i=dem_shade&lt;br /&gt;
d.vect map=streams color=blue width=3&lt;br /&gt;
d.vect map=catchments type=boundary color=red&lt;br /&gt;
d.vect roads color=black width=2&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Determine drainage points ===&lt;br /&gt;
Now we need to find all the points where streams cross roads. The {{cmd|v.overlay}} module does not deal with point vectors (hint: {{cmd|v.select}} does). Instead we use a trick in v.clean. When cleaning a line vector, all points where lines cross and no node exists are considered topological &amp;quot;errors&amp;quot; and can be saved to a new point vector. So by merging the roads and streams vectors, we create a vector with lines (streams) crossing other lines (roads) without a node. Then we run {{cmd|v.clean}}, and we get all those intersection points in a new vector.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Patch the streams and roads vectors together&lt;br /&gt;
v.patch in=streams,roads out=streams_roads&lt;br /&gt;
v.clean in=streams_roads out=streams_roads_clean tool=break error=cross_points&lt;br /&gt;
&lt;br /&gt;
#View cross points on display&lt;br /&gt;
d.vect cross_points icon=basic/circle color=green size=12&lt;br /&gt;
&lt;br /&gt;
# Save crossing points to a text file&lt;br /&gt;
v.out.ascii in=cross_points out=cross_points.txt format=point fs=space&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Looping thru drainage outlet points ===&lt;br /&gt;
Once we have the crossing points in a file, we simply run {{cmd|r.water.outlet}} in a loop to create a watershed for each cross point. However the raster result of r.water.outlet has value '1' in each cell that is upstream of the drainage point, and '0' everywhere else. For our purposes, we want to patch the rasters together after running the loop, so we need to have '''null values''' outside of the watersheds, and each watershed must use a '''different value''' in the upstream cells for its drainage point. To achieve these results, we use the r.null module to set '0' value cells to null. Then, we take advantage of the {{cmd|r.reclass}} function to make a reclassed raster with different values for each watershed.  Here's how it works:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
i=0   #an iterator to give consecutive names to the new watersheds&lt;br /&gt;
while read X Y; do \&lt;br /&gt;
    i=$(( ${i} + 1 ))  \&lt;br /&gt;
    r.water.outlet drain=fdir east=$X north=$Y basin=tmp_$i  \&lt;br /&gt;
    r.null tmp_$i setnull=0 \&lt;br /&gt;
    echo 1=$i | r.reclass in=tmp_$i out=tmp_reclass_$i&lt;br /&gt;
done &amp;lt; cross_points.txt&lt;br /&gt;
echo &amp;quot;Created $i watersheds&amp;quot;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Combining watersheds into one patched vector ===&lt;br /&gt;
Next we patch together all the reclassed rasters (watersheds), convert to vector and clean the merged watersheds vector.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
r.patch in=`g.mlist rast pattern=tmp_reclass* separator=,` out=wshed_patch&lt;br /&gt;
r.to.vect in=wshed_patch out=wshed_patch feature=area&lt;br /&gt;
# Use v.clean to remove tiny areas (that were a string of single cells in the raster)&lt;br /&gt;
v.clean wshed_patch out=wshed_final tool=rmarea thresh=150&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Choose an appropriate threshold value based on your region resolution.  With a region resolution of 10, each individual cell will be 100 sqm, so choosing 150 as the threshold for v.clean allows removing these small areas. Additional manual cleaning may be required. &lt;br /&gt;
&lt;br /&gt;
Clean up tmp rasters:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
g.remove rast=`g.mlist pattern=tmp* sep=,`&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Most likely we'll want to calculate the area for each watershed.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Add a table with a column for area in sq.km.&lt;br /&gt;
v.db.addcol map=wshed_final col=&amp;quot;area_sqkm double&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Use unit=k(ilometers) to get area in sq. km.&lt;br /&gt;
v.to.db map=wshed_final option=area col=area_sqkm unit=k&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And finally, we can view the catchments, and their area values (you may use the [[wxGUI]]):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
d.vect wshed_final type=boundary,centroid display=shape,attr attrcol=area_sqkm size=0 width=3 color=orange&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category: FAQ]]&lt;br /&gt;
[[Category: Documentation]]&lt;br /&gt;
[[Category: Hydrology]]&lt;/div&gt;</summary>
		<author><name>⚠️Pmav99</name></author>
	</entry>
</feed>