split.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Tue Apr 2 19:00:15 2013
  4. @author: pietro
  5. """
  6. from grass.pygrass.gis.region import Region
  7. from grass.pygrass.vector.basic import Bbox
  8. def get_bbox(reg, row, col, width, height, overlap):
  9. """Return a Bbox"""
  10. north = reg.north - (row * height - overlap) * reg.nsres
  11. south = reg.north - ((row + 1) * height + overlap) * reg.nsres
  12. east = reg.west + ((col + 1) * width + overlap) * reg.ewres
  13. west = reg.west + (col * width - overlap) * reg.ewres
  14. return Bbox(north=north if north <= reg.north else reg.north,
  15. south=south if south >= reg.south else reg.south,
  16. east=east if east <= reg.east else reg.east,
  17. west=west if west >= reg.west else reg.west,)
  18. def split_region_tiles(region=None, width=100, height=100, overlap=0):
  19. """Spit a region into a list of Bbox. ::
  20. >>> reg = Region()
  21. >>> reg.north = 1350
  22. >>> reg.south = 0
  23. >>> reg.nsres = 1
  24. >>> reg.east = 1500
  25. >>> reg.west = 0
  26. >>> reg.ewres = 1
  27. >>> reg.cols
  28. 1500
  29. >>> reg.rows
  30. 1350
  31. >>> split_region_tiles(region=reg, width=1000, height=700, overlap=0)
  32. [[Bbox(1350.0, 650.0, 1000.0, 0.0), Bbox(1350.0, 650.0, 1500.0, 1000.0)],
  33. [Bbox(650.0, 0.0, 1000.0, 0.0), Bbox(650.0, 0.0, 1500.0, 1000.0)]]
  34. >>> split_region_tiles(region=reg, width=1000, height=700, overlap=10)
  35. [[Bbox(1350.0, 640.0, 1010.0, 0.0), Bbox(1350.0, 640.0, 1500.0, 990.0)],
  36. [Bbox(660.0, 0.0, 1010.0, 0.0), Bbox(660.0, 0.0, 1500.0, 990.0)]]
  37. """
  38. reg = region if region else Region()
  39. ncols = (reg.cols + width - 1) // width
  40. nrows = (reg.rows + height - 1) // height
  41. box_list = []
  42. #print reg
  43. for row in xrange(nrows):
  44. row_list = []
  45. for col in xrange(ncols):
  46. #print 'c', c, 'r', r
  47. row_list.append(get_bbox(reg, row, col, width, height, overlap))
  48. box_list.append(row_list)
  49. return box_list
  50. def get_overlap_region_tiles(region=None, width=100, height=100, overlap=0):
  51. """Get the Bbox ov the overlapped region. ::
  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)
  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)
  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 xrange(nrows):
  76. row_list = []
  77. for col in xrange(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