浏览代码

Add '_repr_html_' to raster Info and RasterAbstract, add r_export to functions, and fix some indent in the the docstring.

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@56895 15284696-431f-4ddb-bdfa-cd5b030d7da7
Pietro Zambelli 12 年之前
父节点
当前提交
c5d87e7f72
共有 2 个文件被更改,包括 41 次插入14 次删除
  1. 12 0
      lib/python/pygrass/functions.py
  2. 29 14
      lib/python/pygrass/raster/abstract.py

+ 12 - 0
lib/python/pygrass/functions.py

@@ -204,3 +204,15 @@ def get_raster_for_points(poi_vector, raster, column=None):
     else:
         poi.attrs.commit()
         return True
+
+
+def r_export(rast, output='', fmt='png', **kargs):
+    from grass.pygrass.modules import Module
+    if rast.exist():
+        output = output if output else "%s_%s.%s" % (rast.name, rast.mapset,
+                                                     fmt)
+        Module('r.out.%s' % fmt, input=rast.fullname(), output=output,
+               overwrite=True, **kargs)
+        return output
+    else:
+        raise ValueError('Raster map does not exist.')

+ 29 - 14
lib/python/pygrass/raster/abstract.py

@@ -19,6 +19,8 @@ import grass.lib.raster as libraster
 from grass.pygrass import functions
 from grass.pygrass.gis.region import Region
 from grass.pygrass.errors import must_be_open
+from grass.pygrass.shell.conversion import dict2html
+from grass.pygrass.shell.show import raw_figure
 
 #
 # import raster classes
@@ -142,6 +144,17 @@ class Info(object):
                            tbres=self.tbres, zone=self.zone,
                            proj=self.proj, min=self.min, max=self.max)
 
+    def keys(self):
+        return ['name', 'mapset', 'rows', 'cols', 'north', 'south',
+                'east', 'west', 'top', 'bottom', 'nsres', 'ewres', 'tbres',
+                'zone', 'proj', 'min', 'max']
+
+    def items(self):
+        return [(k, self.__getattribute__(k)) for k in self.keys()]
+
+    def _repr_html_(self):
+        return dict2html(dict(self.items()), keys=self.keys(),
+                         border='1', kdec='b')
 
 
 class RasterAbstractBase(object):
@@ -249,7 +262,6 @@ class RasterAbstractBase(object):
 
     name = property(fget=_get_name, fset=_set_name)
 
-
     @must_be_open
     def _get_cats_title(self):
         return self.cats.title
@@ -293,15 +305,18 @@ class RasterAbstractBase(object):
         """Return a constructor of the class"""
         return (self.__getitem__(irow) for irow in xrange(self._rows))
 
+    def _repr_png_(self):
+        return raw_figure(functions.r_export(self))
+
     def exist(self):
         """Return True if the map already exist, and
-           set the mapset if were not set.
+        set the mapset if were not set.
 
-           call the C function `G_find_raster`.
+        call the C function `G_find_raster`. ::
 
-           >>> ele = RasterAbstractBase('elevation')
-           >>> ele.exist()
-           True
+            >>> ele = RasterAbstractBase('elevation')
+            >>> ele.exist()
+            True
         """
         if self.name:
             if self.mapset == '':
@@ -313,11 +328,11 @@ class RasterAbstractBase(object):
             return False
 
     def is_open(self):
-        """Return True if the map is open False otherwise
+        """Return True if the map is open False otherwise. ::
 
-           >>> ele = RasterAbstractBase('elevation')
-           >>> ele.is_open()
-           False
+            >>> ele = RasterAbstractBase('elevation')
+            >>> ele.is_open()
+            False
 
         """
         return True if self._fd is not None and self._fd >= 0 else False
@@ -342,11 +357,11 @@ class RasterAbstractBase(object):
         return "{name}@{mapset}".format(name=self.name, mapset=self.mapset)
 
     def name_mapset(self, name=None, mapset=None):
-        """Return the full name of the Raster
+        """Return the full name of the Raster. ::
 
-           >>> ele = RasterAbstractBase('elevation')
-           >>> ele.name_mapset()
-           'elevation@PERMANENT'
+            >>> ele = RasterAbstractBase('elevation')
+            >>> ele.name_mapset()
+            'elevation@PERMANENT'
 
         """
         if name is None: