I wanted to begin with customized tool development in Grass GIS or QGIS grass. How should i begin in to develop the tool. I search a lot in google search engine for begin with tool development in Grass GIS or QGIS grass. Can any one please help me in the tool development process in Grass.

1 Answer 1


If you want to develop a module for GRASS GIS as a Python script or just a script which uses GRASS GIS (which is, in fact, almost the same), start with http://grasswiki.osgeo.org/wiki/GRASS_and_Python.

You need to be in GRASS session and start a Python script from there, then you can import

from grass.script import core as gcore

which enables you to call GRASS modules/commands like this:

return_code = gcore.run_command('r.lake', elevation=elevation,
                                lake=outputs[i], xy=coordinates,

The rest is just the normal Python. However, if you want your script to have command line parameters, it is great idea to use GRASS mechanism to process command line parameters because at the same time you turned your Python script into GRASS module. Here is the sample code of a full module:

#!/usr/bin/env python

#% description: Fills lake at given point(s) to given levels
#% keywords: raster
#% keywords: hydrology
#%option G_OPT_R_ELEV
#% key: output
#% type: string
#% label: Basename for output maps
#% description: Maps will start with this basename
#% key: start_water_level
#% type: double
#% label: Start water level
#% description: Initial water level at the start of the simulation
#% required: yes
#% guisection: Water
#% key: end_water_level
#% type: double
#% label: Final (maximal) water level
#% description: Maximal water level to be reached
#% required: yes
#% guisection: Water
#%option G_OPT_M_COORDS
#% label: Seed point coordinates
#% description: Either this coordinates pair or a seed map name have to be specified
#% required: no
#% guisection: Water

import sys

from grass.script import core as gcore

def frange(x, y, step):
    while x <= y:
        yield x
        x += step

def main():
    options, flags = gcore.parser()

    elevation = options['elevation']
    basename = options['output']
    start_water_level = float(options['start_water_level'])
    end_water_level = float(options['end_water_level'])
    water_level_step = 0.5
    coordinates = options['coordinates']

    water_levels = [step for step in frange(start_water_level,
                                            end_water_level, water_level_step)]
    outputs = ['%s%s%s' % (basename, '_', water_level)
               for water_level in water_levels]

    for i, water_level in enumerate(water_levels):
        return_code = gcore.run_command('r.lake', elevation=elevation,
                                        lake=outputs[i], xy=coordinates,

if __name__ == '__main__':

The #! line enables running in unix-like systems without explicitly using Python interpreter in command line. The #% lines are used by GRASS to parse the options and provide them in options, flags = gcore.parser() dictionaries. The rest of the code, in this case, is computing different water level which are used as an input to GRASS r.lake module which simulates what is flooded when water has certain level on DEM. This script is a simple version of r.lake.series GRASS addon (which source you can google using svn r.lake.series and find how to check parameters, remove maps, include license and authors).

You can run this script/module using:

# inside GRASS GIS session (command line)
python ~/myscripts/r.lake.series.simple.py --help
python ~/myscripts/r.lake.series.simple.py elevation=elevation output=lake start=15 end=18 coor=548841.585956,4388325.94431

If you want to develop a module for GRASS GIS as program written in C or C++, start with http://grass.osgeo.org/development/how-to-start/ and check the sample modules in GRASS source code (doc/raster/r.example/ and doc/vector/v.example/).

Note that C API functions are accessible in Python through ctypes and, speaking about GRASS GIS 7, there is also a more Pythonic interface call PyGRASS, so in Python you can use all things available in GRASS C API.

In QGIS you can use QGIS Processing. How to use it in scripts is described in Documentation for QGIS 2.0 -> User guide/Manual -> QGIS processing framework -> Using processing algorithms from the console (I cannot use a link now). Basically, you import processing module and use runalg function:

import processing
processing.runalg('algorithm_name', params, ..., and_outputs, ...)

This suppose that you have QGIS configured with GRASS and you know the parameters and outputs of the algorithm (this can be found also using processing functions such as algoptions and alghelp).


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.