split.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. north = reg.north - (row * height - overlap) * reg.nsres
  13. south = reg.north - ((row + 1) * height + overlap) * reg.nsres
  14. east = reg.west + ((col + 1) * width + overlap) * reg.ewres
  15. west = reg.west + (col * width - overlap) * reg.ewres
  16. return Bbox(north=north if north <= reg.north else reg.north,
  17. south=south if south >= reg.south else reg.south,
  18. east=east if east <= reg.east else reg.east,
  19. west=west if west >= reg.west else reg.west,)
  20. def split_region_tiles(region=None, width=100, height=100, overlap=0):
  21. """Spit a region into a list of Bbox. ::
  22. >>> reg = Region()
  23. >>> reg.north = 1350
  24. >>> reg.south = 0
  25. >>> reg.nsres = 1
  26. >>> reg.east = 1500
  27. >>> reg.west = 0
  28. >>> reg.ewres = 1
  29. >>> reg.cols
  30. 1500
  31. >>> reg.rows
  32. 1350
  33. >>> split_region_tiles(region=reg, width=1000, height=700, overlap=0)
  34. [[Bbox(1350.0, 650.0, 1000.0, 0.0), Bbox(1350.0, 650.0, 1500.0, 1000.0)],
  35. [Bbox(650.0, 0.0, 1000.0, 0.0), Bbox(650.0, 0.0, 1500.0, 1000.0)]]
  36. >>> split_region_tiles(region=reg, width=1000, height=700, overlap=10)
  37. [[Bbox(1350.0, 640.0, 1010.0, 0.0), Bbox(1350.0, 640.0, 1500.0, 990.0)],
  38. [Bbox(660.0, 0.0, 1010.0, 0.0), Bbox(660.0, 0.0, 1500.0, 990.0)]]
  39. """
  40. reg = region if region else Region()
  41. ncols = (reg.cols + width - 1) // width
  42. nrows = (reg.rows + height - 1) // height
  43. box_list = []
  44. #print reg
  45. for row in range(nrows):
  46. row_list = []
  47. for col in range(ncols):
  48. #print 'c', c, 'r', r
  49. row_list.append(get_bbox(reg, row, col, width, height, overlap))
  50. box_list.append(row_list)
  51. return box_list
  52. def get_overlap_region_tiles(region=None, width=100, height=100, overlap=0):
  53. """Get the Bbox ov the overlapped region. ::
  54. >>> reg = Region()
  55. >>> reg.north = 1350
  56. >>> reg.south = 0
  57. >>> reg.nsres = 1
  58. >>> reg.east = 1500
  59. >>> reg.west = 0
  60. >>> reg.ewres = 1
  61. >>> reg.cols
  62. 1500
  63. >>> reg.rows
  64. 1350
  65. >>> split_region_tiles(region=reg, width=1000, height=700, overlap=0)
  66. [[Bbox(1350.0, 650.0, 1000.0, 0.0), Bbox(1350.0, 650.0, 1500.0, 1000.0)],
  67. [Bbox(650.0, 0.0, 1000.0, 0.0), Bbox(650.0, 0.0, 1500.0, 1000.0)]]
  68. >>> split_region_tiles(region=reg, width=1000, height=700, overlap=10)
  69. [[Bbox(1350.0, 640.0, 1010.0, 0.0), Bbox(1350.0, 640.0, 1500.0, 990.0)],
  70. [Bbox(660.0, 0.0, 1010.0, 0.0), Bbox(660.0, 0.0, 1500.0, 990.0)]]
  71. """
  72. reg = region if region else Region()
  73. ncols = (reg.cols + width - 1) // width
  74. nrows = (reg.rows + height - 1) // height
  75. box_list = []
  76. #print reg
  77. for row in range(nrows):
  78. row_list = []
  79. for col in range(ncols):
  80. #print 'c', c, 'r', r
  81. row_list.append(get_bbox(reg, row, col, width, height, -overlap))
  82. box_list.append(row_list)
  83. return box_list