segment.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 int(
  25. ((rows + self.srows - 1) / self.srows)
  26. * ((cols + self.scols - 1) / self.scols)
  27. )
  28. def segments_in_mem(self):
  29. if self.maxmem > 0 and self.maxmem < 100:
  30. seg_in_mem = (self.maxmem * self.nseg()) / 100
  31. else:
  32. seg_in_mem = 4 * (self.rows() / self.srows + self.cols() / self.scols + 2)
  33. if seg_in_mem == 0:
  34. seg_in_mem = 1
  35. return seg_in_mem
  36. def open(self, mapobj):
  37. """Open a segment it is necessary to pass a RasterSegment object."""
  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(
  42. self.c_seg,
  43. file_name,
  44. self.rows(),
  45. self.cols(),
  46. self.srows,
  47. self.scols,
  48. size,
  49. self.nseg(),
  50. )
  51. self.flush()
  52. def format(self, mapobj, file_name="", fill=True):
  53. """The segmentation routines require a disk file to be used for paging
  54. segments in and out of memory. This routine formats the file open for
  55. write on file descriptor fd for use as a segment file.
  56. """
  57. if file_name == "":
  58. file_name = libgis.G_tempfile()
  59. mapobj.temp_file = open(file_name, "w")
  60. size = ctypes.sizeof(RTYPE[mapobj.mtype]["ctypes"])
  61. if fill:
  62. libseg.Segment_format(
  63. mapobj.temp_file.fileno(),
  64. self.rows(),
  65. self.cols(),
  66. self.srows,
  67. self.scols,
  68. size,
  69. )
  70. else:
  71. libseg.Segment_format_nofill(
  72. mapobj.temp_file.fileno(),
  73. self.rows(),
  74. self.cols(),
  75. self.srows,
  76. self.scols,
  77. size,
  78. )
  79. # TODO: why should I close and then re-open it?
  80. mapobj.temp_file.close()
  81. def init(self, mapobj, file_name=""):
  82. if file_name == "":
  83. file_name = mapobj.temp_file.name
  84. mapobj.temp_file = open(file_name, "w")
  85. libseg.Segment_init(self.c_seg, mapobj.temp_file.fileno(), self.segments_in_mem)
  86. def get_row(self, row_index, buf):
  87. """Return the row using, the `segment` method"""
  88. libseg.Segment_get_row(self.c_seg, buf.p, row_index)
  89. return buf
  90. def put_row(self, row_index, buf):
  91. """Write the row using the `segment` method"""
  92. libseg.Segment_put_row(self.c_seg, buf.p, row_index)
  93. def get(self, row_index, col_index):
  94. """Return the value of the map"""
  95. libseg.Segment_get(self.c_seg, ctypes.byref(self.val), row_index, col_index)
  96. return self.val.value
  97. def put(self, row_index, col_index):
  98. """Write the value to the map"""
  99. libseg.Segment_put(self.c_seg, ctypes.byref(self.val), row_index, col_index)
  100. def get_seg_number(self, row_index, col_index):
  101. """Return the segment number"""
  102. return row_index / self.srows * self.cols / self.scols + col_index / self.scols
  103. def flush(self):
  104. """Flush pending updates to disk.
  105. Forces all pending updates generated by Segment_put() to be written to
  106. the segment file seg. Must be called after the final Segment_put()
  107. to force all pending updates to disk. Must also be called before the
  108. first call to Segment_get_row."""
  109. libseg.Segment_flush(self.c_seg)
  110. def close(self):
  111. """Free memory allocated to segment and delete temp file. """
  112. libseg.Segment_close(self.c_seg)
  113. def release(self):
  114. """Free memory allocated to segment.
  115. Releases the allocated memory associated with the segment file seg.
  116. Note: Does not close the file. Does not flush the data which may be
  117. pending from previous Segment_put() calls."""
  118. libseg.Segment_release(self.c_seg)