[GRASS-SVN] r54939 - grass/trunk/lib/python/pygrass/raster

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Feb 5 09:29:29 PST 2013


Author: zarch
Date: 2013-02-05 09:29:28 -0800 (Tue, 05 Feb 2013)
New Revision: 54939

Modified:
   grass/trunk/lib/python/pygrass/raster/__init__.py
   grass/trunk/lib/python/pygrass/raster/abstract.py
Log:
Add Info class with all the data of the raster file

Modified: grass/trunk/lib/python/pygrass/raster/__init__.py
===================================================================
--- grass/trunk/lib/python/pygrass/raster/__init__.py	2013-02-05 16:40:29 UTC (rev 54938)
+++ grass/trunk/lib/python/pygrass/raster/__init__.py	2013-02-05 17:29:28 UTC (rev 54939)
@@ -29,7 +29,7 @@
 #
 # import raster classes
 #
-from abstract import RasterAbstractBase
+from abstract import RasterAbstractBase, Info
 from raster_type import TYPE as RTYPE, RTYPE_STR
 from buffer import Buffer
 from segment import Segment
@@ -162,6 +162,7 @@
 
         # check if exist and instantiate all the private attributes
         if self.exist():
+            self.info = Info(self.name, self.mapset)
             if self.mode == 'r':
                 # the map exist, read mode
                 self._fd = libraster.Rast_open_old(self.name, self.mapset)
@@ -387,6 +388,7 @@
         self.mtype = mtype
 
         if self.exist():
+            self.info = Info(self.name, self.mapset)
             if ((self.mode == "w" or self.mode == "rw") and
                 self.overwrite is False):
                 str_err = _("Raster map <{0}> already exists. Use overwrite.")

Modified: grass/trunk/lib/python/pygrass/raster/abstract.py
===================================================================
--- grass/trunk/lib/python/pygrass/raster/abstract.py	2013-02-05 16:40:29 UTC (rev 54938)
+++ grass/trunk/lib/python/pygrass/raster/abstract.py	2013-02-05 17:29:28 UTC (rev 54939)
@@ -38,8 +38,118 @@
 ## Define global variables to not exceed the 80 columns
 WARN_OVERWRITE = "Raster map <{0}> already exists and will be overwritten"
 INDXOUTRANGE = "The index (%d) is out of range, have you open the map?."
+INFO = """{name}@{mapset}
+rows: {rows}
+cols: {cols}
+north: {north} south: {south} nsres:{nsres}
+east:  {east} west: {west} ewres:{ewres}
+range: {min}, {max}
+proj: {proj}
+"""
 
 
+class Info(object):
+    def __init__(self, name, mapset=''):
+        """Read the information for a raster map. ::
+
+            >>> info = Info('elevation')
+            >>> info
+            elevation@
+            rows: 1350
+            cols: 1500
+            north: 228500.0 south: 215000.0 nsres:10.0
+            east:  645000.0 west: 630000.0 ewres:10.0
+            range: 56, 156
+            proj: 99
+
+        """
+        self.name = name
+        self.mapset = mapset
+        self.c_region = ctypes.pointer(libgis.Cell_head())
+        libraster.Rast_get_cellhd(name, mapset,
+                                  self.c_region)
+        self._get_range()
+
+    def _get_range(self):
+        self.c_range = ctypes.pointer(libraster.Range())
+        libraster.Rast_read_range(self.name, self.mapset, self.c_range)
+
+    @property
+    def north(self):
+        return self.c_region.contents.north
+
+    @property
+    def south(self):
+        return self.c_region.contents.south
+
+    @property
+    def east(self):
+        return self.c_region.contents.east
+
+    @property
+    def west(self):
+        return self.c_region.contents.west
+
+    @property
+    def top(self):
+        return self.c_region.contents.top
+
+    @property
+    def bottom(self):
+        return self.c_region.contents.bottom
+
+    @property
+    def rows(self):
+        return self.c_region.contents.rows
+
+    @property
+    def cols(self):
+        return self.c_region.contents.cols
+
+    @property
+    def nsres(self):
+        return self.c_region.contents.ns_res
+
+    @property
+    def ewres(self):
+        return self.c_region.contents.ew_res
+
+    @property
+    def tbres(self):
+        return self.c_region.contents.tb_res
+
+    @property
+    def zone(self):
+        return self.c_region.contents.zone
+
+    @property
+    def proj(self):
+        return self.c_region.contents.proj
+
+    @property
+    def min(self):
+        return self.c_range.contents.min
+
+    @property
+    def max(self):
+        return self.c_range.contents.max
+
+    @property
+    def range(self):
+        return self.c_range.contents.min, self.c_range.contents.max
+
+    def __repr__(self):
+        return INFO.format(name=self.name, mapset=self.mapset,
+                           rows=self.rows, cols=self.cols,
+                           north=self.north, south=self.south,
+                           east=self.east, west=self.west,
+                           top=self.top, bottom=self.bottom,
+                           nsres=self.nsres, ewres=self.ewres,
+                           tbres=self.tbres, zone=self.zone,
+                           proj=self.proj, min=self.min, max=self.max)
+
+
+
 class RasterAbstractBase(object):
     """Raster_abstract_base: The base class from which all sub-classes
     inherit. It does not implement any row or map access methods:
@@ -82,6 +192,8 @@
         #self.region = Region()
         self.cats = Category()
         self.hist = History()
+        if self.exist():
+            self.info = Info(self.name, self.mapset)
 
 
     def _get_mtype(self):
@@ -153,29 +265,6 @@
     cols = property(fget=_get_cols, fset=_set_unchangeable)
 
     @must_be_open
-    def _get_range(self):
-        if self.mtype == 'CELL':
-            maprange = libraster.Range()
-            libraster.Rast_read_range(self.name, self.mapset,
-                                      ctypes.byref(maprange))
-            self._min = libgis.CELL()
-            self._max = libgis.CELL()
-            self._min.value = maprange.min
-            self._max.value = maprange.max
-        else:
-            maprange = libraster.FPRange()
-            libraster.Rast_read_fp_range(self.name, self.mapset,
-                                         ctypes.byref(maprange))
-            self._min = libgis.DCELL()
-            self._max = libgis.DCELL()
-            libraster.Rast_get_fp_range_min_max(ctypes.byref(maprange),
-                                                ctypes.byref(self._min),
-                                                ctypes.byref(self._max))
-        return self._min.value, self._max.value
-
-    range = property(fget=_get_range, fset=_set_unchangeable)
-
-    @must_be_open
     def _get_cats_title(self):
         return self.cats.title
 



More information about the grass-commit mailing list