split.py 3.6 KB

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