[GRASS-user] syntax for setting GRASS_PNGFILE from Python script

I am new to scripting GRASS with Python. I am having trouble with the proper
syntax for setting the value of GRASS_PNGFILE within Python for the purposes
of automated map creation using the PNG driver. I can do this in the BASH
shell as:

GRASS_PNGFILE=mywaycoolmap.png
export GRASS_PNGFILE

What is the proper syntax for setting this in Python?

I have tried something like:

def jpgMap():
    #SET PNG OUTPUT FILE NAME
    grass.run_command("g.gisenv", set="GRASS_PNGFILE=testName.png")
    #BUILD MAP
    grass.run_command("d.mon", start="PNG")
    grass.run_command("d.vect", map="parcelBoundaries", col="black",
fcolor="none", width="2")
    grass.run_command("d.barscale", bcolor="white", tcolor="black",
at="0,95")
    grass.run_command("d.text", text='Test Python Map', at="29,95",
color="black")
    #CLOSE PNG FILE
    grass.run_command("d.mon", stop="PNG")
jpgMap()

This does not work. I still get map.png.

I am using 6.4svn complied from source on Ubuntu 9.10.

Thank you.
--
View this message in context: http://osgeo-org.1803224.n2.nabble.com/syntax-for-setting-GRASS-PNGFILE-from-Python-script-tp5484418p5484418.html
Sent from the Grass - Users mailing list archive at Nabble.com.

Hi,

2010/8/31 Damian M <maddalena@nc.rr.com>:

GRASS_PNGFILE=mywaycoolmap.png
export GRASS_PNGFILE

What is the proper syntax for setting this in Python?

I have tried something like:

def jpgMap():
#SET PNG OUTPUT FILE NAME
grass.run_command("g.gisenv", set="GRASS_PNGFILE=testName.png")

GRASS_PNGFILE is a shell environment variable, not a GRASS gisenv
variable [1]. To set environment variable use e.g. os.putenv()

os.putenv('GRASS_PNGFILE', 'test.png')

Martin

[1] http://grass.osgeo.org/grass64/manuals/html64_user/variables.html

--
Martin Landa <landa.martin gmail.com> * http://gama.fsv.cvut.cz/~landa

Martin,

Thanks! Much appreciated.

-Damian

--
View this message in context: http://osgeo-org.1803224.n2.nabble.com/syntax-for-setting-GRASS-PNGFILE-from-Python-script-tp5484418p5487870.html
Sent from the Grass - Users mailing list archive at Nabble.com.

Martin Landa wrote:

> GRASS_PNGFILE=mywaycoolmap.png
> export GRASS_PNGFILE
>
> What is the proper syntax for setting this in Python?
>
> I have tried something like:
>
> def jpgMap():
> #SET PNG OUTPUT FILE NAME
> grass.run_command("g.gisenv", set="GRASS_PNGFILE=testName.png")

GRASS_PNGFILE is a shell environment variable, not a GRASS gisenv
variable [1]. To set environment variable use e.g. os.putenv()

os.putenv('GRASS_PNGFILE', 'test.png')

If you want to set an environment variable for the duration of a
script, the preferred mechanism is to modify the os.environ array,
i.e.:
  os.environ['GRASS_PNGFILE'] = 'test.png'

Modifying os.environ will call os.putenv() if it is available (it
doesn't exist on all platforms supported by Python), but calling
os.putenv() doesn't modify os.environ, so calling os.putenv() will
result in os.environ and the actual environment used by subprocesses
getting out of sync.

If you want to set an environment variable for specific commands, use
the "env" parameter to run_command() etc to pass an updated
environment, e.g.:

  environ = os.environ.copy()
  environ['GRASS_PNGFILE'] = 'test.png'

  grass.run_command(..., env = environ)

This approach should be used if you will be running different commands
with different environment settings. Changing os.environ then
reverting it after the command completes is error-prone and should not
be used. Only modify os.environ if the changes will be "permanent"
(i.e. for the duration of the script).

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