1

I am using the GRASS Python Scripting Library on Ubuntu 14.04 with the packages available from the Universe repository. I have the following block of code:

        try:
            grass.mapcalc("$weightLine = $seg_rast * $cost",
                        weightLine = "weightLine",
                        seg_rast = "seg_rast",
                        cost = "cost")
        except:
            self.logger.debug("Exception running mapcalc: " + 
                              str(sys.exc_info()))

        kwargs = {}
        kwargs['stdout'] = grass.PIPE
        kwargs['stderr'] = grass.PIPE
        ps = grass.start_command("r.sum", rast="weightLine", **kwargs)
        mesg = ps.communicate()
        self.logger.info("r.sum: %s", str(mesg))

Which is logging the following:

[2014-06-03 09:57:35,532] DEBUG: Exception running mapcalc: (<type 'exceptions.SystemExit'>, SystemExit(1,), <traceback object at 0x7ffcbab1de18>)
[2014-06-03 09:57:35,538] INFO: r.sum: ('', 'ERROR: Raster map <weightLine> not found\n')

The employment of sys.exit() is not very helpful, even though it can be worked around. Without the except: block it exits my script and any other processes that may have invoked it. Note also that except Exception, e: does not catch exceptions.SystemExit.

But worst of all, the mapcalc instruction is not succeeding. It is either failing - in which case an error message is lacking - or it is simply not being executed.

How can I know the cause of this?

Update I: I have tried to run grass.mapcalc in different ways:

grass.mapcalc("weightLine = seg_rast * cost")

And:

weightLine = "weightLine"
seg_rast = "seg_rast"
cost = "cost"            
grass.mapcalc("%s = %s * %s" % (weightLine, seg_rast, cost))

But the result is always the same: exceptions.SystemExit and the command is not executed.

  • 1
    You need to use %s in the mapcalc function (it is Python, not shell). See e.g. this randomly selected example: trac.osgeo.org/grass/browser/grass/trunk/scripts/i.pansharpen/… – markusN Jun 3 '14 at 19:22
  • Hi Markus, if I try with grass.mapcalc("weightLine = seg_rast * cost") I get the exact same result: the SystemExit exception and no map calculated. – Luís de Sousa Jun 4 '14 at 5:55
  • I think what Markus meant is something like: grass.mapcalc('%s = %s * %s" % (weightLine, seg_rast, cost)) – Micha Jun 4 '14 at 7:26
  • Hi Micha, I already tried this too. I do not think this problem is related to string formatting, in that case I would probably be receiving a different exception or an error message. – Luís de Sousa Jun 4 '14 at 7:30
  • I guess you have already confirmed that the command directly in GRASS works as expected? – Micha Jun 4 '14 at 9:14
1

Here a working grass.mapcalc() example based on your map names:

import grass.script as grass

weightLine = "weightLine"
seg_rast = "seg_rast"
cost = "cost"
grass.mapcalc("%s = %s * %s" % (weightLine, seg_rast, cost))

In case of errors, check if the input maps are existing.

| improve this answer | |
  • Hi again Markus. I do not get any error messages up to the r.sun command. The question is, if any of datasets is missing, shouldn't I be receiving an error message instead of exceptions.SystemExit? – Luís de Sousa Jun 5 '14 at 6:48
  • Prior to the mapcalc command the seg dataset is being imported with: grass.read_command("v.in.ogr", flags="o", output="seg", input="seg.gml"). I find out this command is failing, but I am getting no exception, nor any error message. I would expect: a) v.in.org to return an error message in case it fails; b) mapcalc not invoking sys.exit() but rather to return an error message too. – Luís de Sousa Jun 5 '14 at 8:29
  • 1
    Hi, you need to check the return code of every process you run (in case of *_command functions). I actually think that the GRASS API should be improved to support what you are asking for (trac.osgeo.org/grass/ticket/2326). mapcalc function exits because it is consistent with the behavior of GRASS C library. However, you can change this behavior by calling grass.script.core.set_raise_on_error(True) at the beginning of your script then ScriptError will be raised. – wenzeslaus Jun 5 '14 at 19:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.