Browse Source

Add '_repr_html_' to Region class and Bbox

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@56893 15284696-431f-4ddb-bdfa-cd5b030d7da7
Pietro Zambelli 12 years ago
parent
commit
1a52ecb7e9
2 changed files with 33 additions and 24 deletions
  1. 20 18
      lib/python/pygrass/gis/region.py
  2. 13 6
      lib/python/pygrass/vector/basic.py

+ 20 - 18
lib/python/pygrass/gis/region.py

@@ -9,6 +9,7 @@ import grass.lib.gis as libgis
 import grass.script as grass
 
 from grass.pygrass.errors import GrassError
+from grass.pygrass.shell.conversion import dict2html
 
 
 class Region(object):
@@ -168,7 +169,7 @@ class Region(object):
     @property
     def zone(self):
         """Return the zone of projection
-        
+
         >>> reg = Region()
         >>> reg.zone
         0
@@ -178,19 +179,28 @@ class Region(object):
     @property
     def proj(self):
         """Return a code for projection
-        
+
         >>> reg = Region()
         >>> reg.proj
         99
-        """        
+        """
         return self.c_region.contents.proj
 
+    @property
+    def cells(self):
+        """Return the number of cells"""
+        return self.rows * self.cols
+
     #----------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 _repr_html_(self):
+        return dict2html(dict(self.items()), keys=self.keys(),
+                         border='1', kdec='b')
+
     def __unicode__(self):
         return grass.pipe_command("g.region", flags="p").communicate()[0]
 
@@ -205,21 +215,13 @@ class Region(object):
                 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)]
+    def keys(self):
+        return ['proj', 'zone', 'north', 'south', 'west', 'east',
+                'top', 'bottom', 'nsres', 'ewres', 'tbres', 'rows',
+                'cols', 'cells']
+
+    def items(self):
+        return [(k, self.__getattribute__(k)) for k in self.keys()]
 
     #----------METHODS----------
     def zoom(self, raster_name):

+ 13 - 6
lib/python/pygrass/vector/basic.py

@@ -8,6 +8,8 @@ import ctypes
 import grass.lib.vector as libvect
 from collections import Iterable
 
+from grass.pygrass.shell.conversion import dict2html
+
 
 class Bbox(object):
     """Instantiate a Bounding Box class that contains
@@ -109,6 +111,13 @@ class Bbox(object):
         return "Bbox({n}, {s}, {e}, {w})".format(n=self.north, s=self.south,
                                                  e=self.east, w=self.west)
 
+    def _repr_html_(self):
+        return dict2html(dict(self.items()), keys=self.keys(),
+                         border='1', kdec='b')
+
+    def keys(self):
+        return ['north', 'south', 'west', 'east', 'top', 'bottom']
+
     def contains(self, point):
         """Return True if the object is contained by the BoundingBox. ::
 
@@ -125,17 +134,15 @@ class Bbox(object):
                                               self.c_bbox))
 
     def items(self):
-        return [('north', self.north), ('south', self.south),
-                ('east', self.east), ('west', self.west),
-                ('top', self.top), ('bottom', self.bottom)]
+        return [(k, self.__getattribute__(k)) for k in self.keys()]
 
-    def nsewtb(self, is3d=False):
+    def nsewtb(self, tb=True):
         """Return a list
 
-        If is3d parameter is False return only:
+        If tb parameter is False return only:
         north, south, east, west
         """
-        if is3d:
+        if tb:
             return (self.north, self.south, self.east, self.west,
                     self.top, self.bottom)
         else: