[GRASS-SVN] r54371 - in grass/trunk/lib/python/pygrass: . gis

svn_grass at osgeo.org svn_grass at osgeo.org
Sun Dec 23 03:13:18 PST 2012


Author: zarch
Date: 2012-12-23 03:13:18 -0800 (Sun, 23 Dec 2012)
New Revision: 54371

Added:
   grass/trunk/lib/python/pygrass/gis/
   grass/trunk/lib/python/pygrass/gis/__init__.py
   grass/trunk/lib/python/pygrass/gis/region.py
Removed:
   grass/trunk/lib/python/pygrass/region.py
Modified:
   grass/trunk/lib/python/pygrass/__init__.py
Log:
Add the 'gis' module with Gisdbase, Location and Mapset, and move region.py to gis/region.py

Modified: grass/trunk/lib/python/pygrass/__init__.py
===================================================================
--- grass/trunk/lib/python/pygrass/__init__.py	2012-12-23 10:43:37 UTC (rev 54370)
+++ grass/trunk/lib/python/pygrass/__init__.py	2012-12-23 11:13:18 UTC (rev 54371)
@@ -13,7 +13,7 @@
 
 _sys.path.append(_os.path.join(_os.sep, *_pygrasspath[:-1]))
 
-import region
+import gis
 import raster
 import vector
 import modules

