1

I'm using GRASS 7.0.3 on Windows 7 (part of the QGIS 2.14 distribution) with the extra installed add-on module r.catchment. I'm trying to use GRASS to calculate a catchment raster, but I keep getting this error related to the use of the r.reclass module by r.catchment (see output below).

Here are the GRASS commands that I execute:

v.in.ogr input=location_utm.shp output=V_LOCATION_UTM --overwrite --quiet
r.in.gdal input=astgtm_utm.img output=R_ASTGTM_UTM --overwrite --quiet
r.catchment elevation=R_ASTGTM_UTM start_points=V_LOCATION_UTM buffer=R_CATCHMENT sigma=15 area=100000000 map_val=1 --verbose
r.out.gdal input=R_CATCHMENT output=catchment.img

This is the output (and error) I am getting:

C:\Temp>r.catchment elevation=R_ASTGTM_UTM start_points=V_LOCATION_UTM buffer=R_CATCHMENT sigma=15 area=100000000 map_val=1 --verbose
There is already a MASK in place, and you have also selected to mask slope
values above 15.
The high slope areas (slope mask) will be temporarily added to current
MASKED areas for the calcualtion of the catchment geometry.
The original MASK will be restored when the module finishes
Wanted buffer area=100000000
step 1 of 4: Calculating cost surface
Calculating for time costs only
Creating optional slope mask
 100%
Maximum cost distance value 58426 covers an area of 2243889760 square map
units
Commencing to find a catchment configuration.....
Catchment configuration found!
Cost cutoff 5671 produces a catchment of 100003308 square map units.
Creating output map
ERROR: Cannot open rules file <c:\users\xxx\appdata\local\temp\tmpeopqnp>
Traceback (most recent call last):
  File "C:\Users\xxx\AppData\Roaming\GRASS7\addons/scripts/r.catchment.py", line 379, in <module>
    main()
  File "C:\Users\xxx\AppData\Roaming\GRASS7\addons/scripts/r.catchment.py", line 329, in main
    output='cost.reclass.%s' % pid, rules=temp.name)
  File "C:\PROGRA~1\QGISES~1\apps\grass\grass-7.0.3\etc\python\grass\script\core.py", line 394, in run_command
    return handle_errors(returncode, returncode, args, kwargs)
  File "C:\PROGRA~1\QGISES~1\apps\grass\grass-7.0.3\etc\python\grass\script\core.py", line 312, in handle_errors
    returncode=returncode)
grass.exceptions.CalledModuleError: Module run None ['r.reclass', 'rules=c:\\users\\xxx\\appdata\\local\\temp\\tmpeopqnp', 'input=temporary.cost.9304', 'output=cost.reclass.9304'] ended with error
Process ended with non-zero return code 1. See errors in the (error) output.

I have no idea why this error occurs or how to solve this.

3
  • Are you running this from QGIS or in GRASS?
    – BERA
    Dec 4 '16 at 17:12
  • I'm not running this from QGIS: I'm using the GRASS console. I conveniently use the GRASS distribution that comes with QGIS. I have no problem using GRASS this way for other GRASS modules.
    – JTh
    Dec 4 '16 at 17:30
  • Just so you all know, the real culprit for this issue I am having is this first remark to the second answer of this thread. Lines 328-329 in the r.catchment.py script: on Windows, it is not allowed to pass "temp.name" as a parameter to the call of another script. This is exactly what is happening in this script and where it goes wrong (on Windows). Like the answer from @gene, you need to hack the script to make it work.
    – JTh
    Dec 5 '16 at 20:12
2

This script is a Python script: (r.catchment., r.catchment.py and in the error message r.catchment.py) The error is in lines 328-329 with rules=temp.name -> Cannot open rules file <c:\users\xxx\appdata\local\temp\tmpeopqnp>

grass.run_command('r.reclass', overwrite=grass.overwrite(), input=cost,
                output='cost.reclass.%s' % pid, rules=temp.name)

And I think it is a Windows problem because temp is defined with the standard Python module tempfile as a temporary file in the lines 325-330

#(import tempfile, line 130)
temp = tempfile.NamedTemporaryFile()
temp.write('0 thru %s = %s\n' % (int(cutoff),  mapval))
temp.flush()
grass.run_command('r.reclass', overwrite=grass.overwrite(), input=cost,
                output='cost.reclass.%s' % pid, rules=temp.name)
temp.close()

On my PC (Mac OS X, Unix), in the Python shell or in the Python console of GRASS GIS

import tempfile
temp = tempfile.NamedTemporaryFile()
tempfile.gettempdir()
'/var/folders/k9/7s3l5fvd5g1dy31b6sms18fr0000gn/T'
temp.name
'/var/folders/k9/7s3l5fvd5g1dy31b6sms18fr0000gn/T/tmpWcTD_A'

and the script find this file (in general C:\TEMP, C:\TMP, /tmp, /var/tmp, or /usr/tmp according to the PC, the OS and the environment variables)

In your case, the generated temp.name is c:\users\xxx\appdata\local\temp\tmpeopqnp but the script don't find it.

Therefore the question of BERA ? (GRASS GIS is not QGIS, nor GRASS GIS from QGIS, particularly with an add-on module)

8
  • Thanks for the elaborate reply: I already checked the temp-file and it is not there. I believe it is a Windows problem as well. I'm gonna try a standalone GRASS 7 installation and see what happens.
    – JTh
    Dec 4 '16 at 17:35
  • I installed a standalone GRASS GIS 7 for Windows and I got the same problem: ERROR: Cannot open rules file <c:\users\xxx\appdata\local\temp\tmpz6qeer>
    – JTh
    Dec 4 '16 at 17:47
  • Try the simple solution in the Python shell and what are the results ?
    – gene
    Dec 4 '16 at 17:49
  • Sure, and I explitely set the Windows environment variables TMP and TEMP to C:\Temp. These are the Python console results: import tempfile temp = tempfile.NamedTemporaryFile() tempfile.gettempdir() 'c:\\temp' temp.name 'c:\\temp\\tmpdes0yg'
    – JTh
    Dec 4 '16 at 17:56
  • Therefore, try to modify the Python script r.catchment.py with your parameters
    – gene
    Dec 4 '16 at 18:15
0

To make this r.catchment.py script work on Windows, replace lines 325-330, being:

temp = tempfile.NamedTemporaryFile()
temp.write('0 thru %s = %s\n' % (int(cutoff),  mapval))
temp.flush()
grass.run_command('r.reclass', overwrite=grass.overwrite(), input=cost, output='cost.reclass.%s' % pid, rules=temp.name)
temp.close()

with these lines:

temp = tempfile.NamedTemporaryFile(delete=False)
temp.write('0 thru %s = %s\n' % (int(cutoff),  mapval))
temp.flush()
temp.close()
grass.run_command('r.reclass', overwrite=grass.overwrite(), input=cost, output='cost.reclass.%s' % pid, rules=temp.name)
0

I am the author of r.catchment.py, so this is a very enlightening thread. Indeed, I developed on Linux and was only able to test on MacOS, but not on Windows. I was not aware of this issue with temp files on Windows, but the final solution listed seems to make some sense. I will update the r.catchment.py script with a check for a Windows OS, in which case the alternative temp file construct will be used. It may take a little while before I can get around to it, but hopefully before the end of the year...

Thanks!

~Isaac

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.