Convert points to lines: Difference between revisions

From GRASS-Wiki
Jump to navigation Jump to search
m (fix template url key)
(Script contributed by getis.diettrich)
 
(5 intermediate revisions by 2 users not shown)
Line 1: Line 1:
'''Q:''' Is there a way to construct a vector line(s) map connecting selected points?
'''Q:''' Is there a way to construct a vector line(s) map connecting selected points?


'''A:''' You can use {{cmd|v.in.lines|version=65}} for that. The input can be generated with {{cmd|v.out.ascii}}.
'''A:''' There are several possibilities:
 
=== Converting points map into lines map ===
 
You can use {{cmd|v.in.lines}} for that. The input can be generated with {{cmd|v.out.ascii}}.


Spearfish example:
Spearfish example:
 
<source lang="bash">
   v.out.ascii archsites fs=, where="cat=1 or cat= 3"
   v.out.ascii archsites fs=, where="cat=1 or cat= 3"
   593493,4914730,1
   593493,4914730,1
Line 10: Line 14:
   
   
   # so:
   # so:
   v.out.ascii archsites fs=, where="cat=1 or cat= 3" | cut -d',' -f1,2 | v.in.lines in=- out=myline fs=,
   v.out.ascii archsites fs=, where="cat=1 or cat= 3" | cut -d',' -f1,2 | \
      v.in.lines in=- out=myline fs=,
   v.category in=myline out=line_with_cat option=add
   v.category in=myline out=line_with_cat option=add
</source>
=== Converting CSV points file into lines map ===
Suppose you have a CSV file "mypoints.csv" containing three point-pairs to be connected (start point coordinates, end point coordinates):
  east1,north1,east2,north2
  593493,4914730,589860,4922000
  590400,4922820,593549,4925500
  600375,4925235,606635,4920773
We can convert these three point pairs into three lines:
<source lang="bash">
  cat mypoints.csv | grep -v "north1" | awk -F',' '{printf "%f,%f\n%f,%f\nNaN,NaN\n",$1, $2 ,$3 ,$4}' > mypoints_formatted.csv
  v.in.lines in=mypoints_formatted.csv out=mylines fs=,
  v.category in=mylines out=lines_with_cat option=add
</source>
=== Script to convert CSV points file into ASCII file for v.in.lines import ===
<source lang="bash">
#!/bin/bash                                   
# letzte Änderung 09.01.2015
# gpx Leitungsdaten für v.in.lines konvertieren       
FILE=test.csv
head -1 $FILE > te3.tmp
N=`cut -c 11-20 te3.tmp`
E=`cut -c 22-31 te3.tmp`
rm *.tmp
awk -v E=$E -v N=$N ' 
BEGIN {
FS=";"
z=0
}
{if(z != 1){
z=z+1
printf("%10.7f\%s\%10.7f\%s\n" \
,E,"|",N,"|")
}
else{
E=$5
N=$4
printf("%10.7f\%s\%10.7f\%s\nNaN|NaN|\n" \
,$5,"|",$4,"|")
z=0
}
}
END{
}' $FILE > formatted_csv.txt
## Now run import:
# v.in.lines in=formatted_csv.txt  out=test
</source>
=== Triangulate the points into a lines map ===
Use {{cmd|v.delaunay}} for that.
== See also ==
* [[Convert vector types]]
* [[Convert lines to areas]]
* [[Convert areas to lines]]


[[Category: FAQ]]
[[Category: FAQ]]
[[Category: Vector]]

Latest revision as of 16:22, 9 January 2015

Q: Is there a way to construct a vector line(s) map connecting selected points?

A: There are several possibilities:

Converting points map into lines map

You can use v.in.lines for that. The input can be generated with v.out.ascii.

Spearfish example:

  v.out.ascii archsites fs=, where="cat=1 or cat= 3"
  593493,4914730,1
  589860,4922000,3
 
  # so:
  v.out.ascii archsites fs=, where="cat=1 or cat= 3" | cut -d',' -f1,2 | \
      v.in.lines in=- out=myline fs=,
  v.category in=myline out=line_with_cat option=add

Converting CSV points file into lines map

Suppose you have a CSV file "mypoints.csv" containing three point-pairs to be connected (start point coordinates, end point coordinates):

 east1,north1,east2,north2
 593493,4914730,589860,4922000
 590400,4922820,593549,4925500
 600375,4925235,606635,4920773

We can convert these three point pairs into three lines:

  cat mypoints.csv | grep -v "north1" | awk -F',' '{printf "%f,%f\n%f,%f\nNaN,NaN\n",$1, $2 ,$3 ,$4}' > mypoints_formatted.csv
  v.in.lines in=mypoints_formatted.csv out=mylines fs=,
  v.category in=mylines out=lines_with_cat option=add

Script to convert CSV points file into ASCII file for v.in.lines import

#!/bin/bash                                    
# letzte Änderung 09.01.2015 
# gpx Leitungsdaten für v.in.lines konvertieren         

FILE=test.csv

head -1 $FILE > te3.tmp
N=`cut -c 11-20 te3.tmp`
E=`cut -c 22-31 te3.tmp`
rm *.tmp

awk -v E=$E -v N=$N '  
BEGIN {
FS=";"
z=0
}
		{if(z != 1){
		z=z+1
		printf("%10.7f\%s\%10.7f\%s\n" \
		,E,"|",N,"|")
		}						
		else{
		E=$5
		N=$4
		printf("%10.7f\%s\%10.7f\%s\nNaN|NaN|\n" \
		,$5,"|",$4,"|")
		z=0
		}
	}
END{ 	
}' $FILE > formatted_csv.txt

## Now run import:
# v.in.lines in=formatted_csv.txt  out=test

Triangulate the points into a lines map

Use v.delaunay for that.

See also