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

Hi!

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.

The idea is to feed later on v.what.vect with a "dmax=" as this seems to
reduce the eXtremely slow "v.distance" process as suggested by MoritzL in [1]
("v.distance" is running for 2 days here over large files[*]!).

I timed two different versions of v.what.vect (with and without "dmax=") and
it seems to make a difference (tested however in small spearfish vector data).
All this as part of a horrible script of mine [2]. I feel however that this
fits as an enhancement to "v.what.vect" itself.

Regards, Nikos
---

[1] <http://lists.osgeo.org/pipermail/grass-user/2009-May/050330.html&gt;

[*] e.g. first result after >10h:

Finding nearest lines...
Finding nearest areas...
404347 categories read from the map
404347 categories exist in the table
404347 categories read from the map exist in the table
404347 records updated
...

[2] Script "pareto_3" within the tarred attached file in
<http://trac.osgeo.org/grass/ticket/804&gt;

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

For your task the use of lists and the split tool may help.
The different lines have are seperated by a '\n' (which is interpreted as a
new line). After splitting the string into a line list you can make a loop
each item of this list. Something like this:

output='7839|16\n7839|17\n7839|22\n7839|23\n7839|24'

seccond_column_list=
max_value=0

line_list=output.split('\n')

for line in line_list:
  columns=line.split('|')
  seccond_column_list.append(float(columns[1]))

max_value=max(seccond_column_list)

print seccond_column_list
print max_value

I may missunderstood your question but I hope this helps.
--
View this message in context: http://osgeo-org.1803224.n2.nabble.com/Feed-and-manipulate-the-result-of-v-distance-pa-in-python-tp5371195p5372457.html
Sent from the Grass - Users mailing list archive at Nabble.com.

On Wednesday 04 of August 2010 15:20:58 schorschli wrote:

For your task the use of lists and the split tool may help.
The different lines have are seperated by a '\n' (which is interpreted as
a new line). After splitting the string into a line list you can make a
loop each item of this list. Something like this:

output='7839|16\n7839|17\n7839|22\n7839|23\n7839|24'

seccond_column_list=
max_value=0

line_list=output.split('\n')

for line in line_list:
  columns=line.split('|')
  seccond_column_list.append(float(columns[1]))

max_value=max(seccond_column_list)

print seccond_column_list
print max_value

I may missunderstood your question but I hope this helps.

Thanks Hamish and Matthias.

Here one (more) solution:

# get distances from v.distance -pa
distances = grass.read_command("v.distance",\
flags = 'pa',\
_from = reference_points_map,\
to = lowres_vector_grid,\
column = gridcell_column,\
to_column = "cat",\
upload = "to_attr")

# get max distance
max_distance=max([float(d.split('|')[1]) for d in distances.splitlines()
[1:]])

(Thanks to Aggelos Nikolaou for the solution. It's something trivial but when
_not_ working all-the-time with python looks difficult in the beginning.)

Nikos

Hi,

2010/8/6 Nikos Alexandris <nikos.alexandris@felis.uni-freiburg.de>:

distances = grass.read_command("v.distance",\
flags = 'pa',\
_from = reference_points_map,\
to = lowres_vector_grid,\
column = gridcell_column,\
to_column = "cat",\
upload = "to_attr")

btw, there is no need for '\' at the of the line.

Martin

--
Martin Landa <landa.martin gmail.com> * http://gama.fsv.cvut.cz/~landa

Nikos Alexandris:

> distances = grass.read_command("v.distance",\
> flags = 'pa',\
> _from = reference_points_map,\
> to = lowres_vector_grid,\
> column = gridcell_column,\
> to_column = "cat",\
> upload = "to_attr")

Martin Landa wrote:

btw, there is no need for '\' at the of the line.

Thanks, Martin. Good to know. That's my bash-habit :wink:

Nikos