Hi everybody<br>A few weeks ago, a discussion abour how to break a vector line by a vector point was beginned.<br>I create a script in shell to solve this issue in grass 6.0.1. You need the add on v.append and v.out.ascii.db
to launch this script (add on avalaible on <a href="http://grass.gdf-hannover.de/wiki/GRASS_AddOns">http://grass.gdf-hannover.de/wiki/GRASS_AddOns</a>)<br>You choose a vector line and a vector point (the points need to be on the line to cut the lines) and the script create a new intersected vector line
<br>Any comments or suggestions are welcome.<br><br>Cheers <br><br>Mick<br><br> #!/bin/bash<br>#break a vector line by a vector point and create a new vector line<br>#need the Add On v.out.ascii.db and v.append<br>#the different vectors do not have the columns x,y,along,lcat and type
<br>#because this script create these columns and there are no securities for<br>#checking their presence <br>#in line 179 of this script, we delete an esri column because we worked with esri<br>#vector imported in grass format
<br><br>if [ -z "$GISBASE" ]<br>then<br> echo ""<br> echo "You must be in GRASS to launch this script"<br> echo ""<br> exit 1<br>fi<br>eval `g.gisenv`<br>: ${GISDBASE?} ${LOCATION_NAME?} ${MAPSET?}
<br>#verification of the presence of v.out.ascii.db<br>if [ ! -e "/usr/lib/grass/scripts/v.out.ascii.db" ] ; then<br> echo "You need the Add on v.out.ascii.db"<br> echo " Segmentation process exit"
<br> exit<br>fi<br><br><br>#verification of the presence of v.append<br>if [ ! -e "/usr/lib/grass/scripts/v.append" ] ; then<br> echo "You need the Add on v.append "<br> echo " Segmentation process exit"
<br> exit<br>fi<br><br># ask the mapset where the user want to work<br>while test ! $Seg_mapset<br> do<br> echo -n " Name of the mapset for the work : "<br> read Seg_mapset<br> done<br><br>while test ! -d "$GISDBASE/$LOCATION_NAME/$Seg_mapset"
<br>do<br> echo ""<br> echo " the mapset $Seg_mapset doesn't exist!"<br> echo ""<br> echo " Choose an another name"<br> read Seg_mapset<br>done<br><br>g.gisenv set="MAPSET=$Seg_mapset"
<br>echo " We work now in the mapset $Seg_mapset"<br>echo ""<br><br><br>echo "enter the name of the vector line: "<br>read rh<br>#we verify the presence of this vector<br>g.findfile element=vector file="${rh}" > /dev/null
<br> if [ $? -ne 0 ] ; then<br> echo "The object $rh doesn't exist !!"<br> echo "Segmentation process exit"<br> exit<br> fi<br># we verify the type (line) of this vector<br><a href="http://v.info">
v.info</a> map=$rh | grep "lines:" | cut -c30 > /tmp/line<br>lines=`cat /tmp/line`<br> if [ "$lines" = "0" ] ; then<br> echo "The Object $rh doesn't have any lines !!"<br>
echo "Segmentation process exit"<br> exit<br> fi<br><br><br>echo " "<br>echo "The vector line choosen is $rh"<br>echo ""<br>echo "enter the name of the points vector : "
<br>read exut<br>#we verify the presence of this vector<br>g.findfile element=vector file="${exut}" > /dev/null<br> if [ $? -ne 0 ] ; then<br> echo "The Object $exut doesn't exist !!"<br> echo "Segmentation process exit"
<br> exit<br> fi<br># we verify the type (point) of this vector<br><a href="http://v.info">v.info</a> map=$exut | grep "points:" | cut -c30 > /tmp/pt<br>pts=`cat /tmp/pt`<br> if [ "$pts" = "0" ] ; then
<br> echo "The Object $exut doesn't have any points!!"<br> echo "Segmentation process exit"<br> exit<br> fi<br>rm /tmp/line<br>rm /tmp/pt<br><br>echo "The vector point choosen is $exut"
<br>echo ""<br>echo "enter a name for the new vector line: "<br><br>while test ! $rhfin<br> do<br> echo -n "Enter the name: "<br> read rhfin<br> done<br><br>while test -d "$GISDBASE/$LOCATION_NAME/$MAPSET/vector/$rhfin"
<br>do<br> echo ""<br> echo " The Object $rhfin already exist"<br> echo ""<br> echo " Choose an another name"<br> read rhfin<br>done<br><br><br><br>echo "the name for the new vector line is $rhfin"
<br>echo " "<br>echo "***************"<br>echo "add the columns lcat,along,x and y in the vector points"<br>echo "and calcul of the x,y coordinate of these points"<br>echo "ALTER TABLE $exut ADD COLUMN lcat INTEGER"|db.execute
<br>echo "ALTER TABLE $exut ADD COLUMN along DOUBLE"|db.execute<br>echo "ALTER TABLE $exut ADD COLUMN x DOUBLE"|db.execute<br>echo "ALTER TABLE $exut ADD COLUMN y DOUBLE"|db.execute<br><br>v.to.db
map=$exut type=point option=coor units=me column=x,y<br><br>echo " "<br>echo "***************"<br>echo "calcul of the distance between vector line and vector point"<br>#allow to calculate the cat of the line where the point is
<br>v.distance from=$exut to=$rh from_type=point to_type=line upload=cat column=lcat<br>#allow to find the distance entre the beginning of the line and the point<br>v.distance from=$exut to=$rh from_type=point to_type=line upload=to_along column=along
<br><br>echo " "<br>echo "***************"<br>echo "export of the cat of the selected lines"<br><br>v.out.ascii.db input=$exut output=/tmp/segcat columns=lcat<br><br>cut -d"|" -f4 /tmp/segcat>/tmp/segcat2
<br><br>echo " "<br>echo "***************"<br>echo "extract of the selected lines"<br><br>v.extract input=rh output=rh2 type=line file=/tmp/segcat2<br><br>echo " "<br>echo "***************"
<br>echo "export of the points of the selected lines"<br>echo "and calcul of the distance between the beginning of the line and the point, result in layer 2"<br>v.to.points input=rh2 type=line output=rh3
<br><br>echo " "<br>echo "***************"<br>echo "calcul of the x,y coordinate of the points"<br>echo "ALTER TABLE rh3_2 ADD COLUMN x DOUBLE"|db.execute<br>echo "ALTER TABLE rh3_2 ADD COLUMN y DOUBLE"|db.execute
<br>v.to.db map=rh3 type=point layer=2 option=coor units=me column=x,y<br><br>echo " "<br>echo "***************"<br>echo "creation of the column type"<br>echo "ALTER TABLE rh3_2 ADD COLUMN type VARCHAR(10)"|db.execute
<br>echo "ALTER TABLE $exut ADD COLUMN type VARCHAR(10)"|db.execute<br>echo "UPDATE rh3_2 SET type='rh'"|db.execute<br>echo "UPDATE $exut SET type='exut'"|db.execute<br><br>echo " "
<br>echo "***************"<br>echo "export the attributes of the points of lines and of the points"<br>#segcat3: fichier contenant points et attributs des segments rh3<br>db.select -c table=rh3_2>/tmp/segcat3
<br>db.select -c table=$exut>/tmp/segcat4<br>echo "we suppress an esri column "<br>cut -d"|" -f1,3,4,5,6,7 /tmp/segcat4>/tmp/segcat5<br><br><br>echo " "<br>echo "***************"
<br>echo "sum of all the points"<br><br>sort -k2n -t'|' /tmp/segcat3 >/tmp/segcat30<br><br>sort -k2n -t'|' /tmp/segcat5 >/tmp/segcat50<br><br>sort -m -k2n -k3g -t'|' /tmp/segcat30 /tmp/segcat50>/tmp/segcat6
<br><br>cut -d"|" -f2 /tmp/segcat6>/tmp/segcat7<br>uniq /tmp/segcat7>/tmp/segcat8<br><br><br>cut -d"|" -f2,3,4,5,6 /tmp/segcat6>/tmp/segcat9<br><br><br>echo " "<br>echo "***************"
<br>echo "creation of the ascii input file for the creation of the new vector line"<br><br> for k in `cat /tmp/segcat8`;do<br> grep "^$k" /tmp/segcat9 > /tmp/segcat10_"$k"<br> grep -n exut$ /tmp/segcat10_"$k" |cut -d":" -f1 > /tmp/segcat11_"$k"
<br> typeset -i nb<br> nb=`cat /tmp/segcat11_"$k"`<br> head -"$nb" /tmp/segcat10_"$k" > /tmp/segcat20_"$k"<br> echo "`cat /tmp/segcat10_"$k"`" | wc -l > /tmp/segcat13_"$k"
<br> typeset -i nbexut<br> nbexut=`cat /tmp/segcat13_"$k"`<br> typeset -i nb2<br> nb2=`cat /tmp/segcat13_"$k"`-`cat /tmp/segcat11_"$k"`+1<br> <br> tail -"$nb2" /tmp/segcat10_"$k" > /tmp/segcat21_"$k"
<br> <br> touch /tmp/import_"$k"<br> echo "`cat /tmp/segcat20_"$k"`" | wc -l > /tmp/segcat30_"$k"<br> nb20=`cat /tmp/segcat30_"$k"`<br> echo "`cat /tmp/segcat21_"$k"`" | wc -l > /tmp/segcat31_"$k"
<br> nb21=`cat /tmp/segcat31_"$k"`<br><br> cut -d'|' -f3,4 /tmp/segcat20_"$k" | awk -F'|' '{OFS=" ";$1=$1;print " "$0}' > /tmp/segcat40_"$k"<br> cut -d'|' -f3,4 /tmp/segcat21_"$k" | awk -F'|' '{OFS=" ";$1=$1;print " "$0}' > /tmp/segcat41_"$k"
<br> echo "L "$nb20" 1" >>/tmp/import_"$k" <br> echo "`cat /tmp/segcat40_"$k"`" >>/tmp/import_"$k" <br> echo " 1 1">>/tmp/import_"$k"
<br> echo "L "$nb21" 1" >>/tmp/import_"$k" <br> echo "`cat /tmp/segcat41_"$k"`" >>/tmp/import_"$k" <br> echo " 1 2">>/tmp/import_"$k"
<br><br> done<br>#the file import will be the fileascii input<br>touch /tmp/import<br> echo "ORGANIZATION:" >>/tmp/import<br> echo "DIGIT DATE:" >>/tmp/import<br> echo "DIGIT NAME:" >>/tmp/import
<br> echo "MAP NAME:" >>/tmp/import<br> echo "MAP DATE:" >>/tmp/import<br> echo "MAP SCALE:" >>/tmp/import<br> echo "OTHER INFO:" >>/tmp/import<br>
echo "ZONE:" >>/tmp/import<br> echo "MAP THRESH:" >>/tmp/import<br> echo "VERTI:" >>/tmp/import <br> for k in `cat /tmp/segcat8`;do<br> echo "`cat /tmp/import_"$k"`" >>/tmp/import
<br> done<br> rm /tmp/import_*<br> <br><br>echo " "<br>echo "***************"<br>echo "creation of the vector line $rhfin "<br><br>v.in.ascii input=/tmp/import output="$rh"_tmp format=standard
<br>v.category input=$rh option=print > /tmp/cattot<br>sort -k1n /tmp/segcat8 > /tmp/segcat8s<br>diff /tmp/cattot /tmp/segcat8s > /tmp/cattot2<br>grep -v "," /tmp/cattot2 > /tmp/cattot3<br>cut -d" " -f2 /tmp/cattot3 > /tmp/cattot4
<br>v.extract input=$rh output=ext_$rh type=line file=/tmp/cattot4<br><br>rm /tmp/import<br>rm /tmp/cattot*<br><br><br><br>echo "`cat /tmp/segcat8s`"| awk -F'|' '{ORS=") or (cat=";$1=$1;print $0}' > /tmp/segwhere
<br>awk '{print NF}' /tmp/segwhere > /tmp/segwherenb<br>typeset -i nbwhere<br>nbwhere=`cat /tmp/segwherenb`-2<br>cut -d" " -f1-"$nbwhere" /tmp/segwhere > /tmp/segwhere2<br><br>db.copy from_driver=dbf from_database="$GISDBASE/$LOCATION_NAME/$MAPSET/dbf/" from_table="$rh" to_driver=dbf to_database="$GISDBASE/$LOCATION_NAME/$MAPSET/dbf/" to_table="$rh"_tmp where="(cat=`cat /tmp/segwhere2`"
<br>v.db.connect map="$rh"_tmp driver=dbf database="$GISDBASE/$LOCATION_NAME/$MAPSET/dbf/" table="$rh"_tmp key=cat layer=1<br><br>v.category input="$rh"_tmp output="$rh"_tmp2 type=line option=del
<br>v.category input="$rh"_tmp2 output="$rh"_tmp3 type=line option=add<br>#utilisation du module v.append pour merger les deux réseaux <br>v.append vect1="$rh"_tmp3 vect2=ext_$rh vmerged=$rhfin vtype=line vlayer=1
<br><br>rm /tmp/seg*<br>g.remove vect=rh3,rh2,ext_$rh,"$rh"_tmp,"$rh"_tmp2,"$rh"_tmp3<br>echo " "<br>echo "***************"<br>echo "Segmentation process end"<br>
<br>