rowio.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Mon Jun 18 13:22:38 2012
  4. @author: pietro
  5. """
  6. import ctypes
  7. import grass.lib.rowio as librowio
  8. import grass.lib.raster as librast
  9. from grass.pygrass.errors import GrassError
  10. from .raster_type import TYPE as RTYPE
  11. CMPFUNC = ctypes.CFUNCTYPE(ctypes.c_int,
  12. ctypes.c_int, ctypes.c_void_p,
  13. ctypes.c_int, ctypes.c_int)
  14. def getmaprow_CELL(fd, buf, row, l):
  15. librast.Rast_get_c_row(fd, ctypes.cast(buf, ctypes.POINTER(librast.CELL)),
  16. row)
  17. return 1
  18. def getmaprow_FCELL(fd, buf, row, l):
  19. librast.Rast_get_f_row(fd, ctypes.cast(buf, ctypes.POINTER(librast.FCELL)),
  20. row)
  21. return 1
  22. def getmaprow_DCELL(fd, buf, row, l):
  23. librast.Rast_get_d_row(fd, ctypes.cast(buf, ctypes.POINTER(librast.DCELL)),
  24. row)
  25. return 1
  26. get_row = {
  27. 'CELL': CMPFUNC(getmaprow_CELL),
  28. 'FCELL': CMPFUNC(getmaprow_FCELL),
  29. 'DCELL': CMPFUNC(getmaprow_DCELL),
  30. }
  31. class RowIO(object):
  32. def __init__(self):
  33. self.c_rowio = librowio.ROWIO()
  34. self.fd = None
  35. self.rows = None
  36. self.cols = None
  37. self.mtype = None
  38. self.row_size = None
  39. def open(self, fd, rows, cols, mtype):
  40. self.fd = fd
  41. self.rows = rows
  42. self.cols = cols
  43. self.mtype = mtype
  44. self.row_size = ctypes.sizeof(RTYPE[mtype]['grass def'] * cols)
  45. if (librowio.Rowio_setup(ctypes.byref(self.c_rowio), self.fd,
  46. self.rows,
  47. self.row_size,
  48. get_row[self.mtype],
  49. get_row[self.mtype]) == -1):
  50. raise GrassError('Fatal error, Rowio not setup correctly.')
  51. def release(self):
  52. librowio.Rowio_release(ctypes.byref(self.c_rowio))
  53. self.fd = None
  54. self.rows = None
  55. self.cols = None
  56. self.mtype = None
  57. def get(self, row_index, buf):
  58. rowio_buf = librowio.Rowio_get(ctypes.byref(self.c_rowio), row_index)
  59. ctypes.memmove(buf.p, rowio_buf, self.row_size)
  60. return buf