[GRASS-user] How to define the png image dimensions when using d.mon?

Moritz Lennert mlennert at club.worldonline.be
Wed Mar 9 07:12:08 PST 2016


On 09/03/16 15:39, jean pierre huart wrote:
> Thanks Vinay, for the suggestion but I get an error on the d.out.file
> instruction.
> By the way, I've investigated the problem a bit further and found that I
> mixed up instructions from manuals of different versions.
> So coming back to GRASS v 7.0.x references I finally succeed in
> obtaining a solution but not 100% satisfactory.
>
> First trial create a unique image combining raster and vector, but width
> and height remains at default and background is not transparent:
>
>      os.environ['GRASS_RENDER_TRANSPARENT'] = 'TRUE'
>      os.environ['GRASS_RENDER_WIDTH'] = str(1280)
>      os.environ['GRASS_RENDER_HEIGHT'] = str(960)
>      os.environ['GRASS_RENDER_FILE'] = filenameCombined.png
>
>      self.gscript.run_command('d.mon', overwrite=True, start='png',
> output=filename)
>      self.gscript.run_command('d.rast', map='{0}@{1}'.format(raster,
> currentmapset)
>      self.gscript.run_command('d.vect', map='{0}@{1}'.format(map,
> currentmapset), color='white', fill_color='none')
>      self.gscript.run_command('d.mon', stop="png")
>
>
> Second trial successfull for obtaining desired width, height and
> transparency of the background  but creating 2 images one for the raster
> and the other for the vector, now I would like to combine them to have
> only one image:
>
>      os.environ['GRASS_RENDER_IMMEDIATE'] = 'png'
>      os.environ['GRASS_RENDER_TRANSPARENT'] = 'TRUE'
>      os.environ['GRASS_RENDER_WIDTH'] = str(1280)
>      os.environ['GRASS_RENDER_HEIGHT'] = str(960)
>
>      os.environ['GRASS_RENDER_FILE'] = filenameOfRaster.png
>      self.gscript.run_command('d.rast', map='{0}@{1}'.format(raster,
> currentmapset))
>
>      os.environ['GRASS_RENDER_FILE'] = filenameOfVector.png
>      self.gscript.run_command('d.vect', map='{0}@{1}'.format(map,
> currentmapset), color='white', fill_color='none')
>
> Modifying this with the following
>
>      os.environ['GRASS_RENDER_FILE'] = filenameCombined.png
>      self.gscript.run_command('d.rast', map='{0}@{1}'.format(raster,
> currentmapset))
>      self.gscript.run_command('d.vect', map='{0}@{1}'.format(map,
> currentmapset), color='white', fill_color='none')
>
> ends with only the image of the vector.
>
> I've the feeling that using d.mon would be preferable, but it seems that
> it does not take into account the environment variables (at the
> exception of GRASS_RENDER_FILE) that I propose.

This script gives me differently sized png files with both maps 
displayed and transparent background:

import grass.script as gscript
import os

os.environ['GRASS_RENDER_TRANSPARENT']='TRUE'

for height in range(500,2000,500):
     for width in range(500,2000,500):

         os.system('rm map.png')
         os.environ['GRASS_RENDER_HEIGHT']=str(height)
         os.environ['GRASS_RENDER_WIDTH']=str(width)

         gscript.run_command('d.mon', overwrite=True, start='png', 
output='map.png')
         gscript.run_command('d.rast', map_='elevation')
         gscript.run_command('d.vect', map_='roadsmajor')
         gscript.run_command('d.mon', stop="png")

         os.system('identify map.png')


You can reach the same result with something like this:

import grass.script as gscript
import os

os.environ['GRASS_RENDER_TRANSPARENT']='TRUE'

for height in range(500,2000,500):
     for width in range(500,2000,500):

         os.system('rm map.png')
         os.environ['GRASS_RENDER_IMMEDIATE']='png'
         os.environ['GRASS_RENDER_FILE']='map.png'
         os.environ['GRASS_RENDER_FILE_READ']='TRUE'
         os.environ['GRASS_RENDER_HEIGHT']=str(height)
         os.environ['GRASS_RENDER_WIDTH']=str(width)

         gscript.run_command('d.rast', map_='elevation')
         gscript.run_command('d.vect', map_='roadsmajor')

         os.system('identify map.png')


You might have to watch out for existing files. If a file already exists 
with the indicated name then GRASS cannot change its width and height.

Hope this helps,
Moritz


More information about the grass-user mailing list