patch.py 3.1 KB

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