Skip to content

Commit

Permalink
pygrass: improve documentation about grass database managment, region…
Browse files Browse the repository at this point in the history
… and raster

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@60511 15284696-431f-4ddb-bdfa-cd5b030d7da7
  • Loading branch information
lucadelu committed May 27, 2014
1 parent b6fe566 commit 5a62908
Show file tree
Hide file tree
Showing 8 changed files with 277 additions and 151 deletions.
3 changes: 3 additions & 0 deletions lib/python/pygrass/docs/gis.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.. _GRASSdatabase-label:

GRASS database management
===============================
Expand All @@ -17,6 +18,8 @@ of GRASS database: Gisdbase, Location and Mapset
.. autoclass:: pygrass.gis.VisibleMapset
:members:

.. _Region-label:

Region management
======================

Expand Down
1 change: 1 addition & 0 deletions lib/python/pygrass/docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Contents:
vector
attributes
modules
messages


References
Expand Down
21 changes: 10 additions & 11 deletions lib/python/pygrass/docs/raster.rst
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ Export and import from a file: ::
>>> land.write_cats_rules('land_rules.csv', ';')
>>> land.read_cats_rules('land_rules.csv', ';')

.. autoclass:: pygrass.raster.category.Category
:members:

.. _RasterRow-label:

Expand Down Expand Up @@ -210,6 +212,8 @@ time you are writing a new map, the row is add to the file as the last row. ::
>>> new.exist()
False

.. autoclass:: pygrass.raster.RasterRow
:members:

.. _RasterRowIO-label:

Expand All @@ -233,7 +237,8 @@ for reading and use the default row write access as in the RasterRow class. ::
[ 144.99488831 145.22894287 145.57142639]
>>> elev.close()


.. autoclass:: pygrass.raster.RasterRowIO
:members:

.. _RasterSegment-label:

Expand Down Expand Up @@ -302,7 +307,8 @@ to write a single value to the map. ::
>>> elev.close()
>>> elev.remove()


.. autoclass:: pygrass.raster.RasterSegment
:members:

.. _RasterNumpy-label:

Expand Down Expand Up @@ -342,7 +348,8 @@ to load all the map in memory. ::
True
>>> el.remove()


.. autoclass:: pygrass.raster.RasterNumpy
:members:

.. _Buffer-label:

Expand Down Expand Up @@ -384,14 +391,6 @@ History
:members:


.. _Category-label:

Category
--------

.. autoclass:: pygrass.raster.category.Category
:members:

.. _Raster library: http://grass.osgeo.org/programming7/rasterlib.html
.. _RowIO library: http://grass.osgeo.org/programming7/rowiolib.html
.. _Segmentation library: http://grass.osgeo.org/programming7/segmentlib.html
Expand Down
70 changes: 59 additions & 11 deletions lib/python/pygrass/gis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ def _check(value, path, type):


def set_current_mapset(mapset, location=None, gisdbase=None):
"""Set the current mapset as working area
"""
libgis.G_setenv('MAPSET', mapset)
if location:
libgis.G_setenv('LOCATION_NAME', location)
Expand All @@ -68,6 +70,7 @@ def set_current_mapset(mapset, location=None, gisdbase=None):


