[GRASS-dev] Helpp needed to call Grass from inside Python Plugin

Hi All,

I want to call the grass functionalities from inside my python plugins, in QGIS
For example: the Grass’s raster to polygon script, with out going inside the grass plugin.
Can I set the Grass Mapset, and location from inside my python plugin?
Is this possible?
my configurations are:
QGIS 0.11, GRASS 6.3, python 2.5, Windows

Thank you in advance

On Mon, Mar 22, 2010 at 1:28 PM, Bishwarup Banerjee
<bishwarup.banerjee@gmail.com> wrote:

Hi All,

I want to call the grass functionalities from inside my python plugins, in
QGIS
For example: the Grass's raster to polygon script, with out going inside the
grass plugin.
Can I set the Grass Mapset, and location from inside my python plugin?
Is this possible?

I think yes. See also
http://grass.osgeo.org/wiki/GRASS_and_Python

(@all: please extend the Wiki page!)

my configurations are:
QGIS 0.11, GRASS 6.3, python 2.5, Windows

Note that there are substantial improvements in GRASS 6.4+ for Python.

Markus

Hello Markus,

I have tried to iimplement the example in my way. The code is as follows

import os
import sys
import core as grass

def main():
input_map = options[‘H:/Shared_data/test1.tif’]
output_map = options[‘Test_24’]
ret = grass.run_command(“r.in.gdal”, input = input_map, output = output_map)
sys.exit(ret)

if name == “main”:
options, flags = grass.parser()
main()

But I get the following error, I cant make out where I am going wrong. I have set all the Env variables as suggested in the wiki page. Please help me to implement it.

F:\GRASS-6-SVN\etc\python\grass\script>python test.py
Traceback (most recent call last):
File “test.py”, line 18, in
options, flags = grass.parser()
File “F:\GRASS-6-SVN\etc\python\grass\script\core.py”, line 359, in parser
os.execvp(“g.parser.exe”, [name] + argv)
File “F:\Quantum GIS\python\os.py”, line 353, in execvp
_execvpe(file, args)
File “F:\Quantum GIS\python\os.py”, line 389, in _execvpe
func(fullname, *argrest)
OSError: [Errno 22] Invalid argument

With Regards
Bishwarup

On Wed, Mar 24, 2010 at 2:24 AM, Markus Neteler <neteler@osgeo.org> wrote:

On Mon, Mar 22, 2010 at 1:28 PM, Bishwarup Banerjee
<bishwarup.banerjee@gmail.com> wrote:

Hi All,

I want to call the grass functionalities from inside my python plugins, in
QGIS
For example: the Grass’s raster to polygon script, with out going inside the
grass plugin.
Can I set the Grass Mapset, and location from inside my python plugin?
Is this possible?

I think yes. See also
http://grass.osgeo.org/wiki/GRASS_and_Python

(@all: please extend the Wiki page!)

my configurations are:
QGIS 0.11, GRASS 6.3, python 2.5, Windows

Note that there are substantial improvements in GRASS 6.4+ for Python.

Markus

Bishwarup Banerjee wrote:

I have tried to iimplement the example in my way. The code is as follows

import core as grass

This should be:

  import grass.script as grass
or:
  import grass.script.core as grass

def main():
    input_map = options['H:/Shared_data/test1.tif']
    output_map = options['Test_24']

This is bogus. The keys in the "options" dictionary are the names of
the options defined in the g.parser comments (assuming that you have
them; if you don't, grass.parser() won't work).

But I get the following error, I cant make out where I am going wrong. I
have set all the Env variables as suggested in the wiki page. Please help me
to implement it.

F:\GRASS-6-SVN\etc\python\grass\script>python test.py
Traceback (most recent call last):
  File "test.py", line 18, in <module>
    options, flags = grass.parser()
  File "F:\GRASS-6-SVN\etc\python\grass\script\core.py", line 359, in parser
    os.execvp("g.parser.exe", [name] + argv)
  File "F:\Quantum GIS\python\os.py", line 353, in execvp
    _execvpe(file, args)
  File "F:\Quantum GIS\python\os.py", line 389, in _execvpe
    func(fullname, *argrest)
OSError: [Errno 22] Invalid argument

I have no idea what's causing this.

I suggest modifying the parser() function to print the arguments which
are being passed to os.execvp():

  File "F:\GRASS-6-SVN\etc\python\grass\script\core.py", line 359, in parser
    os.execvp("g.parser.exe", [name] + argv)

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

Dear All,

Thank you very much for all your inputs and you guys have done a wonderful work.
I am able to solve my problem.
Three Cheers for Grass and Open Source Softwares.
Thanks you all once again.

With Regards,
Bishwarup

On Thu, Mar 25, 2010 at 7:50 AM, Glynn Clements <glynn@gclements.plus.com> wrote:

Bishwarup Banerjee wrote:

I have tried to iimplement the example in my way. The code is as follows

import core as grass

This should be:

import grass.script as grass
or:
import grass.script.core as grass

def main():
input_map = options[‘H:/Shared_data/test1.tif’]
output_map = options[‘Test_24’]

This is bogus. The keys in the “options” dictionary are the names of
the options defined in the g.parser comments (assuming that you have
them; if you don’t, grass.parser() won’t work).

But I get the following error, I cant make out where I am going wrong. I
have set all the Env variables as suggested in the wiki page. Please help me
to implement it.

F:\GRASS-6-SVN\etc\python\grass\script>python test.py
Traceback (most recent call last):
File “test.py”, line 18, in
options, flags = grass.parser()
File “F:\GRASS-6-SVN\etc\python\grass\script\core.py”, line 359, in parser
os.execvp(“g.parser.exe”, [name] + argv)
File “F:\Quantum GIS\python\os.py”, line 353, in execvp
_execvpe(file, args)
File “F:\Quantum GIS\python\os.py”, line 389, in _execvpe
func(fullname, *argrest)
OSError: [Errno 22] Invalid argument

I have no idea what’s causing this.

I suggest modifying the parser() function to print the arguments which
are being passed to os.execvp():

File “F:\GRASS-6-SVN\etc\python\grass\script\core.py”, line 359, in parser
os.execvp(“g.parser.exe”, [name] + argv)


Glynn Clements <glynn@gclements.plus.com>