[GRASS-user] d.rast in python script

Hello,

I wrote some python script and I do not find how to display created rasters.

I tried to use d.rast but I believe this command is not designed to be used in python script (no monitor available with d.mon -l). Am I wright ? so how to do ?...

Thank you for your help.

Frank

Hello Frank,

It is not very straightforward, but it is possible.
Here is an example on how I do it:

import os
import grass.script as gscript
from grass.pygrass.gis.region import Region

Set general env

os.environ[‘GRASS_RENDER_IMMEDIATE’] = “cairo”
os.environ[‘GRASS_RENDER_FILE_COMPRESSION’] = “9”
os.environ[‘GRASS_RENDER_FILE_READ’] = “TRUE”

Set image size using the region

region = Region()
xr = region.cols
yr = region.rows
ratio = xr/yr
height = int(WIDTH / ratio) # Choose the width you like
os.environ[‘GRASS_RENDER_WIDTH’] = str(WIDTH)
os.environ[‘GRASS_RENDER_HEIGHT’] = str(height)

Then you can draw

os.environ[‘GRASS_RENDER_FILE’] = img_file_name
gscript.run_command(‘d.rast’, map=my_map, quiet=True)

Be aware that every time you run your script, it will add layer on the file.
So you might want to delete the png file between the runs.

Cheers,

Laurent

Le ven. 19 oct. 2018 à 13:00, Frank David <frank.david@geophom.fr> a écrit :

Hello,

I wrote some python script and I do not find how to display created
rasters.

I tried to use d.rast but I believe this command is not designed to be
used in python script (no monitor available with d.mon -l). Am I wright
? so how to do ?..

Thank you for your help.

Frank


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

Hello Laurent,

Thank you for your help. I’m very surprised by the complexity of the way to display a raster. I thought that only d.rast was needed. Other grass command are so simple to use…

Anayway, I don’t understand why you need to set a image width and height. is it pixel number between west to east and south to north from region ?

and from where “img_file_name” is coming ? (my layer’s name is “my_map”)

To remove raster from display, I just manually remove from the layer tree.

Cheers,

Frank

···

Le 20/10/2018 à 23:06, Laurent C. a écrit :

Hello Frank,

It is not very straightforward, but it is possible.
Here is an example on how I do it:

import os
import grass.script as gscript
from grass.pygrass.gis.region import Region

Set general env

os.environ[‘GRASS_RENDER_IMMEDIATE’] = “cairo”
os.environ[‘GRASS_RENDER_FILE_COMPRESSION’] = “9”
os.environ[‘GRASS_RENDER_FILE_READ’] = “TRUE”

Set image size using the region

region = Region()
xr = region.cols
yr = region.rows
ratio = xr/yr
height = int(WIDTH / ratio) # Choose the width you like
os.environ[‘GRASS_RENDER_WIDTH’] = str(WIDTH)
os.environ[‘GRASS_RENDER_HEIGHT’] = str(height)

Then you can draw

os.environ[‘GRASS_RENDER_FILE’] = img_file_name
gscript.run_command(‘d.rast’, map=my_map, quiet=True)

Be aware that every time you run your script, it will add layer on the file.
So you might want to delete the png file between the runs.

Cheers,

Laurent

Le ven. 19 oct. 2018 à 13:00, Frank David <frank.david@geophom.fr> a écrit :

Hello,

I wrote some python script and I do not find how to display created
rasters.

I tried to use d.rast but I believe this command is not designed to be
used in python script (no monitor available with d.mon -l). Am I wright
? so how to do ?..

Thank you for your help.

Frank


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

Geophom
327 rue de Vieille Cour 44521 OUDON
Tel +33(0)2 85 52 02 59 - Port +33(0)6 04 47 91 06
www.geophom.fr

Hi Frank,

I think Laurents solution is for rendering maps to file/image.

Do I understand you correctly, that you are looking for a solution for adding maps to the currently active map display (and respective Layer manager)?

I have not found a documentation and would be interested too. I assume one has to use wxgui code, like e.g. here:

https://trac.osgeo.org/grass/browser/grass/trunk/gui/wxpython/core/render.py

?

Yet, having the “Add created map(s) into layer tree” tick-box that core modules have available also for addons (without GUI programming for addon devs) would be nice (if possible)…

Kind regards,

Stefan

···