Added: grass/trunk/lib/python/pygrass/gis/__init__.py
===================================================================
--- grass/trunk/lib/python/pygrass/gis/__init__.py	                        (rev 0)
+++ grass/trunk/lib/python/pygrass/gis/__init__.py	2012-12-23 11:13:18 UTC (rev 54371)
@@ -0,0 +1,281 @@
+# -*- coding: utf-8 -*-
+#!/usr/bin/env python2.7
+from __future__ import print_function
+#import os
+from os import listdir
+from os.path import join
+import ctypes as ct
+
+from grass import script
+#from grass.script import setup
+#
+#
+#GISBASE = "/home/pietro/docdat/src/gis/grass/grass70/dist.x86_64-unknown-linux-gnu"
+#LOCATION = "nc_basic_spm_grass7'"
+#GISDBASE = "/home/pietro/docdat/gis"
+#MAPSET = "sqlite"
+#GUI = "wxpython"
+#
+#setup.init(GISBASE, GISDBASE, LOCATION, MAPSET)
+script.gisenv()
+
+import grass.lib.gis as libgis
+from grass.pygrass.functions import getenv
+from grass.pygrass.errors import GrassError
+
+
+#write dec to check if user have permissions or not
+
+ETYPE = {'rast': libgis.G_ELEMENT_RASTER,
+         'rast3d': libgis.G_ELEMENT_RASTER3D,
+         'vect': libgis.G_ELEMENT_VECTOR,
+         'oldvect': libgis.G_ELEMENT_OLDVECTOR,
+         'asciivect': libgis.G_ELEMENT_ASCIIVECTOR,
+         'icon': libgis.G_ELEMENT_ICON,
+         'labels': libgis.G_ELEMENT_LABEL,
+         'sites': libgis.G_ELEMENT_SITE,
+         'region': libgis.G_ELEMENT_REGION,
+         'region3d': libgis.G_ELEMENT_REGION3D,
+         'group': libgis.G_ELEMENT_GROUP,
+         'view3d': libgis.G_ELEMENT_3DVIEW}
+
+
+CHECK_IS = {"GISBASE": libgis.G_is_gisbase,
+            "GISDBASE": lambda x: True,
+            "LOCATION_NAME": libgis.G_is_location,
+            "MAPSET": libgis.G_is_mapset}
+
+
+def _check(value, path, type):
+    #import pdb; pdb.set_trace()
+    if value and CHECK_IS[type](join(path, value)):
+        return value
+    elif value is '':
+        return getenv(type)
+    else:
+        raise GrassError("%s <%s> not found." % (type.title(),
+                                                 join(path, value)))
+
+
+class Gisdbase(object):
+    """Return Gisdbase object. ::
+
+        >>> gisdbase = Gisdbase()
+        >>> gisdbase.name          # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
+        '/home/...'
+
+
+    """
+    def __init__(self, gisdbase=''):
+        self.name = gisdbase
+
+    def _get_name(self):
+        return self._name
+
+    def _set_name(self, name):
+        self._name = _check(name, '', "GISDBASE")
+
+    name = property(fget=_get_name, fset=_set_name)
+
+    def __str__(self):
+        return self.name
+
+    def __repr__(self):
+        return 'Gisdbase(%s)' % self.name
+
+    def __getitem__(self, location):
+        """Return a Location object. ::
+
+            >>> gisdbase = Gisdbase()
+            >>> gisdbase['nc_basic_spm_grass7']
+            Location('nc_basic_spm_grass7')
+
+        ..
+        """
+        if location in self.locations():
+            return Location(location, self.name)
+        else:
+            raise KeyError('Location: %s does not exist' % location)
+
+    def __iter__(self):
+        for loc in self.locations():
+            yield Location(loc, self.name)
+
+    def new_location(self):
+        if libgis.G__make_location() != 0:
+            raise GrassError("I cannot create a new mapset.")
+
+    def locations(self):
+        """Return a list of locations that are available in the gisdbase: ::
+
+            >>> gisdbase = Gisdbase()
+            >>> gisdbase.locations()                     # doctest: +ELLIPSIS
+            [...]
+
+        ..
+        """
+        locations = []
+        for loc in listdir(self.name):
+            if libgis.G_is_location(join(self.name, loc)):
+                locations.append(loc)
+        locations.sort()
+        return locations
+
+
+class Location(object):
+    """Location object ::
+
+        >>> location = Location()
+        >>> location                                      # doctest: +ELLIPSIS
+        Location(...)
+        >>> location.gisdbase                              # doctest: +ELLIPSIS
+        '/home/...'
+        >>> location.name
+        'nc_basic_spm_grass7'
+    """
+    def __init__(self, location='', gisdbase=''):
+        self.gisdbase = gisdbase
+        self.name = location
+
+    def _get_gisdb(self):
+        return self._gisdb
+
+    def _set_gisdb(self, gisdb):
+        self._gisdb = _check(gisdb, '', "GISDBASE")
+
+    gisdbase = property(fget=_get_gisdb, fset=_set_gisdb)
+
+    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)
+
+    def __getitem__(self, mapset):
+        if mapset in self.mapsets():
+            return Mapset(mapset)
+        else:
+            raise KeyError('Mapset: %s does not exist' % mapset)
+
+    def __iter__(self):
+        for mapset in libgis.G_available_mapsets():
+            mapset_name = ct.cast(mapset, ct.c_char_p).value
+            if mapset_name and libgis.G__mapset_permissions(mapset):
+                yield mapset_name
+            else:
+                break
+
+    def __len__(self):
+        return len(self.mapsets())
+
+    def __str__(self):
+        return self.name
+
+    def __repr__(self):
+        return 'Location(%r)' % self.name
+
+    def mapsets(self):
+        """Return a list of the available mapsets. ::
+
+            >>> location = Location()
+            >>> location.mapsets()
+            ['PERMANENT', 'user1']
+        """
+        return [mapset for mapset in self]
+
+    def new_mapset(self, mapset):
+        if libgis.G__make_mapset(self.gisdbase, self.location, mapset) != 0:
+            raise GrassError("I cannot create a new mapset.")
+
+
+class Mapset(object):
+    """Mapset ::
+
+        >>> mapset = Mapset()
+        >>> mapset
+        Mapset('user1')
+        >>> mapset.gisdbase                               # doctest: +ELLIPSIS
+        '/home/...'
+        >>> mapset.location
+        'nc_basic_spm_grass7'
+        >>> mapset.name
+        'user1'
+    """
+    def __init__(self, mapset='', location='', gisdbase=''):
+        self.gisdbase = gisdbase
+        self.location = location
+        self.name = mapset
+
+    def _get_gisdb(self):
+        return self._gisdb
+
+    def _set_gisdb(self, gisdb):
+        self._gisdb = _check(gisdb, '', "GISDBASE")
+
+    gisdbase = property(fget=_get_gisdb, fset=_set_gisdb)
+
+    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)
+
+    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)
+
+    def __str__(self):
+        return self.name
+
+    def __repr__(self):
+        return 'Mapset(%r)' % self.name
+
+    def glist(self, type):
+        """Return a list of grass types like:
+
+            * 'asciivect',
+            * 'group',
+            * 'icon',
+            * 'labels',
+            * 'oldvect',
+            * 'rast',
+            * 'rast3d',
+            * 'region',
+            * 'region3d',
+            * 'sites',
+            * 'vect',
+            * 'view3d'
+
+        ::
+
+            >>> mapset = Mapset('PERMANENT')
+            >>> rast = mapset.glist('rast')
+            >>> rast.sort()
+            >>> rast                                      # doctest: +ELLIPSIS
+            ['basins', 'elevation', ...]
+        """
+        if type not in ETYPE:
+            str_err = "Type %s is not valid, valid types are: %s."
+            raise TypeError(str_err % (type, ', '.join(ETYPE.keys())))
+        clist = libgis.G_list(ETYPE[type], self.gisdbase,
+                              self.location, self.name)
+        elist = []
+        for el in clist:
+            el_name = ct.cast(el, ct.c_char_p).value
+            if el_name:
+                elist.append(el_name)
+            else:
+                return elist
+
+
+if __name__ == "__main__":
+    import doctest
+    doctest.testmod(verbose=False)
\ No newline at end of file

