Browse Source

Add Info class with all the data of the raster file

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@54939 15284696-431f-4ddb-bdfa-cd5b030d7da7
Pietro Zambelli 12 years ago
parent
commit
be4b9614de
2 changed files with 115 additions and 24 deletions
  1. 3 1
      lib/python/pygrass/raster/__init__.py
  2. 112 23
      lib/python/pygrass/raster/abstract.py

+ 3 - 1
lib/python/pygrass/raster/__init__.py

@@ -29,7 +29,7 @@ from grass.pygrass import functions
 #
 #
 # import raster classes
 # import raster classes
 #
 #
-from abstract import RasterAbstractBase
+from abstract import RasterAbstractBase, Info
 from raster_type import TYPE as RTYPE, RTYPE_STR
 from raster_type import TYPE as RTYPE, RTYPE_STR
 from buffer import Buffer
 from buffer import Buffer
 from segment import Segment
 from segment import Segment
@@ -162,6 +162,7 @@ class RasterRow(RasterAbstractBase):
 
 
         # check if exist and instantiate all the private attributes
         # check if exist and instantiate all the private attributes
         if self.exist():
         if self.exist():
+            self.info = Info(self.name, self.mapset)
             if self.mode == 'r':
             if self.mode == 'r':
                 # the map exist, read mode
                 # the map exist, read mode
                 self._fd = libraster.Rast_open_old(self.name, self.mapset)
                 self._fd = libraster.Rast_open_old(self.name, self.mapset)
@@ -387,6 +388,7 @@ class RasterSegment(RasterAbstractBase):
         self.mtype = mtype
         self.mtype = mtype
 
 
         if self.exist():
         if self.exist():
+            self.info = Info(self.name, self.mapset)
             if ((self.mode == "w" or self.mode == "rw") and
             if ((self.mode == "w" or self.mode == "rw") and
                 self.overwrite is False):
                 self.overwrite is False):
                 str_err = _("Raster map <{0}> already exists. Use overwrite.")
                 str_err = _("Raster map <{0}> already exists. Use overwrite.")

+ 112 - 23
lib/python/pygrass/raster/abstract.py

@@ -38,6 +38,116 @@ from history import History
 ## Define global variables to not exceed the 80 columns
 ## Define global variables to not exceed the 80 columns
 WARN_OVERWRITE = "Raster map <{0}> already exists and will be overwritten"
 WARN_OVERWRITE = "Raster map <{0}> already exists and will be overwritten"
 INDXOUTRANGE = "The index (%d) is out of range, have you open the map?."
 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):
 class RasterAbstractBase(object):
@@ -82,6 +192,8 @@ class RasterAbstractBase(object):
         #self.region = Region()
         #self.region = Region()
         self.cats = Category()
         self.cats = Category()
         self.hist = History()
         self.hist = History()
+        if self.exist():
+            self.info = Info(self.name, self.mapset)
 
 
 
 
     def _get_mtype(self):
     def _get_mtype(self):
@@ -153,29 +265,6 @@ class RasterAbstractBase(object):
     cols = property(fget=_get_cols, fset=_set_unchangeable)
     cols = property(fget=_get_cols, fset=_set_unchangeable)
 
 
     @must_be_open
     @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):
     def _get_cats_title(self):
         return self.cats.title
         return self.cats.title