def make_mapset(mapset, location=None, gisdbase=None):
"""Create a new mapset"""
res = libgis.G_make_mapset(gisdbase, location, mapset)
if res == -1:
raise GrassError("Cannot create new mapset")
Expand All @@ -83,6 +86,7 @@ class Gisdbase(object):
>>> gisdbase.name == gisenv()['GISDBASE']
True
..
"""
def __init__(self, gisdbase=''):
self.name = gisdbase
Expand All @@ -93,7 +97,8 @@ def _get_name(self):
def _set_name(self, name):
self._name = _check(name, '', "GISDBASE")

name = property(fget=_get_name, fset=_set_name)
name = property(fget=_get_name, fset=_set_name,
doc="Set or obtain the name of GISDBASE")

def __str__(self):
return self.name
Expand Down Expand Up @@ -122,6 +127,7 @@ def __iter__(self):
for loc in self.locations():
yield Location(loc, self.name)

# TODO remove or complete this function
def new_location(self):
if libgis.G__make_location() != 0:
raise GrassError("Cannot create new location")
Expand Down Expand Up @@ -167,15 +173,17 @@ def _get_gisdb(self):
def _set_gisdb(self, gisdb):
self._gisdb = _check(gisdb, '', "GISDBASE")

gisdbase = property(fget=_get_gisdb, fset=_set_gisdb)
gisdbase = property(fget=_get_gisdb, fset=_set_gisdb,
doc="Set or obtain the name of GISDBASE")

def _get_name(self):
return self._name

def _set_name(self, name):
self._name = _check(name, self._gisdb, "LOCATION_NAME")

name = property(fget=_get_name, fset=_set_name)
name = property(fget=_get_name, fset=_set_name,
doc="Set or obtain the name of LOCATION")

def __getitem__(self, mapset):
if mapset in self.mapsets():
Expand All @@ -198,11 +206,20 @@ def __repr__(self):
return 'Location(%r)' % self.name

def mapsets(self, pattern=None, permissions=True):
"""Return a list of the available mapsets. ::
"""Return a list of the available mapsets.
:param pattern: the pattern to filter the result
:type pattern: str
:param permissions: check the permission of mapset
:type permissions: bool
:returns: a list of mapset's names
::
>>> location = Location()
>>> location.mapsets()
['PERMANENT', 'user1']
..
"""
mapsets = [mapset for mapset in self]
if permissions:
Expand All @@ -228,6 +245,8 @@ class Mapset(object):
'nc_basic_spm_grass7'
>>> mapset.name
'user1'
..
"""
def __init__(self, mapset='', location='', gisdbase=''):
self.gisdbase = gisdbase
Expand All @@ -241,23 +260,26 @@ def _get_gisdb(self):
def _set_gisdb(self, gisdb):
self._gisdb = _check(gisdb, '', "GISDBASE")

gisdbase = property(fget=_get_gisdb, fset=_set_gisdb)
gisdbase = property(fget=_get_gisdb, fset=_set_gisdb,
doc="Set or obtain the name of GISDBASE")

def _get_loc(self):
return self._loc

def _set_loc(self, loc):
self._loc = _check(loc, self._gisdb, "LOCATION_NAME")

location = property(fget=_get_loc, fset=_set_loc)
location = property(fget=_get_loc, fset=_set_loc,
doc="Set or obtain the name of LOCATION")

def _get_name(self):
return self._name

def _set_name(self, name):
self._name = _check(name, join(self._gisdb, self._loc), "MAPSET")

name = property(fget=_get_name, fset=_set_name)
name = property(fget=_get_name, fset=_set_name,
doc="Set or obtain the name of MAPSET")

def __str__(self):
return self.name
Expand All @@ -281,6 +303,11 @@ def glist(self, type, pattern=None):
* 'vect',
* 'view3d'
:param type: the type of element to query
:type type: str
:param pattern: the pattern to filter the result
:type pattern: str
::
>>> mapset = Mapset('PERMANENT')
Expand All @@ -290,6 +317,8 @@ def glist(self, type, pattern=None):
['basins', 'elevation', ...]
>>> mapset.glist('rast', pattern='el*')
['elevation_shade', 'elevation']
..
"""
if type not in ETYPE:
str_err = "Type %s is not valid, valid types are: %s."
Expand All @@ -307,6 +336,7 @@ def glist(self, type, pattern=None):
return elist

def is_current(self):
"""Check if the MAPSET is the working MAPSET"""
return (self.name == libgis.G_getenv('MAPSET') and
self.location == libgis.G_getenv('LOCATION_NAME') and
self.gisdbase == libgis.G_getenv('GISDBASE'))
Expand All @@ -331,6 +361,8 @@ class VisibleMapset(object):
>>> mapset = VisibleMapset('user1')
>>> mapset
['user1', 'PERMANENT']
..
"""
def __init__(self, mapset, location='', gisdbase=''):
self.mapset = mapset
Expand All @@ -357,27 +389,43 @@ def read(self):
return lns

def _write(self, mapsets):
"""Write to SEARCH_PATH file the changes in the search path"""
"""Write to SEARCH_PATH file the changes in the search path
:param mapsets: a list of mapset's names
:type mapsets: list
"""
with open(self.spath, "w+") as f:
ms = self.location.mapsets()
f.write('%s' % '\n'.join([m for m in mapsets if m in ms]))

def add(self, mapset):
"""Add a mapset to the search path"""
"""Add a mapset to the search path
:param mapset: a mapset's name
:type mapset: str
"""
if mapset not in self.read() and mapset in self.location:
with open(self.spath, "a+") as f:
f.write('\n%s' % mapset)
else:
raise TypeError('Mapset not found')

def remove(self, mapset):
"""Remove mapset to the search path"""
"""Remove mapset to the search path
:param mapset: a mapset's name
:type mapset: str
"""
mapsets = self.read()
mapsets.remove(mapset)
self._write(mapsets)

def extend(self, mapsets):
"""Add more mapsets to the search path"""
"""Add more mapsets to the search path
:param mapsets: a list of mapset's names
:type mapsets: list
"""
ms = self.location.mapsets()
final = self.read()
final.extend([m for m in mapsets if m in ms and m not in final])
Expand Down
Loading

0 comments on commit 5a62908

Please sign in to comment.