Copied: grass/trunk/lib/python/pygrass/gis/region.py (from rev 54370, grass/trunk/lib/python/pygrass/region.py)
===================================================================
--- grass/trunk/lib/python/pygrass/gis/region.py	                        (rev 0)
+++ grass/trunk/lib/python/pygrass/gis/region.py	2012-12-23 11:13:18 UTC (rev 54371)
@@ -0,0 +1,224 @@
+# -*- coding: utf-8 -*-
+"""
+Created on Fri May 25 12:57:10 2012
+
+ at author: Pietro Zambelli
+"""
+import ctypes
+import grass.lib.gis as libgis
+import grass.script as grass
+
+from pygrass.errors import GrassError
+
+
+class Region(object):
+    def __init__(self, default=False):
+        """::
+
+            >>> default = Region(default=True)
+            >>> current = Region()
+            >>> default == current
+            True
+            >>> current.cols
+            1500
+            >>> current.ewres
+            10.0
+            >>> current.cols = 3000
+            >>> current.ewres
+            5.0
+            >>> current.ewres = 20.0
+            >>> current.cols
+            750
+            >>> current.set_current()
+            >>> default == current
+            False
+            >>> current.set_default()
+            >>> default = Region(default=True)
+            >>> default == current
+            True
+            >>> default
+            Region(n=228500, s=215000, e=645000, w=630000, nsres=10, ewres=20)
+            >>> current
+            Region(n=228500, s=215000, e=645000, w=630000, nsres=10, ewres=20)
+            >>> default.ewres = 10.
+            >>> default.set_default()
+        """
+        self.c_region = ctypes.pointer(libgis.Cell_head())
+        if default:
+            self.get_default()
+        else:
+            self.get_current()
+
+    def _set_param(self, key, value):
+        grass.run_command('g.region', **{key: value})
+
+    #----------LIMITS----------
+    def _get_n(self):
+        return self.c_region.contents.north
+
+    def _set_n(self, value):
+        self.c_region.contents.north = value
+
+    north = property(fget=_get_n, fset=_set_n)
+
+    def _get_s(self):
+        return self.c_region.contents.south
+
+    def _set_s(self, value):
+        self.c_region.contents.south = value
+
+    south = property(fget=_get_s, fset=_set_s)
+
+    def _get_e(self):
+        return self.c_region.contents.east
+
+    def _set_e(self, value):
+        self.c_region.contents.east = value
+
+    east = property(fget=_get_e, fset=_set_e)
+
+    def _get_w(self):
+        return self.c_region.contents.west
+
+    def _set_w(self, value):
+        self.c_region.contents.west = value
+
+    west = property(fget=_get_w, fset=_set_w)
+
+    def _get_t(self):
+        return self.c_region.contents.top
+
+    def _set_t(self, value):
+        self.c_region.contents.top = value
+
+    top = property(fget=_get_t, fset=_set_t)
+
+    def _get_b(self):
+        return self.c_region.contents.bottom
+
+    def _set_b(self, value):
+        self.c_region.contents.bottom = value
+
+    bottom = property(fget=_get_b, fset=_set_b)
+
+    #----------RESOLUTION----------
+    def _get_rows(self):
+        return self.c_region.contents.rows
+
+    def _set_rows(self, value):
+        self.c_region.contents.rows = value
+        self.adjust(rows=True)
+
+    rows = property(fget=_get_rows, fset=_set_rows)
+
+    def _get_cols(self):
+        return self.c_region.contents.cols
+
+    def _set_cols(self, value):
+        self.c_region.contents.cols = value
+        self.adjust(cols=True)
+
+    cols = property(fget=_get_cols, fset=_set_cols)
+
+    def _get_nsres(self):
+        return self.c_region.contents.ns_res
+
+    def _set_nsres(self, value):
+        self.c_region.contents.ns_res = value
+        self.adjust()
+
+    nsres = property(fget=_get_nsres, fset=_set_nsres)
+
+    def _get_ewres(self):
+        return self.c_region.contents.ew_res
+
+    def _set_ewres(self, value):
+        self.c_region.contents.ew_res = value
+        self.adjust()
+
+    ewres = property(fget=_get_ewres, fset=_set_ewres)
+
+    def _get_tbres(self):
+        return self.c_region.contents.tb_res
+
+    def _set_tbres(self, value):
+        self.c_region.contents.tb_res = value
+        self.adjust()
+
+    tbres = property(fget=_get_tbres, fset=_set_tbres)
+
+    @property
+    def zone(self):
+        return self.c_region.contents.zone
+
+    @property
+    def proj(self):
+        return self.c_region.contents.proj
+
+    #----------MAGIC METHODS----------
+    def __repr__(self):
+        return 'Region(n=%g, s=%g, e=%g, w=%g, nsres=%g, ewres=%g)' % (
+               self.north, self.south, self.east, self.west,
+               self.nsres, self.ewres)
+
+    def __unicode__(self):
+        return grass.pipe_command("g.region", flags="p").communicate()[0]
+
+    def __str__(self):
+        return self.__unicode__()
+
+    def __eq__(self, reg):
+        attrs = ['north', 'south', 'west', 'east', 'top', 'bottom',
+                 'nsres', 'ewres', 'tbres']
+        for attr in attrs:
+            if getattr(self, attr) != getattr(reg, attr):
+                return False
+        return True
+
+    def iteritems(self):
+        return [('projection', self.proj),
+                ('zone', self.zone),
+                ('north', self.north),
+                ('south', self.south),
+                ('west', self.west),
+                ('east', self.east),
+                ('top', self.top),
+                ('bottom', self.bottom),
+                ('nsres', self.nsres),
+                ('ewres', self.ewres),
+                ('tbres', self.tbres),
+                ('rows', self.rows),
+                ('cols', self.cols),
+                ('cells', self.rows * self.cols)]
+
+    #----------METHODS----------
+    def zoom(self, raster_name):
+        """Shrink region until it meets non-NULL data from this raster map:"""
+        self._set_param('zoom', str(raster_name))
+        self.get_current()
+
+    def align(self, raster_name):
+        """Adjust region cells to cleanly align with this raster map"""
+        self._set_param('align', str(raster_name))
+        self.get_current()
+
+    def adjust(self, rows=False, cols=False):
+        """Adjust rows and cols number according with the nsres and ewres
+        resolutions. If rows or cols parameters are True, the adjust method
+        update nsres and ewres according with the rows and cols numbers.
+        """
+        libgis.G_adjust_Cell_head(self.c_region, bool(rows), bool(cols))
+
+    def get_current(self):
+        libgis.G_get_set_window(self.c_region)
+
+    def set_current(self):
+        libgis.G_set_window(self.c_region)
+
+    def get_default(self):
+        libgis.G_get_window(self.c_region)
+
+    def set_default(self):
+        self.adjust()
+        if libgis.G_put_window(self.c_region) < 0:
+            raise GrassError("Cannot change region (WIND file).")
\ No newline at end of file

