2

Do d.* commands from GRASS GIS 7.0 run from a python script? I'm trying with this code:

import grass.script as grass

grass.run_command('d.mon', start="wx3", resolution = '1')
grass.run_command('d.mon', select="wx3")
grass.run_command('d.rast', map="M01relief")
grass.run_command('d.mon', select="wx3")
grass.run_command('d.vect', map="M03PTEtramos", color="black",  width='2')

When executing the complete script, the wx3 monitor starts, but remains empty. However, if I paste the commands one by one in GRASS Python Command-line, I get the layers drawn in the monitor. And using Cairo driver, I also get the layers drawn in a PNG file (directly from the script).

-

PS1: In the aforementioned Python code, the monitor seems not to apply the code, because if I check for the monitor commands, I get no info:

grass.run_command('d.mon', flags="c")
[Empty output]

(The monitor wx3 opens and remains empty)

Using a Bash script, the monitor is again empty:

d.mon start=wx3 resolution=1
d.rast map=M01relief
d.vect map=M03PTEtramos color=black width=2
d.redraw

But if I ask then for the list of commands I get results:

d.mon -c
Enlistar comandos para el monitor <wx3>:
d.rast map="M01relief" bgcolor="white"
d.vect map="M03PTEtramos" layer="1" display="shape" type="point,line,boundary,area,face" color="black" fill_color="200:200:200" width=2 width_scale=1 icon="basic/x" size=5 label_layer="1" label_color="red" label_bgcolor="none" label_bcolor="none" label_size=8 xref="left" yref="center"

Actually, doing d.redraw from command line (not from the script), shows the map.

The problem happens both in Linux Mint 64 bits and Windows7 64 bits (the second running in a VirtualBox machine)

PS2: This is weird. The commands are written to the cmd file of the monitor (e.g.: .tmp/24518.0.cmd):

d.rast map="M01relief" bgcolor="white"
d.vect map="M03PTEtramos" layer="1" display="shape" type="point,line,boundary,area,face" color="black" fill_color="200:200:200" width=2 width_scale=1 icon="basic/x" size=5 label_layer="1" label_color="red" label_bgcolor="none" label_bcolor="none" label_size=8 xref="left" yref="center"

But I do not get the layers shown until I manually open the file (in a text editor) and save it. Using some python code to open, edit and save the trick doesn't work [here 'rutamon' is the path to cmd file of wx3 monitor]:

with open(rutamon, 'a') as comandos:
    comandos.write('d.redraw')
    comandos.flush()
    os.fsync(comandos.fileno())

PROVISIONAL WORKAROUND: I have found that if I open a second monitor after the first, the layers are shown:

grass.run_command('d.mon', start="wx3", resolution = '2')
grass.run_command('d.rast', map="M01relief",  overwrite="true")
grass.run_command('d.mon', start="wx0", resolution = '1')
grass.run_command('d.rast', map="M01relief",  overwrite="true")
grass.run_command('d.mon', select="wx3")
grass.run_command('d.redraw', verbose="true")

With this code, I get an empty "wx0" monitor, and a "wx3" monitor showing my layers. But I need that second 'ghost' monitor, which is not very elegant... Any help with this is welcome.

  • I assume you run your script in the GRASS environment, right? I tried it and it worked fine (and in the GRASS Python shell as well). There seems to be another non-obvious problem. – webrian Apr 29 '15 at 2:47
  • Yes, I'm running it from a GRASS environment. I have tried using a Bash script, and I got the same problem (main post edited). – umalandscape Apr 29 '15 at 7:18
  • Sorry, I can't retrace your issue, both works for me. Did you set the region properly? – webrian Apr 30 '15 at 6:44
  • Yes, the region seems to be properly set. I have found a provisional workaround, not very elegant, but working now... I've added it to my post. I'll update it if I find a better solution. Thanks! – umalandscape Apr 30 '15 at 7:55

Your Answer

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

Browse other questions tagged or ask your own question.