[GRASS-dev] using g.mapset in python script

Hi devs,

I am trying to create a new mapset and open that mapset using g.mapset. When doing this from the command line, I can import layers in the new mapset using r.in.gdal without problems.However, running this in a python script doesn’t work. E.g., when I run:

gs.run_command("g.mapset", flags="c", mapset="LST", location="latlon_clark66")

Next, I try to import a layer:

infile="lst_01.tif"
outfile="lst_01"
gs.run_command("r.in.gdal", input=infile, output=outfile)

This gives me the error:


gs.run_command("r.in.gdal", input="lst_01.tif", output="lst_01")
Traceback (most recent call last):
  File "<ipython-input-18-2c415e16d79f>", line 1, in <module>
    gs.run_command("r.in.gdal", input="lst_01.tif", output="lst_01")
  File "/usr/local/grass7/grass-7.5.svn/etc/python/grass/script/core.py", line 421, in run_command
    return handle_errors(returncode, returncode, args, kwargs)
  File "/usr/local/grass7/grass-7.5.svn/etc/python/grass/script/core.py", line 332, in handle_errors
    returncode=returncode)
CalledModuleError: Module run None ['r.in.gdal', 'input=lst_01.tif', 'output=lst_01'] ended with error
Process ended with non-zero return code 1. See errors in the (error) output.

Any idea why this doesn’t work?

Best wishes,

Paulo

On Sun, Dec 24, 2017 at 3:10 PM, Paulo van Breugel <p.vanbreugel@gmail.com> wrote:

Hi devs,

I am trying to create a new mapset and open that mapset using g.mapset. When doing this from the command line, I can import layers in the new mapset using r.in.gdal without problems.However, running this in a python script doesn’t work. E.g., when I run:

gs.run_command(“g.mapset”, flags=“c”, mapset=“LST”, location=“latlon_clark66”)

Next, I try to import a layer:

infile=“lst_01.tif”

outfile=“lst_01”

gs.run_command(“r.in.gdal”, input=infile, output=outfile)

This gives me the error:

gs.run_command(“r.in.gdal”, input=“lst_01.tif”, output=“lst_01”)

Traceback (most recent call last):

File “”, line 1, in

gs.run_command(“r.in.gdal”, input=“lst_01.tif”, output=“lst_01”)

File “/usr/local/grass7/grass-7.5.svn/etc/python/grass/script/core.py”, line 421, in run_command

return handle_errors(returncode, returncode, args, kwargs)

File “/usr/local/grass7/grass-7.5.svn/etc/python/grass/script/core.py”, line 332, in handle_errors

returncode=returncode)

CalledModuleError: Module run None [‘r.in.gdal’, ‘input=lst_01.tif’, ‘output=lst_01’] ended with error

Process ended with non-zero return code 1. See errors in the (error) output.

Any idea why this doesn’t work?

It is a pity that the output to stderr is not available when a module is called in python with gs.run_command() and the module fails. The python traceback is in this case useless. Maybe handle_errors() [0] could be improved to also provide the stderr output of the current module.

Markus M

[0] https://trac.osgeo.org/grass/browser/grass/trunk/lib/python/script/core.py#L318

Best wishes,

Paulo


grass-dev mailing list
grass-dev@lists.osgeo.org
https://lists.osgeo.org/mailman/listinfo/grass-dev