patch.py 3.1 KB

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