Map export to raster image files

To allow you to export what you would normally see on your screen to a raster file, there are two alternative "monitor" drivers to the standard terminal monitors. These are the CELL driver and the PNG driver. Both are started just like a normal monitor and from that point on, all display commands are directed into the resulting raster file and not to a terminal monitor.

The difference between the two is that the CELL driver creates a GRASS raster file that you then have to export using one of the r.out.* raster export modules, whereas the PNG driver directly creates a file outside of GRASS.

PNG driver

The PNG driver has the advantage of exporting directly to an external file. Its disadvantage is the fact that it depends on the libpng library which might not always be present on all systems (although it should be on many). Its usage is very simple:

d.mon start=PNG

any display commands

d.mon stop=PNG

This creates a file map.png in the current directory containing all the layers that you drew with the display commands. You can use environmental variables to set options such as an alternative file name, the resolution of the resulting image, the background color, etc. Please the manual page for more details.

CELL driver

The CELL driver is a bit more complicated to use, as it requires the extra step of exporting the resulting CELL file to an external raster file. It has the advantage, however, of using internal GRASS routines, and, therefore, not being dependent on external libraries. Other than that its usage is exactly the same as the PNG driver. See the manual page for details and differences.

Very important: before exporting the D_cell raster file which is the output of the CELL driver, you will have to set the region to cover this file (otherwise your export results will be empty):

g.region rast=D_cell

Scripting the creation of maps

By definition you will not see the results of your display commands when you direct them into one of the above drivers. You, therefore, might want to test your commands in a normal monitor before (or test in the Display Manager, see the Section called Creating a PNG map from the Display Manager below. You can then enter all the commands into a text file which you can then launch once you have started the driver of your choice. Or you can use d.save to help you create the script file.

d.mon start=PNG

sh your_text_file

d.mon stop=PNG

Often, one needs to create several maps with a similar layout. In that case it would be quite tedious to retype the same commands over and over again. Again, the use of a script file can be very helpful here. Just change the names and options of the maps to be displayed, relaunch the driver and rerun the script. (But be careful: if you do not rename the result at each run of the driver, or change the GRASS_PNGFILE variable in the case of the PNG driver, your new map will overwrite the previous one !)

If you combine the scripting with the use of d.frame, you can create quite sophisticated layouts. Here's a (very) simple example of such a layout script:


d.frame -e # erase any existing frames
d.erase col=white # create a white background
d.frame -c frame=title at=90,100,0,100 # create a title frame
echo "TITRE" | d.text -b size=75 at=45,30 col=black # write the title
d.frame -c frame=map at=10,90,0,100 # create a map frame
d.rast combel # draw a raster map
d.vect arrondissements col=red # draw a vector map
d.vect autoroute col=violet # draw a second vector map
d.frame -c frame=legend at=0,10,0,100 # create a legend frame
echo "Cartography: M. Lennert" | d.text size=20 at=75,50 col=black # draw text
echo "Digitalisation: TeleAtlas" | d.text size=20 at=75,30 col=black # draw more text beneath the first
d.barscale at=10,920 bcolor=white tcolor=black # create a barscale

Creating a PNG map from the Display Manager

If you are involved in one of those "anti-command line leagues", GRASS offers you a compromise in the form of the Display Manager. The interface includes a "PNG" button which allows you to put into a map.png file everything that you see on the screen when pushing the "Display" button (except for legends). In addition, you have the choice of entering a "command" instead of a specific map type. This allows you to enter any command that you might use in GRASS, as for example d.frame. The script example above, would look like this in d.dm:

From there, just click on the "PNG" button and you have your map (don't forget to rename it, or it will be overwritten by the next run of the PNG driver). Use the "Add set" button and the "Save" button to create different types of layouts that you can then reuse with different maps (TIP: double-click on a set to rename it...).