User Tools

Site Tools


Hands on GRASS - First steps

  • Lecture: Introduction to GRASS

    The objective of this page is to start using GRASS and become familiar with some general GIS operations. We are going to use a command line approach. This will enable us to carry out stand alone processes and understand step-by-step the functions and available options. Let´s start using grass.

    download the script and save VM@:~/ost4sem/exercise/basicadvgrass/

Starting Grass

A common problem of new users is not really to use grass but to start a grass session. The main reason is due to the somewhat complex grass data structure and the way a grass session has to be set up before starting !

Every Grass project has a predefined data structure

  • GISDBASE - Grass data are stored in a directory referred to as DATABASE “GISDBASE”. This directory has to be created with mkdir or a file manager, before starting to work with GRASS. Within this DATABASE, the projects are organized by project areas stored in subdirectories called LOCATIONs.
    • LOCATION - A Location is defined by its coordinate system, map projection and geographical boundaries. The subdirectories and files defining a LOCATION are created automatically when GRASS is started the first time with a new LOCATION.
    • MAPSET - Location can have many MAPSETs. Each MAPSET is a LOCATION's subdirectory. New MAPSET can be added at GRASS startup.

    To remove a MAPSET, remove it's directory using your file manager or by

    rm -rf /path/./mapsettodelete The wxGUI graphical user interface provides options to rename/remove LOCATIONs and MAPSETs.

    There are several way to use and open Grass. The simplest way is to open a terminal and type grass -wxpython the Graphical User Interface (GUI) will ask you to define the GISDBASE, LOCATION and MAPSET to use. If you want you can select the Location wizard to create a new location with newest projection parameters or the Create mapset button to create a new mapset inside a pre-existing location.

    To start grass in a terminal:

    grass -text ~/ost4sem/grassdb/europe/PCEM

    Using the above command we have already entered into the Grass environment with the GISDBASE, LOCATION and MAPSET defined by the ~/ost4sem/grassdb/europe/PCEM path

    Once you are running Grass through the bash shell terminal you can always start the graphical user interface with:
      g.gui wxpython


prefix function class type of command example
d.* display graphical output d.rast: views raster map
d.vect: views vector map
db.* database database management selects value(s) from table
g.* general general file operations g.rename: renames map
i.* imagery image processing i.smap: image classifier
ps.* postscript map creation format map creation in Postscript
r.* raster raster data processing r.buffer: buffer around raster features
r.mapcalc: map algebra
r3.* voxel raster voxel data processing r3.mapcalc: volume map algebra
v.* vector vector data processing v.overlay: vector map intersections

For detailed instructions on grass command syntax and use go to the GRASSonline manual or type the function name –man. As an example --man

Grass working environment

The g.gisenv command informs you on your current grass environment settings


If you started grass correctly you should visualize the following lines on your terminal

 GRASS 7.0 (europe):~ > 
Running grass through the bash shell terminal allows you to use all command lines functionality of both grass and shell. As an example you can type

and visualize all files available in your shell current working directory. This means that all your output files produced by bash command lines functionality will be saved in the current working directory (if not specified differently). Many non-geographical grass output features such as text file reports or images will be saved as well in the current working directory. The GRASS 6.4.0RC5 (europe):~ > informs you are currently working in the home folder. See


for double checking the same information using a bash command instead of the grass g.gisenv command.

It is a good working habit within Grass to set as bash shell working directory the same as your GRASS LOCATION folder.
cd ~/ost4sem/grassdb/europe/

The terminal prompt will change from:

GRASS 7.0 (europe):~ >


GRASS 7.0 (europe):~/ost4sem/grassdb/europe >

and you will be aware that your current bash shell working directory matches your grass location directory.


We have explained that grass projects can be organised in MAPSETs by users, by themes, by extent or locations, and grouped within the same grass LOCATION. This LOCATION will group several MAPSETS defined by a common projection and includes access a common set of maps in the PERMANENT MAPSET folder. When we work in a specific MAPSET we will have no rights to edit, write, or delete maps in a different MAPSET. For doing so you will have to change your working MAPSET directory and then delete or produce new maps.

The g.mapset command allows you to change Grass working directory and successively generate delete or modify maps as you wish.

g.mapset -l
g.mapset mapset=PCEM

Now you should see the following settings

 GRASS 7.0 (europe):~/ost4sem/grassdb/europe > g.gisenv
 GRASS 7.0 (europe):~/ost4sem/grassdb/europe >
From grass 6.3 only the -c flag create new mapset if it doesn't exist.; -l List available mapsets (!)

Manage maps

To list your available maps:

g.list type=vect 
g.list vect  
g.list rast

The user can add, modify, and delete data layers that exist under his current mapset. Although the user can also access (i.e., use) data that are stored under other mapsets in the same GRASS location, the user can only make permanent changes (create or modify data) located in the current mapset. To access maps in a different current mapset we use the addmapset option in the g.mapset function. As an example:

g.mapsets addmapset=Vmodel

