split.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. """
  2. Created on Tue Apr 2 19:00:15 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.vector.basic import Bbox
  16. def get_bbox(reg, row, col, width, height, overlap):
  17. """Return a Bbox
  18. :param reg: a Region object to split
  19. :type reg: Region object
  20. :param row: the number of row
  21. :type row: int
  22. :param col: the number of row
  23. :type col: int
  24. :param width: the width of tiles
  25. :type width: int
  26. :param height: the width of tiles
  27. :type height: int
  28. :param overlap: the value of overlap between tiles
  29. :type overlap: int
  30. """
  31. north = reg.north - (row * height - overlap) * reg.nsres
  32. south = reg.north - ((row + 1) * height + overlap) * reg.nsres
  33. east = reg.west + ((col + 1) * width + overlap) * reg.ewres
  34. west = reg.west + (col * width - overlap) * reg.ewres
  35. return Bbox(
  36. north=north if north <= reg.north else reg.north,
  37. south=south if south >= reg.south else reg.south,
  38. east=east if east <= reg.east else reg.east,
  39. west=west if west >= reg.west else reg.west,
  40. )
  41. def split_region_tiles(region=None, width=100, height=100, overlap=0):
  42. """Spit a region into a list of Bbox.
  43. :param region: a Region object to split
  44. :type region: Region object
  45. :param width: the width of tiles
  46. :type width: int
  47. :param height: the width of tiles
  48. :type height: int
  49. :param overlap: the value of overlap between tiles
  50. :type overlap: int
  51. >>> reg = Region()
  52. >>> reg.north = 1350
  53. >>> reg.south = 0
  54. >>> reg.nsres = 1
  55. >>> reg.east = 1500
  56. >>> reg.west = 0
  57. >>> reg.ewres = 1
  58. >>> reg.cols
  59. 1500
  60. >>> reg.rows
  61. 1350
  62. >>> split_region_tiles(region=reg, width=1000, height=700, overlap=0) # doctest: +NORMALIZE_WHITESPACE
  63. [[Bbox(1350.0, 650.0, 1000.0, 0.0), Bbox(1350.0, 650.0, 1500.0, 1000.0)],
  64. [Bbox(650.0, 0.0, 1000.0, 0.0), Bbox(650.0, 0.0, 1500.0, 1000.0)]]
  65. >>> split_region_tiles(region=reg, width=1000, height=700, overlap=10) # doctest: +NORMALIZE_WHITESPACE
  66. [[Bbox(1350.0, 640.0, 1010.0, 0.0), Bbox(1350.0, 640.0, 1500.0, 990.0)],
  67. [Bbox(660.0, 0.0, 1010.0, 0.0), Bbox(660.0, 0.0, 1500.0, 990.0)]]
  68. """
  69. reg = region if region else Region()
  70. ncols = (reg.cols + width - 1) // width
  71. nrows = (reg.rows + height - 1) // height
  72. box_list = []
  73. # print reg
  74. for row in range(nrows):
  75. row_list = []
  76. for col in range(ncols):
  77. # print 'c', c, 'r', r
  78. row_list.append(get_bbox(reg, row, col, width, height, overlap))
  79. box_list.append(row_list)
  80. return box_list
  81. def get_overlap_region_tiles(region=None, width=100, height=100, overlap=0):
  82. """Get the Bbox of the overlapped region.
  83. :param region: a Region object to split
  84. :type region: Region object
  85. :param width: the width of tiles
  86. :type width: int
  87. :param height: the width of tiles
  88. :type height: int
  89. :param overlap: the value of overlap between tiles
  90. :type overlap: int
  91. """
  92. reg = region if region else Region()
  93. ncols = (reg.cols + width - 1) // width
  94. nrows = (reg.rows + height - 1) // height
  95. box_list = []
  96. # print reg
  97. for row in range(nrows):
  98. row_list = []
  99. for col in range(ncols):
  100. # print 'c', c, 'r', r
  101. row_list.append(get_bbox(reg, row, col, width, height, -overlap))
  102. box_list.append(row_list)
  103. return box_list