[GRASS-user] Trying to correct river network

Dear friends,

I derived stream network from r.watershed and converted it to vector.

I found that there are a number of small lines. And they are not merging into long lines.

Reason: Some line segments have different directions.

Now, in a river network direction should be from upstream to downstream.

i.e. at every junction, there should be exactly and only one start point and other points should be end points.

I utilized this algorithm and wrote this script.

Initially, it gave syntax errors.

Now , there are no errors but no results either.

script

elcat=4907;
for j in $elcat
do
cats=echo "select cat from gis_schema.riy3 where start_long=(select start_long from gis_schema.riy3 where cat=$elcat) and start_lat=(select start_lat from gis_schema.riy3 where cat=$elcat) and cat <> $elcat; " | db.select -c;
for i in $cats
do
v.edit map=riy3@work layer=1 tool=flip cats=$cats;
sl=echo "select start_long from gis_schema.riy3 where cat=$cats;" | db.select -c;
slat=echo "select start_lat from gis_schema.riy3 where cat=$cats;" | db.select -c;
el=echo "select end_long from gis_schema.riy3 where cat=$cats;" | db.select -c;
elat=echo "select end_lat from gis_schema.riy3 where cat=$cats;" | db.select -c;
v.db.update map=riy3@work layer=1 column=start_long value=$el where=“cat=$cats”;
v.db.update map=riy3@work layer=1 column=start_lat value=$elat where=“cat=$cats”;
v.db.update map=riy3@work layer=1 column=end_long value=$sl where=“cat=$cats”;
v.db.update map=riy3@work layer=1 column=end_lat value=$slat where=“cat=$cats”;
done
elcat=echo "select cat from gis_schema.riy3 where end_long=(select start_long from gis_schema.riy3 where cat=$elcat) and end_lat=(select start_lat from gis_schema.riy3 where cat=$elcat); " | db.select -c ;
done

end of script

I ran this from grass command shell.

It ends silently.

Can someone help please.

Dear friends,

Some days back , I had problems with a script used to correct river network.
With help from a database expert and friend, the script worked perfectly for me.
With thought that it may help, correct script is posted below:

elcat=4907;
echo “delete from gis_schema.cat_temp;” > file.sql;
db.execute input=file.sql;
echo “delete from gis_schema.cat_temp_done;” > file.sql;
db.execute input=file.sql;
while [ $elcat ]
do
cats=echo "select cat from gis_schema.riy4 where start_long=(select start_long from gis_schema.riy4 where cat=$elcat) and start_lat=(select start_lat from gis_schema.riy4 where cat=$elcat) and cat <> $elcat; " | db.select -c;
for i in $cats
do
echo $cats;
v.edit map=riy4@work layer=1 tool=flip cats=$i;
sl=echo "select start_long from gis_schema.riy4 where cat=$i;" | db.select -c;
slat=echo "select start_lat from gis_schema.riy4 where cat=$i;" | db.select -c;
el=echo "select end_long from gis_schema.riy4 where cat=$i;" | db.select -c;
elat=echo "select end_lat from gis_schema.riy4 where cat=$i;" | db.select -c;
v.db.update map=riy4@work layer=1 column=start_long value=$el where=“cat=$i”;
v.db.update map=riy4@work layer=1 column=start_lat value=$elat where=“cat=$i”;
v.db.update map=riy4@work layer=1 column=end_long value=$sl where=“cat=$i”;
v.db.update map=riy4@work layer=1 column=end_lat value=$slat where=“cat=$i”;
echo " $i loop1"
done
echo " This value $elcat will be used to calculate cats_to_write" ;
echo “insert into gis_schema.cat_temp_done (cat) values($elcat);” > file.sql;
db.execute input=file.sql;
cats_to_write=echo "select cat from gis_schema.riy4 where end_long=(select start_long from gis_schema.riy4 where cat=$elcat) and end_lat=(select start_lat from gis_schema.riy4 where cat=$elcat) and cat not in (select cat from gis_schema.cat_temp_done); " | db.select -c;
echo $cats_to_write;
echo “delete from gis_schema.cat_temp where cat= $elcat;” > file.sql;
db.execute input=file.sql;
echo " After populating cats_to_write this value $elcat is deleted in loop2"
for k in $cats_to_write
do
echo “insert into gis_schema.cat_temp (cat) values($k);” > file.sql;
db.execute input=file.sql;
done
echo " These values of cats_to_write $k are written in loop2"
elcat=echo "select cat from gis_schema.cat_temp limit 1;" | db.select -c;
echo “from database for next loop $elcat”;
done

Note:
elcat means end line category.
And, please create database links and tables appropriately as mentioned above.