From: grass-user grass-user-bounces@lists.osgeo.org On Behalf Of Frank David
Sent: søndag 21. oktober 2018 21:35
To: Laurent C. lrntct@gmail.com
Cc: GRASS user list grass-user@lists.osgeo.org
Subject: Re: [GRASS-user] d.rast in python script

Hello Laurent,

Thank you for your help. I’m very surprised by the complexity of the way to display a raster. I thought that only d.rast was needed. Other grass command are so simple to use…

Anayway, I don’t understand why you need to set a image width and height. is it pixel number between west to east and south to north from region ?

and from where “img_file_name” is coming ? (my layer’s name is “my_map”)

To remove raster from display, I just manually remove from the layer tree.

Cheers,

Frank

Le 20/10/2018 à 23:06, Laurent C. a écrit :

Hello Frank,

It is not very straightforward, but it is possible.
Here is an example on how I do it:

import os
import grass.script as gscript
from grass.pygrass.gis.region import Region

Set general env

os.environ[‘GRASS_RENDER_IMMEDIATE’] = “cairo”
os.environ[‘GRASS_RENDER_FILE_COMPRESSION’] = “9”
os.environ[‘GRASS_RENDER_FILE_READ’] = “TRUE”

Set image size using the region

region = Region()
xr = region.cols
yr = region.rows
ratio = xr/yr
height = int(WIDTH / ratio) # Choose the width you like
os.environ[‘GRASS_RENDER_WIDTH’] = str(WIDTH)
os.environ[‘GRASS_RENDER_HEIGHT’] = str(height)

Then you can draw

os.environ[‘GRASS_RENDER_FILE’] = img_file_name
gscript.run_command(‘d.rast’, map=my_map, quiet=True)

Be aware that every time you run your script, it will add layer on the file.

So you might want to delete the png file between the runs.

Cheers,

Laurent

Le ven. 19 oct. 2018 à 13:00, Frank David <frank.david@geophom.fr> a écrit :

Hello,

I wrote some python script and I do not find how to display created
rasters.

I tried to use d.rast but I believe this command is not designed to be
used in python script (no monitor available with d.mon -l). Am I wright
? so how to do ?..

Thank you for your help.

Frank


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

Geophom
327 rue de Vieille Cour 44521 OUDON
Tel +33(0)2 85 52 02 59 - Port +33(0)6 04 47 91 06
www.geophom.fr

Hi Stefan,

Sorry If I’m not clear… Yes I want add my new map in the active map display. Unfortunately "add created map(s) into layer tree” tick-box does not appear in my GUI script (I don’t know why) and I would like to make it visible or to add another one in options tab. Just do run_command(“d.rast”, map=my_map) does not work…

Cheers,

Frank

···

Le 22/10/2018 à 10:20, Stefan Blumentrath a écrit :

Hi Frank,

I think Laurents solution is for rendering maps to file/image.

Do I understand you correctly, that you are looking for a solution for adding maps to the currently active map display (and respective Layer manager)?

I have not found a documentation and would be interested too. I assume one has to use wxgui code, like e.g. here:

https://trac.osgeo.org/grass/browser/grass/trunk/gui/wxpython/core/render.py

?

Yet, having the “Add created map(s) into layer tree” tick-box that core modules have available also for addons (without GUI programming for addon devs) would be nice (if possible)…

Kind regards,

Stefan

From: grass-user grass-user-bounces@lists.osgeo.org On Behalf Of Frank David
Sent: søndag 21. oktober 2018 21:35
To: Laurent C. lrntct@gmail.com
Cc: GRASS user list grass-user@lists.osgeo.org
Subject: Re: [GRASS-user] d.rast in python script

Hello Laurent,

Thank you for your help. I’m very surprised by the complexity of the way to display a raster. I thought that only d.rast was needed. Other grass command are so simple to use…

Anayway, I don’t understand why you need to set a image width and height. is it pixel number between west to east and south to north from region ?

and from where “img_file_name” is coming ? (my layer’s name is “my_map”)

To remove raster from display, I just manually remove from the layer tree.

Cheers,

Frank

Le 20/10/2018 à 23:06, Laurent C. a écrit :

Hello Frank,

It is not very straightforward, but it is possible.
Here is an example on how I do it:

import os
import grass.script as gscript
from grass.pygrass.gis.region import Region

Set general env

os.environ[‘GRASS_RENDER_IMMEDIATE’] = “cairo”
os.environ[‘GRASS_RENDER_FILE_COMPRESSION’] = “9”
os.environ[‘GRASS_RENDER_FILE_READ’] = “TRUE”

