Note: A new GRASS GIS stable version has been released: GRASS GIS 7.4. Go directly to the new manual page here

pygrass package


pygrass.errors module


pygrass.orderdict module

class pygrass.orderdict.OrderedDict(*args, **kwds)[source]

Bases: dict

Dictionary that remembers insertion order

clear() → None. Remove all items from od.[source]
copy() → a shallow copy of od[source]
classmethod fromkeys(S[, v]) → New ordered dictionary with keys from S[source]

and values equal to v (which defaults to None).

items() → list of (key, value) pairs in od[source]

od.iteritems -> an iterator over the (key, value) items in od

iterkeys() → an iterator over the keys in od[source]

od.itervalues -> an iterator over the values in od

keys() → list of keys in od[source]
pop(k[, d]) → v, remove specified key and return the corresponding value.[source]

If key is not found, d is returned if given, otherwise KeyError is raised.

popitem() → (k, v), return and remove a (key, value) pair.[source]

Pairs are returned in LIFO order if last is true or FIFO order if false.

setdefault(k[, d]) → od.get(k,d), also set od[k]=d if k not in od[source]
update(E, **F) → None. Update od from dict/iterable E and F.[source]

If E is a dict instance, does: for k in E: od[k] = E[k] If E has a .keys() method, does: for k in E.keys(): od[k] = E[k] Or if E is an iterable of items, does: for k, v in E: od[k] = v In either case, this is followed by: for k, v in F.items(): od[k] = v

values() → list of values in od[source]
viewitems() → a set-like object providing a view on od's items[source]
viewkeys() → a set-like object providing a view on od's keys[source]
viewvalues() → an object providing a view on od's values[source]

pygrass.utils module

pygrass.utils.coor2pixel(coord, region)[source]

Convert coordinates into a pixel row and col

>>> reg = Region()
>>> coor2pixel((reg.west, reg.north), reg)
(0.0, 0.0)
>>> coor2pixel((reg.east, reg.south), reg) == (reg.rows, reg.cols)
pygrass.utils.copy(existingmap, newmap, maptype, **kwargs)[source]

Copy a map

>>> copy('census', 'mycensus', 'vect')
>>> rename('mycensus', 'mynewcensus', 'vect')
>>> remove('mynewcensus', 'vect')
pygrass.utils.findfiles(dirpath, match=None)[source]

Return a list of the files

pygrass.utils.findmaps(type, pattern=None, mapset='', location='', gisdbase='')[source]

Return a list of tuple contining the names of the:

  • map
  • mapset,
  • location,
  • gisdbase
pygrass.utils.get_lib_path(modname, libname=None)[source]

Return the path of the libname contained in the module.

pygrass.utils.get_mapset_raster(mapname, mapset='')[source]

Return the mapset of the raster map

>>> get_mapset_raster('elevation')
pygrass.utils.get_mapset_vector(mapname, mapset='')[source]

Return the mapset of the vector map

>>> get_mapset_vector('census')
pygrass.utils.get_raster_for_points(poi_vector, raster, column=None, region=None)[source]

Query a raster map for each point feature of a vector


>>> from grass.pygrass.vector import VectorTopo
>>> from grass.pygrass.raster import RasterRow
>>> ele = RasterRow('elevation')
>>> copy('schools','myschools','vect')
>>> sch = VectorTopo('myschools')
>>> get_raster_for_points(sch, ele)               
[(1, 633649.2856743174, 221412.94434781274, 145.06602)...
>>> sch.table.columns.add('elevation','double precision')
>>> 'elevation' in sch.table.columns
>>> get_raster_for_points(sch, ele, 'elevation')
Filters(u'SELECT NAMESHORT, elevation FROM myschools;')
>>> cur = sch.table.execute()
>>> cur.fetchall()                                
[(u'SWIFT CREEK', 145.06602), ... (u'9TH GRADE CTR', None)]
>>> remove('myschools','vect')
  • point – point vector object
  • raster – raster object
  • column (str) – column name to update

Return the current grass environment variables

>>> getenv("MAPSET")

Return if the name is valid

>>> is_clean_name('census')
>>> is_clean_name('0census')
>>> is_clean_name('census?')
>>> is_clean_name('cénsus')
pygrass.utils.looking(obj, filter_string)[source]
>>> import grass.lib.vector as libvect
>>> sorted(looking(libvect, '*by_box*'))  
['Vect_select_areas_by_box', 'Vect_select_isles_by_box',
 'Vect_select_lines_by_box', 'Vect_select_nodes_by_box']
pygrass.utils.pixel2coor(pixel, region)[source]

Convert row and col of a pixel into a coordinates

>>> reg = Region()
>>> pixel2coor((0, 0), reg) == (reg.north, reg.west)
>>> pixel2coor((reg.cols, reg.rows), reg) == (reg.south, reg.east)
pygrass.utils.r_export(rast, output='', fmt='png', **kargs)[source]
pygrass.utils.remove(oldname, maptype)[source]

Remove a map

pygrass.utils.rename(oldname, newname, maptype, **kwargs)[source]

Rename a map

pygrass.utils.set_path(modulename, dirname=None, path='.')[source]

Set sys.path looking in the the local directory GRASS directories.

  • modulename – string with the name of the GRASS module
  • dirname – string with the directory name containing the python libraries, default None
  • path – string with the path to reach the dirname locally.

“set_path” example working locally with the source code of a module ( calling the function with all the parameters. Below it is reported the directory structure on the module.

grass_prompt> pwd

grass_prompt> tree ../../../
|-- ...
|-- libgreen
|   |--
|   +--
   |-- Makefile
   |-- libhydro
   |   |--
   |   +--
       |-- Makefile
       |-- ...

21 directories, 125 files

in the source code the function is called with the following parameters:

set_path('', 'libhydro', '..')
set_path('', 'libgreen', os.path.join('..', '..'))

when we are executing the module: locally from the command line:

grass_prompt> python --ui

In this way we are executing the local code even if the module was already installed as grass-addons and it is available in GRASS standards path.

The function is cheching if the dirname is provided and if the directory exists and it is available using the path provided as third parameter, if yes add the path to sys.path to be importable, otherwise it will check on GRASS GIS standard paths.

pygrass.utils.split_in_chunk(iterable, length=10)[source]

Split a list in chunk.

>>> for chunk in split_in_chunk(range(25)): print chunk
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
(10, 11, 12, 13, 14, 15, 16, 17, 18, 19)
(20, 21, 22, 23, 24)
>>> for chunk in split_in_chunk(range(25), 3): print chunk
(0, 1, 2)
(3, 4, 5)
(6, 7, 8)
(9, 10, 11)
(12, 13, 14)
(15, 16, 17)
(18, 19, 20)
(21, 22, 23)
pygrass.utils.table_exist(cursor, table_name)[source]

Return True if the table exist False otherwise

Module contents

Table Of Contents

Previous topic

PyGRASS message interface

Next topic

pygrass.gis package

Note: A new GRASS GIS stable version has been released: GRASS GIS 7.4. Go directly to the new manual page here

Help Index | Topics Index | Keywords Index | Full Index

© 2003-2018 GRASS Development Team, GRASS GIS 7.0.7svn Reference Manual