segment.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Mon Jun 11 18:02:27 2012
  4. @author: pietro
  5. """
  6. import ctypes
  7. import grass.lib.gis as libgis
  8. import grass.lib.raster as libraster
  9. import grass.lib.segment as libseg
  10. from grass.pygrass.raster.raster_type import TYPE as RTYPE
  11. class Segment(object):
  12. def __init__(self, srows=64, scols=64, maxmem=100):
  13. self.srows = srows
  14. self.scols = scols
  15. self.maxmem = maxmem
  16. self.c_seg = ctypes.pointer(libseg.SEGMENT())
  17. def rows(self):
  18. return libraster.Rast_window_rows()
  19. def cols(self):
  20. return libraster.Rast_window_cols()
  21. def nseg(self):
  22. rows = self.rows()
  23. cols = self.cols()
  24. return ((rows + self.srows - 1) / self.srows) * \
  25. ((cols + self.scols - 1) / self.scols)
  26. def segments_in_mem(self):
  27. if self.maxmem > 0 and self.maxmem < 100:
  28. seg_in_mem = (self.maxmem * self.nseg()) / 100
  29. else:
  30. seg_in_mem = 4 * (self.rows() / self.srows +
  31. self.cols() / self.scols + 2)
  32. if seg_in_mem == 0:
  33. seg_in_mem = 1
  34. return seg_in_mem
  35. def open(self, mapobj):
  36. """Open a segment it is necessary to pass a RasterSegment object.
  37. """
  38. self.val = RTYPE[mapobj.mtype]['grass def']()
  39. size = ctypes.sizeof(RTYPE[mapobj.mtype]['ctypes'])
  40. file_name = libgis.G_tempfile()
  41. libseg.Segment_open(self.c_seg, file_name,
  42. self.rows(), self.cols(),
  43. self.srows, self.scols,
  44. size,
  45. self.nseg())
  46. self.flush()
  47. def format(self, mapobj, file_name='', fill=True):
  48. """The segmentation routines require a disk file to be used for paging
  49. segments in and out of memory. This routine formats the file open for
  50. write on file descriptor fd for use as a segment file.
  51. """
  52. if file_name == '':
  53. file_name = libgis.G_tempfile()
  54. mapobj.temp_file = file(file_name, 'w')
  55. size = ctypes.sizeof(RTYPE[mapobj.mtype]['ctypes'])
  56. if fill:
  57. libseg.Segment_format(mapobj.temp_file.fileno(), self.rows(),
  58. self.cols(), self.srows, self.scols, size)
  59. else:
  60. libseg.Segment_format_nofill(mapobj.temp_file.fileno(),
  61. self.rows(), self.cols(),
  62. self.srows, self.scols, size)
  63. # TODO: why should I close and then re-open it?
  64. mapobj.temp_file.close()
  65. def init(self, mapobj, file_name=''):
  66. if file_name == '':
  67. file_name = mapobj.temp_file.name
  68. mapobj.temp_file = open(file_name, 'w')
  69. libseg.Segment_init(self.c_seg, mapobj.temp_file.fileno(),
  70. self.segments_in_mem)
  71. def get_row(self, row_index, buf):
  72. """Return the row using, the `segment` method"""
  73. libseg.Segment_get_row(self.c_seg, buf.p, row_index)
  74. return buf
  75. def put_row(self, row_index, buf):
  76. """Write the row using the `segment` method"""
  77. libseg.Segment_put_row(self.c_seg, buf.p, row_index)
  78. def get(self, row_index, col_index):
  79. """Return the value of the map
  80. """
  81. libseg.Segment_get(self.c_seg,
  82. ctypes.byref(self.val), row_index, col_index)
  83. return self.val.value
  84. def put(self, row_index, col_index):
  85. """Write the value to the map
  86. """
  87. libseg.Segment_put(self.c_seg,
  88. ctypes.byref(self.val), row_index, col_index)
  89. def get_seg_number(self, row_index, col_index):
  90. """Return the segment number
  91. """
  92. return row_index / self.srows * self.cols / self.scols + \
  93. col_index / self.scols
  94. def flush(self):
  95. """Flush pending updates to disk.
  96. Forces all pending updates generated by Segment_put() to be written to
  97. the segment file seg. Must be called after the final Segment_put()
  98. to force all pending updates to disk. Must also be called before the
  99. first call to Segment_get_row."""
  100. libseg.Segment_flush(self.c_seg)
  101. def close(self):
  102. """Free memory allocated to segment and delete temp file. """
  103. libseg.Segment_close(self.c_seg)
  104. def release(self):
  105. """Free memory allocated to segment.
  106. Releases the allocated memory associated with the segment file seg.
  107. Note: Does not close the file. Does not flush the data which may be
  108. pending from previous Segment_put() calls."""
  109. libseg.Segment_release(self.c_seg)