GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
GRASS Python Scripting Library

by GRASS Development Team (http://grass.osgeo.org)

Introduction

The code in lib/python/ provides grass.script in order to support GRASS scripts written in Python. The scripts/ directory of GRASS 7 contains a series of examples actually provided to the end users.

See code in:

Table of content

GRASS scripting tasks for Python provided by "grass.script"

The statement

import grass.script as grass

imports core.py, db.py, raster.py and vector.py modules.

To import only selected module

from grass.script import core as grass

Sample script (See the GRASS Wiki at http://grass.osgeo.org/wiki/GRASS_and_Python for more examples)

#!/usr/bin/env python
#%module
#% description: Checks if vector map is 3D
#% keywords: vector
#%end
#%option
#% key: map
#% type: string
#% gisprompt: old,vector,vector
#% key_desc: name
#% description: Name of vector map
#% required: yes
#%end
import sys
import grass.script as grass
def main():
info = grass.parse_command('v.info',
flags = 't',
map = options['map'])
if info['map3d'] == '1':
print 'Vector map is 3D'
print 'Vector map is 2D'
if __name__ == "__main__":
options, flags = grass.parser()
sys.exit(main())

List of modules

Core

GRASS-oriented interface to subprocess module

Interface to g.message

These all run g.message, differing only in which flag (if any) is used. fatal() is error(), but also calls sys.exit(1).

Interface to g.parser

Interface to g.parser, intended to be run from the top-level, e.g.

if __name__ == "__main__":
options, flags = grass.parser()
main()

Interface to g.tempfile

Returns the name of a temporary file, created with g.tempfile.

Key-value parsers

Interface to g.gisenv

Interface to g.region

Interface to g.findfile

Interface to g.list

Interface to g.mapsets

Interface to g.version

Color parsing

Check GRASS environment variables

Create new GRASS location

Various utilities, not specific to GRASS

Database

Interface for db.* modules.

from grass.script import db as grass

Raster

Interface for r.* modules.

from grass.script import raster as grass

Vector

Interface for v.* modules.

from grass.script import vector as grass

Setup

from grass.script import setup as gsetup

Array

from grass.script import array as garray

Authors

Glynn Clements

Martin Landa <landa.martin gmail.com>