rowio.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 grass.pygrass.raster.raster_type import TYPE as RTYPE
  11. CMPFUNC = ctypes.CFUNCTYPE(
  12. ctypes.c_int, ctypes.c_int, ctypes.c_void_p, ctypes.c_int, ctypes.c_int
  13. )
  14. def getmaprow_CELL(fd, buf, row, l):
  15. librast.Rast_get_c_row(fd, ctypes.cast(buf, ctypes.POINTER(librast.CELL)), row)
  16. return 1
  17. def getmaprow_FCELL(fd, buf, row, l):
  18. librast.Rast_get_f_row(fd, ctypes.cast(buf, ctypes.POINTER(librast.FCELL)), row)
  19. return 1
  20. def getmaprow_DCELL(fd, buf, row, l):
  21. librast.Rast_get_d_row(fd, ctypes.cast(buf, ctypes.POINTER(librast.DCELL)), row)
  22. return 1
  23. get_row = {
  24. "CELL": CMPFUNC(getmaprow_CELL),
  25. "FCELL": CMPFUNC(getmaprow_FCELL),
  26. "DCELL": CMPFUNC(getmaprow_DCELL),
  27. }
  28. class RowIO(object):
  29. def __init__(self):
  30. self.c_rowio = librowio.ROWIO()
  31. self.fd = None
  32. self.rows = None
  33. self.cols = None
  34. self.mtype = None
  35. self.row_size = None
  36. def open(self, fd, rows, cols, mtype):
  37. self.fd = fd
  38. self.rows = rows
  39. self.cols = cols
  40. self.mtype = mtype
  41. self.row_size = ctypes.sizeof(RTYPE[mtype]["grass def"] * cols)
  42. if (
  43. librowio.Rowio_setup(
  44. ctypes.byref(self.c_rowio),
  45. self.fd,
  46. self.rows,
  47. self.row_size,
  48. get_row[self.mtype],
  49. get_row[self.mtype],
  50. )
  51. == -1
  52. ):
  53. raise GrassError("Fatal error, Rowio not setup correctly.")
  54. def release(self):
  55. librowio.Rowio_release(ctypes.byref(self.c_rowio))
  56. self.fd = None
  57. self.rows = None
  58. self.cols = None
  59. self.mtype = None
  60. def get(self, row_index, buf):
  61. rowio_buf = librowio.Rowio_get(ctypes.byref(self.c_rowio), row_index)
  62. ctypes.memmove(buf.p, rowio_buf, self.row_size)
  63. return buf