Deleted: grass/trunk/lib/python/pygrass/region.py
===================================================================
--- grass/trunk/lib/python/pygrass/region.py	2012-12-23 10:43:37 UTC (rev 54370)
+++ grass/trunk/lib/python/pygrass/region.py	2012-12-23 11:13:18 UTC (rev 54371)
@@ -1,224 +0,0 @@
-# -*- coding: utf-8 -*-
-"""
-Created on Fri May 25 12:57:10 2012
-
- at author: Pietro Zambelli
-"""
-import ctypes
-import grass.lib.gis as libgis
-import grass.script as grass
-
-from errors import GrassError
-
-
-class Region(object):
-    def __init__(self, default=False):
-        """::
-
-            >>> default = Region(default=True)
-            >>> current = Region()
-            >>> default == current
-            True
-            >>> current.cols
-            1500
-            >>> current.ewres
-            10.0
-            >>> current.cols = 3000
-            >>> current.ewres
-            5.0
-            >>> current.ewres = 20.0
-            >>> current.cols
-            750
-            >>> current.set_current()
-            >>> default == current
-            False
-            >>> current.set_default()
-            >>> default = Region(default=True)
-            >>> default == current
-            True
-            >>> default
-            Region(n=228500, s=215000, e=645000, w=630000, nsres=10, ewres=20)
-            >>> current
-            Region(n=228500, s=215000, e=645000, w=630000, nsres=10, ewres=20)
-            >>> default.ewres = 10.
-            >>> default.set_default()
-        """
-        self.c_region = ctypes.pointer(libgis.Cell_head())
-        if default:
-            self.get_default()
-        else:
-            self.get_current()
-
-    def _set_param(self, key, value):
-        grass.run_command('g.region', **{key: value})
-
-    #----------LIMITS----------
-    def _get_n(self):
-        return self.c_region.contents.north
-
-    def _set_n(self, value):
-        self.c_region.contents.north = value
-
-    north = property(fget=_get_n, fset=_set_n)
-
-    def _get_s(self):
-        return self.c_region.contents.south
-
-    def _set_s(self, value):
-        self.c_region.contents.south = value
-
-    south = property(fget=_get_s, fset=_set_s)
-
-    def _get_e(self):
-        return self.c_region.contents.east
-
-    def _set_e(self, value):
-        self.c_region.contents.east = value
-
-    east = property(fget=_get_e, fset=_set_e)
-
-    def _get_w(self):
-        return self.c_region.contents.west
-
-    def _set_w(self, value):
-        self.c_region.contents.west = value
-
-    west = property(fget=_get_w, fset=_set_w)
-
-    def _get_t(self):
-        return self.c_region.contents.top
-
-    def _set_t(self, value):
-        self.c_region.contents.top = value
-
-    top = property(fget=_get_t, fset=_set_t)
-
-    def _get_b(self):
-        return self.c_region.contents.bottom
-
-    def _set_b(self, value):
-        self.c_region.contents.bottom = value
-
-    bottom = property(fget=_get_b, fset=_set_b)
-
-    #----------RESOLUTION----------
-    def _get_rows(self):
-        return self.c_region.contents.rows
-
-    def _set_rows(self, value):
-        self.c_region.contents.rows = value
-        self.adjust(rows=True)
-
-    rows = property(fget=_get_rows, fset=_set_rows)
-
-    def _get_cols(self):
-        return self.c_region.contents.cols
-
-    def _set_cols(self, value):
-        self.c_region.contents.cols = value
-        self.adjust(cols=True)
-
-    cols = property(fget=_get_cols, fset=_set_cols)
-
-    def _get_nsres(self):
-        return self.c_region.contents.ns_res
-
-    def _set_nsres(self, value):
-        self.c_region.contents.ns_res = value
-        self.adjust()
-
-    nsres = property(fget=_get_nsres, fset=_set_nsres)
-
-    def _get_ewres(self):
-        return self.c_region.contents.ew_res
-
-    def _set_ewres(self, value):
-        self.c_region.contents.ew_res = value
-        self.adjust()
-
-    ewres = property(fget=_get_ewres, fset=_set_ewres)
-
-    def _get_tbres(self):
-        return self.c_region.contents.tb_res
-
-    def _set_tbres(self, value):
-        self.c_region.contents.tb_res = value
-        self.adjust()
-
-    tbres = property(fget=_get_tbres, fset=_set_tbres)
-
-    @property
-    def zone(self):
-        return self.c_region.contents.zone
-
-    @property
-    def proj(self):
-        return self.c_region.contents.proj
-
-    #----------MAGIC METHODS----------
-    def __repr__(self):
-        return 'Region(n=%g, s=%g, e=%g, w=%g, nsres=%g, ewres=%g)' % (
-               self.north, self.south, self.east, self.west,
-               self.nsres, self.ewres)
-
-    def __unicode__(self):
-        return grass.pipe_command("g.region", flags="p").communicate()[0]
-
-    def __str__(self):
-        return self.__unicode__()
-
-    def __eq__(self, reg):
-        attrs = ['north', 'south', 'west', 'east', 'top', 'bottom',
-                 'nsres', 'ewres', 'tbres']
-        for attr in attrs:
-            if getattr(self, attr) != getattr(reg, attr):
-                return False
-        return True
-
-    def iteritems(self):
-        return [('projection', self.proj),
-                ('zone', self.zone),
-                ('north', self.north),
-                ('south', self.south),
-                ('west', self.west),
-                ('east', self.east),
-                ('top', self.top),
-                ('bottom', self.bottom),
-                ('nsres', self.nsres),
-                ('ewres', self.ewres),
-                ('tbres', self.tbres),
-                ('rows', self.rows),
-                ('cols', self.cols),
-                ('cells', self.rows * self.cols)]
-
-    #----------METHODS----------
-    def zoom(self, raster_name):
-        """Shrink region until it meets non-NULL data from this raster map:"""
-        self._set_param('zoom', str(raster_name))
-        self.get_current()
-
-    def align(self, raster_name):
-        """Adjust region cells to cleanly align with this raster map"""
-        self._set_param('align', str(raster_name))
-        self.get_current()
-
-    def adjust(self, rows=False, cols=False):
-        """Adjust rows and cols number according with the nsres and ewres
-        resolutions. If rows or cols parameters are True, the adjust method
-        update nsres and ewres according with the rows and cols numbers.
-        """
-        libgis.G_adjust_Cell_head(self.c_region, bool(rows), bool(cols))
-
-    def get_current(self):
-        libgis.G_get_set_window(self.c_region)
-
-    def set_current(self):
-        libgis.G_set_window(self.c_region)
-
-    def get_default(self):
-        libgis.G_get_window(self.c_region)
-
-    def set_default(self):
-        self.adjust()
-        if libgis.G_put_window(self.c_region) < 0:
-            raise GrassError("Cannot change region (WIND file).")
\ No newline at end of file



More information about the grass-commit mailing list