split.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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