[GRASS-user] Feed and manipulate the result of "v.distance -pa" in python

Hamish hamish_b at yahoo.com
Wed Aug 4 06:04:27 EDT 2010


Nikos Alexandris wrote:
> I need to convert the following bash-one-liner:
> 
> v.distance --q -pa from=ref_points to=coi_points upload=cat \
>   column=anycol | cut -d"|" -f2 | sort -nu | tail -1
> 
> into python.
> 
> My unsolved problem is that "v.distance -pa" returns for
> example thousands of 
> lines in the following format:
> ...
> 7839|16
> 7839|17
> 7839|22
> 7839|23
> 7839|24
> 
> Reading the main command in python is not the problem
> (although I am a bit 
> confused between grass.pipe, grass.feed, grass.read). Are
> there any examples 
> (scripts in grass trunk maybe?) to follow in order to split
> the output, keep 
> the second "column", sort and keep the maximum? Actually
> the "split" is what 
> worries me most. I 've been looking for quite some time to
> find something that 
> gives me a strait answer but no luck yet.

try v.rast.stats.py:   cats.append(line.rstrip('\r\n').split(';')[0])

that splits on ";" as the sep, and takes the first entry (column counting
starts at 0 not 1 here)

so for you:   line.split('|')[1]

no idea about sorting, but you may be able to program a loop which 
starts out empty and only updates if the line's dmax is bigger that the
one stored in the overall variable's, or if the variable is empty (for
the first).


Hamish



      


More information about the grass-user mailing list