patch.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. """
  2. Created on Tue Apr 2 18:57:42 2013
  3. @author: pietro
  4. """
  5. from __future__ import (
  6. nested_scopes,
  7. generators,
  8. division,
  9. absolute_import,
  10. with_statement,
  11. print_function,
  12. unicode_literals,
  13. )
  14. from grass.pygrass.gis.region import Region
  15. from grass.pygrass.raster import RasterRow
  16. from grass.pygrass.utils import coor2pixel
  17. def get_start_end_index(bbox_list):
  18. """Convert a Bounding Box to a list of the index of
  19. column start, end, row start and end
  20. :param bbox_list: a list of BBox object to convert
  21. :type bbox_list: list of BBox object
  22. """
  23. ss_list = []
  24. reg = Region()
  25. for bbox in bbox_list:
  26. r_start, c_start = coor2pixel((bbox.west, bbox.north), reg)
  27. r_end, c_end = coor2pixel((bbox.east, bbox.south), reg)
  28. ss_list.append((int(r_start), int(r_end), int(c_start), int(c_end)))
  29. return ss_list
  30. def rpatch_row(rast, rasts, bboxes):
  31. """Patch a row of bound boxes.
  32. :param rast: a Raster object to write
  33. :type rast: Raster object
  34. :param rasts: a list of Raster object to read
  35. :type rasts: list of Raster object
  36. :param bboxes: a list of BBox object
  37. :type bboxes: list of BBox object
  38. """
  39. sei = get_start_end_index(bboxes)
  40. # instantiate two buffer
  41. buff = rasts[0][0]
  42. rbuff = rasts[0][0]
  43. r_start, r_end, c_start, c_end = sei[0]
  44. for row in range(r_start, r_end):
  45. for col, ras in enumerate(rasts):
  46. r_start, r_end, c_start, c_end = sei[col]
  47. buff = ras.get_row(row, buff)
  48. rbuff[c_start:c_end] = buff[c_start:c_end]
  49. rast.put_row(rbuff)
  50. def rpatch_map(
  51. raster,
  52. mapset,
  53. mset_str,
  54. bbox_list,
  55. overwrite=False,
  56. start_row=0,
  57. start_col=0,
  58. prefix="",
  59. ):
  60. # TODO is prefix useful??
  61. """Patch raster using a bounding box list to trim the raster.
  62. :param raster: the name of output raster
  63. :type raster: str
  64. :param mapset: the name of mapset to use
  65. :type mapset: str
  66. :param mset_str:
  67. :type mset_str: str
  68. :param bbox_list: a list of BBox object to convert
  69. :type bbox_list: list of BBox object
  70. :param overwrite: overwrite existing raster
  71. :type overwrite: bool
  72. :param start_row: the starting row of original raster
  73. :type start_row: int
  74. :param start_col: the starting column of original raster
  75. :type start_col: int
  76. :param prefix: the prefix of output raster
  77. :type prefix: str
  78. """
  79. # Instantiate the RasterRow input objects
  80. rast = RasterRow(prefix + raster, mapset)
  81. rtype = RasterRow(name=raster, mapset=mset_str % (0, 0))
  82. rtype.open("r")
  83. rast.open("w", mtype=rtype.mtype, overwrite=overwrite)
  84. rtype.close()
  85. rasts = []
  86. for row, rbbox in enumerate(bbox_list):
  87. rrasts = []
  88. for col in range(len(rbbox)):
  89. rrasts.append(
  90. RasterRow(
  91. name=raster, mapset=mset_str % (start_row + row, start_col + col)
  92. )
  93. )
  94. rrasts[-1].open("r")
  95. rasts.append(rrasts)
  96. rpatch_row(rast, rrasts, rbbox)
  97. for rst in rrasts:
  98. rst.close()
  99. del rst
  100. rast.close()