split.py 3.6 KB

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