[GRASS-user] Returning values to Python

Hi

Firstly I apologize if this is a duplicate, but I am not getting my
own posts emailed to me, and I don't see them in the list archive, so
I am using another email account.

How does one return attribute values from a call to the 'r.what'
module running in a python script?

When this query executes, it prints the result to the terminal, but I
need to get the value back in the script.

One of the documentation pages mentioned a function "stdout2dict" from
"grass.pygrass.modules", but it does not look like this is in the
pygrass package any longer?

Many thanks, Kevin

Kevin Williams wrote:

Firstly I apologize if this is a duplicate, but I am not getting my
own posts emailed to me, and I don't see them in the list archive, so
I am using another email account.

How does one return attribute values from a call to the 'r.what'
module running in a python script?

When this query executes, it prints the result to the terminal, but I
need to get the value back in the script.

One of the documentation pages mentioned a function "stdout2dict" from
"grass.pygrass.modules", but it does not look like this is in the
pygrass package any longer?

If you use grass.script.raster_what(), it returns a list of
dictionaries.

AFAICT, PyGRASS requires you to add stdout_=PIPE, then you can get the
output as a string from module.outputs["stdout"].value.

--
Glynn Clements <glynn@gclements.plus.com>

On Thu, Oct 30, 2014 at 8:04 AM, Glynn Clements
<glynn@gclements.plus.com> wrote:

One of the documentation pages mentioned a function "stdout2dict" from
"grass.pygrass.modules", but it does not look like this is in the
pygrass package any longer?

If you use grass.script.raster_what(), it returns a list of
dictionaries.

AFAICT, PyGRASS requires you to add stdout_=PIPE, then you can get the
output as a string from module.outputs["stdout"].value.

or simplier: module.outputs.stdout

or using directly the C API through python with:

{{{
from grass.pygrass.vector import VectorTopo
from grass.pygrass.raster import RasterRow
from grass.pygrass.gis.region import Region

with RasterRow('elevation', mode='r') as rast:
    with VectorTopo('hospitals', mode='r') as hospitals:
        region = Region()
        for hosp in hospitals:
            value = rast.get_value(hosp, region)
            if value is not None:
                print(hosp.cat, value)
}}}

Pietro

Thank you all!

Cheers, Kevin

On 30 October 2014 10:38, Pietro <peter.zamb@gmail.com> wrote:

On Thu, Oct 30, 2014 at 8:04 AM, Glynn Clements
<glynn@gclements.plus.com> wrote:

One of the documentation pages mentioned a function "stdout2dict" from
"grass.pygrass.modules", but it does not look like this is in the
pygrass package any longer?

If you use grass.script.raster_what(), it returns a list of
dictionaries.

AFAICT, PyGRASS requires you to add stdout_=PIPE, then you can get the
output as a string from module.outputs["stdout"].value.

or simplier: module.outputs.stdout

or using directly the C API through python with:

{{{
from grass.pygrass.vector import VectorTopo
from grass.pygrass.raster import RasterRow
from grass.pygrass.gis.region import Region

with RasterRow('elevation', mode='r') as rast:
    with VectorTopo('hospitals', mode='r') as hospitals:
        region = Region()
        for hosp in hospitals:
            value = rast.get_value(hosp, region)
            if value is not None:
                print(hosp.cat, value)
}}}

Pietro