Now we can access the Vmodel mapset and eventually copy a map from the Vmodel mapset to our current mapset directory using the g.copy function:

g.copy rast=potveg_ita@Vmodel,pvegita 
g.list rast

And delete a map with the g.remove command

g.remove rast=pvegita
g.list rast

You can access but not delete or modify a map in a different mapset from your current

  g.remove rast=potveg_europe@Vmodel
  g.remove rast=potveg_europe
We have to be careful in GRASS to understand the possible differences existing in the same MAPSET between the whole extent and resolution of the MAPSET itself, the extent, resolution and geographic location of our working region and the extent and location of what we are visualising.

Region settings

In GRASS, a region refers to a geographic area with some defined boundaries, based on a specific map coordinate system and map projection. Grass region definition and details. This crucial GRASS setting will allow us to define, within a MAPSET, the settings a particular region of work.
Once the grass region has been defined, grass modules (or programs) will operate within this region. The user can create, modify, and store as many geographic region definitions as desired for any given mapset. However, only one of these geographic region definitions will be current at any given moment, for a specified mapset. GRASS programs that respect the geographic region settings will use the current geographic region settings.

to ask for current region settings type

 g.region -d

and to query your current region settings type

 g.region -p
-p stands for “print” ; -d stands for default

You can modify your default g. region with a -s flag within the PERMANENT directory.

A common way to write GRASS scripts while you are learning is to use the GUI for a particular tool. For example, let's say we want to rename a raster dataset from fnfpcalpinecarpatien5k to ac5. To do this you use the g.copy command. If you type just the name of the GRASS command without any parameters, the GUI will open up with various tabs detailing all parameters and flags. There is also a help tab with the full help file. You can fill in the parameters and choose your flags using the GUI, then click “Copy” at the bottom and paste the command into your script.

For example, open up the g.region GUI by running this:


Now we define a new study area for the Scandinavia region.

g.region -p
g.region n=6015390 e=5676400 s=3303955 w=3876180 res=1000 save=scandinavia --overwrite
g.region -p

You will visualize

  projection: 99 (Lambert Azimuthal Equal Area)
  zone:       0
  datum:      ** unknown (default: WGS84) **
  ellipsoid:  grs80
  north:      6015390
  south:      3303955
  west:       3876180
  east:       5676400
  nsres:      200.00258169
  ewres:      200.00222198
  rows:       13557
  cols:       9001
  cells:      122026557
  GRASS 6.4.0RC5 (europe):~/ost4sem/grassdb/europe >

If you open Qgis in another terminal

 qgis & 

You can load a raster map on Qgis from the menue: Plugin GRASS > add GRASS raster layer >

  • Gisdbase : /home/user/ost4sem/grassdb
  • Location : europe
  • Mapset : PERMANENT
  • Map name : fnfpc

If you click on “ Display current Grass region ” you will see the Scandinavia area within a red frame.

Back to GRASS if you reset the default grass region

g.region -d

And then you again look at Qgis, you can visualise the red frame changing position by twice clicking on “ Display current Grass region ”.

Clipping maps and changing resolution using

g.region allow us to do two basic GIS function: resample and clip.
We would like to have 3 new maps of forest non forest percentage clipped and resampled with a different resolution from a forest non forest map. The original forest non forest map has the European extent and 1km resolution. We need to generate:

  • Italian extent map at 20km resolution,
  • Alpine extent map at 10km resolution
  • Alpine and Carpathians extent map at 5km reslution.

In the europe LOCATION different g.region exists already saved:

ls PERMANENT/windows/ 
ls */windows/

Their names and correstponding location are: EUforest/windows/: europe PCEMstat/windows/: alpine PCEM/windows/: alp_car PERMANENT/windows/: scandinavia Vmodel/windows/: italy

The Italian g.region is in Vmodel mapset, the Alpine and Carpathiens g.region is in PCEM mapset, and the Alpine region is in PCEMstat.

g.region italy@Vmodel -p 

we see that the Italian g.region has 1km res and 1500 x 1140 pixels

We now resample the g.region at 20km using the res=new_res option:

g.region res=20000 -p 

We have now 75 x 57 pixels of 20k resolution. We can create a new forest non forest map with the new extent and resolution using r.mapcalc function r.mapcalc details this uses a very simple nearest neighbor algorithm to resample to a coarser grid, for other options, check out r.resample (for producing a finer grid) or r.resamp.stats (to produce a coarser grid).

r.mapcalc fnfpc_italy20k = fnfpc 

Now create the two other maps:

g.region alpine@PCEMstat res=10000 -p
r.mapcalc fnfpc_alpine10k = fnfpc 
g.region alp_car@PCEM res=5000 -p
r.mapcalc fnfpc_alpine_carpatien5k = fnfpc 
You can save a new extent and resolution as g.mapset with the save=namenew option
g.region alp_car@PCEM res=5000 -p save=alp_car_5km_res

Visualize maps

