rowio.py 2.0 KB

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