Browse Source

pygrass: Change mtype attribute in read-only, now is using numpy dtype.

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@62188 15284696-431f-4ddb-bdfa-cd5b030d7da7
Pietro Zambelli 10 years ago
parent
commit
ba64172f85
1 changed files with 16 additions and 2 deletions
  1. 16 2
      lib/python/pygrass/raster/buffer.py

+ 16 - 2
lib/python/pygrass/raster/buffer.py

@@ -9,10 +9,26 @@ import ctypes
 import numpy as np
 
 
+CELL = (np.int, np.int0, np.int8, np.int16, np.int32, np.int64)
+FCELL = (np.float, np.float16, np.float32)
+DCELL = (np.float64, np.float128)
+
+
 class Buffer(np.ndarray):
     """shape, mtype='FCELL', buffer=None, offset=0,
     strides=None, order=None
     """
+    @property
+    def mtype(self):
+        if self.dtype in CELL:
+            return 'CELL'
+        elif self.dtype in FCELL:
+            return 'FCELL'
+        elif self.dtype in DCELL:
+            return DCELL
+        else:
+            err = "Raster type: %r not supported by GRASS."
+            raise TypeError(err % self.dtype)
 
     def __new__(cls, shape, mtype='FCELL', buffer=None, offset=0,
                 strides=None, order=None):
@@ -21,13 +37,11 @@ class Buffer(np.ndarray):
                                  buffer, offset, strides, order)
         obj.pointer_type = ctypes.POINTER(RTYPE[mtype]['ctypes'])
         obj.p = obj.ctypes.data_as(obj.pointer_type)
-        obj.mtype = mtype
         return obj
 
     def __array_finalize__(self, obj):
         if obj is None:
             return
-        self.mtype = getattr(obj, 'mtype', None)
         self.pointer_type = getattr(obj, 'pointer_type', None)
         self.p = getattr(obj, 'p', None)