File: buffer.py

package info (click to toggle)
grass 7.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 135,976 kB
  • ctags: 44,148
  • sloc: ansic: 410,300; python: 166,939; cpp: 34,819; sh: 9,358; makefile: 6,618; xml: 3,551; sql: 769; lex: 519; yacc: 450; asm: 387; perl: 282; sed: 17; objc: 7
file content (54 lines) | stat: -rw-r--r-- 1,988 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# -*- coding: utf-8 -*-
from grass.pygrass.raster.raster_type import TYPE as RTYPE
import ctypes
import numpy as np


_CELL = ('int', 'int0', 'int8', 'int16', 'int32', 'int64')
CELL = tuple([getattr(np, attr) for attr in _CELL if hasattr(np, attr)])
_FCELL = 'float', 'float16', 'float32'
FCELL = tuple([getattr(np, attr) for attr in _FCELL if hasattr(np, attr)])
_DCELL = 'float64', 'float128'
DCELL = tuple([getattr(np, attr) for attr in _DCELL if hasattr(np, attr)])


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):
        #import pdb; pdb.set_trace()
        obj = np.ndarray.__new__(cls, shape, RTYPE[mtype]['numpy'],
                                 buffer, offset, strides, order)
        obj.pointer_type = ctypes.POINTER(RTYPE[mtype]['ctypes'])
        obj.p = obj.ctypes.data_as(obj.pointer_type)
        return obj

    def __array_finalize__(self, obj):
        if obj is None:
            return
        self.pointer_type = getattr(obj, 'pointer_type', None)
        self.p = getattr(obj, 'p', None)

    def __array_wrap__(self, out_arr, context=None):
        """See:
        http://docs.scipy.org/doc/numpy/user/
        basics.subclassing.html#array-wrap-for-ufuncs"""
        if out_arr.dtype == np.bool:
            # there is not support for boolean maps, so convert into integer
            out_arr = out_arr.astype(np.int32)
        out_arr.p = out_arr.ctypes.data_as(out_arr.pointer_type)
        return np.ndarray.__array_wrap__(self, out_arr, context)