File: rowio.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 (78 lines) | stat: -rw-r--r-- 2,205 bytes parent folder | download | duplicates (3)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 18 13:22:38 2012

@author: pietro
"""
import ctypes

import grass.lib.rowio as librowio
import grass.lib.raster as librast

from grass.pygrass.errors import GrassError
from grass.pygrass.raster.raster_type import TYPE as RTYPE


CMPFUNC = ctypes.CFUNCTYPE(ctypes.c_int,
                           ctypes.c_int, ctypes.c_void_p,
                           ctypes.c_int, ctypes.c_int)


def getmaprow_CELL(fd, buf, row, l):
    librast.Rast_get_c_row(fd, ctypes.cast(buf, ctypes.POINTER(librast.CELL)),
                           row)
    return 1


def getmaprow_FCELL(fd, buf, row, l):
    librast.Rast_get_f_row(fd, ctypes.cast(buf, ctypes.POINTER(librast.FCELL)),
                           row)
    return 1


def getmaprow_DCELL(fd, buf, row, l):
    librast.Rast_get_d_row(fd, ctypes.cast(buf, ctypes.POINTER(librast.DCELL)),
                           row)
    return 1

get_row = {
    'CELL':  CMPFUNC(getmaprow_CELL),
    'FCELL': CMPFUNC(getmaprow_FCELL),
    'DCELL': CMPFUNC(getmaprow_DCELL),
}


class RowIO(object):

    def __init__(self):
        self.c_rowio = librowio.ROWIO()
        self.fd = None
        self.rows = None
        self.cols = None
        self.mtype = None
        self.row_size = None

    def open(self, fd, rows, cols, mtype):
        self.fd = fd
        self.rows = rows
        self.cols = cols
        self.mtype = mtype
        self.row_size = ctypes.sizeof(RTYPE[mtype]['grass def'] * cols)
        if (librowio.Rowio_setup(ctypes.byref(self.c_rowio), self.fd,
                                 self.rows,
                                 self.row_size,
                                 get_row[self.mtype],
                                 get_row[self.mtype]) == -1):
            raise GrassError('Fatal error, Rowio not setup correctly.')

    def release(self):
        librowio.Rowio_release(ctypes.byref(self.c_rowio))
        self.fd = None
        self.rows = None
        self.cols = None
        self.mtype = None

    def get(self, row_index, buf):
        rowio_buf = librowio.Rowio_get(ctypes.byref(self.c_rowio), row_index)
        ctypes.memmove(buf.p, rowio_buf, self.row_size)
        return buf