Set image size using the region

region = Region()
xr = region.cols
yr = region.rows
ratio = xr/yr
height = int(WIDTH / ratio) # Choose the width you like
os.environ[‘GRASS_RENDER_WIDTH’] = str(WIDTH)
os.environ[‘GRASS_RENDER_HEIGHT’] = str(height)

Then you can draw

os.environ[‘GRASS_RENDER_FILE’] = img_file_name
gscript.run_command(‘d.rast’, map=my_map, quiet=True)

Be aware that every time you run your script, it will add layer on the file.

So you might want to delete the png file between the runs.

Cheers,

Laurent

Le ven. 19 oct. 2018 à 13:00, Frank David <frank.david@geophom.fr> a écrit :

Hello,

I wrote some python script and I do not find how to display created
rasters.

I tried to use d.rast but I believe this command is not designed to be
used in python script (no monitor available with d.mon -l). Am I wright
? so how to do ?..

Thank you for your help.

Frank


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

Geophom
327 rue de Vieille Cour 44521 OUDON
Tel +33(0)2 85 52 02 59 - Port +33(0)6 04 47 91 06
www.geophom.fr

* Stefan Blumentrath <Stefan.Blumentrath@nina.no> [2018-10-22 08:20:05 +0000]:

I think Laurents solution is for rendering maps to file/image.

If of interest, see also: https://lists.osgeo.org/pipermail/grass-user/2018-October/079436.html

Nikos

* Laurent C. <lrntct@gmail.com> [2018-10-20 22:06:38 +0100]:

Hello Frank,

It is not very straightforward, but it is possible.
Here is an example on how I do it:

import os
import grass.script as gscript
from grass.pygrass.gis.region import Region

# Set general env
os.environ['GRASS_RENDER_IMMEDIATE'] = "cairo"
os.environ['GRASS_RENDER_FILE_COMPRESSION'] = "9"
os.environ['GRASS_RENDER_FILE_READ'] = "TRUE"

For reasons I don't understand, some times, the above way to set an environment
variable, does not work for me!

Then, I also use:

os.putenv("GRASS_RENDER_FILE", render_file)

Else, as we know, one can set these variables in a bash environment (see also
https://grass.osgeo.org/grass74/manuals/variables.html#setting-shell-environment-variables
https://grass.osgeo.org/grass74/manuals/variables.html#grass-related-files),
for example:

export GRASS_RENDER_IMMEDIATE=cairo

and retrieve them via

RENDER_PATH = os.getenv(GRASS_RENDER_PATH)

Nikos

# Set image size using the region
region = Region()
xr = region.cols
yr = region.rows
ratio = xr/yr
height = int(WIDTH / ratio) # Choose the width you like
os.environ['GRASS_RENDER_WIDTH'] = str(WIDTH)
os.environ['GRASS_RENDER_HEIGHT'] = str(height)

# Then you can draw
os.environ['GRASS_RENDER_FILE'] = img_file_name
gscript.run_command('d.rast', map=my_map, quiet=True)

Be aware that every time you run your script, it will add layer on the file.
So you might want to delete the png file between the runs.

Cheers,
Laurent

Thank you everybody for your help. Do you know why a run_command(“d.rast”… in a python script is not enough to display a raster ? I don’t understand why this command is so different.

Frank

···

Le 23/10/2018 à 07:41, Nikos Alexandris a écrit :

I think Laurents solution is for rendering maps to file/image.

If of interest, see also: https://lists.osgeo.org/pipermail/grass-user/2018-October/079436.html

Nikos

Geophom
327 rue de Vieille Cour 44521 OUDON
Tel +33(0)2 85 52 02 59 - Port +33(0)6 04 47 91 06
www.geophom.fr

* Frank David <frank.david@geophom.fr> [2018-10-23 22:06:01 +0200]:

Thank you everybody for your help. Do you know why a run_command("d.rast"... in a python script is not enough to display a raster ? I don't understand why this command is so different.

Display a raster map where?

Nikos

Frank

···

Le 23/10/2018 à 23:26, Nikos Alexandris a écrit :

Thank you everybody for your help. Do you know why a run_command(“d.rast”… in a python script is not enough to display a raster ? I don’t understand why this command is so different.

Display a raster map where?

Nikos

In the active map display (and respective Layer manager) as d.rast do it with gui…