region.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Fri May 25 12:57:10 2012
  4. @author: Pietro Zambelli
  5. """
  6. import ctypes
  7. import grass.lib.gis as libgis
  8. import grass.script as grass
  9. from grass.pygrass.errors import GrassError
  10. from grass.pygrass.shell.conversion import dict2html
  11. class Region(object):
  12. def __init__(self, default=False):
  13. """::
  14. >>> default = Region(default=True)
  15. >>> current = Region()
  16. >>> current_good = Region()
  17. >>> default == current
  18. True
  19. >>> current.cols
  20. 1500
  21. >>> current.ewres
  22. 10.0
  23. >>> current.cols = 3000
  24. >>> current.ewres
  25. 5.0
  26. >>> current.ewres = 20.0
  27. >>> current.cols
  28. 750
  29. >>> current.set_current()
  30. >>> default == current
  31. False
  32. >>> current.get_default()
  33. >>> default = Region(default=True)
  34. >>> default == current
  35. True
  36. >>> current_good.set_current()
  37. """
  38. self.c_region = ctypes.pointer(libgis.Cell_head())
  39. if default:
  40. self.get_default()
  41. else:
  42. self.get_current()
  43. def _set_param(self, key, value):
  44. grass.run_command('g.region', **{key: value})
  45. #----------LIMITS----------
  46. def _get_n(self):
  47. """Private function to obtain north value"""
  48. return self.c_region.contents.north
  49. def _set_n(self, value):
  50. """Private function to set north value"""
  51. self.c_region.contents.north = value
  52. north = property(fget=_get_n, fset=_set_n)
  53. def _get_s(self):
  54. """Private function to obtain south value"""
  55. return self.c_region.contents.south
  56. def _set_s(self, value):
  57. """Private function to set south value"""
  58. self.c_region.contents.south = value
  59. south = property(fget=_get_s, fset=_set_s)
  60. def _get_e(self):
  61. """Private function to obtain east value"""
  62. return self.c_region.contents.east
  63. def _set_e(self, value):
  64. """Private function to set east value"""
  65. self.c_region.contents.east = value
  66. east = property(fget=_get_e, fset=_set_e)
  67. def _get_w(self):
  68. """Private function to obtain west value"""
  69. return self.c_region.contents.west
  70. def _set_w(self, value):
  71. """Private function to set west value"""
  72. self.c_region.contents.west = value
  73. west = property(fget=_get_w, fset=_set_w)
  74. def _get_t(self):
  75. """Private function to obtain top value"""
  76. return self.c_region.contents.top
  77. def _set_t(self, value):
  78. """Private function to set top value"""
  79. self.c_region.contents.top = value
  80. top = property(fget=_get_t, fset=_set_t)
  81. def _get_b(self):
  82. """Private function to obtain bottom value"""
  83. return self.c_region.contents.bottom
  84. def _set_b(self, value):
  85. """Private function to set bottom value"""
  86. self.c_region.contents.bottom = value
  87. bottom = property(fget=_get_b, fset=_set_b)
  88. #----------RESOLUTION----------
  89. def _get_rows(self):
  90. """Private function to obtain rows value"""
  91. return self.c_region.contents.rows
  92. def _set_rows(self, value):
  93. """Private function to set rows value"""
  94. self.c_region.contents.rows = value
  95. self.adjust(rows=True)
  96. rows = property(fget=_get_rows, fset=_set_rows)
  97. def _get_cols(self):
  98. """Private function to obtain columns value"""
  99. return self.c_region.contents.cols
  100. def _set_cols(self, value):
  101. """Private function to set columns value"""
  102. self.c_region.contents.cols = value
  103. self.adjust(cols=True)
  104. cols = property(fget=_get_cols, fset=_set_cols)
  105. def _get_nsres(self):
  106. """Private function to obtain north-south value"""
  107. return self.c_region.contents.ns_res
  108. def _set_nsres(self, value):
  109. """Private function to obtain north-south value"""
  110. self.c_region.contents.ns_res = value
  111. self.adjust()
  112. nsres = property(fget=_get_nsres, fset=_set_nsres)
  113. def _get_ewres(self):
  114. """Private function to obtain east-west value"""
  115. return self.c_region.contents.ew_res
  116. def _set_ewres(self, value):
  117. """Private function to set east-west value"""
  118. self.c_region.contents.ew_res = value
  119. self.adjust()
  120. ewres = property(fget=_get_ewres, fset=_set_ewres)
  121. def _get_tbres(self):
  122. """Private function to obtain top-botton 3D value"""
  123. return self.c_region.contents.tb_res
  124. def _set_tbres(self, value):
  125. """Private function to set top-bottom 3D value"""
  126. self.c_region.contents.tb_res = value
  127. self.adjust()
  128. tbres = property(fget=_get_tbres, fset=_set_tbres)
  129. @property
  130. def zone(self):
  131. """Return the zone of projection
  132. >>> reg = Region()
  133. >>> reg.zone
  134. 0
  135. """
  136. return self.c_region.contents.zone
  137. @property
  138. def proj(self):
  139. """Return a code for projection
  140. >>> reg = Region()
  141. >>> reg.proj
  142. 99
  143. """
  144. return self.c_region.contents.proj
  145. @property
  146. def cells(self):
  147. """Return the number of cells"""
  148. return self.rows * self.cols
  149. #----------MAGIC METHODS----------
  150. def __repr__(self):
  151. return 'Region(n=%g, s=%g, e=%g, w=%g, nsres=%g, ewres=%g)' % (
  152. self.north, self.south, self.east, self.west,
  153. self.nsres, self.ewres)
  154. def _repr_html_(self):
  155. return dict2html(dict(self.items()), keys=self.keys(),
  156. border='1', kdec='b')
  157. def __unicode__(self):
  158. return grass.pipe_command("g.region", flags="p").communicate()[0]
  159. def __str__(self):
  160. return self.__unicode__()
  161. def __eq__(self, reg):
  162. attrs = ['north', 'south', 'west', 'east', 'top', 'bottom',
  163. 'nsres', 'ewres', 'tbres']
  164. for attr in attrs:
  165. if getattr(self, attr) != getattr(reg, attr):
  166. return False
  167. return True
  168. def keys(self):
  169. return ['proj', 'zone', 'north', 'south', 'west', 'east',
  170. 'top', 'bottom', 'nsres', 'ewres', 'tbres', 'rows',
  171. 'cols', 'cells']
  172. def items(self):
  173. return [(k, self.__getattribute__(k)) for k in self.keys()]
  174. #----------METHODS----------
  175. def zoom(self, raster_name):
  176. """Shrink region until it meets non-NULL data from this raster map:"""
  177. self._set_param('zoom', str(raster_name))
  178. self.get_current()
  179. def align(self, raster_name):
  180. """Adjust region cells to cleanly align with this raster map"""
  181. self._set_param('align', str(raster_name))
  182. self.get_current()
  183. def adjust(self, rows=False, cols=False):
  184. """Adjust rows and cols number according with the nsres and ewres
  185. resolutions. If rows or cols parameters are True, the adjust method
  186. update nsres and ewres according with the rows and cols numbers.
  187. """
  188. libgis.G_adjust_Cell_head(self.c_region, bool(rows), bool(cols))
  189. def vect(self, vector_name):
  190. """Adjust bounding box of region using a vector ::
  191. >>> reg = Region()
  192. >>> reg.vect('census')
  193. >>> reg.get_bbox()
  194. Bbox(230963.640878, 212125.562878, 645837.437393, 628769.374393)
  195. >>> reg.get_default()
  196. """
  197. from grass.pygrass.vector import VectorTopo
  198. vect = VectorTopo(vector_name)
  199. vect.open()
  200. bbox = vect.bbox()
  201. self.set_bbox(bbox)
  202. def get_current(self):
  203. """Set the current GRASS region to the Region object"""
  204. libgis.G_get_set_window(self.c_region)
  205. def set_current(self):
  206. """Set the Region object to the current GRASS region"""
  207. libgis.G_set_window(self.c_region)
  208. def get_default(self):
  209. """Set the default GRASS region to the Region object"""
  210. libgis.G_get_window(self.c_region)
  211. def set_default(self):
  212. """Set the Region object to the default GRASS region.
  213. It works only in PERMANENT mapset"""
  214. from grass.pygrass.gis import Mapset
  215. mapset = Mapset()
  216. if mapset.name != 'PERMANENT':
  217. raise GrassError("ERROR: Unable to change default region. The " \
  218. "current mapset is not <PERMANENT>.")
  219. self.adjust()
  220. if libgis.G_put_window(self.c_region) < 0:
  221. raise GrassError("Cannot change region (DEFAUL_WIND file).")
  222. def get_bbox(self):
  223. """Return a Bbox object with the extension of the region ::
  224. >>> reg = Region()
  225. >>> reg.get_bbox()
  226. Bbox(228500.0, 215000.0, 645000.0, 630000.0)
  227. """
  228. from grass.pygrass.vector.basic import Bbox
  229. return Bbox(north=self.north, south=self.south,
  230. east=self.east, west=self.west,
  231. top=self.top, bottom=self.bottom)
  232. def set_bbox(self, bbox):
  233. """Set region from Bbox ::
  234. >>> from grass.pygrass.vector.basic import Bbox
  235. >>> b = Bbox(230963.640878, 212125.562878,
  236. ... 645837.437393, 628769.374393)
  237. >>> reg = Region()
  238. >>> reg.set_bbox(b)
  239. >>> reg.get_bbox()
  240. Bbox(230963.640878, 212125.562878, 645837.437393, 628769.374393)
  241. >>> reg.get_current()
  242. """
  243. self.north = bbox.north
  244. self.south = bbox.south
  245. self.east = bbox.east
  246. self.west = bbox.west