In the forest/non-forest map fnfpc each pixel value corresponds to ha of tree cover or percentage cover within each sq km pixel.
Lets visualise the map in different ways.


The Qgis is a very simple and practical tools for visualising maps from grass or any spatial dataset. Open Qgis and visualize the maps you just created: Plugin GRASS - add GRASS raster layer -

  • Gisdbase : /home/user/ost4sem/grassdb
  • Location : europe
  • Mapset : PERMANENT
  • Map name : fnfpcalpinecarpatien5k

Also grass has a GUI for visualising maps. Open it and charge the maps just created

 g.gui wxpython

Click on: add raster layer Base map (select your maps and refresh the Map display window.

GRASS display monitor

Or alternatively (if two options wasn't enough), it's also possible to open simple windows (monitors) to visualize GRASS content These are reminiscent of older unix programs and are the least powerful and flexible option. For example, you have to run an additional command (d.zoom) to zoom in and out. In GRASS we can visualize maps in different monitors. To list available monitors type

 d.mon -l

We can open a monitor and display a raster

 g.region rast=fnfpc
 d.mon start=x0
 d.rast fnfpc

Generating multiple maps with standardized settings

Lets rename our maps for simplification

g.copy rast=fnfpc_alpine_carpatien5k,ac5 
g.copy rast=fnfpc,eu1 
g.copy rast=fnfpc_alpine10k,a10
g.copy rast=fnfpc_italy20k,i20
g.remove rast=fnfpc_alpine_carpatien5k,fnfpc_alpine10k,fnfpc_italy20k

a simple loop to

   for map in a10 ac5 eu1 i20 ; do
      g.region rast=$map
      cat ~/ost4sem/exercise/basic_adv_grass/green_palett_5class |r.colors map=$map  color=rules 
      d.mon start=PNG 
      d.rast $map 
      d.vect map=EUcountry@EUforest fcolor="none" color=red
      d.mon stop=PNG
      mv  map.png ~/ost4sem/exercise/basic_adv_grass/output_map_$map

Starting a new Grass project, new mapset, new location or new grass database

There are several ways to start a new grass project.

Grass7 create location

Using command line and an existing dataset with the correct projection information.

grass -text -c ~/ost4sem/exercise/basicadvgdalogr/inputproj.tif -c ~/ost4sem/grassdb/locationgrass7

Using a bash script and an existing projected map (useful for grass6)

The bash script: ~/ost4sem/exercise/basicadvgrass/ allows automatic creation of a new GRASS LOCATION using projection parameters defined in an existing map.
See this wiki page for details on how to use the script.


A general procedure for starting GRASS after installation, is to use a demo dataset. The general procedure consists of the following steps:

  • Creating a new empty directory to use as GISDBASE.
  • Download and move the demo dataset inside the new GISDBASE
  • Import a vector or raster map using GDAL/OGR functions and create a new location while importing grass dataset (including a location and a mapset) inside. Several template datasets exists. Very common is the spearfish DB. (~20MB)

mkdir ~/ost4sem/tmp

 cd ~/ost4sem/tmp
 mkdir ~/ost4sem/new_grass_DBASE
 mv ~/ost4sem/new_grass_DBASE/
 cd ~/ost4sem/new_grass_DBASE
 rm -r ~/ost4sem/tmp
 rm -f
 grass -text ~/ost4sem/new_grass_DBASE/spearfish60/PERMANENT input=~/ost4sem/exercise/basic_adv_grass/inputs/lc_cor2000/hdr.adf  output=landcover location=new_Location --overwrite 

or if you use a vector dsn=~/ost4sem/exercise/EUforeststats/EUcountryglgpwc3/EUcountrygpwv3.shp output=worldcountry location=newLocation

Now delete the temporary folder rm -r ~/ost4sem/newgrassDBASE/spearfish60/
As a result we will have generated:

  • a new grass GISDBASE: “newgrassDBASE”
  • a new Location setted with the projection and extent of the map we wanted to open: “new_Location”
  • a new PERMANENT Mapset including the raster or vector map we opened.

To easily create a new GRASS working directory (DBASE, LOCATION, MAPSET), we can use Qgis.
See Quantum gis.

A new LOCATION within an an existing grass Database

If you have an existing grassdb and location, you can create a new location using a GDAL / OGR import functions by specifying location=xx. For example: input=input_raster output=output_GRASS_raster location=new_Location --overwrite dsn=input_vector output=output_GRASS_vector location=new_Location  --overwrite 

A new Mapset within an an existing grass Location

From GRASS 6.4 a -c flag option allow to use g.mapset to create a new mapset

g.mapset mapset=NEW_MAPSET_NAME -c

To generate a new Mapset from a pre-existing location you can also copy the PERMANENT folder and rename it.

 cp -r PERMANENT NEW_Mapset

Then you start grass in the NEW_Mapset directory environment

grass -text /home/user/ots4sem/studycase/ITA_veg/grassdata/NEW_Mapset
wiki/basicgrass2.txt · Last modified: 2017/12/05 22:53 (external edit)